@atcute/oauth-browser-client 1.0.4 → 1.0.5
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/lib/agents/exchange.ts +115 -0
- package/lib/agents/server-agent.ts +149 -0
- package/lib/agents/sessions.ts +142 -0
- package/lib/agents/user-agent.ts +99 -0
- package/lib/constants.ts +1 -0
- package/lib/dpop.ts +154 -0
- package/lib/environment.ts +27 -0
- package/lib/errors.ts +76 -0
- package/lib/index.ts +17 -0
- package/lib/resolvers.ts +222 -0
- package/lib/store/db.ts +184 -0
- package/lib/types/client.ts +82 -0
- package/lib/types/dpop.ts +7 -0
- package/lib/types/identity.ts +7 -0
- package/lib/types/par.ts +4 -0
- package/lib/types/server.ts +67 -0
- package/lib/types/store.ts +6 -0
- package/lib/types/token.ts +46 -0
- package/lib/utils/misc.ts +14 -0
- package/lib/utils/response.ts +3 -0
- package/lib/utils/runtime.ts +55 -0
- package/lib/utils/strings.ts +5 -0
- package/package.json +8 -4
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { At } from '@atcute/client/lexicons';
|
|
2
|
+
|
|
3
|
+
import type { DPoPKey } from './dpop.js';
|
|
4
|
+
import type { PersistedAuthorizationServerMetadata } from './server.js';
|
|
5
|
+
|
|
6
|
+
export interface OAuthTokenResponse {
|
|
7
|
+
access_token: string;
|
|
8
|
+
// Can be DPoP or Bearer, normalize casing.
|
|
9
|
+
token_type: string;
|
|
10
|
+
issuer?: string;
|
|
11
|
+
sub?: string;
|
|
12
|
+
scope?: string;
|
|
13
|
+
id_token?: `${string}.${string}.${string}`;
|
|
14
|
+
refresh_token?: string;
|
|
15
|
+
expires_in?: number;
|
|
16
|
+
authorization_details?:
|
|
17
|
+
| {
|
|
18
|
+
type: string;
|
|
19
|
+
locations?: string[];
|
|
20
|
+
actions?: string[];
|
|
21
|
+
datatypes?: string[];
|
|
22
|
+
identifier?: string;
|
|
23
|
+
privileges?: string[];
|
|
24
|
+
}[]
|
|
25
|
+
| undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface TokenInfo {
|
|
29
|
+
scope: string;
|
|
30
|
+
type: string;
|
|
31
|
+
expires_at?: number;
|
|
32
|
+
refresh?: string;
|
|
33
|
+
access: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface ExchangeInfo {
|
|
37
|
+
sub: At.DID;
|
|
38
|
+
aud: string;
|
|
39
|
+
server: PersistedAuthorizationServerMetadata;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface Session {
|
|
43
|
+
dpopKey: DPoPKey;
|
|
44
|
+
info: ExchangeInfo;
|
|
45
|
+
token: TokenInfo;
|
|
46
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
type UnwrapArray<T> = T extends (infer V)[] ? V : never;
|
|
2
|
+
|
|
3
|
+
export const pick = <T, K extends (keyof T)[]>(obj: T, keys: K): Pick<T, UnwrapArray<K>> => {
|
|
4
|
+
const cloned = {};
|
|
5
|
+
|
|
6
|
+
for (let idx = 0, len = keys.length; idx < len; idx++) {
|
|
7
|
+
const key = keys[idx];
|
|
8
|
+
|
|
9
|
+
// @ts-expect-error
|
|
10
|
+
cloned[key] = obj[key];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return cloned as Pick<T, UnwrapArray<K>>;
|
|
14
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export const encoder = new TextEncoder();
|
|
2
|
+
|
|
3
|
+
export const locks = navigator.locks as LockManager | undefined;
|
|
4
|
+
|
|
5
|
+
export const toBase64Url = (input: Uint8Array): string => {
|
|
6
|
+
const CHUNK_SIZE = 0x8000;
|
|
7
|
+
const arr = [];
|
|
8
|
+
|
|
9
|
+
for (let i = 0; i < input.byteLength; i += CHUNK_SIZE) {
|
|
10
|
+
// @ts-expect-error
|
|
11
|
+
arr.push(String.fromCharCode.apply(null, input.subarray(i, i + CHUNK_SIZE)));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return btoa(arr.join('')).replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const fromBase64Url = (input: string): Uint8Array => {
|
|
18
|
+
try {
|
|
19
|
+
const binary = atob(input.replace(/-/g, '+').replace(/_/g, '/').replace(/\s/g, ''));
|
|
20
|
+
const bytes = new Uint8Array(binary.length);
|
|
21
|
+
|
|
22
|
+
for (let i = 0; i < binary.length; i++) {
|
|
23
|
+
bytes[i] = binary.charCodeAt(i);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return bytes;
|
|
27
|
+
} catch (err) {
|
|
28
|
+
throw new TypeError(`invalid base64url`, { cause: err });
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const toSha256 = async (input: string): Promise<string> => {
|
|
33
|
+
const bytes = encoder.encode(input);
|
|
34
|
+
const digest = await crypto.subtle.digest('SHA-256', bytes);
|
|
35
|
+
|
|
36
|
+
return toBase64Url(new Uint8Array(digest));
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export const randomBytes = (length: number): string => {
|
|
40
|
+
return toBase64Url(crypto.getRandomValues(new Uint8Array(length)));
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const generateState = (): string => {
|
|
44
|
+
return randomBytes(16);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export const generatePKCE = async (): Promise<{ verifier: string; challenge: string; method: string }> => {
|
|
48
|
+
const verifier = randomBytes(32);
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
verifier: verifier,
|
|
52
|
+
challenge: await toSha256(verifier),
|
|
53
|
+
method: 'S256',
|
|
54
|
+
};
|
|
55
|
+
};
|
package/package.json
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@atcute/oauth-browser-client",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.5",
|
|
5
5
|
"description": "minimal OAuth browser client implementation for AT Protocol",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
8
|
-
"url": "https://
|
|
8
|
+
"url": "https://github.com/mary-ext/atcute",
|
|
9
|
+
"directory": "packages/oauth/browser-client"
|
|
9
10
|
},
|
|
10
11
|
"files": [
|
|
11
|
-
"dist/"
|
|
12
|
+
"dist/",
|
|
13
|
+
"lib/",
|
|
14
|
+
"!lib/**/*.bench.ts",
|
|
15
|
+
"!lib/**/*.test.ts"
|
|
12
16
|
],
|
|
13
17
|
"exports": {
|
|
14
18
|
".": "./dist/index.js"
|
|
@@ -16,7 +20,7 @@
|
|
|
16
20
|
"sideEffects": false,
|
|
17
21
|
"dependencies": {
|
|
18
22
|
"nanoid": "^5.0.7",
|
|
19
|
-
"@atcute/client": "^2.0.
|
|
23
|
+
"@atcute/client": "^2.0.4"
|
|
20
24
|
},
|
|
21
25
|
"devDependencies": {
|
|
22
26
|
"@types/bun": "^1.1.12"
|