@chipmobilesdk/rn-auth 0.3.0 → 0.4.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 +239 -113
- package/README.md +221 -190
- package/package.json +72 -54
- package/skills/sdk-rn-auth/SKILL.md +65 -63
- package/src/access/accessTier.ts +88 -88
- package/src/app-auth.ts +6 -0
- package/src/config/types.ts +154 -154
- package/src/config/validateConfig.ts +114 -108
- package/src/core/authMachine.ts +380 -364
- package/src/core/profile.ts +57 -60
- package/src/core/session.ts +105 -112
- package/src/diagnostics/errors.ts +99 -85
- package/src/diagnostics/messageKeys.ts +72 -61
- package/src/index.ts +72 -83
- package/src/keychain.ts +6 -0
- package/src/oidc/authClient.ts +236 -199
- package/src/oidc/classifyError.ts +76 -76
- package/src/oidc/socialRouting.ts +66 -73
- package/src/react/AuthProvider.tsx +30 -47
- package/src/react/hooks.ts +103 -91
- package/src/testing/conformance.ts +177 -0
- package/src/testing/index.ts +30 -0
- package/src/tokens/accessToken.ts +159 -164
- package/src/tokens/secureStore.ts +85 -52
package/src/core/authMachine.ts
CHANGED
|
@@ -1,364 +1,380 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Auth state machine / orchestrator (FR-005, FR-022, R5, R7). Holds the single
|
|
3
|
-
* active session, wires the OidcAuthClient + SecureTokenStore ports, profile
|
|
4
|
-
* mapping, access-tier evaluation, per-resource tokens, restore, and sign-out.
|
|
5
|
-
*
|
|
6
|
-
* Every operation that starts or ends a session bumps `sessionEpoch`. Work that
|
|
7
|
-
* awaited across such a change — a token refresh, a restore, an authorize —
|
|
8
|
-
* compares the epoch it started with before writing anything back, so a sign-out
|
|
9
|
-
* can never be undone by a request that was already in flight.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
} from '../oidc/
|
|
32
|
-
import {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
if (
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
setState({
|
|
212
|
-
status: 'authenticated',
|
|
213
|
-
profile: result.profile,
|
|
214
|
-
accessTier: result.accessTier
|
|
215
|
-
restore: '
|
|
216
|
-
error: undefined,
|
|
217
|
-
});
|
|
218
|
-
} else {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
const
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Auth state machine / orchestrator (FR-005, FR-022, R5, R7). Holds the single
|
|
3
|
+
* active session, wires the OidcAuthClient + SecureTokenStore ports, profile
|
|
4
|
+
* mapping, access-tier evaluation, per-resource tokens, restore, and sign-out.
|
|
5
|
+
*
|
|
6
|
+
* Every operation that starts or ends a session bumps `sessionEpoch`. Work that
|
|
7
|
+
* awaited across such a change — a token refresh, a restore, an authorize —
|
|
8
|
+
* compares the epoch it started with before writing anything back, so a sign-out
|
|
9
|
+
* can never be undone by a request that was already in flight.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
createDiagnosticChannel,
|
|
14
|
+
createListenerSet,
|
|
15
|
+
currentPlatform,
|
|
16
|
+
shallowEqual,
|
|
17
|
+
} from '@chipmobilesdk/rn-core';
|
|
18
|
+
|
|
19
|
+
import type {
|
|
20
|
+
Auth,
|
|
21
|
+
AuthConfig,
|
|
22
|
+
AuthState,
|
|
23
|
+
CreateAuthOptions,
|
|
24
|
+
DevicePlatform,
|
|
25
|
+
GlobalSignOutResult,
|
|
26
|
+
PremiumGateResult,
|
|
27
|
+
Profile,
|
|
28
|
+
ProviderId,
|
|
29
|
+
} from '../config/types';
|
|
30
|
+
import { validateConfig } from '../config/validateConfig';
|
|
31
|
+
import { createAppAuthClient } from '../oidc/authClient';
|
|
32
|
+
import {
|
|
33
|
+
availableProvidersForPlatform,
|
|
34
|
+
buildAdditionalParameters,
|
|
35
|
+
defaultProviderForPlatform,
|
|
36
|
+
resolveProvider,
|
|
37
|
+
} from '../oidc/socialRouting';
|
|
38
|
+
import { mapClaimsToProfile } from './profile';
|
|
39
|
+
import { evaluateAccessTier, failClosedTier } from '../access/accessTier';
|
|
40
|
+
import { restoreSession, type PersistedSession } from './session';
|
|
41
|
+
import { AUTH_STORAGE_KEY } from '../tokens/secureStore';
|
|
42
|
+
import { createAccessTokenManager } from '../tokens/accessToken';
|
|
43
|
+
import { AuthError, toAuthError, type AuthErrorCode } from '../diagnostics/errors';
|
|
44
|
+
|
|
45
|
+
const SIGNED_OUT: Partial<AuthState> = {
|
|
46
|
+
status: 'signed-out',
|
|
47
|
+
profile: undefined,
|
|
48
|
+
accessTier: undefined,
|
|
49
|
+
restore: 'unavailable',
|
|
50
|
+
error: undefined,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export function createAuthMachine(config: AuthConfig, options: CreateAuthOptions = {}): Auth {
|
|
54
|
+
// Throws AuthConfigError before anything else (FR-005, US4 scenario 2).
|
|
55
|
+
validateConfig(config);
|
|
56
|
+
|
|
57
|
+
// rn-core's one platform source (CORE-09). Outside Android and iOS (tests on
|
|
58
|
+
// Node, web) the providers are resolved as for iOS, as before.
|
|
59
|
+
const detected = currentPlatform();
|
|
60
|
+
const platform: DevicePlatform =
|
|
61
|
+
options.platform ?? (detected === 'unsupported' ? 'ios' : detected);
|
|
62
|
+
const secureStore = config.secureStore;
|
|
63
|
+
const authClient = config.authClient ?? createAppAuthClient();
|
|
64
|
+
const resources = config.resources ?? [];
|
|
65
|
+
|
|
66
|
+
let state: AuthState = Object.freeze({ status: 'loading', restore: 'pending' });
|
|
67
|
+
let refreshToken: string | null = null;
|
|
68
|
+
/** Kept for the end-session `id_token_hint`; without it a global sign-out is a no-op. */
|
|
69
|
+
let idToken: string | null = null;
|
|
70
|
+
let sessionEpoch = 0;
|
|
71
|
+
const listeners = createListenerSet<[AuthState]>();
|
|
72
|
+
/**
|
|
73
|
+
* Every Keychain write and delete, in order. A rotation persisted while a
|
|
74
|
+
* sign-out runs must not land after the sign-out's delete and resurrect the
|
|
75
|
+
* session on the next launch.
|
|
76
|
+
*/
|
|
77
|
+
let storeQueue: Promise<void> = Promise.resolve();
|
|
78
|
+
|
|
79
|
+
const enqueueStoreOp = (op: () => Promise<void>): Promise<void> => {
|
|
80
|
+
const next = storeQueue.then(op);
|
|
81
|
+
// The queue itself never rejects, so one failed write does not poison the
|
|
82
|
+
// writes after it; the caller still sees its own failure through `next`.
|
|
83
|
+
storeQueue = next.catch(() => undefined);
|
|
84
|
+
return next;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const writeSession = (session: PersistedSession, epoch: number): Promise<void> =>
|
|
88
|
+
enqueueStoreOp(async () => {
|
|
89
|
+
if (epoch !== sessionEpoch) {
|
|
90
|
+
return; // The session this belonged to has already ended.
|
|
91
|
+
}
|
|
92
|
+
await secureStore.setItem(AUTH_STORAGE_KEY, JSON.stringify(session));
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
const tokenManager = createAccessTokenManager({
|
|
96
|
+
authority: config.authority,
|
|
97
|
+
clientId: config.clientId,
|
|
98
|
+
redirectUrl: config.redirectUrl,
|
|
99
|
+
resources,
|
|
100
|
+
authClient,
|
|
101
|
+
getRefreshToken: () => refreshToken,
|
|
102
|
+
sessionEpoch: () => sessionEpoch,
|
|
103
|
+
async onRefreshTokenRotated(token, epoch) {
|
|
104
|
+
if (epoch !== sessionEpoch) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
// Live immediately whatever happens to the write: the provider may
|
|
108
|
+
// already have retired the old token, so memory must not keep using it.
|
|
109
|
+
refreshToken = token;
|
|
110
|
+
if (!state.profile) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
try {
|
|
114
|
+
await writeSession(
|
|
115
|
+
{ refreshToken: token, profile: state.profile, idToken: idToken ?? undefined },
|
|
116
|
+
epoch,
|
|
117
|
+
);
|
|
118
|
+
} catch {
|
|
119
|
+
// The session stays usable in memory. The stored artifact keeps the
|
|
120
|
+
// previous token until the next successful write; a restart before
|
|
121
|
+
// then restores through it if the provider still accepts it, and
|
|
122
|
+
// otherwise lands on sign-in — never on a corrupted state.
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* The current snapshot, the same object until the state changes, so React reads
|
|
129
|
+
* it through `useSyncExternalStore` without re-rendering for nothing (PERF-02).
|
|
130
|
+
* Frozen: a consumer cannot change the machine's state by mutating it.
|
|
131
|
+
*/
|
|
132
|
+
function getState(): AuthState {
|
|
133
|
+
return state;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function setState(patch: Partial<AuthState>): void {
|
|
137
|
+
const next = { ...state, ...patch };
|
|
138
|
+
// No change, no new snapshot and no notification.
|
|
139
|
+
if (shallowEqual(state, next)) return;
|
|
140
|
+
state = Object.freeze(next);
|
|
141
|
+
if (patch.error) {
|
|
142
|
+
// The code and audience only; `detail` is already redacted but stays local.
|
|
143
|
+
channel.emit(patch.error.code, patch.error.audience === 'user' ? 'info' : 'warn', {
|
|
144
|
+
status: state.status,
|
|
145
|
+
retryable: patch.error.retryable,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
// Isolated: a subscriber that throws neither stops the others nor turns a
|
|
149
|
+
// completed sign-in into an error; rn-core reports it instead (CORE-03).
|
|
150
|
+
listeners.notify(getState());
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Ends the local session: memory, cached access tokens, and the stored artifact. */
|
|
154
|
+
async function clearPersisted(): Promise<void> {
|
|
155
|
+
sessionEpoch += 1;
|
|
156
|
+
refreshToken = null;
|
|
157
|
+
idToken = null;
|
|
158
|
+
tokenManager.clear();
|
|
159
|
+
await enqueueStoreOp(() => secureStore.removeItem(AUTH_STORAGE_KEY));
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
getState,
|
|
164
|
+
|
|
165
|
+
subscribe: listener => listeners.add(listener),
|
|
166
|
+
|
|
167
|
+
async restore() {
|
|
168
|
+
sessionEpoch += 1;
|
|
169
|
+
const epoch = sessionEpoch;
|
|
170
|
+
setState({ status: 'loading', restore: 'pending', error: undefined });
|
|
171
|
+
|
|
172
|
+
let result;
|
|
173
|
+
try {
|
|
174
|
+
result = await restoreSession({
|
|
175
|
+
secureStore,
|
|
176
|
+
authority: config.authority,
|
|
177
|
+
clientId: config.clientId,
|
|
178
|
+
redirectUrl: config.redirectUrl,
|
|
179
|
+
scopes: config.scopes,
|
|
180
|
+
authClient,
|
|
181
|
+
mapping: config.accessTierMapping,
|
|
182
|
+
});
|
|
183
|
+
} catch (error) {
|
|
184
|
+
// The store could not be read. Nothing is known, nothing is deleted.
|
|
185
|
+
if (epoch === sessionEpoch) {
|
|
186
|
+
setState({ ...SIGNED_OUT, status: 'error', error: toAuthError(error) });
|
|
187
|
+
}
|
|
188
|
+
return getState();
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (epoch !== sessionEpoch) {
|
|
192
|
+
// A sign-in or sign-out ran meanwhile; its outcome stands.
|
|
193
|
+
return getState();
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (result.outcome === 'restored' && result.profile && result.refreshToken) {
|
|
197
|
+
refreshToken = result.refreshToken;
|
|
198
|
+
idToken = result.idToken ?? null;
|
|
199
|
+
try {
|
|
200
|
+
await writeSession(
|
|
201
|
+
{ refreshToken: result.refreshToken, profile: result.profile, idToken: result.idToken },
|
|
202
|
+
epoch,
|
|
203
|
+
);
|
|
204
|
+
} catch {
|
|
205
|
+
// The refresh succeeded, so the user is signed in for this run. Only
|
|
206
|
+
// the next launch is affected, and it falls back to sign-in.
|
|
207
|
+
}
|
|
208
|
+
if (epoch !== sessionEpoch) {
|
|
209
|
+
return getState();
|
|
210
|
+
}
|
|
211
|
+
setState({
|
|
212
|
+
status: 'authenticated',
|
|
213
|
+
profile: result.profile,
|
|
214
|
+
accessTier: result.accessTier,
|
|
215
|
+
restore: 'restored',
|
|
216
|
+
error: undefined,
|
|
217
|
+
});
|
|
218
|
+
} else if (result.outcome === 'offline' && result.profile) {
|
|
219
|
+
refreshToken = result.refreshToken ?? null;
|
|
220
|
+
idToken = result.idToken ?? null;
|
|
221
|
+
setState({
|
|
222
|
+
status: 'authenticated',
|
|
223
|
+
profile: result.profile,
|
|
224
|
+
accessTier: result.accessTier ?? failClosedTier(),
|
|
225
|
+
restore: 'offline',
|
|
226
|
+
error: undefined,
|
|
227
|
+
});
|
|
228
|
+
} else {
|
|
229
|
+
await clearPersisted();
|
|
230
|
+
setState(SIGNED_OUT);
|
|
231
|
+
}
|
|
232
|
+
return getState();
|
|
233
|
+
},
|
|
234
|
+
|
|
235
|
+
async signIn(providerId: ProviderId) {
|
|
236
|
+
sessionEpoch += 1;
|
|
237
|
+
const epoch = sessionEpoch;
|
|
238
|
+
setState({ status: 'authenticating', error: undefined });
|
|
239
|
+
let provider;
|
|
240
|
+
try {
|
|
241
|
+
provider = resolveProvider(config, providerId, platform);
|
|
242
|
+
} catch (error) {
|
|
243
|
+
setState({
|
|
244
|
+
status: 'error',
|
|
245
|
+
error: toAuthError(error, 'configuration-error', 'auth.error.configuration'),
|
|
246
|
+
});
|
|
247
|
+
return getState();
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
try {
|
|
251
|
+
const tokens = await authClient.authorize({
|
|
252
|
+
authority: config.authority,
|
|
253
|
+
clientId: config.clientId,
|
|
254
|
+
redirectUrl: config.redirectUrl,
|
|
255
|
+
scopes: config.scopes,
|
|
256
|
+
additionalParameters: buildAdditionalParameters(provider),
|
|
257
|
+
});
|
|
258
|
+
if (epoch !== sessionEpoch) {
|
|
259
|
+
return getState();
|
|
260
|
+
}
|
|
261
|
+
if (!tokens.refreshToken) {
|
|
262
|
+
// `offline_access` is required by configuration, so its absence is the
|
|
263
|
+
// tenant's doing. Signing in anyway would give a session that silently
|
|
264
|
+
// never survives a restart.
|
|
265
|
+
setState({
|
|
266
|
+
status: 'error',
|
|
267
|
+
error: new AuthError({
|
|
268
|
+
code: 'configuration-error',
|
|
269
|
+
messageKey: 'auth.error.configuration',
|
|
270
|
+
audience: 'developer',
|
|
271
|
+
detail:
|
|
272
|
+
'authorize returned no refresh token; check that the app registration grants offline_access',
|
|
273
|
+
}),
|
|
274
|
+
});
|
|
275
|
+
return getState();
|
|
276
|
+
}
|
|
277
|
+
const profile = mapClaimsToProfile(tokens.claims, providerId);
|
|
278
|
+
const accessTier = evaluateAccessTier(tokens.claims, config.accessTierMapping);
|
|
279
|
+
// Written first, committed to memory second: a failed write leaves no
|
|
280
|
+
// half-signed-in session holding a refresh token that was never stored.
|
|
281
|
+
await writeSession(
|
|
282
|
+
{ refreshToken: tokens.refreshToken, profile, idToken: tokens.idToken },
|
|
283
|
+
epoch,
|
|
284
|
+
);
|
|
285
|
+
if (epoch !== sessionEpoch) {
|
|
286
|
+
return getState();
|
|
287
|
+
}
|
|
288
|
+
// Single active account: a new sign-in replaces any prior session.
|
|
289
|
+
refreshToken = tokens.refreshToken;
|
|
290
|
+
idToken = tokens.idToken ?? null;
|
|
291
|
+
tokenManager.clear();
|
|
292
|
+
setState({
|
|
293
|
+
status: 'authenticated',
|
|
294
|
+
profile,
|
|
295
|
+
accessTier,
|
|
296
|
+
restore: 'restored',
|
|
297
|
+
error: undefined,
|
|
298
|
+
});
|
|
299
|
+
} catch (error) {
|
|
300
|
+
if (epoch !== sessionEpoch) {
|
|
301
|
+
return getState();
|
|
302
|
+
}
|
|
303
|
+
const authError = toAuthError(error);
|
|
304
|
+
if (authError.code === 'provider-cancelled') {
|
|
305
|
+
setState({ status: 'cancelled', error: authError });
|
|
306
|
+
} else {
|
|
307
|
+
setState({ status: 'error', error: authError });
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return getState();
|
|
311
|
+
},
|
|
312
|
+
|
|
313
|
+
async signOut() {
|
|
314
|
+
await clearPersisted();
|
|
315
|
+
setState(SIGNED_OUT);
|
|
316
|
+
},
|
|
317
|
+
|
|
318
|
+
async signOutGlobal(): Promise<GlobalSignOutResult> {
|
|
319
|
+
let result: GlobalSignOutResult = { global: 'ended' };
|
|
320
|
+
try {
|
|
321
|
+
await authClient.endSession({
|
|
322
|
+
authority: config.authority,
|
|
323
|
+
clientId: config.clientId,
|
|
324
|
+
redirectUrl: config.redirectUrl,
|
|
325
|
+
idToken: idToken ?? undefined,
|
|
326
|
+
});
|
|
327
|
+
} catch (error) {
|
|
328
|
+
// Reported, not swallowed: the tenant session may still be alive. It
|
|
329
|
+
// still must not block clearing local state.
|
|
330
|
+
result = { global: 'failed', error: toAuthError(error) };
|
|
331
|
+
}
|
|
332
|
+
await clearPersisted();
|
|
333
|
+
setState(SIGNED_OUT);
|
|
334
|
+
return result;
|
|
335
|
+
},
|
|
336
|
+
|
|
337
|
+
async requestAccessToken(resourceKey) {
|
|
338
|
+
const epoch = sessionEpoch;
|
|
339
|
+
// Only a real network refresh is a `token-refreshing` phase; a cache hit
|
|
340
|
+
// would otherwise flash every subscriber twice for nothing.
|
|
341
|
+
const refreshing =
|
|
342
|
+
state.status === 'authenticated' && !tokenManager.hasFreshToken(resourceKey);
|
|
343
|
+
if (refreshing) {
|
|
344
|
+
setState({ status: 'token-refreshing' });
|
|
345
|
+
}
|
|
346
|
+
const result = await tokenManager.request(resourceKey);
|
|
347
|
+
// Restored only if nothing else moved the session meanwhile: a sign-out
|
|
348
|
+
// during the refresh used to be overwritten back to `authenticated`.
|
|
349
|
+
if (refreshing && epoch === sessionEpoch && state.status === 'token-refreshing') {
|
|
350
|
+
setState({ status: 'authenticated' });
|
|
351
|
+
}
|
|
352
|
+
return result;
|
|
353
|
+
},
|
|
354
|
+
|
|
355
|
+
getAccessTier() {
|
|
356
|
+
return state.accessTier ?? failClosedTier();
|
|
357
|
+
},
|
|
358
|
+
|
|
359
|
+
requirePremium(): PremiumGateResult {
|
|
360
|
+
const tier = state.accessTier ?? failClosedTier();
|
|
361
|
+
const allowed = tier.tier === 'premium' && tier.verification === 'verified';
|
|
362
|
+
if (!allowed) {
|
|
363
|
+
setState({ status: 'access-denied' });
|
|
364
|
+
}
|
|
365
|
+
return { allowed, tier };
|
|
366
|
+
},
|
|
367
|
+
|
|
368
|
+
getPlatformProvider() {
|
|
369
|
+
return defaultProviderForPlatform(config, platform);
|
|
370
|
+
},
|
|
371
|
+
|
|
372
|
+
getPlatformProviders() {
|
|
373
|
+
return availableProvidersForPlatform(config, platform);
|
|
374
|
+
},
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
export type { Profile };
|
|
379
|
+
/** Every error the machine lands in also goes to the app's SDK sinks (CORE-08). */
|
|
380
|
+
const channel = createDiagnosticChannel<AuthErrorCode>('rn-auth');
|