-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathmiddleware.ts
97 lines (94 loc) · 2.86 KB
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { AccessType, AccessAction } from './grant'
import { Transaction } from 'objection'
import { GrantReference } from '../grantReference/model'
import { HttpSigContext, verifySigAndChallenge } from 'auth'
export function createAuthMiddleware({
type,
action
}: {
type: AccessType
action: AccessAction
}) {
return async (
ctx: HttpSigContext,
next: () => Promise<unknown>
): Promise<void> => {
const config = await ctx.container.use('config')
const grantReferenceService = await ctx.container.use(
'grantReferenceService'
)
const logger = await ctx.container.use('logger')
try {
const parts = ctx.request.headers.authorization?.split(' ')
if (parts?.length !== 2 || parts[0] !== 'GNAP') {
ctx.throw(401, 'Unauthorized')
}
const token = parts[1]
if (
process.env.NODE_ENV !== 'production' &&
token === config.devAccessToken
) {
await next()
return
}
const authService = await ctx.container.use('authService')
const grant = await authService.introspect(token)
if (!grant || !grant.active) {
ctx.throw(401, 'Invalid Token')
}
const access = grant.findAccess({
type,
action,
identifier: ctx.paymentPointer.url
})
if (!access) {
ctx.throw(403, 'Insufficient Grant')
}
if (!config.bypassSignatureValidation) {
try {
if (!(await verifySigAndChallenge(grant.key.jwk, ctx))) {
ctx.throw(401, 'Invalid signature')
}
} catch (e) {
ctx.status = 401
ctx.throw(401, `Invalid signature`)
}
}
await GrantReference.transaction(async (trx: Transaction) => {
const grantRef = await grantReferenceService.get(grant.grant, trx)
if (grantRef) {
if (grantRef.clientId !== grant.clientId) {
logger.debug(
`clientID ${grant.clientId} for grant ${grant.grant} does not match internal reference clientId ${grantRef.clientId}.`
)
ctx.throw(500)
}
} else if (action === AccessAction.Create) {
// Grant and client ID's are only stored for create routes
await grantReferenceService.create(
{
id: grant.grant,
clientId: grant.clientId
},
trx
)
}
})
ctx.grant = grant
// Unless the relevant grant action is ReadAll/ListAll add the
// clientId to ctx for Read/List filtering
if (access.actions.includes(action)) {
ctx.clientId = grant.clientId
}
await next()
} catch (err) {
if (err.status === 401) {
ctx.status = 401
ctx.message = err.message
ctx.set('WWW-Authenticate', `GNAP as_uri=${config.authServerGrantUrl}`)
} else {
throw err
}
}
}
}