Skip to content

Commit

Permalink
fix(core): use tracing wrappers in email login (#3982)
Browse files Browse the repository at this point in the history
  • Loading branch information
dolcalmi authored Feb 12, 2024
1 parent 89b59ad commit 6dc4a87
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
8 changes: 6 additions & 2 deletions core/api/src/graphql/public/root/mutation/user-logout.ts
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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 }
Expand Down
54 changes: 23 additions & 31 deletions core/api/src/servers/authentication/index.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,35 @@
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 {
checkedToAuthToken,
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 })

Expand Down Expand Up @@ -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 })
Expand Down Expand Up @@ -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" })
Expand Down Expand Up @@ -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,
})
Expand Down

0 comments on commit 6dc4a87

Please sign in to comment.