diff --git a/api/routers/core.py b/api/routers/core.py index d28c1d4d2..49782e2a0 100644 --- a/api/routers/core.py +++ b/api/routers/core.py @@ -1,8 +1,6 @@ from typing import Union -from decouple import config -from fastapi import Depends, Body, HTTPException, Response, status -from fastapi.responses import JSONResponse +from fastapi import Depends, Body import schemas from chalicelib.core import log_tool_rollbar, sourcemaps, events, sessions_assignments, projects, \ @@ -14,52 +12,12 @@ from chalicelib.core import log_tool_rollbar, sourcemaps, events, sessions_assig custom_metrics, saved_search, integrations_global, feature_flags from chalicelib.core.collaboration_msteams import MSTeams from chalicelib.core.collaboration_slack import Slack -from chalicelib.utils import helper, captcha, s3 from or_dependencies import OR_context from routers.base import get_routers public_app, app, app_apikey = get_routers() -@public_app.post('/login', tags=["authentication"]) -async def login(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 = users.authenticate(data.email, data.password) - 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] - ) - - r["smtp"] = helper.has_smtp() - content = { - 'jwt': r.pop('jwt'), - 'data': { - "user": r - } - } - response = JSONResponse(content=content) - response.set_cookie(key="jwt", value=content['jwt'], domain=helper.get_domain(), - expires=config("JWT_EXPIRATION", cast=int)) - return response - - -@app.get('/logout', tags=["login", "logout"]) -async def logout_user(response: Response, context: schemas.CurrentContext = Depends(OR_context)): - response.delete_cookie("jwt") - return {"data": "success"} - - @app.post('/{projectId}/sessions/search', tags=["sessions"]) async def sessions_search(projectId: int, data: schemas.FlatSessionsSearchPayloadSchema = Body(...), context: schemas.CurrentContext = Depends(OR_context)): diff --git a/api/routers/core_dynamic.py b/api/routers/core_dynamic.py index 9fe664d95..92788da3f 100644 --- a/api/routers/core_dynamic.py +++ b/api/routers/core_dynamic.py @@ -2,6 +2,7 @@ from typing import Optional, Union from decouple import config from fastapi import Body, Depends, BackgroundTasks +from fastapi import HTTPException, status from starlette.responses import RedirectResponse, FileResponse import schemas @@ -11,6 +12,7 @@ from chalicelib.core import sessions_viewed from chalicelib.core import tenants, users, projects, license from chalicelib.core import webhook from chalicelib.core.collaboration_slack import Slack +from chalicelib.utils import captcha from chalicelib.utils import helper from chalicelib.utils.TimeUTC import TimeUTC from or_dependencies import OR_context @@ -35,6 +37,42 @@ if not tenants.tenants_exists(use_pool=False): return signup.create_tenant(data) +@public_app.post('/login', tags=["authentication"]) +async def login_user(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 = users.authenticate(data.email, data.password) + 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] + ) + + r["smtp"] = helper.has_smtp() + content = { + 'jwt': r.pop('jwt'), + 'data': { + "user": r + } + } + + return content + + +@app.get('/logout', tags=["login", "logout"]) +async def logout_user(context: schemas.CurrentContext = Depends(OR_context)): + return {"data": "success"} + + @app.get('/account', tags=['accounts']) async def get_account(context: schemas.CurrentContext = Depends(OR_context)): r = users.get(tenant_id=context.tenant_id, user_id=context.user_id) diff --git a/ee/api/routers/core_dynamic.py b/ee/api/routers/core_dynamic.py index ed9d14212..6bacfa3be 100644 --- a/ee/api/routers/core_dynamic.py +++ b/ee/api/routers/core_dynamic.py @@ -2,6 +2,7 @@ from typing import Optional, Union from decouple import config from fastapi import Body, Depends, BackgroundTasks, Request +from fastapi import HTTPException, status from starlette.responses import RedirectResponse, FileResponse import schemas @@ -13,6 +14,7 @@ from chalicelib.core import tenants, users, projects, license from chalicelib.core import webhook from chalicelib.core.collaboration_slack import Slack from chalicelib.utils import SAML2_helper +from chalicelib.utils import captcha from chalicelib.utils import helper from chalicelib.utils.TimeUTC import TimeUTC from or_dependencies import OR_context, OR_scope @@ -39,6 +41,42 @@ if config("MULTI_TENANTS", cast=bool, default=False) or not tenants.tenants_exis return signup.create_tenant(data) +@public_app.post('/login', tags=["authentication"]) +async def login_user(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 = users.authenticate(data.email, data.password) + 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] + ) + + r["smtp"] = helper.has_smtp() + content = { + 'jwt': r.pop('jwt'), + 'data': { + "user": r + } + } + + return content + + +@app.get('/logout', tags=["login", "logout"]) +async def logout_user(context: schemas.CurrentContext = Depends(OR_context)): + return {"data": "success"} + + @app.get('/account', tags=['accounts']) async def get_account(context: schemas.CurrentContext = Depends(OR_context)): r = users.get(tenant_id=context.tenant_id, user_id=context.user_id)