Skip to content

Commit 3d40648

Browse files
authored
feat(auth): use gnap error middleware on idp api (#3094)
* feat(auth): use gnap error middleware on idp api * feat: add gnap error schema to idp spec * feat: use schemas from updated OP package where available * feat: add missing responses to spec
1 parent 286f146 commit 3d40648

File tree

6 files changed

+444
-211
lines changed

6 files changed

+444
-211
lines changed

packages/auth/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"@graphql-tools/load": "^8.0.12",
3131
"@graphql-tools/schema": "^10.0.16",
3232
"@interledger/http-signature-utils": "2.0.2",
33-
"@interledger/open-payments": "6.13.2",
33+
"@interledger/open-payments": "6.14.0",
3434
"@interledger/openapi": "2.0.2",
3535
"@koa/cors": "^5.0.0",
3636
"@koa/router": "^12.0.2",

packages/auth/src/app.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ export class App {
421421

422422
const router = new Router<DefaultState, AppContext>()
423423
router.use(bodyParser())
424+
router.use(gnapServerErrorMiddleware)
424425

425426
const openApi = await this.container.use('openApi')
426427
const interactionRoutes = await this.container.use('interactionRoutes')

packages/auth/src/interaction/routes.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ describe('Interaction Routes', (): void => {
9191
)
9292

9393
await expect(interactionRoutes.start(ctx)).rejects.toMatchObject({
94-
status: 401,
94+
status: 400,
9595
code: GNAPErrorCode.UnknownInteraction,
9696
message: 'unknown interaction'
9797
})
@@ -120,9 +120,9 @@ describe('Interaction Routes', (): void => {
120120
)
121121

122122
await expect(interactionRoutes.start(ctx)).rejects.toMatchObject({
123-
status: 401,
124-
code: GNAPErrorCode.UnknownInteraction,
125-
message: 'unknown interaction'
123+
status: 403,
124+
code: GNAPErrorCode.InvalidInteraction,
125+
message: 'invalid interaction'
126126
})
127127
})
128128

packages/auth/src/interaction/routes.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,22 @@ async function startInteraction(
168168
const { config, interactionService, grantService, logger } = deps
169169
const interaction = await interactionService.getBySession(interactId, nonce)
170170

171+
if (!interaction) {
172+
throw new GNAPServerRouteError(
173+
400,
174+
GNAPErrorCode.UnknownInteraction,
175+
'unknown interaction'
176+
)
177+
}
178+
171179
if (
172-
!interaction ||
173180
interaction.state !== InteractionState.Pending ||
174181
isRevokedGrant(interaction.grant)
175182
) {
176183
throw new GNAPServerRouteError(
177-
401,
178-
GNAPErrorCode.UnknownInteraction,
179-
'unknown interaction'
184+
403,
185+
GNAPErrorCode.InvalidInteraction,
186+
'invalid interaction'
180187
)
181188
}
182189

packages/auth/src/openapi/specs/id-provider.yaml

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,24 @@ paths:
3737
schema:
3838
type: string
3939
description: Interaction id
40-
'401':
40+
'400':
4141
description: Unauthorized
42+
content:
43+
application/json:
44+
schema:
45+
$ref: '#/components/schemas/error-unknown-interaction'
46+
'403':
47+
description: Invalid Request
48+
content:
49+
application/json:
50+
schema:
51+
$ref: '#/components/schemas/error-invalid-interaction'
52+
'500':
53+
description: Internal Server Error
54+
content:
55+
application/json:
56+
schema:
57+
$ref: './auth-server.yaml#/components/schemas/error-request-denied'
4258
operationId: get-interact
4359
parameters:
4460
- schema:
@@ -89,9 +105,19 @@ paths:
89105
description: Client finish endpoint
90106
'401':
91107
description: Unauthorized
108+
content:
109+
application/json:
110+
schema:
111+
oneOf:
112+
- $ref: './auth-server.yaml#/components/schemas/error-invalid-request'
113+
- $ref: '#/components/schemas/error-invalid-interaction'
92114
'404':
93115
description: Not Found
94-
description: "This endpoint is called by the identity provider to end the user interaction and redirect the user to the client's finish URL."
116+
content:
117+
application/json:
118+
schema:
119+
$ref: '#/components/schemas/error-unknown-interaction'
120+
description: "To finish the user interaction for grant approval, this endpoint redirects the user to the client's finish url."
95121
parameters:
96122
- schema:
97123
type: string
@@ -136,8 +162,16 @@ paths:
136162
type: string
137163
'401':
138164
description: Unauthorized
165+
content:
166+
application/json:
167+
schema:
168+
$ref: './auth-server.yaml#/components/schemas/error-invalid-request'
139169
'404':
140170
description: Not Found
171+
content:
172+
application/json:
173+
schema:
174+
$ref: '#/components/schemas/error-unknown-interaction'
141175
operationId: get-grant
142176
description: |
143177
This endpoint is called by the identity provider to get the grant details associated with the `interactId` on the front-channel. The identity provider will display the details to the user to either accept or deny.
@@ -164,12 +198,28 @@ paths:
164198
'202':
165199
description: Accepted
166200
'400':
167-
description: Not Found
201+
description: Bad Request
202+
content:
203+
application/json:
204+
schema:
205+
oneOf:
206+
- $ref: '#/components/schemas/error-invalid-interaction'
207+
- $ref: './auth-server.yaml#/components/schemas/error-invalid-request'
168208
'401':
169209
description: Unauthorized
210+
content:
211+
application/json:
212+
schema:
213+
oneOf:
214+
- $ref: '#/components/schemas/error-invalid-interaction'
215+
- $ref: '#/components/schemas/error-user-denied'
170216
'404':
171217
description: Not Found
172-
description: This endpoint is called by the identity provider to communicate the user's choice (acceptance or rejection) to the authorization server.
218+
content:
219+
application/json:
220+
schema:
221+
$ref: '#/components/schemas/error-unknown-interaction'
222+
description: The Identity Provider uses this endpoint to submit the user's choice regarding accepting or rejecting a grant to Authorization Server.
173223
parameters:
174224
- schema:
175225
type: string
@@ -195,7 +245,43 @@ paths:
195245
tags:
196246
- back-channel
197247
components:
198-
schemas: {}
248+
schemas:
249+
error-unknown-interaction:
250+
type: object
251+
properties:
252+
error:
253+
type: object
254+
properties:
255+
description:
256+
type: string
257+
code:
258+
type: string
259+
enum:
260+
- unknown_interaction
261+
error-invalid-interaction:
262+
type: object
263+
properties:
264+
error:
265+
type: object
266+
properties:
267+
description:
268+
type: string
269+
code:
270+
type: string
271+
enum:
272+
- invalid_interaction
273+
error-user-denied:
274+
type: object
275+
properties:
276+
error:
277+
type: object
278+
properties:
279+
description:
280+
type: string
281+
code:
282+
type: string
283+
enum:
284+
- user_denied
199285
securitySchemes:
200286
GNAP:
201287
name: Authorization

0 commit comments

Comments
 (0)