From 093216af8aac5c56f53795152c04deb08d575cd9 Mon Sep 17 00:00:00 2001 From: Juan P Lopez Date: Mon, 12 Feb 2024 09:27:26 -0500 Subject: [PATCH] fix(core): use tracing wrappers in email login --- .../public/root/mutation/user-logout.ts | 8 ++- core/api/src/servers/authentication/index.ts | 54 ++++++++----------- 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/core/api/src/graphql/public/root/mutation/user-logout.ts b/core/api/src/graphql/public/root/mutation/user-logout.ts index c31710f059..d909f9f2ab 100644 --- a/core/api/src/graphql/public/root/mutation/user-logout.ts +++ b/core/api/src/graphql/public/root/mutation/user-logout.ts @@ -1,6 +1,6 @@ import { GT } from "@/graphql/index" -import { logoutToken } from "@/app/authentication" +import { Authentication } from "@/app" import { mapAndParseErrorForGqlResponse } from "@/graphql/error-map" import SuccessPayload from "@/graphql/shared/types/payload/success-payload" @@ -30,7 +30,11 @@ const UserLogoutMutation = GT.Field< resolve: async (_, args, { sessionId, user }) => { const deviceToken = args?.input?.deviceToken - const logoutResp = await logoutToken({ sessionId, deviceToken, userId: user.id }) + const logoutResp = await Authentication.logoutToken({ + sessionId, + deviceToken, + userId: user.id, + }) if (logoutResp instanceof Error) return { errors: [mapAndParseErrorForGqlResponse(logoutResp)], success: false } return { errors: [], success: true } diff --git a/core/api/src/servers/authentication/index.ts b/core/api/src/servers/authentication/index.ts index 9de9ead6c1..2c890930fe 100644 --- a/core/api/src/servers/authentication/index.ts +++ b/core/api/src/servers/authentication/index.ts @@ -1,25 +1,23 @@ +import basicAuth from "basic-auth" +import bodyParser from "body-parser" import cors from "cors" import express, { NextFunction, Request, Response } from "express" -import basicAuth from "basic-auth" - -import bodyParser from "body-parser" +import { mapError } from "@/graphql/error-map" import { Authentication } from "@/app" +import { registerCaptchaGeetest } from "@/app/captcha" -import { mapError } from "@/graphql/error-map" -import { - addAttributesToCurrentSpan, - recordExceptionInCurrentSpan, - tracer, -} from "@/services/tracing" +import { UNSECURE_IP_FROM_REQUEST_OBJECT } from "@/config" -import { - elevatingSessionWithTotp, - loginWithEmailToken, - requestEmailCode, -} from "@/app/authentication" import { parseIps } from "@/domain/accounts-ips" +import { checkedToEmailCode, validOneTimeAuthCodeValue } from "@/domain/authentication" +import { + EmailCodeInvalidError, + EmailValidationSubmittedTooOftenError, +} from "@/domain/authentication/errors" +import { UserLoginIpRateLimiterExceededError } from "@/domain/rate-limit/errors" +import { parseErrorMessageFromUnknown } from "@/domain/shared" import { checkedToEmailAddress, checkedToPhoneNumber } from "@/domain/users" import { @@ -27,21 +25,11 @@ import { checkedToEmailLoginId, checkedToTotpCode, } from "@/services/kratos" - -import { UNSECURE_IP_FROM_REQUEST_OBJECT } from "@/config" - -import { parseErrorMessageFromUnknown } from "@/domain/shared" - -import { checkedToEmailCode, validOneTimeAuthCodeValue } from "@/domain/authentication" - import { - EmailCodeInvalidError, - EmailValidationSubmittedTooOftenError, -} from "@/domain/authentication/errors" - -import { UserLoginIpRateLimiterExceededError } from "@/domain/rate-limit/errors" - -import { registerCaptchaGeetest } from "@/app/captcha" + addAttributesToCurrentSpan, + recordExceptionInCurrentSpan, + tracer, +} from "@/services/tracing" const authRouter = express.Router({ caseSensitive: true }) @@ -136,7 +124,7 @@ authRouter.post("/email/code", async (req: Request, res: Response) => { } try { - const emailLoginId = await requestEmailCode({ email, ip }) + const emailLoginId = await Authentication.requestEmailCode({ email, ip }) if (emailLoginId instanceof Error) { recordExceptionInCurrentSpan({ error: emailLoginId.message }) return res.status(500).send({ error: emailLoginId.message }) @@ -174,7 +162,11 @@ authRouter.post("/email/login", async (req: Request, res: Response) => { } try { - const result = await loginWithEmailToken({ ip, emailFlowId: emailLoginId, code }) + const result = await Authentication.loginWithEmailToken({ + ip, + emailFlowId: emailLoginId, + code, + }) if (result instanceof EmailCodeInvalidError) { recordExceptionInCurrentSpan({ error: result }) return res.status(401).send({ error: "invalid code" }) @@ -224,7 +216,7 @@ authRouter.post("/totp/validate", async (req: Request, res: Response) => { } try { - const result = await elevatingSessionWithTotp({ + const result = await Authentication.elevatingSessionWithTotp({ totpCode, authToken, })