@oxog/authkeeper 1.3.1 → 1.5.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/CHANGELOG.md +24 -0
- package/README.md +152 -2
- package/dist/auth-source-CXqrhLDO.d.ts +37 -0
- package/dist/auth-source-rjMWiP1Q.d.cts +37 -0
- package/dist/{chunk-NZPSW4M7.cjs → chunk-47JWRVTL.cjs} +21 -29
- package/dist/chunk-47JWRVTL.cjs.map +1 -0
- package/dist/{chunk-XUTTHBMF.js → chunk-ANGR5HRJ.js} +12 -19
- package/dist/chunk-ANGR5HRJ.js.map +1 -0
- package/dist/chunk-DLCD3SBV.js +17 -0
- package/dist/chunk-DLCD3SBV.js.map +1 -0
- package/dist/chunk-LGZGECYE.cjs +96 -0
- package/dist/chunk-LGZGECYE.cjs.map +1 -0
- package/dist/chunk-UUQDRHGF.js +90 -0
- package/dist/chunk-UUQDRHGF.js.map +1 -0
- package/dist/chunk-XGKVBZSF.cjs +19 -0
- package/dist/chunk-XGKVBZSF.cjs.map +1 -0
- package/dist/index-C4pGS-TI.d.cts +488 -0
- package/dist/index-CQwGO-Wk.d.ts +488 -0
- package/dist/index.cjs +71 -60
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -3
- package/dist/index.d.ts +9 -3
- package/dist/index.js +24 -12
- package/dist/index.js.map +1 -1
- package/dist/plugins/index.cjs +15 -14
- package/dist/plugins/index.d.cts +2 -1
- package/dist/plugins/index.d.ts +2 -1
- package/dist/plugins/index.js +2 -1
- package/dist/react/index.cjs +124 -3
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.d.cts +136 -5
- package/dist/react/index.d.ts +136 -5
- package/dist/react/index.js +118 -3
- package/dist/react/index.js.map +1 -1
- package/dist/svelte/index.cjs +53 -3
- package/dist/svelte/index.cjs.map +1 -1
- package/dist/svelte/index.d.cts +54 -5
- package/dist/svelte/index.d.ts +54 -5
- package/dist/svelte/index.js +51 -3
- package/dist/svelte/index.js.map +1 -1
- package/dist/types-DMPBl-Ym.d.cts +421 -0
- package/dist/types-DMPBl-Ym.d.ts +421 -0
- package/dist/vue/index.cjs +75 -3
- package/dist/vue/index.cjs.map +1 -1
- package/dist/vue/index.d.cts +93 -5
- package/dist/vue/index.d.ts +93 -5
- package/dist/vue/index.js +70 -3
- package/dist/vue/index.js.map +1 -1
- package/package.json +14 -8
- package/dist/chunk-NZPSW4M7.cjs.map +0 -1
- package/dist/chunk-XUTTHBMF.js.map +0 -1
- package/dist/index-BakvZ2rC.d.cts +0 -906
- package/dist/index-BakvZ2rC.d.ts +0 -906
package/dist/svelte/index.js
CHANGED
|
@@ -1,6 +1,54 @@
|
|
|
1
|
-
|
|
2
|
-
var __SVELTE_ADAPTER_PLACEHOLDER__ = true;
|
|
1
|
+
import { createAuthSource, readClaim } from '../chunk-UUQDRHGF.js';
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
// src/adapters/svelte/auth-store.ts
|
|
4
|
+
function readable(source, read) {
|
|
5
|
+
const subscribers = /* @__PURE__ */ new Set();
|
|
6
|
+
let stop = null;
|
|
7
|
+
let value = read(source);
|
|
8
|
+
return function subscribe(run) {
|
|
9
|
+
const entry = { run };
|
|
10
|
+
if (subscribers.size === 0) {
|
|
11
|
+
value = read(source);
|
|
12
|
+
stop = source.subscribe(() => {
|
|
13
|
+
const next = read(source);
|
|
14
|
+
if (next === value) return;
|
|
15
|
+
value = next;
|
|
16
|
+
Array.from(subscribers).forEach((subscriber) => subscriber.run(value));
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
subscribers.add(entry);
|
|
20
|
+
run(value);
|
|
21
|
+
return () => {
|
|
22
|
+
if (!subscribers.delete(entry)) return;
|
|
23
|
+
if (subscribers.size === 0 && stop) {
|
|
24
|
+
stop();
|
|
25
|
+
stop = null;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function createAuthStore(auth) {
|
|
31
|
+
return {
|
|
32
|
+
subscribe: readable(createAuthSource(auth), (source) => source.getState()),
|
|
33
|
+
setTokens: (tokens) => auth.setTokens(tokens),
|
|
34
|
+
logout: (options) => auth.logout(options),
|
|
35
|
+
refresh: () => auth.refresh()
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function createTokenStore(auth) {
|
|
39
|
+
return {
|
|
40
|
+
subscribe: readable(createAuthSource(auth), (source) => source.getTokenState()),
|
|
41
|
+
authFetch: auth.createFetch()
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function createUserStore(auth) {
|
|
45
|
+
const source = createAuthSource(auth);
|
|
46
|
+
return {
|
|
47
|
+
subscribe: readable(source, () => source.getState().user),
|
|
48
|
+
getClaim: (key) => readClaim(source.getState().user, key)
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { createAuthStore, createTokenStore, createUserStore };
|
|
5
53
|
//# sourceMappingURL=index.js.map
|
|
6
54
|
//# sourceMappingURL=index.js.map
|
package/dist/svelte/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/adapters/svelte/
|
|
1
|
+
{"version":3,"sources":["../../src/adapters/svelte/auth-store.ts"],"names":[],"mappings":";;;AAsDA,SAAS,QAAA,CACP,QACA,IAAA,EAC0B;AAC1B,EAAA,MAAM,WAAA,uBAAkB,GAAA,EAAiC;AACzD,EAAA,IAAI,IAAA,GAA4B,IAAA;AAChC,EAAA,IAAI,KAAA,GAAQ,KAAK,MAAM,CAAA;AAEvB,EAAA,OAAO,SAAS,UAAU,GAAA,EAAqC;AAC7D,IAAA,MAAM,KAAA,GAAQ,EAAE,GAAA,EAAI;AACpB,IAAA,IAAI,WAAA,CAAY,SAAS,CAAA,EAAG;AAC1B,MAAA,KAAA,GAAQ,KAAK,MAAM,CAAA;AACnB,MAAA,IAAA,GAAO,MAAA,CAAO,UAAU,MAAM;AAC5B,QAAA,MAAM,IAAA,GAAO,KAAK,MAAM,CAAA;AACxB,QAAA,IAAI,SAAS,KAAA,EAAO;AACpB,QAAA,KAAA,GAAQ,IAAA;AACR,QAAA,KAAA,CAAM,IAAA,CAAK,WAAW,CAAA,CAAE,OAAA,CAAQ,CAAC,UAAA,KAAe,UAAA,CAAW,GAAA,CAAI,KAAK,CAAC,CAAA;AAAA,MACvE,CAAC,CAAA;AAAA,IACH;AACA,IAAA,WAAA,CAAY,IAAI,KAAK,CAAA;AACrB,IAAA,GAAA,CAAI,KAAK,CAAA;AACT,IAAA,OAAO,MAAM;AACX,MAAA,IAAI,CAAC,WAAA,CAAY,MAAA,CAAO,KAAK,CAAA,EAAG;AAChC,MAAA,IAAI,WAAA,CAAY,IAAA,KAAS,CAAA,IAAK,IAAA,EAAM;AAClC,QAAA,IAAA,EAAK;AACL,QAAA,IAAA,GAAO,IAAA;AAAA,MACT;AAAA,IACF,CAAA;AAAA,EACF,CAAA;AACF;AAMO,SAAS,gBAAgB,IAAA,EAA6B;AAC3D,EAAA,OAAO;AAAA,IACL,SAAA,EAAW,SAAS,gBAAA,CAAiB,IAAI,GAAG,CAAC,MAAA,KAAW,MAAA,CAAO,QAAA,EAAU,CAAA;AAAA,IACzE,SAAA,EAAW,CAAC,MAAA,KAAW,IAAA,CAAK,UAAU,MAAM,CAAA;AAAA,IAC5C,MAAA,EAAQ,CAAC,OAAA,KAAY,IAAA,CAAK,OAAO,OAAO,CAAA;AAAA,IACxC,OAAA,EAAS,MAAM,IAAA,CAAK,OAAA;AAAQ,GAC9B;AACF;AAMO,SAAS,iBAAiB,IAAA,EAA8B;AAC7D,EAAA,OAAO;AAAA,IACL,SAAA,EAAW,SAAS,gBAAA,CAAiB,IAAI,GAAG,CAAC,MAAA,KAAW,MAAA,CAAO,aAAA,EAAe,CAAA;AAAA,IAC9E,SAAA,EAAW,KAAK,WAAA;AAAY,GAC9B;AACF;AAOO,SAAS,gBAAgB,IAAA,EAA6B;AAC3D,EAAA,MAAM,MAAA,GAAS,iBAAiB,IAAI,CAAA;AACpC,EAAA,OAAO;AAAA,IACL,WAAW,QAAA,CAAS,MAAA,EAAQ,MAAM,MAAA,CAAO,QAAA,GAAW,IAAI,CAAA;AAAA,IACxD,QAAA,EAAU,CAAC,GAAA,KAAQ,SAAA,CAAU,OAAO,QAAA,EAAS,CAAE,MAAM,GAAG;AAAA,GAC1D;AACF","file":"index.js","sourcesContent":["/**\n * Svelte bindings: readable auth, token and user stores\n *\n * Implements the Svelte store contract directly (no runtime import of\n * `svelte`), so it works with Svelte 3, 4 and 5 (`$auth` in components).\n */\n\nimport type { AuthFetch, AuthKeeper, LogoutOptions, TokenPayload, TokenSet } from '../../types'\nimport {\n createAuthSource,\n readClaim,\n type AuthSource,\n type AuthState,\n type TokenState,\n} from '../auth-source'\n\n/**\n * Svelte readable store contract\n */\nexport interface Readable<T> {\n /** `run` is called now and on every change; returns the unsubscribe function */\n subscribe(run: (value: T) => void, invalidate?: () => void): () => void\n}\n\n/**\n * Readable store of the auth state, plus actions\n */\nexport interface AuthStore extends Readable<AuthState> {\n setTokens: (tokens: TokenSet) => void\n logout: (options?: LogoutOptions) => void\n refresh: () => Promise<TokenSet>\n}\n\n/**\n * Readable store of the tokens, plus an authenticated fetch\n */\nexport interface TokenStore extends Readable<TokenState> {\n /** `fetch` that adds the access token (and refreshes on 401): `auth.createFetch()` */\n authFetch: AuthFetch\n}\n\n/**\n * Readable store of the decoded access token payload (null when signed out or\n * for opaque tokens)\n */\nexport interface UserStore extends Readable<TokenPayload | null> {\n /** One claim of the current payload */\n getClaim: <K extends keyof TokenPayload>(key: K) => TokenPayload[K] | undefined\n}\n\n/**\n * Store of `read(source)` that listens to the instance only while it has\n * subscribers and notifies them only when the snapshot changes\n */\nfunction readable<T>(\n source: AuthSource,\n read: (source: AuthSource) => T,\n): Readable<T>['subscribe'] {\n const subscribers = new Set<{ run: (value: T) => void }>()\n let stop: (() => void) | null = null\n let value = read(source)\n\n return function subscribe(run: (value: T) => void): () => void {\n const entry = { run }\n if (subscribers.size === 0) {\n value = read(source)\n stop = source.subscribe(() => {\n const next = read(source)\n if (next === value) return\n value = next\n Array.from(subscribers).forEach((subscriber) => subscriber.run(value))\n })\n }\n subscribers.add(entry)\n run(value)\n return () => {\n if (!subscribers.delete(entry)) return\n if (subscribers.size === 0 && stop) {\n stop()\n stop = null\n }\n }\n }\n}\n\n/**\n * Create a readable store of the auth state for an AuthKeeper instance. It\n * listens to the instance only while it has subscribers.\n */\nexport function createAuthStore(auth: AuthKeeper): AuthStore {\n return {\n subscribe: readable(createAuthSource(auth), (source) => source.getState()),\n setTokens: (tokens) => auth.setTokens(tokens),\n logout: (options) => auth.logout(options),\n refresh: () => auth.refresh(),\n }\n}\n\n/**\n * Create a readable store of the tokens. It updates when a token or the expiry\n * changes and listens to the instance only while it has subscribers.\n */\nexport function createTokenStore(auth: AuthKeeper): TokenStore {\n return {\n subscribe: readable(createAuthSource(auth), (source) => source.getTokenState()),\n authFetch: auth.createFetch(),\n }\n}\n\n/**\n * Create a readable store of the decoded access token payload. It updates when\n * the access token changes and listens to the instance only while it has\n * subscribers.\n */\nexport function createUserStore(auth: AuthKeeper): UserStore {\n const source = createAuthSource(auth)\n return {\n subscribe: readable(source, () => source.getState().user),\n getClaim: (key) => readClaim(source.getState().user, key),\n }\n}\n"]}
|
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AuthKeeper Type Definitions
|
|
3
|
+
*
|
|
4
|
+
* Complete type system for the AuthKeeper library.
|
|
5
|
+
* All interfaces and types are exported for use by consumers.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Token set returned from authentication/refresh operations
|
|
9
|
+
*/
|
|
10
|
+
interface TokenSet {
|
|
11
|
+
/** JWT access token */
|
|
12
|
+
accessToken: string;
|
|
13
|
+
/** Optional refresh token for obtaining new access tokens */
|
|
14
|
+
refreshToken?: string;
|
|
15
|
+
/** Token expiration in seconds from now */
|
|
16
|
+
expiresIn?: number;
|
|
17
|
+
/** Token expiration as Unix timestamp (seconds) */
|
|
18
|
+
expiresAt?: number;
|
|
19
|
+
/** Token type (default: 'Bearer') */
|
|
20
|
+
tokenType?: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Standard JWT payload with common claims
|
|
24
|
+
*/
|
|
25
|
+
interface TokenPayload {
|
|
26
|
+
/** Subject - usually the user ID */
|
|
27
|
+
sub?: string;
|
|
28
|
+
/** Issuer - who created the token */
|
|
29
|
+
iss?: string;
|
|
30
|
+
/** Audience - who the token is intended for */
|
|
31
|
+
aud?: string | string[];
|
|
32
|
+
/** Expiration time (Unix timestamp in seconds) */
|
|
33
|
+
exp?: number;
|
|
34
|
+
/** Not before time (Unix timestamp in seconds) */
|
|
35
|
+
nbf?: number;
|
|
36
|
+
/** Issued at time (Unix timestamp in seconds) */
|
|
37
|
+
iat?: number;
|
|
38
|
+
/** JWT ID - unique identifier */
|
|
39
|
+
jti?: string;
|
|
40
|
+
/** User email address */
|
|
41
|
+
email?: string;
|
|
42
|
+
/** User display name */
|
|
43
|
+
name?: string;
|
|
44
|
+
/** User roles */
|
|
45
|
+
roles?: string[];
|
|
46
|
+
/** User permissions */
|
|
47
|
+
permissions?: string[];
|
|
48
|
+
/** OAuth2 scope */
|
|
49
|
+
scope?: string;
|
|
50
|
+
[key: string]: unknown;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* JWT header information
|
|
54
|
+
*/
|
|
55
|
+
interface TokenHeader {
|
|
56
|
+
/** Algorithm used to sign the token */
|
|
57
|
+
alg: string;
|
|
58
|
+
/** Token type (usually 'JWT') */
|
|
59
|
+
typ?: string;
|
|
60
|
+
/** Key ID used to sign the token */
|
|
61
|
+
kid?: string;
|
|
62
|
+
[key: string]: unknown;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Complete token information including decoded parts
|
|
66
|
+
*/
|
|
67
|
+
interface TokenInfo {
|
|
68
|
+
/** Raw JWT string */
|
|
69
|
+
raw: string;
|
|
70
|
+
/** Decoded payload */
|
|
71
|
+
payload: TokenPayload;
|
|
72
|
+
/** Decoded header */
|
|
73
|
+
header: TokenHeader;
|
|
74
|
+
/** When the token expires (null if no expiry) */
|
|
75
|
+
expiresAt: Date | null;
|
|
76
|
+
/** Whether the token is expired */
|
|
77
|
+
isExpired: boolean;
|
|
78
|
+
/** Time until expiry in milliseconds (null if no expiry) */
|
|
79
|
+
expiresIn: number | null;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Stored token data with metadata
|
|
83
|
+
*/
|
|
84
|
+
interface StoredTokens {
|
|
85
|
+
/** Access token */
|
|
86
|
+
accessToken: string;
|
|
87
|
+
/** Refresh token (null if not available) */
|
|
88
|
+
refreshToken: string | null;
|
|
89
|
+
/** Token type (e.g., 'Bearer') */
|
|
90
|
+
tokenType: string;
|
|
91
|
+
/** Expiration timestamp in milliseconds (null if no expiry) */
|
|
92
|
+
expiresAt: number | null;
|
|
93
|
+
/** When tokens were set (timestamp in milliseconds) */
|
|
94
|
+
setAt: number;
|
|
95
|
+
/** Number of times tokens have been refreshed */
|
|
96
|
+
refreshCount: number;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Error codes for authentication errors
|
|
100
|
+
*/
|
|
101
|
+
type AuthErrorCode = 'TOKEN_EXPIRED' | 'TOKEN_INVALID' | 'TOKEN_DECODE_FAILED' | 'REFRESH_FAILED' | 'REFRESH_TOKEN_MISSING' | 'STORAGE_ERROR' | 'NETWORK_ERROR' | 'UNAUTHORIZED' | 'FORBIDDEN' | 'OAUTH_ERROR' | 'PKCE_ERROR' | 'CALLBACK_ERROR' | 'CONFIG_ERROR';
|
|
102
|
+
/**
|
|
103
|
+
* Options for creating an AuthError
|
|
104
|
+
*/
|
|
105
|
+
interface AuthErrorOptions {
|
|
106
|
+
/** Additional context about the error */
|
|
107
|
+
context?: Record<string, unknown>;
|
|
108
|
+
/** Original error that caused this error */
|
|
109
|
+
cause?: Error;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Custom error class for authentication errors
|
|
113
|
+
*/
|
|
114
|
+
declare class AuthError extends Error {
|
|
115
|
+
/** Error code identifying the type of error */
|
|
116
|
+
code: AuthErrorCode;
|
|
117
|
+
/** Additional context about the error */
|
|
118
|
+
context?: Record<string, unknown>;
|
|
119
|
+
/** Original error that caused this error */
|
|
120
|
+
cause?: Error;
|
|
121
|
+
constructor(code: AuthErrorCode, message: string, options?: AuthErrorOptions);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* All possible event types
|
|
125
|
+
*/
|
|
126
|
+
type EventType = 'login' | 'logout' | 'refresh' | 'expired' | 'error' | 'storage-change' | 'tab-sync';
|
|
127
|
+
/**
|
|
128
|
+
* Login event - fired when user logs in
|
|
129
|
+
*/
|
|
130
|
+
interface LoginEvent {
|
|
131
|
+
type: 'login';
|
|
132
|
+
tokens: TokenSet;
|
|
133
|
+
timestamp: number;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Logout event - fired when user logs out
|
|
137
|
+
*/
|
|
138
|
+
interface LogoutEvent {
|
|
139
|
+
type: 'logout';
|
|
140
|
+
reason: 'manual' | 'expired' | 'error' | 'tab-sync';
|
|
141
|
+
timestamp: number;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Refresh event - fired when tokens are refreshed
|
|
145
|
+
*/
|
|
146
|
+
interface RefreshEvent {
|
|
147
|
+
type: 'refresh';
|
|
148
|
+
tokens: TokenSet;
|
|
149
|
+
previousExpiresAt: Date | null;
|
|
150
|
+
newExpiresAt: Date | null;
|
|
151
|
+
timestamp: number;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Expired event - fired when token expires
|
|
155
|
+
*/
|
|
156
|
+
interface ExpiredEvent {
|
|
157
|
+
type: 'expired';
|
|
158
|
+
expiredAt: Date;
|
|
159
|
+
timestamp: number;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Error event - fired when an error occurs
|
|
163
|
+
*/
|
|
164
|
+
interface ErrorEvent {
|
|
165
|
+
type: 'error';
|
|
166
|
+
error: AuthError;
|
|
167
|
+
context: 'refresh' | 'storage' | 'decode' | 'network' | 'plugin';
|
|
168
|
+
timestamp: number;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Storage change event - fired when storage changes
|
|
172
|
+
*/
|
|
173
|
+
interface StorageChangeEvent {
|
|
174
|
+
type: 'storage-change';
|
|
175
|
+
key: string;
|
|
176
|
+
oldValue: string | null;
|
|
177
|
+
newValue: string | null;
|
|
178
|
+
timestamp: number;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Tab sync event - fired when syncing across tabs
|
|
182
|
+
*/
|
|
183
|
+
interface TabSyncEvent {
|
|
184
|
+
type: 'tab-sync';
|
|
185
|
+
action: 'login' | 'logout' | 'refresh';
|
|
186
|
+
sourceTabId: string;
|
|
187
|
+
timestamp: number;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Union of all event types
|
|
191
|
+
*/
|
|
192
|
+
type AuthEvent = LoginEvent | LogoutEvent | RefreshEvent | ExpiredEvent | ErrorEvent | StorageChangeEvent | TabSyncEvent;
|
|
193
|
+
/**
|
|
194
|
+
* Event handler function type
|
|
195
|
+
*/
|
|
196
|
+
type EventHandler<E extends EventType> = (event: Extract<AuthEvent, {
|
|
197
|
+
type: E;
|
|
198
|
+
}>) => void;
|
|
199
|
+
/**
|
|
200
|
+
* Unsubscribe function returned from event handlers
|
|
201
|
+
*/
|
|
202
|
+
type Unsubscribe = () => void;
|
|
203
|
+
/**
|
|
204
|
+
* Storage adapter interface for token persistence
|
|
205
|
+
*/
|
|
206
|
+
interface StorageAdapter {
|
|
207
|
+
/** Get value by key */
|
|
208
|
+
get(key: string): string | null;
|
|
209
|
+
/** Set value by key */
|
|
210
|
+
set(key: string, value: string): void;
|
|
211
|
+
/** Remove value by key */
|
|
212
|
+
remove(key: string): void;
|
|
213
|
+
/** Clear all values */
|
|
214
|
+
clear(): void;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Built-in storage types
|
|
218
|
+
*/
|
|
219
|
+
type StorageType = 'memory' | 'localStorage' | 'sessionStorage' | 'cookie';
|
|
220
|
+
/**
|
|
221
|
+
* Plugin interface for extending AuthKeeper
|
|
222
|
+
*/
|
|
223
|
+
interface Plugin {
|
|
224
|
+
/** Unique plugin name */
|
|
225
|
+
name: string;
|
|
226
|
+
/** Plugin version */
|
|
227
|
+
version: string;
|
|
228
|
+
/** Plugin type */
|
|
229
|
+
type: 'core' | 'optional';
|
|
230
|
+
/** Install plugin and return API */
|
|
231
|
+
install(kernel: AuthKeeper): any;
|
|
232
|
+
/** Uninstall plugin and cleanup */
|
|
233
|
+
uninstall?(): void | Promise<void>;
|
|
234
|
+
/** Reserved lifecycle hooks; not invoked by the kernel yet */
|
|
235
|
+
hooks?: {
|
|
236
|
+
/** Called before tokens are set */
|
|
237
|
+
beforeSetTokens?: (tokens: TokenSet) => TokenSet | false;
|
|
238
|
+
/** Called after tokens are set */
|
|
239
|
+
afterSetTokens?: (tokens: TokenSet) => void;
|
|
240
|
+
/** Called before refresh */
|
|
241
|
+
beforeRefresh?: () => boolean;
|
|
242
|
+
/** Called after refresh */
|
|
243
|
+
afterRefresh?: (tokens: TokenSet) => void;
|
|
244
|
+
/** Called before logout */
|
|
245
|
+
beforeLogout?: () => boolean;
|
|
246
|
+
/** Called after logout */
|
|
247
|
+
afterLogout?: () => void;
|
|
248
|
+
/** Called when token expires */
|
|
249
|
+
onExpired?: () => void;
|
|
250
|
+
/** Called on error */
|
|
251
|
+
onError?: (error: AuthError) => void;
|
|
252
|
+
/** Called before request */
|
|
253
|
+
beforeRequest?: (request: Request) => Request;
|
|
254
|
+
/** Called after response */
|
|
255
|
+
afterResponse?: (response: Response) => Response;
|
|
256
|
+
};
|
|
257
|
+
/** Plugin API exposed to consumers */
|
|
258
|
+
api?: Record<string, unknown>;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Plugin information
|
|
262
|
+
*/
|
|
263
|
+
interface PluginInfo {
|
|
264
|
+
name: string;
|
|
265
|
+
version: string;
|
|
266
|
+
type: 'core' | 'optional';
|
|
267
|
+
enabled: boolean;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Refresh token function type
|
|
271
|
+
*/
|
|
272
|
+
type RefreshTokenFn = (refreshToken: string) => Promise<TokenSet>;
|
|
273
|
+
/**
|
|
274
|
+
* Kernel configuration options
|
|
275
|
+
*/
|
|
276
|
+
interface KernelOptions {
|
|
277
|
+
/**
|
|
278
|
+
* Where tokens are persisted (default: 'memory', i.e. not persisted).
|
|
279
|
+
* 'localStorage' / 'sessionStorage' / 'cookie' fall back to memory where
|
|
280
|
+
* unavailable. Persistent storage is readable by any script on the page.
|
|
281
|
+
*/
|
|
282
|
+
storage?: StorageType | StorageAdapter;
|
|
283
|
+
/** Function to refresh tokens */
|
|
284
|
+
refreshToken: RefreshTokenFn;
|
|
285
|
+
/** Enable automatic token refresh (default: true) */
|
|
286
|
+
autoRefresh?: boolean;
|
|
287
|
+
/** Seconds before expiry to refresh (default: 60) */
|
|
288
|
+
refreshThreshold?: number;
|
|
289
|
+
/**
|
|
290
|
+
* Reserved for multi-tab sync; not implemented yet (currently a no-op).
|
|
291
|
+
* `getOptions().syncTabs` always reports `false`.
|
|
292
|
+
*/
|
|
293
|
+
syncTabs?: boolean;
|
|
294
|
+
/** Error handler */
|
|
295
|
+
onError?: (error: AuthError) => void;
|
|
296
|
+
/** Additional plugins to install */
|
|
297
|
+
plugins?: Plugin[];
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Logout options
|
|
301
|
+
*/
|
|
302
|
+
interface LogoutOptions {
|
|
303
|
+
/** Reserved for multi-tab logout; not implemented yet (currently a no-op) */
|
|
304
|
+
allTabs?: boolean;
|
|
305
|
+
/** Redirect URL after logout */
|
|
306
|
+
redirect?: string;
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Authenticated fetch function
|
|
310
|
+
*/
|
|
311
|
+
type AuthFetch = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
312
|
+
/**
|
|
313
|
+
* Fetch interceptor options
|
|
314
|
+
*/
|
|
315
|
+
interface FetchInterceptorOptions {
|
|
316
|
+
/** Header name for auth token (default: 'Authorization') */
|
|
317
|
+
headerName?: string;
|
|
318
|
+
/** Header prefix (default: 'Bearer ') */
|
|
319
|
+
headerPrefix?: string;
|
|
320
|
+
/** Only include auth header for these URLs */
|
|
321
|
+
includeUrls?: (string | RegExp)[];
|
|
322
|
+
/** Exclude auth header for these URLs */
|
|
323
|
+
excludeUrls?: (string | RegExp)[];
|
|
324
|
+
/** Callback on 401 response */
|
|
325
|
+
onUnauthorized?: (response: Response) => void;
|
|
326
|
+
/** Retry on 401 after refresh (default: true) */
|
|
327
|
+
retry401?: boolean;
|
|
328
|
+
/** Max retries (default: 1) */
|
|
329
|
+
maxRetries?: number;
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* OAuth2 login options
|
|
333
|
+
*/
|
|
334
|
+
interface LoginOptions {
|
|
335
|
+
/** OAuth2 scope */
|
|
336
|
+
scope?: string;
|
|
337
|
+
/** OAuth2 state parameter */
|
|
338
|
+
state?: string;
|
|
339
|
+
/** OAuth2 prompt parameter */
|
|
340
|
+
prompt?: 'none' | 'login' | 'consent' | 'select_account';
|
|
341
|
+
/** Login hint (email, etc.) */
|
|
342
|
+
loginHint?: string;
|
|
343
|
+
/** Additional OAuth2 parameters */
|
|
344
|
+
extraParams?: Record<string, string>;
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* User info from OIDC userinfo endpoint
|
|
348
|
+
*/
|
|
349
|
+
interface UserInfo {
|
|
350
|
+
sub: string;
|
|
351
|
+
email?: string;
|
|
352
|
+
name?: string;
|
|
353
|
+
picture?: string;
|
|
354
|
+
[key: string]: unknown;
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Main AuthKeeper interface - the kernel
|
|
358
|
+
*/
|
|
359
|
+
interface AuthKeeper {
|
|
360
|
+
/** Set authentication tokens */
|
|
361
|
+
setTokens(tokens: TokenSet): void;
|
|
362
|
+
/** Get access token */
|
|
363
|
+
getAccessToken(): string | null;
|
|
364
|
+
/** Get refresh token */
|
|
365
|
+
getRefreshToken(): string | null;
|
|
366
|
+
/** Clear all tokens */
|
|
367
|
+
clearTokens(): void;
|
|
368
|
+
/** Check if user is authenticated */
|
|
369
|
+
isAuthenticated(): boolean;
|
|
370
|
+
/** Check if token is expired */
|
|
371
|
+
isExpired(): boolean;
|
|
372
|
+
/** Get token expiration date */
|
|
373
|
+
getExpiresAt(): Date | null;
|
|
374
|
+
/** Get time until expiry in milliseconds */
|
|
375
|
+
getTimeUntilExpiry(): number | null;
|
|
376
|
+
/** Decode token payload */
|
|
377
|
+
decode<T = TokenPayload>(): T | null;
|
|
378
|
+
/** Get specific claim from token */
|
|
379
|
+
getClaim<K extends keyof TokenPayload>(key: K): TokenPayload[K] | undefined;
|
|
380
|
+
/** Get multiple claims from token */
|
|
381
|
+
getClaims<K extends keyof TokenPayload>(keys: K[]): Pick<TokenPayload, K>;
|
|
382
|
+
/** Manually refresh tokens */
|
|
383
|
+
refresh(): Promise<TokenSet>;
|
|
384
|
+
/** Schedule automatic refresh */
|
|
385
|
+
scheduleRefresh(): void;
|
|
386
|
+
/** Cancel scheduled refresh */
|
|
387
|
+
cancelScheduledRefresh(): void;
|
|
388
|
+
/** Logout user */
|
|
389
|
+
logout(options?: LogoutOptions): void;
|
|
390
|
+
/** Create authenticated fetch function */
|
|
391
|
+
createFetch(options?: FetchInterceptorOptions): AuthFetch;
|
|
392
|
+
/** Wrap global fetch with auth */
|
|
393
|
+
wrapFetch(fetch: typeof globalThis.fetch): void;
|
|
394
|
+
/**
|
|
395
|
+
* Register and install a plugin. Rejects if installation fails; the failure
|
|
396
|
+
* is also emitted as an 'error' event with context 'plugin'.
|
|
397
|
+
*/
|
|
398
|
+
register(plugin: Plugin): Promise<void>;
|
|
399
|
+
/** Unregister a plugin */
|
|
400
|
+
unregister(pluginName: string): Promise<void>;
|
|
401
|
+
/** Get plugin by name */
|
|
402
|
+
getPlugin<P extends Plugin>(name: string): P | undefined;
|
|
403
|
+
/** List all plugins */
|
|
404
|
+
listPlugins(): PluginInfo[];
|
|
405
|
+
/** Emit an event */
|
|
406
|
+
emit(event: AuthEvent): void;
|
|
407
|
+
/** Subscribe to events */
|
|
408
|
+
on<E extends EventType>(eventType: E, handler: EventHandler<E>): Unsubscribe;
|
|
409
|
+
/** Unsubscribe from events */
|
|
410
|
+
off<E extends EventType>(eventType: E, handler: EventHandler<E>): void;
|
|
411
|
+
/** Initialize kernel */
|
|
412
|
+
init(): Promise<void>;
|
|
413
|
+
/** Destroy kernel and cleanup */
|
|
414
|
+
destroy(): void;
|
|
415
|
+
/** Update configuration */
|
|
416
|
+
configure(options: Partial<KernelOptions>): void;
|
|
417
|
+
/** Get current configuration */
|
|
418
|
+
getOptions(): KernelOptions;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
export { type AuthFetch as A, type EventType as E, type FetchInterceptorOptions as F, type KernelOptions as K, type LogoutOptions as L, type Plugin as P, type RefreshEvent as R, type StorageAdapter as S, type TokenSet as T, type Unsubscribe as U, type TokenPayload as a, type AuthKeeper as b, AuthError as c, type EventHandler as d, type AuthEvent as e, type PluginInfo as f, type TokenHeader as g, type AuthErrorCode as h, type AuthErrorOptions as i, type ErrorEvent as j, type ExpiredEvent as k, type LoginEvent as l, type LoginOptions as m, type LogoutEvent as n, type RefreshTokenFn as o, type StorageChangeEvent as p, type StorageType as q, type StoredTokens as r, type TabSyncEvent as s, type TokenInfo as t, type UserInfo as u };
|