-
Notifications
You must be signed in to change notification settings - Fork 88
/
service.ts
154 lines (140 loc) · 3.97 KB
/
service.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import * as crypto from 'crypto'
import { v4 } from 'uuid'
import { Transaction, TransactionOrKnex } from 'objection'
import { BaseService } from '../shared/baseService'
import { Grant, GrantState } from '../grant/model'
import { ClientService, KeyInfo } from '../client/service'
import { AccessToken } from './model'
import { IAppConfig } from '../config/app'
import { Access } from '../access/model'
export interface AccessTokenService {
introspect(token: string): Promise<Introspection | undefined>
revoke(id: string): Promise<void>
create(grantId: string, opts?: AccessTokenOpts): Promise<AccessToken>
rotate(managementId: string): Promise<Rotation>
}
interface ServiceDependencies extends BaseService {
knex: TransactionOrKnex
clientService: ClientService
config: IAppConfig
}
export interface Introspection extends Partial<Grant> {
active: boolean
key?: KeyInfo
}
interface AccessTokenOpts {
expiresIn?: number
trx?: Transaction
}
export type Rotation =
| {
success: true
access: Array<Access>
value: string
managementId: string
expiresIn?: number
}
| {
success: false
error: Error
}
export async function createAccessTokenService({
logger,
knex,
config,
clientService
}: ServiceDependencies): Promise<AccessTokenService> {
const log = logger.child({
service: 'TokenService'
})
const deps: ServiceDependencies = {
logger: log,
knex,
clientService,
config
}
return {
introspect: (token: string) => introspect(deps, token),
revoke: (id: string) => revoke(deps, id),
create: (grantId: string, opts?: AccessTokenOpts) =>
createAccessToken(deps, grantId, opts),
rotate: (managementId: string) => rotate(deps, managementId)
}
}
function isTokenExpired(token: AccessToken): boolean {
const now = new Date(Date.now())
const expiresAt = token.expiresIn
? token.createdAt.getTime() + token.expiresIn
: Infinity
return expiresAt < now.getTime()
}
async function introspect(
deps: ServiceDependencies,
value: string
): Promise<Introspection | undefined> {
const token = await AccessToken.query(deps.knex).findOne({ value })
if (!token) return
if (isTokenExpired(token)) {
return { active: false }
} else {
const grant = await Grant.query(deps.knex)
.findById(token.grantId)
.withGraphFetched('access')
if (grant.state === GrantState.Revoked) {
return { active: false }
}
const registryData = await deps.clientService.getRegistryDataByKid(
grant.clientKeyId
)
const { keys } = registryData
return { active: true, ...grant, key: { proof: 'httpsig', jwk: keys[0] } }
}
}
async function revoke(deps: ServiceDependencies, id: string): Promise<void> {
const token = await AccessToken.query(deps.knex).findById(id)
if (token) {
await token.$query(deps.knex).delete()
}
}
async function createAccessToken(
deps: ServiceDependencies,
grantId: string,
opts?: AccessTokenOpts
): Promise<AccessToken> {
return AccessToken.query(deps.knex).insert({
value: crypto.randomBytes(8).toString('hex').toUpperCase(),
managementId: v4(),
grantId,
expiresIn: opts?.expiresIn || deps.config.accessTokenExpirySeconds
})
}
async function rotate(
deps: ServiceDependencies,
managementId: string
): Promise<Rotation> {
let token = await AccessToken.query(deps.knex).findOne({ managementId })
if (token) {
await token.$query(deps.knex).delete()
token = await AccessToken.query(deps.knex).insertAndFetch({
value: crypto.randomBytes(8).toString('hex').toUpperCase(),
grantId: token.grantId,
expiresIn: token.expiresIn,
managementId: v4()
})
const access = await Access.query(deps.knex).where({
grantId: token.grantId
})
return {
success: true,
access,
value: token.value,
managementId: token.managementId,
expiresIn: token.expiresIn
}
} else {
return {
success: false,
error: new Error('token not found')
}
}
}