@atproto/oauth-client 0.7.5 → 0.7.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/src/util.test.ts DELETED
@@ -1,86 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2
- import { timeoutSignal } from './util.js'
3
-
4
- describe(timeoutSignal, () => {
5
- describe('with native AbortSignal.timeout', () => {
6
- it('delegates to the native implementation when available', () => {
7
- using spy = vi.spyOn(AbortSignal, 'timeout')
8
-
9
- const signal = timeoutSignal(1000)
10
-
11
- expect(spy).toHaveBeenCalledOnce()
12
- expect(spy).toHaveBeenCalledWith(1000)
13
- expect(signal).toBeInstanceOf(AbortSignal)
14
- expect(signal.aborted).toBe(false)
15
- })
16
-
17
- it('returns a signal that actually aborts after the timeout', async () => {
18
- const signal = timeoutSignal(5)
19
- expect(signal.aborted).toBe(false)
20
-
21
- await new Promise((resolve) => setTimeout(resolve, 20))
22
- expect(signal.aborted).toBe(true)
23
- })
24
- })
25
-
26
- describe('without native AbortSignal.timeout (e.g. React Native)', () => {
27
- let original: typeof AbortSignal.timeout
28
-
29
- beforeEach(() => {
30
- vi.useFakeTimers()
31
- original = AbortSignal.timeout
32
- // Simulate a runtime that does not implement the static method.
33
- // @ts-expect-error intentionally removing a built-in to emulate RN
34
- AbortSignal.timeout = undefined
35
- })
36
-
37
- afterEach(() => {
38
- AbortSignal.timeout = original
39
- vi.useRealTimers()
40
- })
41
-
42
- it('does not throw and returns a usable AbortSignal', () => {
43
- const signal = timeoutSignal(1000)
44
- expect(signal).toBeInstanceOf(AbortSignal)
45
- expect(signal.aborted).toBe(false)
46
- })
47
-
48
- it('aborts the signal once the timeout elapses', () => {
49
- const signal = timeoutSignal(1000)
50
- const onAbort = vi.fn()
51
- signal.addEventListener('abort', onAbort)
52
-
53
- vi.advanceTimersByTime(999)
54
- expect(signal.aborted).toBe(false)
55
- expect(onAbort).not.toHaveBeenCalled()
56
-
57
- vi.advanceTimersByTime(1)
58
- expect(signal.aborted).toBe(true)
59
- expect(onAbort).toHaveBeenCalledOnce()
60
- })
61
-
62
- it('aborts with a TimeoutError reason', () => {
63
- const signal = timeoutSignal(1000)
64
- vi.advanceTimersByTime(1000)
65
-
66
- expect(signal.reason).toBeInstanceOf(DOMException)
67
- expect(signal.reason.name).toBe('TimeoutError')
68
- })
69
-
70
- it('falls back to a plain Error when DOMException is unavailable', () => {
71
- const originalDomException = globalThis.DOMException
72
- try {
73
- // @ts-expect-error intentionally removing a built-in to emulate RN
74
- globalThis.DOMException = undefined
75
-
76
- const signal = timeoutSignal(1000)
77
- vi.advanceTimersByTime(1000)
78
-
79
- expect(signal.aborted).toBe(true)
80
- expect(signal.reason).toBeInstanceOf(Error)
81
- } finally {
82
- globalThis.DOMException = originalDomException
83
- }
84
- })
85
- })
86
- })
package/src/util.ts DELETED
@@ -1,81 +0,0 @@
1
- export type Awaitable<T> = T | PromiseLike<T>
2
- export type Simplify<T> = { [K in keyof T]: T[K] } & NonNullable<unknown>
3
-
4
- export const ifString = <V>(v: V) => (typeof v === 'string' ? v : undefined)
5
-
6
- export function contentMime(headers: Headers): string | undefined {
7
- return headers.get('content-type')?.split(';')[0]!.trim()
8
- }
9
-
10
- /**
11
- * Returns an {@link AbortSignal} that aborts after `ms` milliseconds.
12
- *
13
- * Uses the native {@link AbortSignal.timeout} when available, and otherwise
14
- * falls back to an {@link AbortController} + `setTimeout`. The static
15
- * `AbortSignal.timeout` method is not implemented in every runtime this package
16
- * targets (notably React Native / Expo), so relying on it directly throws a
17
- * `TypeError: AbortSignal.timeout is not a function` at runtime.
18
- *
19
- * @see {@link https://github.com/facebook/react-native/issues/42042}
20
- */
21
- export function timeoutSignal(ms: number): AbortSignal {
22
- if (typeof AbortSignal.timeout === 'function') {
23
- return AbortSignal.timeout(ms)
24
- }
25
-
26
- const controller = new AbortController()
27
- setTimeout(() => controller.abort(timeoutError(ms)), ms)
28
- return controller.signal
29
- }
30
-
31
- /**
32
- * Builds the reason used to abort a {@link timeoutSignal} fallback. Mirrors the
33
- * native `AbortSignal.timeout` behaviour (a `TimeoutError` `DOMException`) when
34
- * `DOMException` is available, and degrades to a plain `Error` in runtimes that
35
- * lack it.
36
- */
37
- function timeoutError(ms: number): unknown {
38
- const message = `The operation timed out after ${ms} ms`
39
- if (typeof DOMException === 'function') {
40
- return new DOMException(message, 'TimeoutError')
41
- }
42
- return new Error(message)
43
- }
44
-
45
- export function combineSignals(
46
- signals: readonly (AbortSignal | undefined)[],
47
- ): AbortController & Disposable {
48
- const controller = new DisposableAbortController()
49
-
50
- const onAbort = function (this: AbortSignal, _event: Event) {
51
- const reason = new Error('This operation was aborted', {
52
- cause: this.reason,
53
- })
54
-
55
- controller.abort(reason)
56
- }
57
-
58
- try {
59
- for (const sig of signals) {
60
- if (sig) {
61
- sig.throwIfAborted()
62
- sig.addEventListener('abort', onAbort, { signal: controller.signal })
63
- }
64
- }
65
-
66
- return controller
67
- } catch (err) {
68
- controller.abort(err)
69
- throw err
70
- }
71
- }
72
-
73
- /**
74
- * Allows using {@link AbortController} with the `using` keyword, in order to
75
- * automatically abort them once the execution block ends.
76
- */
77
- class DisposableAbortController extends AbortController implements Disposable {
78
- [Symbol.dispose]() {
79
- this.abort(new Error('AbortController was disposed'))
80
- }
81
- }
@@ -1,116 +0,0 @@
1
- import { Keyset } from '@atproto/jwk'
2
- import {
3
- OAuthClientMetadataInput,
4
- assertOAuthDiscoverableClientId,
5
- assertOAuthLoopbackClientId,
6
- } from '@atproto/oauth-types'
7
- import { FALLBACK_ALG } from './constants.js'
8
- import { ClientMetadata, clientMetadataSchema } from './types.js'
9
-
10
- export function validateClientMetadata(
11
- input: OAuthClientMetadataInput,
12
- keyset?: Keyset,
13
- ): ClientMetadata {
14
- // Allow to pass a keyset and omit the jwks/jwks_uri properties
15
- if (!input.jwks && !input.jwks_uri && keyset?.size) {
16
- input = { ...input, jwks: keyset.toJSON() }
17
- }
18
-
19
- const metadata = clientMetadataSchema.parse(input)
20
-
21
- // Validate client ID
22
- if (metadata.client_id.startsWith('http:')) {
23
- assertOAuthLoopbackClientId(metadata.client_id)
24
- } else {
25
- assertOAuthDiscoverableClientId(metadata.client_id)
26
- }
27
-
28
- const scopes = metadata.scope?.split(' ')
29
- if (!scopes?.includes('atproto')) {
30
- throw new TypeError(`Client metadata must include the "atproto" scope`)
31
- }
32
-
33
- if (!metadata.response_types.includes('code')) {
34
- throw new TypeError(`"response_types" must include "code"`)
35
- }
36
-
37
- if (!metadata.grant_types.includes('authorization_code')) {
38
- throw new TypeError(`"grant_types" must include "authorization_code"`)
39
- }
40
-
41
- const method = metadata.token_endpoint_auth_method
42
- const methodAlg = metadata.token_endpoint_auth_signing_alg
43
- switch (method) {
44
- case 'none':
45
- if (methodAlg) {
46
- throw new TypeError(
47
- `"token_endpoint_auth_signing_alg" must not be provided when "token_endpoint_auth_method" is "${method}"`,
48
- )
49
- }
50
- break
51
-
52
- case 'private_key_jwt': {
53
- if (!methodAlg) {
54
- throw new TypeError(
55
- `"token_endpoint_auth_signing_alg" must be provided when "token_endpoint_auth_method" is "${method}"`,
56
- )
57
- }
58
-
59
- if (!keyset) {
60
- throw new TypeError(
61
- `Client authentication method "${method}" requires a keyset`,
62
- )
63
- }
64
-
65
- // @NOTE This reproduces the logic from `negotiateClientAuthMethod` at
66
- // initialization time to ensure that every key that might end-up being
67
- // used is indeed valid & advertised in the metadata.
68
- const signingKeys = Array.from(keyset.list({ usage: 'sign' })).filter(
69
- (key) => key.kid,
70
- )
71
-
72
- if (!signingKeys.length) {
73
- throw new TypeError(
74
- `Client authentication method "${method}" requires at least one active signing key with a "kid" property`,
75
- )
76
- }
77
-
78
- if (!signingKeys.some((key) => key.algorithms.includes(FALLBACK_ALG))) {
79
- throw new TypeError(
80
- `Client authentication method "${method}" requires at least one active "${FALLBACK_ALG}" signing key`,
81
- )
82
- }
83
-
84
- if (metadata.jwks) {
85
- // Ensure that all the signing keys that could end-up being used are
86
- // advertised in the JWKS.
87
- for (const key of signingKeys) {
88
- if (
89
- !metadata.jwks.keys.some((k) => k.kid === key.kid && !k.revoked)
90
- ) {
91
- throw new TypeError(
92
- `Missing or inactive key "${key.kid}" in jwks. Make sure that every signing key of the Keyset is declared as an active key in the Metadata's JWKS.`,
93
- )
94
- }
95
- }
96
- } else if (metadata.jwks_uri) {
97
- // @NOTE we only ensure that all the signing keys are referenced in JWKS
98
- // when it is available (see previous "if") as we don't want to download
99
- // that file here (for efficiency reasons).
100
- } else {
101
- throw new TypeError(
102
- `Client authentication method "${method}" requires a JWKS`,
103
- )
104
- }
105
-
106
- break
107
- }
108
-
109
- default:
110
- throw new TypeError(
111
- `Unsupported "token_endpoint_auth_method" value: ${method}`,
112
- )
113
- }
114
-
115
- return metadata
116
- }
@@ -1,9 +0,0 @@
1
- {
2
- "extends": ["../../../tsconfig/isomorphic.json"],
3
- "compilerOptions": {
4
- "rootDir": "./src",
5
- "outDir": "./dist",
6
- },
7
- "include": ["./src"],
8
- "exclude": ["**/*.test.ts"],
9
- }
@@ -1 +0,0 @@
1
- {"version":"7.0.0-dev.20260614.1","root":["./src/constants.ts","./src/core-js.d.ts","./src/fetch-dpop.ts","./src/identity-resolver.ts","./src/index.ts","./src/lock.ts","./src/oauth-authorization-server-metadata-resolver.ts","./src/oauth-callback-error.ts","./src/oauth-client-auth.ts","./src/oauth-client.ts","./src/oauth-protected-resource-metadata-resolver.ts","./src/oauth-resolver-error.ts","./src/oauth-resolver.ts","./src/oauth-response-error.ts","./src/oauth-server-agent.ts","./src/oauth-server-factory.ts","./src/oauth-session.ts","./src/runtime-implementation.ts","./src/runtime.ts","./src/session-getter.ts","./src/state-store.ts","./src/types.ts","./src/util.ts","./src/validate-client-metadata.ts","./src/errors/auth-method-unsatisfiable-error.ts","./src/errors/token-invalid-error.ts","./src/errors/token-refresh-error.ts","./src/errors/token-revoked-error.ts"]}
package/tsconfig.json DELETED
@@ -1,7 +0,0 @@
1
- {
2
- "include": [],
3
- "references": [
4
- { "path": "./tsconfig.build.json" },
5
- { "path": "./tsconfig.tests.json" },
6
- ],
7
- }
@@ -1,8 +0,0 @@
1
- {
2
- "extends": "../../../tsconfig/vitest.json",
3
- "include": ["./tests", "./src/**/*.test.ts", "./src/core-js.d.ts"],
4
- "compilerOptions": {
5
- "rootDir": ".",
6
- "noUnusedLocals": false,
7
- },
8
- }
package/vitest.config.ts DELETED
@@ -1,5 +0,0 @@
1
- import { defineProject } from 'vitest/config'
2
-
3
- export default defineProject({
4
- test: {},
5
- })