-
Notifications
You must be signed in to change notification settings - Fork 16
Authentication setup #316
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
indy-independence
merged 38 commits into
SUNET:develop
from
Josephine-Rutten:feature.authenticationwithoauth
Jan 16, 2024
Merged
Authentication setup #316
Changes from 17 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
4f58bc9
auth setup
223d765
Add OAUTH identity and required
3c5afa8
Extra error exception added for when the token is not valid yet.
6cdae7b
Merge branch 'develop' into feature.authenticationwithoauth
Josephine-Rutten 50dd5d2
Changes based on Peter's comments and a TODO added to the /auth/test …
c4efe49
Merge branch 'feature.authenticationwithoauth' of https://github.com/…
06bfb98
Made get keys into it's own functions
fb26083
Removed some code smell, still 2 TODO to do
19a5544
Make JWT or OIDC option and some small improvements
34c5005
Made sure all the paths are of type Path
2397f0f
Improved comments
c379ca2
Merge branch 'SUNET:develop' into feature.authenticationwithoauth
Josephine-Rutten 3624a39
added neccersary packages
Josephine-Rutten cf2a9c4
Update requirements.txt
Josephine-Rutten e25516f
Pre-commit update & formatting
Josephine-Rutten 283bff5
make the key process simpler
Josephine-Rutten 0c7133a
Comment at method
Josephine-Rutten 4d9d17d
Improved security to work with different token types
Josephine-Rutten d5df744
Make eroor handling simpler
Josephine-Rutten 16b4056
Chasnge Resource operator
Josephine-Rutten 01692a9
Add an auth config option
Josephine-Rutten b5f681d
Update app_settings.py
Josephine-Rutten c2a38cf
Update app_settings.py
Josephine-Rutten 1c94c0c
more error catching
Josephine-Rutten e287f04
Add exception handling
Josephine-Rutten ffc417b
Update security.py
Josephine-Rutten 1435b15
Added extra error handling
Josephine-Rutten 09ab48c
Add some authorization docs
Josephine-Rutten f2100f3
Add catching for no email error
Josephine-Rutten 0a2edb1
Added option to have token in query string
Josephine-Rutten 51c57cb
simplefied the connect socket call and removed unneccersary code
Josephine-Rutten 6262f87
verify the token in the connect socket io
Josephine-Rutten 6460ca5
Added scope as a env that can be set from env
Josephine-Rutten 95bb738
better user feedback + error messages instead of data
Josephine-Rutten 6924cfe
Added connection error catching for the wellknown url
Josephine-Rutten 5afbf11
Add documentation auth_config
Josephine-Rutten 4b703a2
Add excample auth_conf file
Josephine-Rutten 308c0b5
Update auth_config.yml
Josephine-Rutten 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
repos: | ||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v2.3.0 | ||
rev: v4.5.0 | ||
hooks: | ||
- id: check-yaml | ||
- id: end-of-file-fixer | ||
- id: trailing-whitespace | ||
- repo: https://github.com/psf/black | ||
rev: 22.10.0 | ||
rev: 23.11.0 | ||
hooks: | ||
- id: black | ||
- repo: https://github.com/pycqa/isort | ||
rev: 5.11.2 | ||
rev: 5.12.0 | ||
hooks: | ||
- id: isort | ||
name: isort (python) | ||
- repo: https://github.com/pycqa/flake8 | ||
rev: 5.0.4 | ||
rev: 6.1.0 | ||
hooks: | ||
- id: flake8 |
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 |
---|---|---|
|
@@ -33,3 +33,5 @@ SQLAlchemy-Utils==0.38.3 | |
pydantic==1.10.2 | ||
Werkzeug==2.2.3 | ||
greenlet==3.0.1 | ||
Authlib==1.0.1 | ||
python-jose==3.1.0 |
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,85 @@ | ||
from authlib.integrations.base_client.errors import MismatchingStateError | ||
from flask import current_app, redirect, url_for | ||
from flask_restx import Namespace, Resource | ||
from requests.models import PreparedRequest | ||
|
||
from cnaas_nms.api.generic import empty_result | ||
from cnaas_nms.app_settings import auth_settings | ||
from cnaas_nms.tools.log import get_logger | ||
from cnaas_nms.tools.security import get_identity, login_required | ||
from cnaas_nms.version import __api_version__ | ||
|
||
logger = get_logger() | ||
api = Namespace("auth", description="API for handling auth", prefix="/api/{}".format(__api_version__)) | ||
|
||
|
||
class LoginApi(Resource): | ||
def get(self): | ||
"""Function to initiate a login of the user. | ||
The user will be sent to the page to login. | ||
Our client info will also be checked. | ||
|
||
Note: | ||
We also discussed adding state to this function. | ||
That way you could be sent to the same page once you logged in. | ||
We would put the relevant information in a dictionary, | ||
base64 encode it and sent it around as a parameter. | ||
For now the application is small and it didn't seem needed. | ||
|
||
Returns: | ||
A HTTP redirect response to OIDC_CONF_WELL_KNOWN_URL we have defined. | ||
We give the auth call as a parameter to redirect after login is successfull. | ||
|
||
""" | ||
if not auth_settings.OIDC_ENABLED: | ||
return empty_result(status="error", data="Can't login when OIDC disabled"), 500 | ||
oauth_client = current_app.extensions["authlib.integrations.flask_client"] | ||
redirect_uri = url_for("auth_auth_api", _external=True) | ||
|
||
return oauth_client.connext.authorize_redirect(redirect_uri) | ||
|
||
|
||
class AuthApi(Resource): | ||
def get(self): | ||
"""Function to authenticate the user. | ||
This API call is called by the OAUTH login after the user has logged in. | ||
We get the users token and redirect them to right page in the frontend. | ||
|
||
Returns: | ||
A HTTP redirect response to the url in the frontend that handles the repsonse after login. | ||
The access token is a parameter in the url | ||
|
||
""" | ||
|
||
oauth_client = current_app.extensions["authlib.integrations.flask_client"] | ||
|
||
try: | ||
token = oauth_client.connext.authorize_access_token() | ||
indy-independence marked this conversation as resolved.
Show resolved
Hide resolved
|
||
except MismatchingStateError as e: | ||
logger.error("Exception during authorization of the access token: {}".format(str(e))) | ||
return ( | ||
empty_result( | ||
status="error", | ||
data="Exception during authorization of the access token. Please try to login again.", | ||
), | ||
502, | ||
) | ||
|
||
url = auth_settings.FRONTEND_CALLBACK_URL | ||
parameters = {"token": token["access_token"]} | ||
|
||
req = PreparedRequest() | ||
req.prepare_url(url, parameters) | ||
return redirect(req.url, code=302) | ||
|
||
|
||
class IdentityApi(Resource): | ||
@login_required | ||
def get(self): | ||
identity = get_identity() | ||
return identity | ||
|
||
|
||
api.add_resource(LoginApi, "/login") | ||
api.add_resource(AuthApi, "/auth") | ||
api.add_resource(IdentityApi, "/identity") |
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.
For our satosa server to work with this we seem to need to modify this from just "openid" to "openid email" so we can get the email for logging etc 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.
Great, I've added it to the app_setting, so it can be set as an env variable or in the auth_config.yaml
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 great! Can we add something about the new config options on this documentation page also? docs/configuration/index.rst