-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathmiddleware.ts
82 lines (79 loc) · 2.22 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
import { RequestLike, validateSignature } from 'http-signature-utils'
import { AccessType, AccessAction } from './grant'
import { PaymentPointerContext } from '../../app'
function contextToRequestLike(ctx: PaymentPointerContext): RequestLike {
return {
url: ctx.href,
method: ctx.method,
headers: ctx.headers,
body: ctx.request.body ? JSON.stringify(ctx.request.body) : undefined
}
}
export function createAuthMiddleware({
type,
action
}: {
type: AccessType
action: AccessAction
}) {
return async (
ctx: PaymentPointerContext,
next: () => Promise<unknown>
): Promise<void> => {
const config = await ctx.container.use('config')
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 validateSignature(grant.key.jwk, contextToRequestLike(ctx)))
) {
ctx.throw(401, 'Invalid signature')
}
} catch (e) {
ctx.status = 401
ctx.throw(401, `Invalid signature`)
}
}
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
}
}
}
}