-
Notifications
You must be signed in to change notification settings - Fork 554
/
auth.ts
254 lines (230 loc) · 6.78 KB
/
auth.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import * as crypto from '@atproto/crypto'
import { AuthRequiredError, InvalidRequestError } from '@atproto/xrpc-server'
import * as ui8 from 'uint8arrays'
import express from 'express'
import * as jwt from 'jsonwebtoken'
import AppContext from './context'
import { softDeleted } from './db/util'
const BEARER = 'Bearer '
const BASIC = 'Basic '
export type ServerAuthOpts = {
jwtSecret: string
adminPass: string
moderatorPass?: string
}
// @TODO sync-up with current method names, consider backwards compat.
export enum AuthScope {
Access = 'com.atproto.access',
Refresh = 'com.atproto.refresh',
AppPass = 'com.atproto.appPass',
}
export type AuthToken = {
scope: AuthScope
sub: string
exp: number
}
export type RefreshToken = AuthToken & { jti: string }
export class ServerAuth {
private _secret: string
private _adminPass: string
private _moderatorPass?: string
constructor(opts: ServerAuthOpts) {
this._secret = opts.jwtSecret
this._adminPass = opts.adminPass
this._moderatorPass = opts.moderatorPass
}
createAccessToken(opts: {
did: string
scope?: AuthScope
expiresIn?: string | number
}) {
const { did, scope = AuthScope.Access, expiresIn = '120mins' } = opts
const payload = {
scope,
sub: did,
}
return {
payload: payload as AuthToken, // exp set by sign()
jwt: jwt.sign(payload, this._secret, {
expiresIn: expiresIn,
mutatePayload: true,
}),
}
}
createRefreshToken(opts: {
did: string
jti?: string
expiresIn?: string | number
}) {
const { did, jti = getRefreshTokenId(), expiresIn = '90days' } = opts
const payload = {
scope: AuthScope.Refresh,
sub: did,
jti,
}
return {
payload: payload as RefreshToken, // exp set by sign()
jwt: jwt.sign(payload, this._secret, {
expiresIn: expiresIn,
mutatePayload: true,
}),
}
}
getCredentials(
req: express.Request,
scopes = [AuthScope.Access],
): { did: string; scope: AuthScope } | null {
const token = this.getToken(req)
if (!token) return null
const payload = this.verifyToken(token, scopes)
const sub = payload.sub
if (typeof sub !== 'string' || !sub.startsWith('did:')) {
throw new InvalidRequestError('Malformed token', 'InvalidToken')
}
return { did: sub, scope: payload.scope }
}
getCredentialsOrThrow(
req: express.Request,
scopes: AuthScope[],
): { did: string; scope: AuthScope } {
const creds = this.getCredentials(req, scopes)
if (creds === null) {
throw new AuthRequiredError()
}
return creds
}
verifyUser(req: express.Request, did: string, scopes: AuthScope[]): boolean {
const authorized = this.getCredentials(req, scopes)
return authorized !== null && authorized.did === did
}
verifyAdmin(req: express.Request) {
const parsed = parseBasicAuth(req.headers.authorization || '')
if (!parsed) {
return { admin: false, moderator: false }
}
const { username, password } = parsed
if (username !== 'admin') {
return { admin: false, moderator: false }
}
const admin = password === this._adminPass
const moderator = admin || password === this._moderatorPass
return { admin, moderator }
}
getToken(req: express.Request) {
const header = req.headers.authorization || ''
if (!header.startsWith(BEARER)) return null
return header.slice(BEARER.length)
}
verifyToken(
token: string,
scopes: AuthScope[],
options?: jwt.VerifyOptions,
): jwt.JwtPayload {
try {
const payload = jwt.verify(token, this._secret, options)
if (typeof payload === 'string' || 'signature' in payload) {
throw new InvalidRequestError('Malformed token', 'InvalidToken')
}
if (scopes.length > 0 && !scopes.includes(payload.scope)) {
throw new InvalidRequestError('Bad token scope', 'InvalidToken')
}
return payload
} catch (err) {
if (err instanceof jwt.TokenExpiredError) {
throw new InvalidRequestError('Token has expired', 'ExpiredToken')
}
throw new InvalidRequestError(
'Token could not be verified',
'InvalidToken',
)
}
}
toString(): string {
return 'Server auth: JWT'
}
}
export const parseBasicAuth = (
token: string,
): { username: string; password: string } | null => {
if (!token.startsWith(BASIC)) return null
const b64 = token.slice(BASIC.length)
let parsed: string[]
try {
parsed = ui8.toString(ui8.fromString(b64, 'base64pad'), 'utf8').split(':')
} catch (err) {
return null
}
const [username, password] = parsed
if (!username || !password) return null
return { username, password }
}
export const accessVerifier =
(auth: ServerAuth) =>
async (ctx: { req: express.Request; res: express.Response }) => {
const creds = auth.getCredentialsOrThrow(ctx.req, [
AuthScope.Access,
AuthScope.AppPass,
])
return {
credentials: creds,
artifacts: auth.getToken(ctx.req),
}
}
export const accessVerifierNotAppPassword =
(auth: ServerAuth) =>
async (ctx: { req: express.Request; res: express.Response }) => {
const creds = auth.getCredentialsOrThrow(ctx.req, [AuthScope.Access])
return {
credentials: creds,
artifacts: auth.getToken(ctx.req),
}
}
export const accessVerifierCheckTakedown =
(auth: ServerAuth, { db, services }: AppContext) =>
async (ctx: { req: express.Request; res: express.Response }) => {
const creds = auth.getCredentialsOrThrow(ctx.req, [
AuthScope.Access,
AuthScope.AppPass,
])
const actor = await services.account(db).getAccount(creds.did, true)
if (!actor || softDeleted(actor)) {
throw new AuthRequiredError(
'Account has been taken down',
'AccountTakedown',
)
}
return {
credentials: creds,
artifacts: auth.getToken(ctx.req),
}
}
export const refreshVerifier =
(auth: ServerAuth) =>
async (ctx: { req: express.Request; res: express.Response }) => {
const creds = auth.getCredentialsOrThrow(ctx.req, [AuthScope.Refresh])
return {
credentials: creds,
artifacts: auth.getToken(ctx.req),
}
}
export const adminVerifier =
(auth: ServerAuth) =>
async (ctx: { req: express.Request; res: express.Response }) => {
const credentials = auth.verifyAdmin(ctx.req)
if (!credentials.admin) {
throw new AuthRequiredError()
}
return { credentials }
}
export const moderatorVerifier =
(auth: ServerAuth) =>
async (ctx: { req: express.Request; res: express.Response }) => {
const credentials = auth.verifyAdmin(ctx.req)
if (!credentials.moderator) {
throw new AuthRequiredError()
}
return { credentials }
}
export const getRefreshTokenId = () => {
return ui8.toString(crypto.randomBytes(32), 'base64')
}