Skip to content

Commit

Permalink
fix: raise error
Browse files Browse the repository at this point in the history
  • Loading branch information
richardnguyen99 committed Nov 18, 2023
1 parent 8bff428 commit 6eafbfd
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions cursus/views/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def authorize(provider: str):
return flask.abort(404)

flask.session["oauth2_state"] = secrets.token_urlsafe(16)
next = flask.request.args.get("next")
next = flask.request.args.get("next", None)

query_string = urlencode(
{
Expand Down Expand Up @@ -55,7 +55,9 @@ def callback(provider: str):
if not provider_data:
return flask.abort(404)

if flask.request.args.get("state") != flask.session["oauth2_state"]:
state = flask.request.args.get("state", None)

if state is None or state != flask.session["oauth2_state"]:
return flask.abort(401)

if "code" not in flask.request.args:
Expand Down Expand Up @@ -87,7 +89,10 @@ def callback(provider: str):
if not oauth2_token:
return flask.abort(401)

token_response = response.json()
try:
token_response = response.json()
except Exception:
flask.abort(400, "Invalid token response")

response = requests.get(
provider_data["userinfo"]["url"],
Expand All @@ -98,9 +103,12 @@ def callback(provider: str):
)

if response.status_code != 200:
return flask.abort(401)
return flask.abort(401, "Cannot get user info")

data_response = response.json()
try:
data_response = response.json()
except Exception:
flask.abort(400, "Invalid data response")

uniform_account = get_account(provider, token_response, data_response)
account_from_database = (
Expand Down Expand Up @@ -139,8 +147,6 @@ def callback(provider: str):

db.session.commit()

print(uniform_account.providerAccountId)

# Select User based on Account
user_for_login = (
db.session.query(User)
Expand Down

0 comments on commit 6eafbfd

Please sign in to comment.