@workjournal/shared 0.1.0 → 0.2.0
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/dist/auth.d.ts +22 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +72 -0
- package/dist/auth.js.map +1 -0
- package/package.json +5 -1
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface PkceParams {
|
|
2
|
+
verifier: string;
|
|
3
|
+
challenge: string;
|
|
4
|
+
}
|
|
5
|
+
export declare function generatePkce(): PkceParams;
|
|
6
|
+
export declare function buildAuthorizeUrl(challenge: string, opts?: {
|
|
7
|
+
appUrl?: string;
|
|
8
|
+
}): string;
|
|
9
|
+
export interface ExchangeResult {
|
|
10
|
+
access_token: string;
|
|
11
|
+
refresh_token: string | null;
|
|
12
|
+
expires_in: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Exchange a CLI-flow code (and its PKCE verifier) for Supabase session
|
|
16
|
+
* tokens. Throws on transport failure, request timeout (15s), or non-2xx
|
|
17
|
+
* upstream response. Caller is responsible for persisting the result.
|
|
18
|
+
*/
|
|
19
|
+
export declare function exchangeLoginCode(code: string, verifier: string, opts?: {
|
|
20
|
+
apiUrl?: string;
|
|
21
|
+
}): Promise<ExchangeResult>;
|
|
22
|
+
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAMA,MAAM,WAAW,UAAU;IAC1B,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;CAClB;AAED,wBAAgB,YAAY,IAAI,UAAU,CAIzC;AAMD,wBAAgB,iBAAiB,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAUvF;AAED,MAAM,WAAW,cAAc;IAC9B,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,UAAU,EAAE,MAAM,CAAC;CACnB;AAED;;;;GAIG;AACH,wBAAsB,iBAAiB,CACtC,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,EAChB,IAAI,CAAC,EAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,GACxB,OAAO,CAAC,cAAc,CAAC,CA4CzB"}
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
2
|
+
const APP_URL = process.env['WORKJOURNAL_APP_URL'] ?? 'https://app.workjournal.pro';
|
|
3
|
+
const API_URL = process.env['WORKJOURNAL_API_URL'] ?? 'https://api.workjournal.pro';
|
|
4
|
+
const OOB_REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob';
|
|
5
|
+
export function generatePkce() {
|
|
6
|
+
const verifier = randomBytes(32).toString('base64url');
|
|
7
|
+
const challenge = createHash('sha256').update(verifier).digest('base64url');
|
|
8
|
+
return { verifier, challenge };
|
|
9
|
+
}
|
|
10
|
+
function generateState() {
|
|
11
|
+
return randomBytes(16).toString('hex');
|
|
12
|
+
}
|
|
13
|
+
export function buildAuthorizeUrl(challenge, opts) {
|
|
14
|
+
const base = opts?.appUrl ?? APP_URL;
|
|
15
|
+
const state = generateState();
|
|
16
|
+
return (`${base}/authorize?client_id=workjournal-cli` +
|
|
17
|
+
`&redirect_uri=${encodeURIComponent(OOB_REDIRECT_URI)}` +
|
|
18
|
+
`&state=${state}` +
|
|
19
|
+
`&code_challenge=${challenge}` +
|
|
20
|
+
`&code_challenge_method=S256`);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Exchange a CLI-flow code (and its PKCE verifier) for Supabase session
|
|
24
|
+
* tokens. Throws on transport failure, request timeout (15s), or non-2xx
|
|
25
|
+
* upstream response. Caller is responsible for persisting the result.
|
|
26
|
+
*/
|
|
27
|
+
export async function exchangeLoginCode(code, verifier, opts) {
|
|
28
|
+
const apiUrl = opts?.apiUrl ?? API_URL;
|
|
29
|
+
const controller = new AbortController();
|
|
30
|
+
const timeout = setTimeout(() => controller.abort(), 15_000);
|
|
31
|
+
let res;
|
|
32
|
+
try {
|
|
33
|
+
res = await fetch(`${apiUrl}/v1/auth/cli/exchange`, {
|
|
34
|
+
method: 'POST',
|
|
35
|
+
headers: { 'Content-Type': 'application/json' },
|
|
36
|
+
body: JSON.stringify({ code, code_verifier: verifier }),
|
|
37
|
+
signal: controller.signal,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
clearTimeout(timeout);
|
|
42
|
+
const message = error instanceof Error && error.name === 'AbortError'
|
|
43
|
+
? 'Request timed out after 15s'
|
|
44
|
+
: error instanceof Error
|
|
45
|
+
? error.message
|
|
46
|
+
: String(error);
|
|
47
|
+
throw new Error(`Failed to exchange code: ${message}`);
|
|
48
|
+
}
|
|
49
|
+
// Keep the AbortController active across body-read so a hung-mid-stream
|
|
50
|
+
// upstream still trips the 15s timeout. clearTimeout fires only after the
|
|
51
|
+
// body is fully consumed (or the read itself throws).
|
|
52
|
+
try {
|
|
53
|
+
if (!res.ok) {
|
|
54
|
+
const text = await res.text();
|
|
55
|
+
let message = text;
|
|
56
|
+
try {
|
|
57
|
+
const parsed = JSON.parse(text);
|
|
58
|
+
if (parsed.error?.message)
|
|
59
|
+
message = parsed.error.message;
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
// keep raw text
|
|
63
|
+
}
|
|
64
|
+
throw new Error(`Failed to exchange code (${res.status}): ${message}`);
|
|
65
|
+
}
|
|
66
|
+
return (await res.json());
|
|
67
|
+
}
|
|
68
|
+
finally {
|
|
69
|
+
clearTimeout(timeout);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=auth.js.map
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,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC;YAC3D,CAAC;YAAC,MAAM,CAAC;gBACR,gBAAgB;YACjB,CAAC;YACD,MAAM,IAAI,KAAK,CAAC,4BAA4B,GAAG,CAAC,MAAM,MAAM,OAAO,EAAE,CAAC,CAAC;QACxE,CAAC;QAED,OAAO,CAAC,MAAM,GAAG,CAAC,IAAI,EAAE,CAAmB,CAAC;IAC7C,CAAC;YAAS,CAAC;QACV,YAAY,CAAC,OAAO,CAAC,CAAC;IACvB,CAAC;AACF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@workjournal/shared",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Shared types, constants, and credential helpers for the Workjournal CLI and MCP server.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -12,6 +12,10 @@
|
|
|
12
12
|
"./credentials": {
|
|
13
13
|
"types": "./dist/credentials.d.ts",
|
|
14
14
|
"import": "./dist/credentials.js"
|
|
15
|
+
},
|
|
16
|
+
"./auth": {
|
|
17
|
+
"types": "./dist/auth.d.ts",
|
|
18
|
+
"import": "./dist/auth.js"
|
|
15
19
|
}
|
|
16
20
|
},
|
|
17
21
|
"files": [
|