@atproto/xrpc-server 0.11.5 → 0.11.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +34 -0
- package/dist/rate-limiter-http.d.ts.map +1 -1
- package/dist/rate-limiter-http.js +1 -0
- package/dist/rate-limiter-http.js.map +1 -1
- package/package.json +26 -21
- package/jest.config.cjs +0 -21
- package/src/auth.ts +0 -235
- package/src/errors.ts +0 -312
- package/src/index.ts +0 -14
- package/src/logger.ts +0 -8
- package/src/rate-limiter-http.ts +0 -82
- package/src/rate-limiter.ts +0 -279
- package/src/server.ts +0 -858
- package/src/stream/frames.ts +0 -125
- package/src/stream/index.ts +0 -5
- package/src/stream/logger.ts +0 -6
- package/src/stream/server.ts +0 -66
- package/src/stream/stream.ts +0 -39
- package/src/stream/subscription.ts +0 -96
- package/src/stream/types.ts +0 -27
- package/src/types.ts +0 -330
- package/src/util.ts +0 -708
- package/tests/_util.ts +0 -124
- package/tests/auth.test.ts +0 -333
- package/tests/bodies.test.ts +0 -608
- package/tests/errors.test.ts +0 -299
- package/tests/frames.test.ts +0 -135
- package/tests/ipld.test.ts +0 -97
- package/tests/parameters.test.ts +0 -331
- package/tests/parsing.test.ts +0 -89
- package/tests/procedures.test.ts +0 -176
- package/tests/queries.test.ts +0 -140
- package/tests/rate-limiter.test.ts +0 -312
- package/tests/responses.test.ts +0 -72
- package/tests/stream.test.ts +0 -169
- package/tests/subscriptions.test.ts +0 -398
- package/tsconfig.build.json +0 -8
- package/tsconfig.build.tsbuildinfo +0 -1
- package/tsconfig.json +0 -7
- package/tsconfig.tests.json +0 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,39 @@
|
|
|
1
1
|
# @atproto/xrpc-server
|
|
2
2
|
|
|
3
|
+
## 0.11.7
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#5158](https://github.com/bluesky-social/atproto/pull/5158) [`963b944`](https://github.com/bluesky-social/atproto/commit/963b9440190c7e59abc0c05de70ecea9cab6fe37) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Add `Retry-After` header to rate limited responses
|
|
8
|
+
|
|
9
|
+
- Updated dependencies [[`963b944`](https://github.com/bluesky-social/atproto/commit/963b9440190c7e59abc0c05de70ecea9cab6fe37), [`d1be0ce`](https://github.com/bluesky-social/atproto/commit/d1be0cead444ef95e64cac5ea5318edbec9d8112), [`963b944`](https://github.com/bluesky-social/atproto/commit/963b9440190c7e59abc0c05de70ecea9cab6fe37)]:
|
|
10
|
+
- @atproto/lex-client@0.2.2
|
|
11
|
+
- @atproto/common@0.7.0
|
|
12
|
+
- @atproto/lex-schema@0.2.0
|
|
13
|
+
- @atproto/lexicon@0.7.5
|
|
14
|
+
- @atproto/ws-client@0.1.5
|
|
15
|
+
- @atproto/xrpc@0.8.4
|
|
16
|
+
|
|
17
|
+
## 0.11.6
|
|
18
|
+
|
|
19
|
+
### Patch Changes
|
|
20
|
+
|
|
21
|
+
- [#5099](https://github.com/bluesky-social/atproto/pull/5099) [`b43ec31`](https://github.com/bluesky-social/atproto/commit/b43ec31f247f4461725b01226885f88bd430ca07) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Update TypeScript build to rely on references to composite internal projects
|
|
22
|
+
|
|
23
|
+
- [#5099](https://github.com/bluesky-social/atproto/pull/5099) [`b43ec31`](https://github.com/bluesky-social/atproto/commit/b43ec31f247f4461725b01226885f88bd430ca07) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Bundle only necessary files in the NPM tarball, including the `CHANGELOG.md` and `README.md` files (if present).
|
|
24
|
+
|
|
25
|
+
- Updated dependencies [[`b43ec31`](https://github.com/bluesky-social/atproto/commit/b43ec31f247f4461725b01226885f88bd430ca07), [`b43ec31`](https://github.com/bluesky-social/atproto/commit/b43ec31f247f4461725b01226885f88bd430ca07), [`b43ec31`](https://github.com/bluesky-social/atproto/commit/b43ec31f247f4461725b01226885f88bd430ca07), [`b43ec31`](https://github.com/bluesky-social/atproto/commit/b43ec31f247f4461725b01226885f88bd430ca07)]:
|
|
26
|
+
- @atproto/lex-client@0.2.1
|
|
27
|
+
- @atproto/lex-schema@0.1.6
|
|
28
|
+
- @atproto/lex-cbor@0.1.3
|
|
29
|
+
- @atproto/lex-data@0.1.4
|
|
30
|
+
- @atproto/lex-json@0.1.3
|
|
31
|
+
- @atproto/ws-client@0.1.4
|
|
32
|
+
- @atproto/lexicon@0.7.4
|
|
33
|
+
- @atproto/common@0.6.5
|
|
34
|
+
- @atproto/crypto@0.5.3
|
|
35
|
+
- @atproto/xrpc@0.8.3
|
|
36
|
+
|
|
3
37
|
## 0.11.5
|
|
4
38
|
|
|
5
39
|
### Patch Changes
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rate-limiter-http.d.ts","sourceRoot":"","sources":["../src/rate-limiter-http.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;AAC3D,OAAO,EAGL,kBAAkB,EAClB,YAAY,EACZ,gBAAgB,EAChB,iBAAiB,EAClB,MAAM,mBAAmB,CAAA;AAE1B,MAAM,WAAW,sBAAsB;IACrC,GAAG,EAAE,eAAe,CAAA;IACpB,GAAG,CAAC,EAAE,cAAc,CAAA;CACrB;AAED,MAAM,MAAM,sBAAsB,CAChC,CAAC,SAAS,sBAAsB,GAAG,sBAAsB,IACvD;IACF,MAAM,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,OAAO,CAAA;CAC7B,CAAA;AAED;;;;GAIG;AACH,qBAAa,eAAe,CAC1B,CAAC,SAAS,sBAAsB,GAAG,sBAAsB,CACzD,YAAW,YAAY,CAAC,CAAC,CAAC;IAGxB,OAAO,CAAC,QAAQ,CAAC,WAAW;IAC5B,OAAO,CAAC,QAAQ,CAAC,OAAO;IAF1B,YACmB,WAAW,EAAE,YAAY,CAAC,CAAC,CAAC,EAC5B,OAAO,GAAE,QAAQ,CA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E,YAAY,EAAE,SAAS,YAAY,CAAC,CAAC,CAAC,EAAE,EACxC,EAAE,MAAM,EAAE,GAAE,sBAAsB,CAAC,CAAC,CAAM,GACzC,eAAe,CAAC,CAAC,CAAC,GAAG,SAAS,CAKhC;CACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rate-limiter-http.js","sourceRoot":"","sources":["../src/rate-limiter-http.ts"],"names":[],"mappings":"AACA,OAAO,EACL,mBAAmB,EACnB,sBAAsB,GAKvB,MAAM,mBAAmB,CAAA;AAa1B;;;;GAIG;AACH,MAAM,OAAO,eAAe;IAI1B,YACmB,WAA4B,EAC5B,OAAO,GAAwC,EAAE;2BADjD,WAAW;uBACX,OAAO;IACvB,CAAC;IAEJ,KAAK,CAAC,MAAM,CAAC,GAAM;QACjB,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC,OAAO,CAAA;QAC/B,IAAI,MAAM,IAAI,MAAM,CAAC,GAAG,CAAC;YAAE,OAAM;QAEjC,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;YACtC,IAAI,MAAM,IAAI,IAAI,EAAE,CAAC;gBACnB,gBAAgB,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;YAC/B,CAAC;QACH,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,IAAI,GAAG,YAAY,sBAAsB,EAAE,CAAC;gBAC1C,gBAAgB,CAAC,GAAG,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;
|
|
1
|
+
{"version":3,"file":"rate-limiter-http.js","sourceRoot":"","sources":["../src/rate-limiter-http.ts"],"names":[],"mappings":"AACA,OAAO,EACL,mBAAmB,EACnB,sBAAsB,GAKvB,MAAM,mBAAmB,CAAA;AAa1B;;;;GAIG;AACH,MAAM,OAAO,eAAe;IAI1B,YACmB,WAA4B,EAC5B,OAAO,GAAwC,EAAE;2BADjD,WAAW;uBACX,OAAO;I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D,CAAC;CACF;AAED,SAAS,gBAAgB,CAEvB,GAAM,EAAE,MAAyB;IACjC,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,MAAM,CAAC,YAAY,CAAC,GAAG,GAAG,CAAC,CAAA;IAEpE,GAAG,CAAC,GAAG,EAAE,SAAS,CAAC,iBAAiB,EAAE,MAAM,CAAC,KAAK,CAAC,CAAA;IACnD,GAAG,CAAC,GAAG,EAAE,SAAS,CAAC,iBAAiB,EAAE,OAAO,CAAC,CAAA;IAC9C,GAAG,CAAC,GAAG,EAAE,SAAS,CAAC,qBAAqB,EAAE,MAAM,CAAC,eAAe,CAAC,CAAA;IACjE,GAAG,CAAC,GAAG,EAAE,SAAS,CAAC,kBAAkB,EAAE,GAAG,MAAM,CAAC,KAAK,MAAM,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAA;AAChF,CAAC","sourcesContent":["import { IncomingMessage, ServerResponse } from 'node:http'\nimport {\n CombinedRateLimiter,\n RateLimitExceededError,\n RateLimiterConsume,\n RateLimiterI,\n RateLimiterReset,\n RateLimiterStatus,\n} from './rate-limiter.js'\n\nexport interface HttpRateLimiterContext {\n req: IncomingMessage\n res?: ServerResponse\n}\n\nexport type HttpRateLimiterOptions<\n C extends HttpRateLimiterContext = HttpRateLimiterContext,\n> = {\n bypass?: (ctx: C) => boolean\n}\n\n/**\n * Wraps a {@link RateLimiterI} class with an {@link RateLimiterI}\n * implementation that will apply the appropriate headers to the response if a\n * limit is exceeded.\n */\nexport class HttpRateLimiter<\n C extends HttpRateLimiterContext = HttpRateLimiterContext,\n> implements RateLimiterI<C>\n{\n constructor(\n private readonly rateLimiter: RateLimiterI<C>,\n private readonly options: Readonly<HttpRateLimiterOptions<C>> = {},\n ) {}\n\n async handle(ctx: C): Promise<void> {\n const { bypass } = this.options\n if (bypass && bypass(ctx)) return\n\n try {\n const result = await this.consume(ctx)\n if (result != null) {\n setStatusHeaders(ctx, result)\n }\n } catch (err) {\n if (err instanceof RateLimitExceededError) {\n setStatusHeaders(ctx, err.status)\n\n ctx.res?.setHeader(\n 'Retry-After',\n Math.ceil(err.status.msBeforeNext / 1e3),\n )\n }\n\n throw err\n }\n }\n\n async consume(...args: Parameters<RateLimiterConsume<C>>) {\n return this.rateLimiter.consume(...args)\n }\n\n async reset(...args: Parameters<RateLimiterReset<C>>) {\n return this.rateLimiter.reset(...args)\n }\n\n static from<C extends HttpRateLimiterContext = HttpRateLimiterContext>(\n rateLimiters: readonly RateLimiterI<C>[],\n { bypass }: HttpRateLimiterOptions<C> = {},\n ): HttpRateLimiter<C> | undefined {\n const rateLimiter = CombinedRateLimiter.from(rateLimiters)\n if (!rateLimiter) return undefined\n\n return new HttpRateLimiter<C>(rateLimiter, { bypass })\n }\n}\n\nfunction setStatusHeaders<\n C extends HttpRateLimiterContext = HttpRateLimiterContext,\n>(ctx: C, status: RateLimiterStatus) {\n const resetAt = Math.floor((Date.now() + status.msBeforeNext) / 1e3)\n\n ctx.res?.setHeader('RateLimit-Limit', status.limit)\n ctx.res?.setHeader('RateLimit-Reset', resetAt)\n ctx.res?.setHeader('RateLimit-Remaining', status.remainingPoints)\n ctx.res?.setHeader('RateLimit-Policy', `${status.limit};w=${status.duration}`)\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atproto/xrpc-server",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.7",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "atproto HTTP API (XRPC) server library",
|
|
6
6
|
"keywords": [
|
|
@@ -13,6 +13,18 @@
|
|
|
13
13
|
"url": "https://github.com/bluesky-social/atproto",
|
|
14
14
|
"directory": "packages/xrpc-server"
|
|
15
15
|
},
|
|
16
|
+
"files": [
|
|
17
|
+
"./dist",
|
|
18
|
+
"./README.md",
|
|
19
|
+
"./CHANGELOG.md"
|
|
20
|
+
],
|
|
21
|
+
"type": "module",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"default": "./dist/index.js"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
16
28
|
"engines": {
|
|
17
29
|
"node": ">=22"
|
|
18
30
|
},
|
|
@@ -22,16 +34,16 @@
|
|
|
22
34
|
"mime-types": "^2.1.35",
|
|
23
35
|
"rate-limiter-flexible": "^2.4.1",
|
|
24
36
|
"ws": "^8.12.0",
|
|
25
|
-
"@atproto/common": "^0.
|
|
26
|
-
"@atproto/
|
|
27
|
-
"@atproto/
|
|
28
|
-
"@atproto/lex-
|
|
29
|
-
"@atproto/lex-
|
|
30
|
-
"@atproto/lex-client": "^0.2.
|
|
31
|
-
"@atproto/
|
|
32
|
-
"@atproto/
|
|
33
|
-
"@atproto/
|
|
34
|
-
"@atproto/
|
|
37
|
+
"@atproto/common": "^0.7.0",
|
|
38
|
+
"@atproto/lex-data": "^0.1.4",
|
|
39
|
+
"@atproto/crypto": "^0.5.3",
|
|
40
|
+
"@atproto/lex-cbor": "^0.1.3",
|
|
41
|
+
"@atproto/lex-json": "^0.1.3",
|
|
42
|
+
"@atproto/lex-client": "^0.2.2",
|
|
43
|
+
"@atproto/lex-schema": "^0.2.0",
|
|
44
|
+
"@atproto/lexicon": "^0.7.5",
|
|
45
|
+
"@atproto/ws-client": "^0.1.5",
|
|
46
|
+
"@atproto/xrpc": "^0.8.4"
|
|
35
47
|
},
|
|
36
48
|
"devDependencies": {
|
|
37
49
|
"@jest/globals": "^30.0.0",
|
|
@@ -45,16 +57,9 @@
|
|
|
45
57
|
"jose": "^5.0.1",
|
|
46
58
|
"key-encoder": "^2.0.3",
|
|
47
59
|
"multiformats": "^13.0.0",
|
|
48
|
-
"@atproto/
|
|
49
|
-
"@atproto/
|
|
50
|
-
"@atproto/lex-document": "^0.1.
|
|
51
|
-
},
|
|
52
|
-
"type": "module",
|
|
53
|
-
"exports": {
|
|
54
|
-
".": {
|
|
55
|
-
"types": "./dist/index.d.ts",
|
|
56
|
-
"default": "./dist/index.js"
|
|
57
|
-
}
|
|
60
|
+
"@atproto/lex": "^0.2.0",
|
|
61
|
+
"@atproto/crypto": "^0.5.3",
|
|
62
|
+
"@atproto/lex-document": "^0.1.4"
|
|
58
63
|
},
|
|
59
64
|
"scripts": {
|
|
60
65
|
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
|
package/jest.config.cjs
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
/** @type {import('jest').Config} */
|
|
2
|
-
module.exports = {
|
|
3
|
-
displayName: 'XRPC Server',
|
|
4
|
-
transform: {
|
|
5
|
-
'^.+\\.(t|j)s$': [
|
|
6
|
-
'@swc/jest',
|
|
7
|
-
{
|
|
8
|
-
jsc: {
|
|
9
|
-
parser: { syntax: 'typescript', importAttributes: true },
|
|
10
|
-
experimental: { keepImportAttributes: true },
|
|
11
|
-
transform: {},
|
|
12
|
-
},
|
|
13
|
-
module: { type: 'es6' },
|
|
14
|
-
},
|
|
15
|
-
],
|
|
16
|
-
},
|
|
17
|
-
extensionsToTreatAsEsm: ['.ts'],
|
|
18
|
-
transformIgnorePatterns: [],
|
|
19
|
-
setupFiles: ['<rootDir>/../../test.setup.ts'],
|
|
20
|
-
moduleNameMapper: { '^(\\.\\.?\\/.+)\\.js$': ['$1.ts', '$1.js'] },
|
|
21
|
-
}
|
package/src/auth.ts
DELETED
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
import * as common from '@atproto/common'
|
|
2
|
-
import { MINUTE } from '@atproto/common'
|
|
3
|
-
import * as crypto from '@atproto/crypto'
|
|
4
|
-
import { DidString, isDidString } from '@atproto/lex-schema'
|
|
5
|
-
import { AuthRequiredError } from './errors.js'
|
|
6
|
-
|
|
7
|
-
type ServiceJwtParams = {
|
|
8
|
-
iss: string
|
|
9
|
-
aud: string
|
|
10
|
-
iat?: number
|
|
11
|
-
exp?: number
|
|
12
|
-
lxm: string | null
|
|
13
|
-
keypair: crypto.Keypair
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
type ServiceJwtHeaders = {
|
|
17
|
-
alg: string
|
|
18
|
-
} & Record<string, unknown>
|
|
19
|
-
|
|
20
|
-
type ServiceJwtPayload = {
|
|
21
|
-
iss: DidString | `${DidString}#${string}`
|
|
22
|
-
aud: string
|
|
23
|
-
exp: number
|
|
24
|
-
lxm?: string
|
|
25
|
-
jti?: string
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export const createServiceJwt = async (
|
|
29
|
-
params: ServiceJwtParams,
|
|
30
|
-
): Promise<string> => {
|
|
31
|
-
const { iss, aud, keypair } = params
|
|
32
|
-
const iat = params.iat ?? Math.floor(Date.now() / 1e3)
|
|
33
|
-
const exp = params.exp ?? iat + MINUTE / 1e3
|
|
34
|
-
const lxm = params.lxm ?? undefined
|
|
35
|
-
const jti = await crypto.randomStr(16, 'hex')
|
|
36
|
-
const header = {
|
|
37
|
-
typ: 'JWT',
|
|
38
|
-
alg: keypair.jwtAlg,
|
|
39
|
-
}
|
|
40
|
-
const payload = common.noUndefinedVals({
|
|
41
|
-
iat,
|
|
42
|
-
iss,
|
|
43
|
-
aud,
|
|
44
|
-
exp,
|
|
45
|
-
lxm,
|
|
46
|
-
jti,
|
|
47
|
-
})
|
|
48
|
-
const toSignStr = `${jsonToB64Url(header)}.${jsonToB64Url(payload)}`
|
|
49
|
-
const toSign = Buffer.from(toSignStr, 'utf8')
|
|
50
|
-
const sig = Buffer.from(await keypair.sign(toSign))
|
|
51
|
-
return `${toSignStr}.${sig.toString('base64url')}`
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export const createServiceAuthHeaders = async (params: ServiceJwtParams) => {
|
|
55
|
-
const jwt = await createServiceJwt(params)
|
|
56
|
-
return {
|
|
57
|
-
headers: { authorization: `Bearer ${jwt}` },
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const jsonToB64Url = (json: Record<string, unknown>): string => {
|
|
62
|
-
return Buffer.from(JSON.stringify(json)).toString('base64url')
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export type VerifySignatureWithKeyFn = (
|
|
66
|
-
key: string,
|
|
67
|
-
msgBytes: Uint8Array,
|
|
68
|
-
sigBytes: Uint8Array,
|
|
69
|
-
alg: string,
|
|
70
|
-
) => Promise<boolean>
|
|
71
|
-
|
|
72
|
-
export const verifyJwt = async (
|
|
73
|
-
jwtStr: string,
|
|
74
|
-
ownDid: string | null, // null indicates to skip the audience check
|
|
75
|
-
lxm: string | null, // null indicates to skip the lxm check
|
|
76
|
-
getSigningKey: (
|
|
77
|
-
iss: DidString | `${DidString}#${string}`,
|
|
78
|
-
forceRefresh: boolean,
|
|
79
|
-
) => Promise<string>,
|
|
80
|
-
verifySignatureWithKey: VerifySignatureWithKeyFn = cryptoVerifySignatureWithKey,
|
|
81
|
-
): Promise<ServiceJwtPayload> => {
|
|
82
|
-
const parts = jwtStr.split('.')
|
|
83
|
-
if (parts.length !== 3) {
|
|
84
|
-
throw new AuthRequiredError('poorly formatted jwt', 'BadJwt')
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const header = parseHeader(parts[0])
|
|
88
|
-
|
|
89
|
-
// The spec does not describe what to do with the "typ" claim. We can,
|
|
90
|
-
// however, forbid some values that are not compatible with our use case.
|
|
91
|
-
if (
|
|
92
|
-
// service tokens are not OAuth 2.0 access tokens
|
|
93
|
-
// https://datatracker.ietf.org/doc/html/rfc9068
|
|
94
|
-
header['typ'] === 'at+jwt' ||
|
|
95
|
-
// "refresh+jwt" is a non-standard type used by the @atproto packages
|
|
96
|
-
header['typ'] === 'refresh+jwt' ||
|
|
97
|
-
// "DPoP" proofs are not meant to be used as service tokens
|
|
98
|
-
// https://datatracker.ietf.org/doc/html/rfc9449
|
|
99
|
-
header['typ'] === 'dpop+jwt'
|
|
100
|
-
) {
|
|
101
|
-
throw new AuthRequiredError(
|
|
102
|
-
`Invalid jwt type "${header['typ']}"`,
|
|
103
|
-
'BadJwtType',
|
|
104
|
-
)
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const payload = parsePayload(parts[1])
|
|
108
|
-
const sig = parts[2]
|
|
109
|
-
|
|
110
|
-
if (Date.now() / 1000 > payload.exp) {
|
|
111
|
-
throw new AuthRequiredError('jwt expired', 'JwtExpired')
|
|
112
|
-
}
|
|
113
|
-
if (ownDid !== null && payload.aud !== ownDid) {
|
|
114
|
-
throw new AuthRequiredError(
|
|
115
|
-
'jwt audience does not match service did',
|
|
116
|
-
'BadJwtAudience',
|
|
117
|
-
)
|
|
118
|
-
}
|
|
119
|
-
if (lxm !== null && payload.lxm !== lxm) {
|
|
120
|
-
throw new AuthRequiredError(
|
|
121
|
-
payload.lxm !== undefined
|
|
122
|
-
? `bad jwt lexicon method ("lxm"). must match: ${lxm}`
|
|
123
|
-
: `missing jwt lexicon method ("lxm"). must match: ${lxm}`,
|
|
124
|
-
'BadJwtLexiconMethod',
|
|
125
|
-
)
|
|
126
|
-
}
|
|
127
|
-
if (!payload.iss || !isDidStringOrService(payload.iss)) {
|
|
128
|
-
throw new AuthRequiredError('jwt iss is not a valid did', 'BadJwtIss')
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const msgBytes = Buffer.from(parts.slice(0, 2).join('.'), 'utf8')
|
|
132
|
-
const sigBytes = Buffer.from(sig, 'base64url')
|
|
133
|
-
|
|
134
|
-
const signingKey = await getSigningKey(payload.iss, false)
|
|
135
|
-
const { alg } = header
|
|
136
|
-
|
|
137
|
-
let validSig: boolean
|
|
138
|
-
try {
|
|
139
|
-
validSig = await verifySignatureWithKey(signingKey, msgBytes, sigBytes, alg)
|
|
140
|
-
} catch (err) {
|
|
141
|
-
throw new AuthRequiredError(
|
|
142
|
-
'could not verify jwt signature',
|
|
143
|
-
'BadJwtSignature',
|
|
144
|
-
)
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
if (!validSig) {
|
|
148
|
-
// get fresh signing key in case it failed due to a recent rotation
|
|
149
|
-
const freshSigningKey = await getSigningKey(payload.iss, true)
|
|
150
|
-
try {
|
|
151
|
-
validSig =
|
|
152
|
-
freshSigningKey !== signingKey
|
|
153
|
-
? await verifySignatureWithKey(
|
|
154
|
-
freshSigningKey,
|
|
155
|
-
msgBytes,
|
|
156
|
-
sigBytes,
|
|
157
|
-
alg,
|
|
158
|
-
)
|
|
159
|
-
: false
|
|
160
|
-
} catch (err) {
|
|
161
|
-
throw new AuthRequiredError(
|
|
162
|
-
'could not verify jwt signature',
|
|
163
|
-
'BadJwtSignature',
|
|
164
|
-
)
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
if (!validSig) {
|
|
169
|
-
throw new AuthRequiredError(
|
|
170
|
-
'jwt signature does not match jwt issuer',
|
|
171
|
-
'BadJwtSignature',
|
|
172
|
-
)
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
return payload
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
export const cryptoVerifySignatureWithKey: VerifySignatureWithKeyFn = async (
|
|
179
|
-
key: string,
|
|
180
|
-
msgBytes: Uint8Array,
|
|
181
|
-
sigBytes: Uint8Array,
|
|
182
|
-
alg: string,
|
|
183
|
-
) => {
|
|
184
|
-
return crypto.verifySignature(key, msgBytes, sigBytes, {
|
|
185
|
-
jwtAlg: alg,
|
|
186
|
-
allowMalleableSig: true,
|
|
187
|
-
})
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
const parseB64UrlToJson = (b64: string) => {
|
|
191
|
-
return JSON.parse(Buffer.from(b64, 'base64url').toString('utf8'))
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
const parseHeader = (b64: string): ServiceJwtHeaders => {
|
|
195
|
-
const header = parseB64UrlToJson(b64)
|
|
196
|
-
if (!header || typeof header !== 'object' || typeof header.alg !== 'string') {
|
|
197
|
-
throw new AuthRequiredError('poorly formatted jwt', 'BadJwt')
|
|
198
|
-
}
|
|
199
|
-
return header
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
const parsePayload = (b64: string): ServiceJwtPayload => {
|
|
203
|
-
const payload = parseB64UrlToJson(b64)
|
|
204
|
-
if (
|
|
205
|
-
!payload ||
|
|
206
|
-
typeof payload !== 'object' ||
|
|
207
|
-
typeof payload.iss !== 'string' ||
|
|
208
|
-
typeof payload.aud !== 'string' ||
|
|
209
|
-
typeof payload.exp !== 'number' ||
|
|
210
|
-
(payload.lxm && typeof payload.lxm !== 'string') ||
|
|
211
|
-
(payload.nonce && typeof payload.nonce !== 'string')
|
|
212
|
-
) {
|
|
213
|
-
throw new AuthRequiredError('poorly formatted jwt', 'BadJwt')
|
|
214
|
-
}
|
|
215
|
-
return payload
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
function isDidStringOrService(
|
|
219
|
-
value: string,
|
|
220
|
-
): value is DidString | `${DidString}#${string}` {
|
|
221
|
-
const hashIdx = value.indexOf('#')
|
|
222
|
-
if (hashIdx === -1) {
|
|
223
|
-
return isDidString(value)
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
// basic validation of the fragment part
|
|
227
|
-
const fragmentLen = value.length - hashIdx - 1
|
|
228
|
-
if (fragmentLen < 1 || value.includes('#', hashIdx + 1)) {
|
|
229
|
-
return false
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
// Validate the did part
|
|
233
|
-
const didPart = value.slice(0, hashIdx)
|
|
234
|
-
return isDidString(didPart)
|
|
235
|
-
}
|