Skip to content

Commit 59db85d

Browse files
authored
feat: support Auth0 OAuth2 (#2171)
* feat: support Auth0 OAuth2 * lint
1 parent ba63c5c commit 59db85d

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

docs/security.rst

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,21 @@ Specify a list of OAUTH_PROVIDERS in **config.py** that you want to allow for yo
262262
"client_kwargs": {"scope": "openid profile email groups"},
263263
"access_token_url": "https://OKTA_DOMAIN.okta.com/oauth2/v1/token",
264264
"authorize_url": "https://OKTA_DOMAIN.okta.com/oauth2/v1/authorize",
265-
"server_metadata_url": f"https://OKTA_DOMAIN.okta.com/.well-known/openid-configuration",
265+
"server_metadata_url": "https://OKTA_DOMAIN.okta.com/.well-known/openid-configuration",
266+
},
267+
},
268+
{
269+
"name": "auth0",
270+
"icon": "fa-shield-halved",
271+
"token_key": "access_token",
272+
"remote_app": {
273+
"client_id": "AUTH0_KEY",
274+
"client_secret": "AUTH0_SECRET",
275+
"api_base_url": "https://AUTH0_DOMAIN/oauth2/v1/",
276+
"client_kwargs": {"scope": "openid profile email groups"},
277+
"access_token_url": "https://AUTH0_DOMAIN/oauth/token",
278+
"authorize_url": "https://AUTH0_DOMAIN/authorize",
279+
"server_metadata_url": "https://AUTH0_DOMAIN/.well-known/openid-configuration",
266280
},
267281
},
268282
{

flask_appbuilder/security/manager.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,10 +649,21 @@ def get_oauth_user_info(
649649
data = me.json()
650650
log.debug("User info from Okta: %s", data)
651651
return {
652-
"username": "okta_" + data.get("sub", ""),
652+
"username": f"{provider}_{data['sub']}",
653653
"first_name": data.get("given_name", ""),
654654
"last_name": data.get("family_name", ""),
655-
"email": data.get("email", ""),
655+
"email": data["email"],
656+
"role_keys": data.get("groups", []),
657+
}
658+
# for Auth0
659+
if provider == "auth0":
660+
data = self.appbuilder.sm.oauth_remotes[provider].userinfo()
661+
log.debug("User info from Auth0: %s", data)
662+
return {
663+
"username": f"{provider}_{data['sub']}",
664+
"first_name": data.get("given_name", ""),
665+
"last_name": data.get("family_name", ""),
666+
"email": data["email"],
656667
"role_keys": data.get("groups", []),
657668
}
658669
# for Keycloak

tests/security/test_auth_oauth.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
import os
33
import unittest
4+
from unittest.mock import MagicMock
45

56
from flask import Flask
67
from flask_appbuilder import AppBuilder, SQLA
@@ -47,7 +48,22 @@ def setUp(self):
4748
"AZURE_APPLICATION_ID/"
4849
"oauth2/authorize",
4950
},
50-
}
51+
},
52+
{
53+
"name": "auth0",
54+
"icon": "fa-shield-halved",
55+
"token_key": "access_token",
56+
"remote_app": {
57+
"client_id": "AUTH0_KEY",
58+
"client_secret": "AUTH0_SECRET",
59+
"api_base_url": "https://AUTH0_DOMAIN/oauth2/v1/",
60+
"client_kwargs": {"scope": "openid profile email groups"},
61+
"access_token_url": "https://AUTH0_DOMAIN/oauth/token",
62+
"authorize_url": "https://AUTH0_DOMAIN/authorize",
63+
"server_metadata_url": "https://AUTH0_DOMAIN/.well-known/"
64+
"openid-configuration",
65+
},
66+
},
5167
]
5268

5369
# start Database
@@ -652,3 +668,30 @@ def test_oauth_user_info_azure_with_jwt_validation(self):
652668
"username": "b1a54a40-8dfa-4a6d-a2b8-f90b84d4b1df",
653669
},
654670
)
671+
672+
def test_oauth_user_info_auth0(self):
673+
self.appbuilder = AppBuilder(self.app, self.db.session)
674+
675+
self.appbuilder.sm.oauth_remotes["auth0"].userinfo = MagicMock(
676+
return_value={
677+
"email": "test@gmail.com",
678+
"given_name": "test",
679+
"family_name": "user",
680+
"role_keys": [],
681+
"sub": "test-sub",
682+
}
683+
)
684+
685+
user_info = self.appbuilder.sm.get_oauth_user_info(
686+
"auth0", {"access_token": "", "id_token": ""}
687+
)
688+
self.assertEqual(
689+
user_info,
690+
{
691+
"email": "test@gmail.com",
692+
"first_name": "test",
693+
"last_name": "user",
694+
"role_keys": [],
695+
"username": "auth0_test-sub",
696+
},
697+
)

0 commit comments

Comments
 (0)