-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: SAML Authentication #2426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
27e2902
feat: SAML Authentication
dpgaspar 90fb00a
add docs, and improve structure
dpgaspar 2716a7c
add docs, and improve structure
dpgaspar 268e5e4
add missing types.py and login_saml.html to SAML feature
dpgaspar ee0f0a8
fix test
dpgaspar 71c7525
address comments
dpgaspar a011b02
Merge branch 'master' into feat/saml-auth
dpgaspar 4b8844e
fix type annotations
dpgaspar 0b41395
Merge remote-tracking branch 'origin/feat/saml-auth' into feat/saml-auth
dpgaspar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| """Example Flask-AppBuilder application with SAML / Entra ID authentication.""" | ||
|
|
||
| from app import create_app | ||
|
|
||
| app = create_app() | ||
|
|
||
| if __name__ == "__main__": | ||
| app.run(host="0.0.0.0", port=5000, debug=True) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import logging | ||
|
|
||
| from flask import Flask | ||
|
|
||
| from .extensions import appbuilder, db | ||
|
|
||
|
|
||
| logging.basicConfig(format="%(asctime)s:%(levelname)s:%(name)s:%(message)s") | ||
| logging.getLogger().setLevel(logging.INFO) | ||
|
|
||
|
|
||
| def create_app() -> Flask: | ||
| app = Flask(__name__) | ||
| app.config.from_object("config") | ||
| with app.app_context(): | ||
| db.init_app(app) | ||
| appbuilder.init_app(app, db.session) | ||
| db.create_all() | ||
| return app |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from flask_appbuilder import AppBuilder | ||
| from flask_appbuilder.utils.legacy import get_sqla_class | ||
|
|
||
| db = get_sqla_class()() | ||
| appbuilder = AppBuilder() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| """Example configuration for SAML authentication with Flask-AppBuilder. | ||
|
|
||
| Demonstrates configuring FAB with SAML / Microsoft Entra ID authentication. | ||
| Adjust the IdP settings to match your identity provider. | ||
|
|
||
| Required environment variables: | ||
| SAML_TENANT_ID - Microsoft Entra ID tenant ID | ||
| SAML_IDP_CERT - IdP signing certificate (base64 content, no PEM headers) | ||
| """ | ||
|
|
||
| import os | ||
|
|
||
| from flask_appbuilder.const import AUTH_SAML | ||
|
|
||
| basedir = os.path.abspath(os.path.dirname(__file__)) | ||
|
|
||
| # Entra ID tenant ID and IdP certificate from environment | ||
| SAML_TENANT_ID = os.environ.get("SAML_TENANT_ID", "<YOUR_TENANT_ID>") | ||
| SAML_IDP_CERT = os.environ.get("SAML_IDP_CERT", "<YOUR_IDP_CERTIFICATE>") | ||
|
|
||
| # Flask secret key | ||
| SECRET_KEY = "\2\1thisismyscretkey\1\2\e\y\y\h" | ||
|
|
||
| # Database connection | ||
| SQLALCHEMY_DATABASE_URI = "sqlite:///" + os.path.join(basedir, "app.db") | ||
|
|
||
| # Flask-AppBuilder Security | ||
| AUTH_TYPE = AUTH_SAML | ||
|
|
||
| # Set to True to allow user self registration via SAML | ||
| AUTH_USER_REGISTRATION = True | ||
|
|
||
| # Default role for self-registered users | ||
| AUTH_USER_REGISTRATION_ROLE = "Admin" | ||
|
|
||
| # Sync roles at login (maps SAML groups/roles to FAB roles) | ||
| AUTH_ROLES_SYNC_AT_LOGIN = True | ||
|
|
||
| # Role mapping from SAML group names to FAB role names | ||
| AUTH_ROLES_MAPPING = { | ||
| "admins": ["Admin"], | ||
| "users": ["Public"], | ||
| } | ||
|
|
||
| # ------------------------------------------------------- | ||
| # SAML Configuration | ||
| # ------------------------------------------------------- | ||
|
|
||
| # List of SAML Identity Providers | ||
| # Replace <TENANT_ID> with your Microsoft Entra ID (formerly Azure AD) tenant ID. | ||
| # You can find these values in the Azure Portal under: | ||
| # Entra ID > Enterprise Applications > Your App > Single sign-on | ||
| SAML_PROVIDERS = [ | ||
| { | ||
| "name": "entra_id", | ||
| "icon": "fa-microsoft", | ||
| # IdP configuration from Entra ID SAML metadata: | ||
| # https://login.microsoftonline.com/<TENANT_ID>/federationmetadata/2007-06/federationmetadata.xml | ||
| "idp": { | ||
| "entityId": f"https://sts.windows.net/{SAML_TENANT_ID}/", | ||
| "singleSignOnService": { | ||
| "url": f"https://login.microsoftonline.com/{SAML_TENANT_ID}/saml2", | ||
| "binding": "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect", | ||
| }, | ||
| "singleLogoutService": { | ||
| "url": f"https://login.microsoftonline.com/{SAML_TENANT_ID}/saml2", | ||
| "binding": "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect", | ||
| }, | ||
| "x509cert": SAML_IDP_CERT, | ||
| }, | ||
| # Map SAML assertion attributes to FAB user fields. | ||
| # Left side: SAML attribute name (Entra ID claim URIs). | ||
| # Right side: FAB user field name. | ||
| "attribute_mapping": { | ||
| "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress": "email", | ||
| "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname": "first_name", # noqa: E501 | ||
| "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname": "last_name", | ||
| "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name": "username", | ||
| "http://schemas.microsoft.com/ws/2008/06/identity/claims/groups": "role_keys", | ||
| }, | ||
| }, | ||
| ] | ||
|
|
||
| # Global SAML Service Provider configuration | ||
| SAML_CONFIG = { | ||
| "strict": False, | ||
| "debug": True, | ||
| "sp": { | ||
| "entityId": "http://localhost:9000/saml/metadata/", | ||
| "assertionConsumerService": { | ||
| "url": "http://localhost:9000/saml/acs/", | ||
| "binding": "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST", | ||
| }, | ||
| "singleLogoutService": { | ||
| "url": "http://localhost:9000/saml/slo/", | ||
| "binding": "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect", | ||
| }, | ||
| "NameIDFormat": "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress", | ||
| # SP certificate (optional, needed for signed requests) | ||
| "x509cert": "", | ||
| # "privateKey": "", | ||
| }, | ||
| "security": { | ||
| "nameIdEncrypted": False, | ||
| "authnRequestsSigned": False, | ||
| "logoutRequestSigned": False, | ||
| "logoutResponseSigned": False, | ||
| "signMetadata": False, | ||
| "wantMessagesSigned": False, | ||
| "wantAssertionsSigned": True, | ||
| "wantAssertionsEncrypted": False, | ||
| "wantNameId": True, | ||
| "wantNameIdEncrypted": False, | ||
| "wantAttributeStatement": True, | ||
| }, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.