Skip to content

Commit

Permalink
update naming in AuthManager and AuthView to be consistent with the API
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbinning committed Oct 15, 2024
1 parent 6c91c1d commit d6e5ae5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
21 changes: 10 additions & 11 deletions backend/api/AuthView.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self):
self.am = AuthManager()

async def webauthn_register_options(self, body: RegistrationOptions):
challenge, options = await self.am.registration_options(body["email"])
challenge, options = await self.am.webauthn_register_options(body["email"])

if not options:
return JSONResponse({"error": "Something went wrong"}, status_code=500)
Expand All @@ -20,10 +20,9 @@ async def webauthn_register_options(self, body: RegistrationOptions):
response.set_cookie(key="challenge",value=challenge, secure=True, httponly=True, samesite='strict')
return response


async def webauthn_register(self, body: VerifyRegistration):
challenge = request.cookies.get("challenge")
token = await self.am.registrationResponse(challenge, body["email"], body["user_id"], body["att_resp"])
token = await self.am.webauthn_register(challenge, body["email"], body["user_id"], body["att_resp"])
if not token:
return JSONResponse({"message": "Failed"}, status_code=401)

Expand All @@ -33,24 +32,24 @@ async def webauthn_register(self, body: VerifyRegistration):
return response

async def webauthn_login_options(self, body: AuthenticationOptions):
challenge, options = await self.am.signinRequestOptions(body["email"])
challenge, options = await self.am.webauthn_login_options(body["email"])

if not options:
return JSONResponse({"error": "Something went wrong"}, status_code=500)


response = JSONResponse({"options": options}, status_code=200)
response.set_cookie(key="challenge", value=challenge, secure=True, httponly=True, samesite='strict')
return response

async def webauthn_login(self, body: VerifyAuthentication):
token = await self.am.signinResponse(body["challenge"], body["email"], body["auth_resp"])

async def webauthn_login(self, body: VerifyAuthentication):
challenge = request.cookies.get("challenge")
token = await self.am.webauthn_login(challenge, body["email"], body["auth_resp"])
if not token:
return JSONResponse({"error": "Authentication failed."}, status_code=401)
return JSONResponse({"message": "Failed"}, status_code=401)

response = JSONResponse({"message": "Success", "token": token}, status_code=200)
response.set_cookie(key="challenge", value="", expires=0, secure=True, httponly=True, samesite='strict')
response.set_cookie(key="challenge",value="", expires=0,secure=True, httponly=True, samesite='strict')

return response

async def logout(self):
Expand Down
14 changes: 5 additions & 9 deletions backend/managers/AuthManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def __init__(self):
if not hasattr(self, '_initialized'):
self._initialized = True

async def registration_options(self, email_id: str):
async def webauthn_register_options(self, email_id: str):
async with db_session_context() as session:
result = await session.execute(select(User).where(User.email == email_id))
user = result.scalar_one_or_none()
Expand Down Expand Up @@ -121,7 +121,7 @@ async def registration_options(self, email_id: str):

return challenge, options_to_json(options)

async def registrationResponse(self, challenge: str, email_id: str, user_id: str, response):
async def webauthn_register(self, challenge: str, email_id: str, user_id: str, response):
async with db_session_context() as session:
host = get_env_key('PAIOS_HOST', 'localhost')
port = get_env_key('PAIOS_PORT', '8443')
Expand All @@ -147,12 +147,9 @@ async def registrationResponse(self, challenge: str, email_id: str, user_id: str
await session.refresh(new_user)
user = new_user

# _, token = await self.create_session(user.id)

base64url_cred_id = base64.urlsafe_b64encode(res.credential_id).decode("utf-8").rstrip("=")
base64url_public_key = base64.urlsafe_b64encode(res.credential_public_key).decode("utf-8").rstrip("=")


transports = json.dumps(response["response"]["transports"])
new_cred = Cred(id=base64url_cred_id, public_key=base64url_public_key, webauthn_user_id=user.webauthn_user_id, backed_up=res.credential_backed_up, name=email_id, transports=transports)
session.add(new_cred)
Expand All @@ -167,7 +164,7 @@ async def registrationResponse(self, challenge: str, email_id: str, user_id: str
token = generate_jwt(payload)
return token

async def signinRequestOptions(self, email_id: str):
async def webauthn_login_options(self, email_id: str):
async with db_session_context() as session:
user_result = await session.execute(select(User).where(User.email == email_id))
user = user_result.scalar_one_or_none()
Expand Down Expand Up @@ -198,7 +195,7 @@ async def signinRequestOptions(self, email_id: str):
challenge = base64.urlsafe_b64encode(options.challenge).decode("utf-8").rstrip("=")
return challenge, options_to_json(options)

async def signinResponse(self, challenge: str, email_id:str, response):
async def webauthn_login(self, challenge: str, email_id:str, response):
async with db_session_context() as session:
host = get_env_key('PAIOS_HOST', 'localhost')
port = get_env_key('PAIOS_PORT', '8443')
Expand Down Expand Up @@ -229,7 +226,6 @@ async def signinResponse(self, challenge: str, email_id:str, response):
if not res.new_sign_count != 1:
return None

# _, session_token = await self.create_session(user.id)
payload = {
"sub": user.id,
"iat": datetime.utcnow(),
Expand All @@ -256,4 +252,4 @@ async def delete_session(self, token: str):
async with db_session_context() as session:
stmt = delete(Session).where(Session.token == token)
await session.execute(stmt)
await session.commit()
await session.commit()

0 comments on commit d6e5ae5

Please sign in to comment.