Skip to content

Commit

Permalink
refactor(chalice): refactored and cleaned Spot code
Browse files Browse the repository at this point in the history
refactor(chalice): refactored and cleaned login code
  • Loading branch information
tahayk committed Aug 2, 2024
1 parent a91d2e0 commit de139af
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 42 deletions.
10 changes: 6 additions & 4 deletions api/routers/core_dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

public_app, app, app_apikey = get_routers()

COOKIE_PATH = "/api/refresh"


@public_app.get('/signup', tags=['signup'])
async def get_all_signup():
Expand All @@ -39,7 +41,7 @@ async def signup_handler(response: JSONResponse, data: schemas.UserSignupSchema
return content
refresh_token = content.pop("refreshToken")
refresh_token_max_age = content.pop("refreshTokenMaxAge")
response.set_cookie(key="refreshToken", value=refresh_token, path="/api/refresh",
response.set_cookie(key="refreshToken", value=refresh_token, path=COOKIE_PATH,
max_age=refresh_token_max_age, secure=True, httponly=True)
return content

Expand Down Expand Up @@ -78,7 +80,7 @@ def login_user(response: JSONResponse, spot: Optional[bool] = False, data: schem
spot_refresh_token = r.pop("spotRefreshToken")
spot_refresh_token_max_age = r.pop("spotRefreshTokenMaxAge")

response.set_cookie(key="refreshToken", value=refresh_token, path="/api/refresh",
response.set_cookie(key="refreshToken", value=refresh_token, path=COOKIE_PATH,
max_age=refresh_token_max_age, secure=True, httponly=True)
if spot:
response.set_cookie(key="spotRefreshToken", value=spot_refresh_token, path="/api/spot/refresh",
Expand All @@ -89,7 +91,7 @@ def login_user(response: JSONResponse, spot: Optional[bool] = False, data: schem
@app.get('/logout', tags=["login"])
def logout_user(response: Response, context: schemas.CurrentContext = Depends(OR_context)):
users.logout(user_id=context.user_id)
response.delete_cookie(key="refreshToken", path="/api/refresh")
response.delete_cookie(key="refreshToken", path=COOKIE_PATH)
response.delete_cookie(key="spotRefreshToken", path="/api/spot/refresh")
return {"data": "success"}

Expand All @@ -98,7 +100,7 @@ def logout_user(response: Response, context: schemas.CurrentContext = Depends(OR
def refresh_login(response: JSONResponse, context: schemas.CurrentContext = Depends(OR_context)):
r = users.refresh(user_id=context.user_id)
content = {"jwt": r.get("jwt")}
response.set_cookie(key="refreshToken", value=r.get("refreshToken"), path="/api/refresh",
response.set_cookie(key="refreshToken", value=r.get("refreshToken"), path=COOKIE_PATH,
max_age=r.pop("refreshTokenMaxAge"), secure=True, httponly=True)
return content

Expand Down
35 changes: 1 addition & 34 deletions api/routers/subs/spot.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,10 @@
COOKIE_PATH = "/api/spot/refresh"


@public_app.post('/login')
def login_spot(response: JSONResponse, data: schemas.UserLoginSchema = Body(...)):
if helper.allow_captcha() and not captcha.is_valid(data.g_recaptcha_response):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid captcha."
)

r = spot.authenticate(data.email, data.password.get_secret_value())
if r is None:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="You've entered invalid Email or Password."
)
if "errors" in r:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail=r["errors"][0]
)

refresh_token = r.pop("refreshToken")
refresh_token_max_age = r.pop("refreshTokenMaxAge")
content = {
'jwt': r.pop('jwt'),
'data': {
"user": r
}
}
response.set_cookie(key="spotRefreshToken", value=refresh_token, path=COOKIE_PATH,
max_age=refresh_token_max_age, secure=True, httponly=True)
return content


@app.get('/logout')
def logout_spot(response: Response, context: schemas.CurrentContext = Depends(OR_context)):
spot.logout(user_id=context.user_id)
response.delete_cookie(key="spotRefreshToken", path="/api/refresh")
response.delete_cookie(key="spotRefreshToken", path=COOKIE_PATH)
return {"data": "success"}


Expand Down
10 changes: 6 additions & 4 deletions ee/api/routers/core_dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

public_app, app, app_apikey = get_routers()

COOKIE_PATH = "/api/refresh"


@public_app.get('/signup', tags=['signup'])
async def get_all_signup():
Expand All @@ -45,7 +47,7 @@ async def signup_handler(response: JSONResponse, data: schemas.UserSignupSchema
return content
refresh_token = content.pop("refreshToken")
refresh_token_max_age = content.pop("refreshTokenMaxAge")
response.set_cookie(key="refreshToken", value=refresh_token, path="/api/refresh",
response.set_cookie(key="refreshToken", value=refresh_token, path=COOKIE_PATH,
max_age=refresh_token_max_age, secure=True, httponly=True)
return content

Expand Down Expand Up @@ -84,7 +86,7 @@ def login_user(response: JSONResponse, spot: Optional[bool] = False, data: schem
spot_refresh_token = r.pop("spotRefreshToken")
spot_refresh_token_max_age = r.pop("spotRefreshTokenMaxAge")

response.set_cookie(key="refreshToken", value=refresh_token, path="/api/refresh",
response.set_cookie(key="refreshToken", value=refresh_token, path=COOKIE_PATH,
max_age=refresh_token_max_age, secure=True, httponly=True)
if spot:
response.set_cookie(key="spotRefreshToken", value=spot_refresh_token, path="/api/spot/refresh",
Expand All @@ -95,7 +97,7 @@ def login_user(response: JSONResponse, spot: Optional[bool] = False, data: schem
@app.get('/logout', tags=["login"])
def logout_user(response: Response, context: schemas.CurrentContext = Depends(OR_context)):
users.logout(user_id=context.user_id)
response.delete_cookie(key="refreshToken", path="/api/refresh")
response.delete_cookie(key="refreshToken", path=COOKIE_PATH)
response.delete_cookie(key="spotRefreshToken", path="/api/spot/refresh")
return {"data": "success"}

Expand All @@ -105,7 +107,7 @@ def refresh_login(context: schemas.CurrentContext = Depends(OR_context)):
r = users.refresh(user_id=context.user_id, tenant_id=context.tenant_id)
content = {"jwt": r.get("jwt")}
response = JSONResponse(content=content)
response.set_cookie(key="refreshToken", value=r.get("refreshToken"), path="/api/refresh",
response.set_cookie(key="refreshToken", value=r.get("refreshToken"), path=COOKIE_PATH,
max_age=r.pop("refreshTokenMaxAge"), secure=True, httponly=True)
return response

Expand Down

0 comments on commit de139af

Please sign in to comment.