-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Add generic OIDC authentication flow #6783
Open
palash247
wants to merge
5
commits into
getredash:master
Choose a base branch
from
palash247:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6fcaa67
Add generic OIDC authentication flow (#6781)
palash247 e081003
Add unit tests for generic oidc
palash247 9f81ec4
have option to setup valid domains to login from
palash247 f9f5c9b
add unit tests for oidc auth flow
palash247 c156120
Snapshot: 25.03.0-dev
github-actions[bot] 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions
45
client/app/pages/settings/components/AuthSettings/OIDCLoginSettings.jsx
This file contains 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,45 @@ | ||
import { isEmpty, join } from "lodash"; | ||
import React from "react"; | ||
import Form from "antd/lib/form"; | ||
import Select from "antd/lib/select"; | ||
import Alert from "antd/lib/alert"; | ||
import DynamicComponent from "@/components/DynamicComponent"; | ||
import { clientConfig } from "@/services/auth"; | ||
import { SettingsEditorPropTypes, SettingsEditorDefaultProps } from "../prop-types"; | ||
|
||
export default function OIDCLoginSettings(props) { | ||
const { values, onChange } = props; | ||
|
||
if (!clientConfig.oidcLoginEnabled) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<DynamicComponent name="OrganizationSettings.OIDCLoginSettings" {...props}> | ||
<h4>OIDC Login</h4> | ||
<Form.Item label="Allowed Domains"> | ||
<Select | ||
mode="tags" | ||
value={values.auth_oidc_domains} | ||
onChange={value => onChange({ auth_oidc_domains: value })} | ||
/> | ||
{!isEmpty(values.auth_oidc_domains) && ( | ||
<Alert | ||
message={ | ||
<p> | ||
Any user registered with a <strong>{join(values.auth_oidc_domains, ", ")} </strong> | ||
<span> </span>domain will be able to login. If they don't have an existing user, a new user will be created and join | ||
the <strong>Default</strong> group. | ||
</p> | ||
} | ||
className="m-t-15" | ||
/> | ||
)} | ||
</Form.Item> | ||
</DynamicComponent> | ||
); | ||
} | ||
|
||
OIDCLoginSettings.propTypes = SettingsEditorPropTypes; | ||
|
||
OIDCLoginSettings.defaultProps = SettingsEditorDefaultProps; |
This file contains 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 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 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,142 @@ | ||
import logging | ||
|
||
from authlib.integrations.flask_client import OAuth | ||
from flask import Blueprint, flash, redirect, request, session, url_for | ||
|
||
from redash import models, settings | ||
from redash.authentication import ( | ||
create_and_login_user, | ||
get_next_path, | ||
logout_and_redirect_to_index, | ||
) | ||
from redash.authentication.org_resolving import current_org | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def verify_account(org, email): | ||
if org.is_public: | ||
return True | ||
|
||
domain = email.split("@")[-1] | ||
logger.info(f"org domains: {org.oidc_domains}") | ||
|
||
if domain in org.oidc_domains: | ||
return True | ||
|
||
if org.has_user(email) == 1: | ||
return True | ||
|
||
return False | ||
|
||
|
||
def ensure_required_scope(scope): | ||
""" | ||
Ensures that the required scopes 'openid', 'email', and 'profile' are present in the scope string. | ||
""" | ||
scope_set = set(scope.split()) if scope else set() | ||
required_scopes = {"openid", "email", "profile"} | ||
scope_set.update(required_scopes) | ||
return " ".join(scope_set) | ||
|
||
|
||
def get_name_from_user_info(user_info): | ||
name = user_info.get("name") | ||
if not name: | ||
given_name = user_info.get("given_name", "") | ||
family_name = user_info.get("family_name", "") | ||
name = f"{given_name} {family_name}".strip() | ||
if not name: | ||
name = user_info.get("preferred_username", "") | ||
if not name: | ||
name = user_info.get("nickname", "") | ||
return name | ||
|
||
|
||
def create_oidc_blueprint(app): | ||
if not settings.OIDC_ENABLED: | ||
return None | ||
|
||
oauth = OAuth(app) | ||
|
||
blueprint = Blueprint("oidc", __name__) | ||
|
||
oauth = OAuth(app) | ||
scope = ensure_required_scope(settings.OIDC_SCOPE) | ||
oauth.register( | ||
name="oidc", | ||
server_metadata_url=settings.OIDC_ISSUER_URL, | ||
client_kwargs={ | ||
"scope": scope, | ||
}, | ||
) | ||
|
||
@blueprint.route("/<org_slug>/oidc", endpoint="authorize_org") | ||
def org_login(org_slug): | ||
session["org_slug"] = current_org.slug | ||
return redirect(url_for(".authorize", next=request.args.get("next", None))) | ||
|
||
@blueprint.route("/oidc", endpoint="authorize") | ||
def login(): | ||
redirect_uri = url_for(".callback", _external=True) | ||
|
||
next_path = request.args.get("next", url_for("redash.index", org_slug=session.get("org_slug"))) | ||
logger.debug("Callback url: %s", redirect_uri) | ||
logger.debug("Next is: %s", next_path) | ||
|
||
session["next_url"] = next_path | ||
|
||
return oauth.oidc.authorize_redirect(redirect_uri) | ||
|
||
@blueprint.route("/oidc/callback", endpoint="callback") | ||
def authorized(): | ||
logger.debug("Authorized user inbound") | ||
|
||
token = oauth.oidc.authorize_access_token() | ||
user_info = oauth.oidc.parse_id_token(token) | ||
if user_info: | ||
session["user"] = user_info | ||
else: | ||
logger.warning("Unable to get userinfo from returned token") | ||
flash("Validation error. Please retry.") | ||
return redirect(url_for("redash.login")) | ||
|
||
access_token = token["access_token"] | ||
|
||
if access_token is None: | ||
logger.warning("Access token missing in the callback request.") | ||
flash("Validation error. Please retry.") | ||
return redirect(url_for("redash.login")) | ||
|
||
if "org_slug" in session: | ||
org = models.Organization.get_by_slug(session.pop("org_slug")) | ||
else: | ||
org = current_org | ||
|
||
if not verify_account(org, user_info["email"]): | ||
logger.warning( | ||
"User tried to login with unauthorized domain name: %s (org: %s)", | ||
user_info["email"], | ||
org, | ||
) | ||
flash("Your account ({}) isn't allowed.".format(user_info["email"])) | ||
return redirect(url_for("redash.login", org_slug=org.slug)) | ||
|
||
# see if email is verified | ||
email_verified = user_info.get("email_verified", False) | ||
if not email_verified: | ||
flash("Email not verified.") | ||
return redirect(url_for("redash.login")) | ||
|
||
user_name = get_name_from_user_info(user_info) | ||
|
||
user = create_and_login_user(org, user_name, user_info["email"]) | ||
if user is None: | ||
return logout_and_redirect_to_index() | ||
|
||
unsafe_next_path = session.get("next_url") or url_for("redash.index", org_slug=org.slug) | ||
next_path = get_next_path(unsafe_next_path) | ||
|
||
return redirect(next_path) | ||
|
||
return blueprint |
This file contains 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 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 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 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 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would
user_info
->userinfo
in every single place/file because the endpoint is/userinfo
haha (non-issue really)