Skip to content

Commit d4493ac

Browse files
committed
Configure google and github OAuth providers
Google and GiHub OAuth providers can be used independently or simultaneously when authenticating in sourced-ui. The order of the providers to use will be defined by 'OAUTH_ENABLED_PROVIDERS' Signed-off-by: David Pordomingo <[email protected]>
1 parent 8abcb6f commit d4493ac

File tree

2 files changed

+60
-26
lines changed

2 files changed

+60
-26
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@ You can configure the Docker image using the following environment variables:
5151
| `METADATA_USER` | Username for metadata DB (when `SYNC_MODE` is set to `true`) |
5252
| `METADATA_PASSWORD` | Password for metadata DB (when `SYNC_MODE` is set to `true`) |
5353
| `METADATA_DB` | Database name for metadata (when `SYNC_MODE` is set to `true`) |
54-
| `OAUTH_PROVIDER` | Use OAuth provider for authorization. Currently only `google` |
55-
| `OAUTH_CONSUMER_KEY` | OAuth provider consumer key (aka client_id) |
56-
| `OAUTH_CONSUMER_SECRET` | OAuth provider consumer secret (aka client_secret) |
54+
| `OAUTH_ENABLED_PROVIDERS` | Comma separated list of available OAuth providers (eg: `github,google`) |
5755
| `OAUTH_REGISTRATION_ROLE` | The role for newly registered users using OAuth `Admin`/`Alpha`/`Gamma` |
56+
| `OAUTH_GITHUB_CONSUMER_KEY` | GitHub OAuth provider consumer key (aka client_id) |
57+
| `OAUTH_GITHUB_CONSUMER_SECRET` | GitHub OAuth provider consumer secret (aka client_secret) |
58+
| `OAUTH_GOOGLE_CONSUMER_KEY` | Google OAuth provider consumer key (aka client_id) |
59+
| `OAUTH_GOOGLE_CONSUMER_SECRET` | Google OAuth provider consumer secret (aka client_secret) |
5860

5961
To see the differences between roles in `OAUTH_REGISTRATION_ROLE` variable consult [official superset documentation](https://superset.incubator.apache.org/security.html#provided-roles).
6062

superset/contrib/docker/superset_config.py

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -154,31 +154,63 @@ def mutator(f):
154154
SUPERSET_WEBSERVER_TIMEOUT = 300
155155

156156
# Authorization configuration
157-
158-
OAUTH_PROVIDER = get_env_variable('OAUTH_PROVIDER', False)
159-
if OAUTH_PROVIDER:
160-
OAUTH_PROVIDERS = [
161-
{
162-
'name': 'google',
163-
'icon': 'fa-google',
164-
'token_key': 'access_token',
165-
'remote_app': {
166-
'consumer_key': get_env_variable('OAUTH_CONSUMER_KEY'),
167-
'consumer_secret': get_env_variable('OAUTH_CONSUMER_SECRET'),
168-
'base_url': 'https://www.googleapis.com/oauth2/v2/',
169-
'request_token_params': {
170-
'scope': 'email profile'
171-
},
172-
'request_token_url': None,
173-
'access_token_url': 'https://accounts.google.com/o/oauth2/token',
174-
'authorize_url': 'https://accounts.google.com/o/oauth2/auth'
175-
}
157+
OAUTH_ENABLED_PROVIDERS = get_env_variable('OAUTH_ENABLED_PROVIDERS', False)
158+
OAUTH_GOOGLE_CONSUMER_KEY = get_env_variable('OAUTH_GOOGLE_CONSUMER_KEY', False)
159+
OAUTH_GOOGLE_CONSUMER_SECRET = get_env_variable('OAUTH_GOOGLE_CONSUMER_SECRET', False)
160+
OAUTH_GITHUB_CONSUMER_KEY = get_env_variable('OAUTH_GITHUB_CONSUMER_KEY', False)
161+
OAUTH_GITHUB_CONSUMER_SECRET = get_env_variable('OAUTH_GITHUB_CONSUMER_SECRET', False)
162+
163+
OAUTH_AVAILABLE_CONFIGS = {
164+
'google': {
165+
'name': 'google',
166+
'icon': 'fa-google',
167+
'token_key': 'access_token',
168+
'remote_app': {
169+
'consumer_key': OAUTH_GOOGLE_CONSUMER_KEY,
170+
'consumer_secret': OAUTH_GOOGLE_CONSUMER_SECRET,
171+
'base_url': 'https://www.googleapis.com/oauth2/v2/',
172+
'request_token_params': {
173+
'scope': 'email profile'
174+
},
175+
'request_token_url': None,
176+
'access_token_url': 'https://accounts.google.com/o/oauth2/token',
177+
'authorize_url': 'https://accounts.google.com/o/oauth2/auth'
178+
}
179+
},
180+
'github': {
181+
'name': 'github',
182+
'icon': 'fa-github',
183+
'token_key': 'access_token',
184+
'remote_app': {
185+
'consumer_key': OAUTH_GITHUB_CONSUMER_KEY,
186+
'consumer_secret': OAUTH_GITHUB_CONSUMER_SECRET,
187+
'base_url': 'https://api.github.com/',
188+
'request_token_params': {
189+
'scope': 'user' # read:user
190+
},
191+
'request_token_url': None,
192+
'access_token_method': 'POST',
193+
'access_token_url': 'https://github.com/login/oauth/access_token',
194+
'authorize_url': 'https://github.com/login/oauth/authorize'
176195
}
177-
]
196+
}
197+
}
198+
199+
if OAUTH_ENABLED_PROVIDERS:
200+
providers = []
201+
provider_names = OAUTH_ENABLED_PROVIDERS.split(',')
202+
for provider in provider_names:
203+
if provider in OAUTH_AVAILABLE_CONFIGS:
204+
if not OAUTH_AVAILABLE_CONFIGS[provider]['remote_app']['consumer_key']:
205+
raise EnvironmentError('Not valid OAuth consumer_key provided for {}'.format(provider))
206+
if not OAUTH_AVAILABLE_CONFIGS[provider]['remote_app']['consumer_secret']:
207+
raise EnvironmentError('Not valid OAuth consumer_secret provided for {}'.format(provider))
208+
else:
209+
raise EnvironmentError('Unknown OAuth provider {}'.format(provider))
210+
211+
providers.append(OAUTH_AVAILABLE_CONFIGS[provider])
178212

179-
if OAUTH_PROVIDER not in [p['name'] for p in OAUTH_PROVIDERS]:
180-
raise EnvironmentError(
181-
'Unknown OAuth provider {}'.format(OAUTH_PROVIDER))
213+
OAUTH_PROVIDERS = providers
182214

183215
from flask_appbuilder.security.manager import AUTH_OAUTH
184216

0 commit comments

Comments
 (0)