@authara/react-native 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/README.md +23 -0
- package/dist/client.d.ts +9 -0
- package/dist/client.js +96 -18
- package/dist/nitro-google-signin.js +2 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -47,6 +47,29 @@ through `AutharaTokenStorage`. On startup, `AutharaProvider` restores the
|
|
|
47
47
|
session by rotating that token. Concurrent 401 responses share one refresh,
|
|
48
48
|
then each request retries once with `Authorization: Bearer …`.
|
|
49
49
|
|
|
50
|
+
`baseUrl` must use HTTPS. Local development can explicitly opt in to HTTP with
|
|
51
|
+
`allowInsecureHttp: true`; never enable it in production.
|
|
52
|
+
|
|
53
|
+
### Application API clients
|
|
54
|
+
|
|
55
|
+
An application with its own authenticated API can read the current in-memory
|
|
56
|
+
access token without taking ownership of Authara's token lifecycle. Subscribe
|
|
57
|
+
to session changes to synchronize the application's transport, and ask Authara
|
|
58
|
+
to rotate the token after that API returns 401:
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
const unsubscribe = authara.subscribe(() => {
|
|
62
|
+
appApi.setAccessToken(authara.getAccessToken());
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
appApi.setRefreshHandler(() => authara.refreshAccessToken());
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
`refreshAccessToken()` shares concurrent rotation work and returns the current
|
|
69
|
+
access token or `null`. Refresh tokens remain private and persisted only through
|
|
70
|
+
the configured `AutharaTokenStorage`. Unsubscribe when the application bridge
|
|
71
|
+
is disposed.
|
|
72
|
+
|
|
50
73
|
## Native provider adapters
|
|
51
74
|
|
|
52
75
|
Authara accepts small adapters so applications can control their native setup,
|
package/dist/client.d.ts
CHANGED
|
@@ -23,6 +23,7 @@ export interface AutharaClientOptions {
|
|
|
23
23
|
baseUrl: string;
|
|
24
24
|
storage: AutharaTokenStorage;
|
|
25
25
|
audience?: AutharaAudience;
|
|
26
|
+
allowInsecureHttp?: boolean;
|
|
26
27
|
fetch?: typeof globalThis.fetch;
|
|
27
28
|
}
|
|
28
29
|
export declare class AutharaApiError extends Error {
|
|
@@ -39,14 +40,18 @@ export declare class AutharaClient {
|
|
|
39
40
|
private accessToken;
|
|
40
41
|
private refreshToken;
|
|
41
42
|
private refreshPromise?;
|
|
43
|
+
private sessionGeneration;
|
|
44
|
+
private storageMutation;
|
|
42
45
|
private snapshot;
|
|
43
46
|
private readonly listeners;
|
|
44
47
|
constructor(options: AutharaClientOptions);
|
|
45
48
|
getSessionSnapshot: () => AutharaSessionSnapshot;
|
|
49
|
+
getAccessToken: () => string | null;
|
|
46
50
|
subscribe: (listener: () => void) => (() => void);
|
|
47
51
|
protected setCurrentUser(user: AuthUser | null): void;
|
|
48
52
|
protected restoreTokens(): Promise<boolean>;
|
|
49
53
|
signOut(): Promise<void>;
|
|
54
|
+
refreshAccessToken(): Promise<string | null>;
|
|
50
55
|
protected request<T>(method: string, path: string, options?: {
|
|
51
56
|
query?: Record<string, QueryValue | undefined>;
|
|
52
57
|
body?: unknown;
|
|
@@ -59,6 +64,10 @@ export declare class AutharaClient {
|
|
|
59
64
|
private captureTokens;
|
|
60
65
|
private setTokens;
|
|
61
66
|
private clearSession;
|
|
67
|
+
private invalidateSession;
|
|
68
|
+
private sessionMatches;
|
|
69
|
+
private removeStoredRefreshToken;
|
|
70
|
+
private mutateStorage;
|
|
62
71
|
private updateSnapshot;
|
|
63
72
|
private readJSON;
|
|
64
73
|
}
|
package/dist/client.js
CHANGED
|
@@ -18,10 +18,17 @@ export class AutharaClient {
|
|
|
18
18
|
accessToken = null;
|
|
19
19
|
refreshToken = null;
|
|
20
20
|
refreshPromise;
|
|
21
|
+
sessionGeneration = 0;
|
|
22
|
+
storageMutation = Promise.resolve();
|
|
21
23
|
snapshot = { status: "loading", user: null };
|
|
22
24
|
listeners = new Set();
|
|
23
25
|
constructor(options) {
|
|
24
|
-
|
|
26
|
+
const baseUrl = new URL(options.baseUrl);
|
|
27
|
+
if (baseUrl.protocol !== "https:" &&
|
|
28
|
+
!(options.allowInsecureHttp && baseUrl.protocol === "http:")) {
|
|
29
|
+
throw new Error("Authara baseUrl must use HTTPS unless allowInsecureHttp is enabled");
|
|
30
|
+
}
|
|
31
|
+
this.baseUrl = baseUrl.toString().replace(/\/$/, "");
|
|
25
32
|
this.storage = options.storage;
|
|
26
33
|
this.audience = options.audience ?? "app";
|
|
27
34
|
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
@@ -30,6 +37,7 @@ export class AutharaClient {
|
|
|
30
37
|
this.fetchImpl = fetchImpl;
|
|
31
38
|
}
|
|
32
39
|
getSessionSnapshot = () => this.snapshot;
|
|
40
|
+
getAccessToken = () => this.accessToken;
|
|
33
41
|
subscribe = (listener) => {
|
|
34
42
|
this.listeners.add(listener);
|
|
35
43
|
return () => this.listeners.delete(listener);
|
|
@@ -41,7 +49,19 @@ export class AutharaClient {
|
|
|
41
49
|
});
|
|
42
50
|
}
|
|
43
51
|
async restoreTokens() {
|
|
44
|
-
|
|
52
|
+
const generation = this.sessionGeneration;
|
|
53
|
+
let refreshToken;
|
|
54
|
+
try {
|
|
55
|
+
refreshToken = await this.storage.getRefreshToken();
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
if (this.sessionGeneration === generation)
|
|
59
|
+
this.invalidateSession();
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
if (this.sessionGeneration !== generation)
|
|
63
|
+
return false;
|
|
64
|
+
this.refreshToken = refreshToken;
|
|
45
65
|
if (!this.refreshToken) {
|
|
46
66
|
this.updateSnapshot({ status: "anonymous", user: null });
|
|
47
67
|
return false;
|
|
@@ -53,8 +73,15 @@ export class AutharaClient {
|
|
|
53
73
|
return restored;
|
|
54
74
|
}
|
|
55
75
|
async signOut() {
|
|
56
|
-
|
|
76
|
+
let refreshToken = this.refreshToken;
|
|
77
|
+
this.invalidateSession();
|
|
78
|
+
const generation = this.sessionGeneration;
|
|
57
79
|
try {
|
|
80
|
+
if (!refreshToken) {
|
|
81
|
+
refreshToken = await this.storage.getRefreshToken();
|
|
82
|
+
if (this.sessionGeneration !== generation)
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
58
85
|
if (refreshToken) {
|
|
59
86
|
await this.rawRequest("POST", "/auth/api/v1/tokens/revoke", {
|
|
60
87
|
body: { refresh_token: refreshToken },
|
|
@@ -65,10 +92,14 @@ export class AutharaClient {
|
|
|
65
92
|
// Local sign-out must still complete while offline.
|
|
66
93
|
}
|
|
67
94
|
finally {
|
|
68
|
-
await this.
|
|
95
|
+
await this.removeStoredRefreshToken(generation);
|
|
69
96
|
}
|
|
70
97
|
}
|
|
98
|
+
async refreshAccessToken() {
|
|
99
|
+
return (await this.refresh()) ? this.accessToken : null;
|
|
100
|
+
}
|
|
71
101
|
async request(method, path, options = {}) {
|
|
102
|
+
let generation = this.sessionGeneration;
|
|
72
103
|
const query = { ...options.query };
|
|
73
104
|
if (options.csrf && !options.authenticated)
|
|
74
105
|
query.session_transport = "token";
|
|
@@ -80,6 +111,7 @@ export class AutharaClient {
|
|
|
80
111
|
if (response.status === 401 &&
|
|
81
112
|
options.authenticated &&
|
|
82
113
|
(await this.refresh())) {
|
|
114
|
+
generation = this.sessionGeneration;
|
|
83
115
|
response = await this.rawRequest(method, path, {
|
|
84
116
|
...options,
|
|
85
117
|
query,
|
|
@@ -91,7 +123,7 @@ export class AutharaClient {
|
|
|
91
123
|
throw new AutharaApiError(response.status, payload);
|
|
92
124
|
if (response.status === 204)
|
|
93
125
|
return undefined;
|
|
94
|
-
await this.captureTokens(payload);
|
|
126
|
+
await this.captureTokens(payload, generation);
|
|
95
127
|
return payload;
|
|
96
128
|
}
|
|
97
129
|
async rawRequest(method, path, options = {}) {
|
|
@@ -124,24 +156,31 @@ export class AutharaClient {
|
|
|
124
156
|
if (!this.refreshToken)
|
|
125
157
|
return false;
|
|
126
158
|
if (!this.refreshPromise) {
|
|
127
|
-
|
|
128
|
-
|
|
159
|
+
const refreshToken = this.refreshToken;
|
|
160
|
+
const generation = this.sessionGeneration;
|
|
161
|
+
const refreshPromise = this.performRefresh(refreshToken, generation).finally(() => {
|
|
162
|
+
if (this.refreshPromise === refreshPromise) {
|
|
163
|
+
this.refreshPromise = undefined;
|
|
164
|
+
}
|
|
129
165
|
});
|
|
166
|
+
this.refreshPromise = refreshPromise;
|
|
130
167
|
}
|
|
131
168
|
return this.refreshPromise;
|
|
132
169
|
}
|
|
133
|
-
async performRefresh() {
|
|
170
|
+
async performRefresh(refreshToken, generation) {
|
|
134
171
|
let response;
|
|
135
172
|
try {
|
|
136
173
|
response = await this.rawRequest("POST", "/auth/api/v1/tokens/refresh", {
|
|
137
|
-
body: { refresh_token:
|
|
174
|
+
body: { refresh_token: refreshToken, audience: this.audience },
|
|
138
175
|
});
|
|
139
176
|
}
|
|
140
177
|
catch {
|
|
141
178
|
return false;
|
|
142
179
|
}
|
|
143
180
|
if (response.status === 401) {
|
|
144
|
-
|
|
181
|
+
if (this.sessionMatches(generation, refreshToken)) {
|
|
182
|
+
await this.clearSession();
|
|
183
|
+
}
|
|
145
184
|
return false;
|
|
146
185
|
}
|
|
147
186
|
if (!response.ok)
|
|
@@ -149,31 +188,70 @@ export class AutharaClient {
|
|
|
149
188
|
const tokens = (await this.readJSON(response));
|
|
150
189
|
if (!tokens?.access_token || !tokens.refresh_token)
|
|
151
190
|
return false;
|
|
152
|
-
|
|
153
|
-
return true;
|
|
191
|
+
return this.setTokens(tokens.access_token, tokens.refresh_token, generation, refreshToken);
|
|
154
192
|
}
|
|
155
|
-
async captureTokens(payload) {
|
|
193
|
+
async captureTokens(payload, generation) {
|
|
156
194
|
if (!payload || typeof payload !== "object")
|
|
157
195
|
return;
|
|
158
196
|
const candidate = payload;
|
|
159
197
|
if (!candidate.access_token || !candidate.refresh_token)
|
|
160
198
|
return;
|
|
161
|
-
await this.setTokens(candidate.access_token, candidate.refresh_token);
|
|
162
|
-
if (candidate.user)
|
|
199
|
+
const stored = await this.setTokens(candidate.access_token, candidate.refresh_token, generation);
|
|
200
|
+
if (stored && candidate.user)
|
|
163
201
|
this.setCurrentUser(candidate.user);
|
|
164
202
|
}
|
|
165
|
-
async setTokens(accessToken, refreshToken) {
|
|
203
|
+
async setTokens(accessToken, refreshToken, expectedGeneration, expectedRefreshToken) {
|
|
204
|
+
if (expectedGeneration !== undefined &&
|
|
205
|
+
!this.sessionMatches(expectedGeneration, expectedRefreshToken)) {
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
try {
|
|
209
|
+
await this.mutateStorage(() => this.storage.setRefreshToken(refreshToken));
|
|
210
|
+
}
|
|
211
|
+
catch (error) {
|
|
212
|
+
if (expectedGeneration === undefined ||
|
|
213
|
+
this.sessionMatches(expectedGeneration, expectedRefreshToken)) {
|
|
214
|
+
this.invalidateSession();
|
|
215
|
+
}
|
|
216
|
+
throw error;
|
|
217
|
+
}
|
|
218
|
+
if (expectedGeneration !== undefined &&
|
|
219
|
+
!this.sessionMatches(expectedGeneration, expectedRefreshToken)) {
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
166
222
|
this.accessToken = accessToken;
|
|
167
223
|
this.refreshToken = refreshToken;
|
|
168
|
-
|
|
224
|
+
this.sessionGeneration += 1;
|
|
169
225
|
this.updateSnapshot({ status: "authenticated", user: this.snapshot.user });
|
|
226
|
+
return true;
|
|
170
227
|
}
|
|
171
228
|
async clearSession() {
|
|
229
|
+
this.invalidateSession();
|
|
230
|
+
await this.removeStoredRefreshToken(this.sessionGeneration);
|
|
231
|
+
}
|
|
232
|
+
invalidateSession() {
|
|
233
|
+
this.sessionGeneration += 1;
|
|
234
|
+
this.refreshPromise = undefined;
|
|
172
235
|
this.accessToken = null;
|
|
173
236
|
this.refreshToken = null;
|
|
174
|
-
await this.storage.removeRefreshToken();
|
|
175
237
|
this.updateSnapshot({ status: "anonymous", user: null });
|
|
176
238
|
}
|
|
239
|
+
sessionMatches(generation, refreshToken) {
|
|
240
|
+
return (this.sessionGeneration === generation &&
|
|
241
|
+
(refreshToken === undefined || this.refreshToken === refreshToken));
|
|
242
|
+
}
|
|
243
|
+
removeStoredRefreshToken(generation) {
|
|
244
|
+
return this.mutateStorage(async () => {
|
|
245
|
+
if (this.sessionGeneration === generation) {
|
|
246
|
+
await this.storage.removeRefreshToken();
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
mutateStorage(mutation) {
|
|
251
|
+
const result = this.storageMutation.catch(() => undefined).then(mutation);
|
|
252
|
+
this.storageMutation = result;
|
|
253
|
+
return result;
|
|
254
|
+
}
|
|
177
255
|
updateSnapshot(snapshot) {
|
|
178
256
|
this.snapshot = snapshot;
|
|
179
257
|
for (const listener of this.listeners)
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { GoogleOneTapSignIn, isCancelledResponse, isSuccessResponse, } from "react-native-nitro-google-signin";
|
|
2
|
+
import { Platform } from "react-native";
|
|
2
3
|
export function createNitroGoogleSignIn(config = {}) {
|
|
3
4
|
return {
|
|
4
5
|
async authorize(options) {
|
|
5
|
-
if (!config.iosClientId) {
|
|
6
|
+
if (Platform.OS === "ios" && !config.iosClientId) {
|
|
6
7
|
throw new Error("Google native sign-in needs AUTHARA_OAUTH_GOOGLE_IOS_CLIENT_ID in the React Native .env");
|
|
7
8
|
}
|
|
8
9
|
GoogleOneTapSignIn.configure({
|