Skip to content

Commit 2beb798

Browse files
feat: return b64encoded token after successful login
Align the implementation to @approvio/api v0.0.18
1 parent 3b26df7 commit 2beb798

4 files changed

Lines changed: 41 additions & 18 deletions

File tree

app/controllers/src/auth/auth.controller.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
import {Controller, Get, Post, Res, Logger, UnauthorizedException, Query, Body, HttpCode} from "@nestjs/common"
1+
import {
2+
Controller,
3+
Get,
4+
Post,
5+
Res,
6+
Logger,
7+
UnauthorizedException,
8+
Query,
9+
Body,
10+
HttpCode,
11+
BadRequestException
12+
} from "@nestjs/common"
213
import {Response} from "express"
314
import {AuthService, GenerateChallengeRequest} from "@services"
415
import {isLeft} from "fp-ts/lib/Either"
@@ -8,12 +19,12 @@ import {GetAuthenticatedEntity} from "../../../main/src/auth"
819
import {
920
TokenRequest,
1021
TokenResponse,
11-
AuthMessageResponse,
22+
SuccessfulAuthResponse,
23+
FailedAuthResponse,
1224
AgentChallengeRequest,
1325
AgentChallengeResponse,
1426
AgentTokenResponse
1527
} from "@approvio/api"
16-
1728
import {
1829
mapAgentChallengeRequestToService,
1930
mapChallengeToApiResponse,
@@ -25,6 +36,7 @@ import {
2536
} from "./agent-auth.mappers"
2637
import {pipe} from "fp-ts/lib/function"
2738
import {AuthenticatedEntity} from "@domain"
39+
import {generateErrorPayload} from "@controllers/error"
2840

2941
/**
3042
* ┌─────────────────────────────────────────────────────────────────────────────────────────┐
@@ -155,13 +167,20 @@ export class AuthController {
155167

156168
@PublicRoute()
157169
@Get("success")
158-
async success(): Promise<AuthMessageResponse> {
159-
return {message: "Authentication successful. Use the code and state to generate a JWT token."}
170+
async success(@Query("code") code: string, @Query("state") state: string): Promise<SuccessfulAuthResponse> {
171+
if (!code) throw new BadRequestException(generateErrorPayload("MISSING_CODE", "missing code"))
172+
if (!state) throw new BadRequestException(generateErrorPayload("MISSING_STATE", "missing state"))
173+
return {
174+
message: "Authentication successful. Use the code and state to generate a JWT token.",
175+
code: code,
176+
state: state,
177+
b64encoded: Buffer.from(`${code}:${state}`, "utf-8").toString("base64")
178+
}
160179
}
161180

162181
@PublicRoute()
163182
@Get("error")
164-
async error(): Promise<AuthMessageResponse> {
183+
async error(): Promise<FailedAuthResponse> {
165184
return {message: "Authentication failed. Please try again."}
166185
}
167186

app/main/test/integration/auth/auth.integration.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {cleanDatabase, prepareDatabase} from "@test/database"
77
import {ConfigProvider} from "@external/config"
88
import {MockConfigProvider} from "@test/mock-data"
99
import {PrismaClient} from "@prisma/client"
10+
import "expect-more-jest"
1011

1112
describe("Auth Integration", () => {
1213
let app: INestApplication
@@ -76,11 +77,14 @@ describe("Auth Integration", () => {
7677

7778
describe("GET /auth/success", () => {
7879
it("should return success message for stateless flow", async () => {
79-
const response = await request(app.getHttpServer()).get("/auth/success")
80+
const response = await request(app.getHttpServer()).get("/auth/success?code=123&state=456")
8081

8182
expect(response).toHaveStatusCode(200)
82-
expect(response.body).toEqual({
83-
message: "Authentication successful. Use the code and state to generate a JWT token."
83+
expect(response.body).toMatchObject({
84+
message: "Authentication successful. Use the code and state to generate a JWT token.",
85+
code: expect.toBeString(),
86+
state: expect.toBeString(),
87+
b64encoded: expect.toBeString()
8488
})
8589
})
8690
})

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
},
3333
"packageManager": "yarn@4.7.0",
3434
"dependencies": {
35-
"@approvio/api": "0.0.17",
35+
"@approvio/api": "0.0.18",
3636
"@nestjs/bull": "11.0.4",
3737
"@nestjs/common": "11.1.9",
3838
"@nestjs/core": "11.1.9",
@@ -80,7 +80,7 @@
8080
"expect-more-jest": "5.5.0",
8181
"ioredis": "5.8.2",
8282
"jest": "29.7.0",
83-
"node-loader": "^2.1.0",
83+
"node-loader": "2.1.0",
8484
"prettier": "3.7.4",
8585
"pretty-format": "30.2.0",
8686
"prisma": "7.2.0",

yarn.lock

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,18 @@ __metadata:
100100
languageName: node
101101
linkType: hard
102102

103-
"@approvio/api@npm:0.0.17":
104-
version: 0.0.17
105-
resolution: "@approvio/api@npm:0.0.17"
106-
checksum: 10/67eb3bf7f79c68437a30c88db9ecd77f9753209dfc9d86a6e7e7541ad16bda78788e2dba49fac2c8cb115690f1828fb995f2ee3bf495d187f7b3606bd21f9a41
103+
"@approvio/api@npm:0.0.18":
104+
version: 0.0.18
105+
resolution: "@approvio/api@npm:0.0.18"
106+
checksum: 10/44420111f4b05893fedc2779548ca14c514ef14cb8a21ab19d462202abc9a76b6b2adf65fce3498a2aa8254f7024be68104eb364cfeb29189706f577959707d8
107107
languageName: node
108108
linkType: hard
109109

110110
"@approvio/backend@workspace:.":
111111
version: 0.0.0-use.local
112112
resolution: "@approvio/backend@workspace:."
113113
dependencies:
114-
"@approvio/api": "npm:0.0.17"
114+
"@approvio/api": "npm:0.0.18"
115115
"@nestjs/bull": "npm:11.0.4"
116116
"@nestjs/cli": "npm:11.0.14"
117117
"@nestjs/common": "npm:11.1.9"
@@ -150,7 +150,7 @@ __metadata:
150150
fp-ts: "npm:2.16.11"
151151
ioredis: "npm:5.8.2"
152152
jest: "npm:29.7.0"
153-
node-loader: "npm:^2.1.0"
153+
node-loader: "npm:2.1.0"
154154
nodemailer: "npm:7.0.11"
155155
openid-client: "npm:6.8.1"
156156
passport: "npm:0.7.0"
@@ -9081,7 +9081,7 @@ __metadata:
90819081
languageName: node
90829082
linkType: hard
90839083

9084-
"node-loader@npm:^2.1.0":
9084+
"node-loader@npm:2.1.0":
90859085
version: 2.1.0
90869086
resolution: "node-loader@npm:2.1.0"
90879087
dependencies:

0 commit comments

Comments
 (0)