@oauth-spa-kit/core 2.2.1 → 2.3.1
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/sessionClient.d.ts +11 -0
- package/dist/sessionClient.js +37 -10
- package/dist/tokenExchange.js +2 -0
- package/dist/types.d.ts +17 -0
- package/package.json +1 -1
package/dist/sessionClient.d.ts
CHANGED
|
@@ -36,6 +36,17 @@ export interface SessionClientOptions {
|
|
|
36
36
|
* for anything beyond reactive state, e.g. redirecting straight to `login()`.
|
|
37
37
|
*/
|
|
38
38
|
onSessionExpired?: () => void;
|
|
39
|
+
/**
|
|
40
|
+
* Inactivity window (ms) before an authenticated session is force-logged-out client-side,
|
|
41
|
+
* regardless of whether the server-side session/token is still valid. Neither the liveness
|
|
42
|
+
* poller above nor the navigation-triggered refresh() ever notice a merely idle tab -- both
|
|
43
|
+
* only catch a session the server already killed -- so without this, a tab left open and
|
|
44
|
+
* untouched with an otherwise-valid session stays logged in forever. Reset on `mousemove`,
|
|
45
|
+
* `keydown`, `click`, `scroll`, and `touchstart`. Defaults to 15 minutes; pass `0` to disable.
|
|
46
|
+
*/
|
|
47
|
+
idleTimeoutMs?: number;
|
|
48
|
+
/** Called immediately before the idle-triggered logout redirect fires. */
|
|
49
|
+
onIdleTimeout?: () => void;
|
|
39
50
|
}
|
|
40
51
|
export interface SessionClient {
|
|
41
52
|
getState(): SessionState;
|
package/dist/sessionClient.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
const initialState = { ready: false, loggedIn: false, user: null };
|
|
2
2
|
/** Consecutive liveness-check failures required before treating a session as actually dead. */
|
|
3
3
|
const LIVENESS_FAILURE_THRESHOLD = 2;
|
|
4
|
+
const DEFAULT_IDLE_TIMEOUT_MS = 15 * 60_000;
|
|
5
|
+
const IDLE_ACTIVITY_EVENTS = ["mousemove", "keydown", "click", "scroll", "touchstart"];
|
|
4
6
|
export function createSessionClient(options = {}) {
|
|
5
7
|
const baseUrl = options.baseUrl ?? "";
|
|
6
8
|
const fetchImpl = options.fetchImpl ?? fetch;
|
|
@@ -10,8 +12,26 @@ export function createSessionClient(options = {}) {
|
|
|
10
12
|
: null;
|
|
11
13
|
let state = initialState;
|
|
12
14
|
const listeners = new Set();
|
|
15
|
+
const idleTimeoutMs = options.idleTimeoutMs ?? DEFAULT_IDLE_TIMEOUT_MS;
|
|
16
|
+
const idleTrackingEnabled = typeof window !== "undefined" && idleTimeoutMs > 0;
|
|
17
|
+
let idleTimer;
|
|
18
|
+
// Restarted on every activity event and on every state change (so a fresh login starts the
|
|
19
|
+
// window immediately rather than waiting for the first mouse move) -- see `idleTimeoutMs` above.
|
|
20
|
+
function resetIdleTimer() {
|
|
21
|
+
if (!idleTrackingEnabled)
|
|
22
|
+
return;
|
|
23
|
+
if (idleTimer)
|
|
24
|
+
clearTimeout(idleTimer);
|
|
25
|
+
idleTimer = state.loggedIn
|
|
26
|
+
? setTimeout(() => {
|
|
27
|
+
options.onIdleTimeout?.();
|
|
28
|
+
void logout();
|
|
29
|
+
}, idleTimeoutMs)
|
|
30
|
+
: undefined;
|
|
31
|
+
}
|
|
13
32
|
function setState(next) {
|
|
14
33
|
state = next;
|
|
34
|
+
resetIdleTimer();
|
|
15
35
|
for (const listener of listeners)
|
|
16
36
|
listener(state);
|
|
17
37
|
}
|
|
@@ -71,6 +91,21 @@ export function createSessionClient(options = {}) {
|
|
|
71
91
|
})();
|
|
72
92
|
}, options.livenessCheckIntervalMs);
|
|
73
93
|
}
|
|
94
|
+
function login(returnTo) {
|
|
95
|
+
const url = new URL(`${baseUrl}/auth/login`, window.location.origin);
|
|
96
|
+
if (returnTo)
|
|
97
|
+
url.searchParams.set("returnTo", returnTo);
|
|
98
|
+
window.location.assign(url.toString());
|
|
99
|
+
}
|
|
100
|
+
async function logout() {
|
|
101
|
+
const url = new URL(`${baseUrl}/auth/logout`, window.location.origin);
|
|
102
|
+
window.location.assign(url.toString());
|
|
103
|
+
}
|
|
104
|
+
if (idleTrackingEnabled) {
|
|
105
|
+
for (const event of IDLE_ACTIVITY_EVENTS) {
|
|
106
|
+
window.addEventListener(event, resetIdleTimer, { passive: true });
|
|
107
|
+
}
|
|
108
|
+
}
|
|
74
109
|
return {
|
|
75
110
|
getState: () => state,
|
|
76
111
|
subscribe(callback) {
|
|
@@ -78,15 +113,7 @@ export function createSessionClient(options = {}) {
|
|
|
78
113
|
return () => listeners.delete(callback);
|
|
79
114
|
},
|
|
80
115
|
refresh,
|
|
81
|
-
login
|
|
82
|
-
|
|
83
|
-
if (returnTo)
|
|
84
|
-
url.searchParams.set("returnTo", returnTo);
|
|
85
|
-
window.location.assign(url.toString());
|
|
86
|
-
},
|
|
87
|
-
async logout() {
|
|
88
|
-
const url = new URL(`${baseUrl}/auth/logout`, window.location.origin);
|
|
89
|
-
window.location.assign(url.toString());
|
|
90
|
-
},
|
|
116
|
+
login,
|
|
117
|
+
logout,
|
|
91
118
|
};
|
|
92
119
|
}
|
package/dist/tokenExchange.js
CHANGED
|
@@ -60,6 +60,7 @@ export async function exchangeAuthorizationCode(args) {
|
|
|
60
60
|
code: args.code,
|
|
61
61
|
redirect_uri: args.config.redirectUri,
|
|
62
62
|
code_verifier: args.codeVerifier,
|
|
63
|
+
...(args.config.resource ? { resource: args.config.resource } : {}),
|
|
63
64
|
...assertionParams,
|
|
64
65
|
});
|
|
65
66
|
};
|
|
@@ -90,6 +91,7 @@ export async function exchangeRefreshToken(args) {
|
|
|
90
91
|
grant_type: "refresh_token",
|
|
91
92
|
client_id: args.config.clientId,
|
|
92
93
|
refresh_token: args.refreshToken,
|
|
94
|
+
...(args.config.resource ? { resource: args.config.resource } : {}),
|
|
93
95
|
...assertionParams,
|
|
94
96
|
});
|
|
95
97
|
};
|
package/dist/types.d.ts
CHANGED
|
@@ -57,6 +57,23 @@ export interface OAuthClientConfig {
|
|
|
57
57
|
*/
|
|
58
58
|
par?: boolean;
|
|
59
59
|
extraAuthorizationParams?: Record<string, string>;
|
|
60
|
+
/**
|
|
61
|
+
* RFC 8707 resource indicator -- which protected-resource audience this client wants on its
|
|
62
|
+
* access token. Automatically included on every leg that mints or renews a token: the
|
|
63
|
+
* authorize/PAR request (`createLoginHandler`) *and* every token-endpoint call
|
|
64
|
+
* (`exchangeAuthorizationCode`, `exchangeRefreshToken`) -- set this once here rather than
|
|
65
|
+
* duplicating it into `extraAuthorizationParams` yourself. Some authorization servers scope a
|
|
66
|
+
* resource's inclusion in `aud` to an explicit indicator on the *current* token request (RFC
|
|
67
|
+
* 8707 section 2 permits this; IdentityServer's own `RequireResourceIndicator` is one
|
|
68
|
+
* well-known example of an AS that does) and silently drop that resource from the minted
|
|
69
|
+
* token otherwise, even though it was already validated at authorize time -- the
|
|
70
|
+
* authorization code/refresh token only carries forward what was *granted*, not what gets
|
|
71
|
+
* *reissued* on every subsequent token call. Confirmed against a real independently-built AS
|
|
72
|
+
* with this exact behavior. Single-valued:
|
|
73
|
+
* a consumer needing more than one simultaneous audience should request them as separate
|
|
74
|
+
* tokens.
|
|
75
|
+
*/
|
|
76
|
+
resource?: string;
|
|
60
77
|
}
|
|
61
78
|
/** Raw token endpoint response (RFC 6749 section 5.1). */
|
|
62
79
|
export interface TokenResponse {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oauth-spa-kit/core",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.1",
|
|
4
4
|
"description": "Framework- and runtime-agnostic OAuth 2.1 / OIDC primitives (PKCE, discovery, token exchange, DPoP) plus a fetch-based session client. Runs in the browser, Node, and edge runtimes -- no DOM or Node-only APIs.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|