From 3217a55bca70858febee7585e1dde00ecb86f56c Mon Sep 17 00:00:00 2001 From: Taha Yassine Kraiem Date: Fri, 10 Jun 2022 15:29:54 +0200 Subject: [PATCH] feat(api): changed login response --- api/chalicelib/core/users.py | 2 +- api/routers/core.py | 33 +++++++++++++++++++++-- api/routers/core_dynamic.py | 39 +--------------------------- ee/api/chalicelib/core/users.py | 2 +- ee/api/routers/core_dynamic.py | 46 +++------------------------------ 5 files changed, 37 insertions(+), 85 deletions(-) diff --git a/api/chalicelib/core/users.py b/api/chalicelib/core/users.py index 0e9852e2d..082e9aca9 100644 --- a/api/chalicelib/core/users.py +++ b/api/chalicelib/core/users.py @@ -557,7 +557,7 @@ def authenticate(email, password, for_change_password=False, for_plugin=False): with pg_client.PostgresClient() as cur: query = cur.mogrify( f"""SELECT - users.user_id AS id, + users.user_id, 1 AS tenant_id, users.role, users.name, diff --git a/api/routers/core.py b/api/routers/core.py index 2a38d0a75..c997229ba 100644 --- a/api/routers/core.py +++ b/api/routers/core.py @@ -1,7 +1,8 @@ from typing import Union from decouple import config -from fastapi import Depends, Body, BackgroundTasks +from fastapi import Depends, Body, BackgroundTasks, HTTPException +from starlette import status import schemas from chalicelib.core import log_tool_rollbar, sourcemaps, events, sessions_assignments, projects, \ @@ -13,7 +14,7 @@ from chalicelib.core import log_tool_rollbar, sourcemaps, events, sessions_assig assist, heatmaps, mobile, signup, tenants, errors_favorite_viewed, boarding, notifications, webhook, users, \ custom_metrics, saved_search from chalicelib.core.collaboration_slack import Slack -from chalicelib.utils import email_helper +from chalicelib.utils import email_helper, helper, captcha from chalicelib.utils.TimeUTC import TimeUTC from or_dependencies import OR_context from routers.base import get_routers @@ -21,6 +22,34 @@ from routers.base import get_routers public_app, app, app_apikey = get_routers() +@public_app.post('/login', tags=["authentication"]) +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, for_plugin=False) + 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() + return { + 'jwt': r.pop('jwt'), + 'data': { + "user": r + } + } + + @app.get('/{projectId}/sessions/{sessionId}', tags=["sessions"]) @app.get('/{projectId}/sessions2/{sessionId}', tags=["sessions"]) def get_session2(projectId: int, sessionId: Union[int, str], background_tasks: BackgroundTasks, diff --git a/api/routers/core_dynamic.py b/api/routers/core_dynamic.py index a407e2833..06cd2937a 100644 --- a/api/routers/core_dynamic.py +++ b/api/routers/core_dynamic.py @@ -1,17 +1,15 @@ from typing import Optional from decouple import config -from fastapi import Body, Depends, HTTPException, status, BackgroundTasks +from fastapi import Body, Depends, BackgroundTasks from starlette.responses import RedirectResponse import schemas -from chalicelib.core import assist from chalicelib.core import integrations_manager from chalicelib.core import sessions from chalicelib.core import tenants, users, metadata, 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 or_dependencies import OR_context from routers.base import get_routers @@ -27,41 +25,6 @@ def get_all_signup(): "edition": license.EDITION}} -@public_app.post('/login', tags=["authentication"]) -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, for_plugin=False) - if r is None: - raise HTTPException( - status_code=status.HTTP_401_UNAUTHORIZED, - detail="You’ve entered invalid Email or Password." - ) - - tenant_id = r.pop("tenantId") - - r["limits"] = { - "teamMember": -1, - "projects": -1, - "metadata": metadata.get_remaining_metadata_with_count(tenant_id)} - - c = tenants.get_by_tenant_id(tenant_id) - c.pop("createdAt") - c["smtp"] = helper.has_smtp() - r["smtp"] = c["smtp"] - return { - 'jwt': r.pop('jwt'), - 'data': { - "user": r, - "client": c - } - } - - @app.get('/account', tags=['accounts']) 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/chalicelib/core/users.py b/ee/api/chalicelib/core/users.py index 91c2384c4..6a51a1d80 100644 --- a/ee/api/chalicelib/core/users.py +++ b/ee/api/chalicelib/core/users.py @@ -627,7 +627,7 @@ def authenticate(email, password, for_change_password=False, for_plugin=False): with pg_client.PostgresClient() as cur: query = cur.mogrify( f"""SELECT - users.user_id AS id, + users.user_id, users.tenant_id, users.role, users.name, diff --git a/ee/api/routers/core_dynamic.py b/ee/api/routers/core_dynamic.py index 89f6a9bc9..73e597b52 100644 --- a/ee/api/routers/core_dynamic.py +++ b/ee/api/routers/core_dynamic.py @@ -1,17 +1,17 @@ from typing import Optional from decouple import config -from fastapi import Body, Depends, HTTPException, status, BackgroundTasks +from fastapi import Body, Depends, BackgroundTasks from starlette.responses import RedirectResponse import schemas import schemas_ee from chalicelib.core import integrations_manager from chalicelib.core import sessions -from chalicelib.core import tenants, users, metadata, projects, license, assist +from chalicelib.core import tenants, users, metadata, projects, license from chalicelib.core import webhook from chalicelib.core.collaboration_slack import Slack -from chalicelib.utils import captcha, SAML2_helper +from chalicelib.utils import SAML2_helper from chalicelib.utils import helper from or_dependencies import OR_context from routers.base import get_routers @@ -27,46 +27,6 @@ def get_all_signup(): "edition": license.EDITION}} -@public_app.post('/login', tags=["authentication"]) -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, for_plugin=False) - 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] - ) - - tenant_id = r.pop("tenantId") - - r["limits"] = { - "teamMember": -1, - "projects": -1, - "metadata": metadata.get_remaining_metadata_with_count(tenant_id)} - - c = tenants.get_by_tenant_id(tenant_id) - c.pop("createdAt") - c["smtp"] = helper.has_smtp() - r["smtp"] = c["smtp"] - return { - 'jwt': r.pop('jwt'), - 'data': { - "user": r, - "client": c - } - } - - @app.get('/account', tags=['accounts']) def get_account(context: schemas.CurrentContext = Depends(OR_context)): r = users.get(tenant_id=context.tenant_id, user_id=context.user_id)