-
Notifications
You must be signed in to change notification settings - Fork 39
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 oauth2 authorization code flow support #345
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a61b9e9
Applied some feedbacks from second review.
dd9e6b2
After removal of localhost certificate
95ee0db
Auto-generate Flask secret_key, modified oauth2 docs
e78ddef
Modified oauth2 docs
b613d68
Excluding mypy for now, since version 0.800 broke things
f2e270a
Merge branch 'master' of github.com:equinor/webviz-config into oauth2
b712deb
reenable mypy, remove pinning urllib3 to 1.24.3
81e8447
Removed remaining trace of WEBVIZ_SESSION_SECRET_KEY
2667082
Added a changelog entry
bdb966c
Moved added a changelog entry to Added section of Unreleased
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
import os | ||
import datetime | ||
from typing import Tuple | ||
|
||
import msal | ||
import flask | ||
|
||
|
||
class Oauth2: | ||
"""Oauth2 authorization Code grant flow""" | ||
|
||
def __init__(self, app: flask.app.Flask): | ||
self._app = app | ||
|
||
# Azure AD app registration info (currently the values are taken from environment variables) | ||
self._tenant_id = os.environ["WEBVIZ_TENANT_ID"] | ||
self._client_id = os.environ["WEBVIZ_CLIENT_ID"] | ||
self._client_secret = os.environ["WEBVIZ_CLIENT_SECRET"] | ||
self._scope = os.environ["WEBVIZ_SCOPE"] | ||
|
||
# Initiate msal | ||
self._msal_app = msal.ConfidentialClientApplication( | ||
client_id=self._client_id, | ||
client_credential=self._client_secret, | ||
authority=f"https://login.microsoftonline.com/{self._tenant_id}", | ||
) | ||
self._accounts = self._msal_app.get_accounts() | ||
|
||
# Initiate oauth2 endpoints | ||
self.set_oauth2_endpoints() | ||
|
||
def set_oauth2_endpoints(self) -> None: | ||
"""/login and /auth-return endpoints are added for Oauth2 authorization | ||
code flow. | ||
|
||
At the end of the flow, a session cookie containing a valid access token | ||
and its expiration date is created. This flask session object can be | ||
accessed from Webviz plugin. | ||
|
||
To get the access token: flask.session.get("access_token") | ||
To get the expiration date: flask.session.get("expiration_date") | ||
|
||
An Azure AD application should be registered, and the following environment | ||
variables should be set: WEBVIZ_TENANT_ID, WEBVIZ_CLIENT_ID, | ||
WEBVIZ_CLIENT_SECRET, WEBVIZ_SCOPE. | ||
|
||
WEBVIZ_SESSION_SECRET_KEY environment variable is also needed for signing the | ||
session cookie. | ||
""" | ||
|
||
@self._app.route("/login") | ||
def _login_controller(): # type: ignore[no-untyped-def] | ||
redirect_uri = get_auth_redirect_uri(flask.request.url_root) | ||
|
||
# First leg of Oauth2 authorization code flow | ||
auth_url = self._msal_app.get_authorization_request_url( | ||
scopes=[self._scope], redirect_uri=redirect_uri | ||
) | ||
return flask.redirect(auth_url) | ||
|
||
@self._app.route("/auth-return") | ||
def _auth_return_controller(): # type: ignore[no-untyped-def] | ||
redirect_uri = get_auth_redirect_uri(flask.request.url_root) | ||
returned_query_params = flask.request.args | ||
|
||
# There is an error from the first leg of Oauth2 authorization code flow | ||
if "error" in returned_query_params: | ||
error_description = returned_query_params.get("error_description") | ||
print("Error description:", error_description) | ||
redirect_error_uri = flask.url_for("error_controller") | ||
return flask.redirect(redirect_error_uri) | ||
|
||
code = returned_query_params.get("code") | ||
|
||
# Second leg of Oauth2 authorization code flow | ||
tokens_result = self._msal_app.acquire_token_by_authorization_code( | ||
code=code, scopes=[self._scope], redirect_uri=redirect_uri | ||
) | ||
expires_in = tokens_result.get("expires_in") | ||
expiration_date = datetime.datetime.now() + datetime.timedelta( | ||
seconds=expires_in - 60 | ||
) | ||
print("Access token expiration date:", expiration_date) | ||
|
||
# Set expiration date in the session | ||
flask.session["expiration_date"] = expiration_date | ||
|
||
# Set access token in the session | ||
flask.session["access_token"] = tokens_result.get("access_token") | ||
|
||
return flask.redirect(flask.request.url_root) | ||
|
||
@self._app.route("/error") | ||
def _error_controller(): # type: ignore[no-untyped-def] | ||
return "Error" | ||
|
||
def set_oauth2_before_request_decorator(self) -> None: | ||
"""Check access token existence in session cookie before every request. | ||
If it does not exist, the browser is redirected to /login endpoint. | ||
|
||
If access token exists, its expiration date is checked in session cookie. | ||
If the current date exceeds its expiration date, a new access token is | ||
retrieved and set in the session cookie. A new expiration date is also | ||
set in the session cookie. | ||
""" | ||
|
||
# pylint: disable=inconsistent-return-statements | ||
@self._app.before_request | ||
def _check_access_token(): # type: ignore[no-untyped-def] | ||
# The session of the request does not contain access token, redirect to /login | ||
is_redirected, redirect_url = self.is_empty_token() | ||
if is_redirected: | ||
return flask.redirect(redirect_url) | ||
|
||
# The session contains access token, check its expiration date | ||
self.check_and_set_token_expiry() | ||
|
||
@staticmethod | ||
def is_empty_token() -> Tuple[bool, str]: | ||
if ( | ||
not flask.session.get("access_token") | ||
and flask.request.path != "/login" | ||
and flask.request.path != "/auth-return" | ||
): | ||
login_uri = get_login_uri(flask.request.url_root) | ||
return True, login_uri | ||
|
||
return False, "" | ||
|
||
def check_and_set_token_expiry(self) -> None: | ||
if flask.session.get("access_token"): | ||
expiration_date = flask.session.get("expiration_date") | ||
current_date = datetime.datetime.now() | ||
if current_date > expiration_date: | ||
# Access token has expired | ||
print("Access token has expired.") | ||
if not self._accounts: | ||
self._accounts = self._msal_app.get_accounts() | ||
renewed_tokens_result = self._msal_app.acquire_token_silent( | ||
scopes=[self._scope], account=self._accounts[0] | ||
) | ||
expires_in = renewed_tokens_result.get("expires_in") | ||
new_expiration_date = datetime.datetime.now() + datetime.timedelta( | ||
seconds=expires_in - 60 | ||
) | ||
print("New access token expiration date:", new_expiration_date) | ||
|
||
# Set new expiration date in the session | ||
flask.session["expiration_date"] = new_expiration_date | ||
|
||
# Set new access token in the session | ||
flask.session["access_token"] = renewed_tokens_result.get( | ||
"access_token" | ||
) | ||
|
||
|
||
def get_login_uri(url_root: str) -> str: | ||
return url_root + "login" | ||
|
||
|
||
def get_auth_redirect_uri(url_root: str) -> str: | ||
return url_root + "auth-return" |
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
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.
Replaced by autogenerated key, isn't it?
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.
Ah yes, this should be removed :)