-
Notifications
You must be signed in to change notification settings - Fork 5
236 registration page #241
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 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
90b8160
Added registration for new users.
youssefr26 3c2d641
Added route to search for lab_managers and updated edit and create op…
youssefr26 7f251ac
Merge branch 'main' of https://github.com/LabConnect-RCOS/LabConnect-…
youssefr26 a900f5f
Clean up Deepsource error.
youssefr26 f8d12ed
Updated register route
youssefr26 473ff8f
Fixed Deepsource errors
youssefr26 d046678
Merge branch 'main' into 236-registration-page
RafaelCenzano 69d45ae
fix missing code
RafaelCenzano 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 |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| from uuid import uuid4 | ||
|
|
||
| from flask import current_app, make_response, redirect, request | ||
| from flask_jwt_extended import create_access_token | ||
| from flask_jwt_extended import create_access_token, get_jwt_identity, jwt_required | ||
| from onelogin.saml2.auth import OneLogin_Saml2_Auth | ||
|
|
||
| from labconnect import db | ||
|
|
@@ -14,31 +14,36 @@ | |
| temp_codes = {} | ||
|
|
||
|
|
||
| def generate_temporary_code(user_email: str) -> str: | ||
| def generate_temporary_code(user_email: str, registered: bool) -> str: | ||
| # Generate a unique temporary code | ||
| code = str(uuid4()) | ||
| expires_at = datetime.now() + timedelta(seconds=5) # expires in 5 seconds | ||
| temp_codes[code] = {"email": user_email, "expires_at": expires_at} | ||
| temp_codes[code] = { | ||
| "email": user_email, | ||
| "expires_at": expires_at, | ||
| "registered": registered, | ||
| } | ||
| return code | ||
|
|
||
|
|
||
| def validate_code_and_get_user_email(code: str) -> str | None: | ||
| def validate_code_and_get_user_email(code: str) -> tuple[str | None, bool | None]: | ||
| token_data = temp_codes.get(code, {}) | ||
| if not token_data: | ||
| return None | ||
|
|
||
| user_email = token_data.get("email", None) | ||
| expire = token_data.get("expires_at", None) | ||
| registered = token_data.get("registered", None) | ||
|
|
||
| if user_email and expire and expire > datetime.now(): | ||
| # If found, delete the code to prevent reuse | ||
| del temp_codes[code] | ||
| return user_email | ||
| return user_email, registered | ||
| elif expire: | ||
| # If the code has expired, delete it | ||
| del temp_codes[code] | ||
|
|
||
| return None | ||
| return None, None | ||
|
|
||
|
|
||
| @main_blueprint.get("/login") | ||
|
|
@@ -50,7 +55,7 @@ def saml_login(): | |
| and current_app.config["FRONTEND_URL"] == "http://localhost:3000" | ||
| ): | ||
| # Generate JWT | ||
| code = generate_temporary_code("[email protected]") | ||
| code = generate_temporary_code("[email protected]", True) | ||
|
|
||
| # Send the JWT to the frontend | ||
| return redirect(f"{current_app.config['FRONTEND_URL']}/callback/?code={code}") | ||
|
|
@@ -70,36 +75,48 @@ def saml_callback(): | |
| errors = auth.get_errors() | ||
|
|
||
| if not errors: | ||
| registered = True | ||
| user_info = auth.get_attributes() | ||
| # user_id = auth.get_nameid() | ||
|
|
||
| data = db.session.execute(db.select(User).where(User.email == "email")).scalar() | ||
|
|
||
| # User doesn't exist, create a new user | ||
| if data is None: | ||
|
|
||
| # TODO: add data | ||
| user = User( | ||
| # email=email, | ||
| # first_name=first_name, | ||
| # last_name=last_name, | ||
| # preferred_name=json_request_data.get("preferred_name", None), | ||
| # class_year=class_year, | ||
| ) | ||
|
|
||
| db.session.add(user) | ||
| db.session.commit() | ||
|
|
||
| registered = False | ||
| # Generate JWT | ||
| # token = create_access_token(identity=[user_id, datetime.now()]) | ||
| code = generate_temporary_code(user_info["email"][0]) | ||
| code = generate_temporary_code(user_info["email"][0], registered) | ||
|
|
||
| # Send the JWT to the frontend | ||
| return redirect(f"{current_app.config['FRONTEND_URL']}/callback/?code={code}") | ||
|
|
||
| return {"errors": errors}, 500 | ||
|
|
||
|
|
||
| @main_blueprint.post("/register") | ||
| @jwt_required() | ||
| def registerUser(): | ||
|
|
||
| user_id = get_jwt_identity() | ||
|
|
||
| # Gather the new user's information | ||
| json_data = request.get_json() | ||
| user = User( | ||
| email=user_id, | ||
| first_name=json_data.get("first_name"), | ||
| last_name=json_data.get("last_name"), | ||
| preferred_name=json_data.get("preferred_name"), | ||
|
||
| class_year=json_data.get("class_year"), | ||
| profile_picture=json_data.get("profile_pictures"), | ||
| website=json_data.get("website"), | ||
| description=json_data.get("description"), | ||
| ) | ||
| db.session.add(user) | ||
| db.session.commit() | ||
RafaelCenzano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return {"msg": "New user added"} | ||
|
|
||
|
|
||
| @main_blueprint.post("/token") | ||
| def tokenRoute(): | ||
| if request.json is None or request.json.get("code", None) is None: | ||
|
|
@@ -108,13 +125,13 @@ def tokenRoute(): | |
| code = request.json["code"] | ||
| if code is None: | ||
| return {"msg": "Missing code in request"}, 400 | ||
| user_email = validate_code_and_get_user_email(code) | ||
| user_email, registered = validate_code_and_get_user_email(code) | ||
|
|
||
| if user_email is None: | ||
| return {"msg": "Invalid code"}, 400 | ||
|
|
||
| token = create_access_token(identity=[user_email, datetime.now()]) | ||
| return {"token": token} | ||
| return {"token": token, "registered": registered} | ||
|
|
||
|
|
||
| @main_blueprint.get("/metadata/") | ||
|
|
||
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.
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.