@authara/react-native 0.0.0 → 0.1.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/README.md CHANGED
@@ -47,6 +47,9 @@ 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
+
50
53
  ## Native provider adapters
51
54
 
52
55
  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,6 +40,8 @@ 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);
@@ -59,6 +62,10 @@ export declare class AutharaClient {
59
62
  private captureTokens;
60
63
  private setTokens;
61
64
  private clearSession;
65
+ private invalidateSession;
66
+ private sessionMatches;
67
+ private removeStoredRefreshToken;
68
+ private mutateStorage;
62
69
  private updateSnapshot;
63
70
  private readJSON;
64
71
  }
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
- this.baseUrl = options.baseUrl.replace(/\/$/, "");
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;
@@ -41,7 +48,19 @@ export class AutharaClient {
41
48
  });
42
49
  }
43
50
  async restoreTokens() {
44
- this.refreshToken = await this.storage.getRefreshToken();
51
+ const generation = this.sessionGeneration;
52
+ let refreshToken;
53
+ try {
54
+ refreshToken = await this.storage.getRefreshToken();
55
+ }
56
+ catch {
57
+ if (this.sessionGeneration === generation)
58
+ this.invalidateSession();
59
+ return false;
60
+ }
61
+ if (this.sessionGeneration !== generation)
62
+ return false;
63
+ this.refreshToken = refreshToken;
45
64
  if (!this.refreshToken) {
46
65
  this.updateSnapshot({ status: "anonymous", user: null });
47
66
  return false;
@@ -53,8 +72,15 @@ export class AutharaClient {
53
72
  return restored;
54
73
  }
55
74
  async signOut() {
56
- const refreshToken = this.refreshToken ?? (await this.storage.getRefreshToken());
75
+ let refreshToken = this.refreshToken;
76
+ this.invalidateSession();
77
+ const generation = this.sessionGeneration;
57
78
  try {
79
+ if (!refreshToken) {
80
+ refreshToken = await this.storage.getRefreshToken();
81
+ if (this.sessionGeneration !== generation)
82
+ return;
83
+ }
58
84
  if (refreshToken) {
59
85
  await this.rawRequest("POST", "/auth/api/v1/tokens/revoke", {
60
86
  body: { refresh_token: refreshToken },
@@ -65,10 +91,11 @@ export class AutharaClient {
65
91
  // Local sign-out must still complete while offline.
66
92
  }
67
93
  finally {
68
- await this.clearSession();
94
+ await this.removeStoredRefreshToken(generation);
69
95
  }
70
96
  }
71
97
  async request(method, path, options = {}) {
98
+ let generation = this.sessionGeneration;
72
99
  const query = { ...options.query };
73
100
  if (options.csrf && !options.authenticated)
74
101
  query.session_transport = "token";
@@ -80,6 +107,7 @@ export class AutharaClient {
80
107
  if (response.status === 401 &&
81
108
  options.authenticated &&
82
109
  (await this.refresh())) {
110
+ generation = this.sessionGeneration;
83
111
  response = await this.rawRequest(method, path, {
84
112
  ...options,
85
113
  query,
@@ -91,7 +119,7 @@ export class AutharaClient {
91
119
  throw new AutharaApiError(response.status, payload);
92
120
  if (response.status === 204)
93
121
  return undefined;
94
- await this.captureTokens(payload);
122
+ await this.captureTokens(payload, generation);
95
123
  return payload;
96
124
  }
97
125
  async rawRequest(method, path, options = {}) {
@@ -124,24 +152,31 @@ export class AutharaClient {
124
152
  if (!this.refreshToken)
125
153
  return false;
126
154
  if (!this.refreshPromise) {
127
- this.refreshPromise = this.performRefresh().finally(() => {
128
- this.refreshPromise = undefined;
155
+ const refreshToken = this.refreshToken;
156
+ const generation = this.sessionGeneration;
157
+ const refreshPromise = this.performRefresh(refreshToken, generation).finally(() => {
158
+ if (this.refreshPromise === refreshPromise) {
159
+ this.refreshPromise = undefined;
160
+ }
129
161
  });
162
+ this.refreshPromise = refreshPromise;
130
163
  }
131
164
  return this.refreshPromise;
132
165
  }
133
- async performRefresh() {
166
+ async performRefresh(refreshToken, generation) {
134
167
  let response;
135
168
  try {
136
169
  response = await this.rawRequest("POST", "/auth/api/v1/tokens/refresh", {
137
- body: { refresh_token: this.refreshToken, audience: this.audience },
170
+ body: { refresh_token: refreshToken, audience: this.audience },
138
171
  });
139
172
  }
140
173
  catch {
141
174
  return false;
142
175
  }
143
176
  if (response.status === 401) {
144
- await this.clearSession();
177
+ if (this.sessionMatches(generation, refreshToken)) {
178
+ await this.clearSession();
179
+ }
145
180
  return false;
146
181
  }
147
182
  if (!response.ok)
@@ -149,31 +184,70 @@ export class AutharaClient {
149
184
  const tokens = (await this.readJSON(response));
150
185
  if (!tokens?.access_token || !tokens.refresh_token)
151
186
  return false;
152
- await this.setTokens(tokens.access_token, tokens.refresh_token);
153
- return true;
187
+ return this.setTokens(tokens.access_token, tokens.refresh_token, generation, refreshToken);
154
188
  }
155
- async captureTokens(payload) {
189
+ async captureTokens(payload, generation) {
156
190
  if (!payload || typeof payload !== "object")
157
191
  return;
158
192
  const candidate = payload;
159
193
  if (!candidate.access_token || !candidate.refresh_token)
160
194
  return;
161
- await this.setTokens(candidate.access_token, candidate.refresh_token);
162
- if (candidate.user)
195
+ const stored = await this.setTokens(candidate.access_token, candidate.refresh_token, generation);
196
+ if (stored && candidate.user)
163
197
  this.setCurrentUser(candidate.user);
164
198
  }
165
- async setTokens(accessToken, refreshToken) {
199
+ async setTokens(accessToken, refreshToken, expectedGeneration, expectedRefreshToken) {
200
+ if (expectedGeneration !== undefined &&
201
+ !this.sessionMatches(expectedGeneration, expectedRefreshToken)) {
202
+ return false;
203
+ }
204
+ try {
205
+ await this.mutateStorage(() => this.storage.setRefreshToken(refreshToken));
206
+ }
207
+ catch (error) {
208
+ if (expectedGeneration === undefined ||
209
+ this.sessionMatches(expectedGeneration, expectedRefreshToken)) {
210
+ this.invalidateSession();
211
+ }
212
+ throw error;
213
+ }
214
+ if (expectedGeneration !== undefined &&
215
+ !this.sessionMatches(expectedGeneration, expectedRefreshToken)) {
216
+ return false;
217
+ }
166
218
  this.accessToken = accessToken;
167
219
  this.refreshToken = refreshToken;
168
- await this.storage.setRefreshToken(refreshToken);
220
+ this.sessionGeneration += 1;
169
221
  this.updateSnapshot({ status: "authenticated", user: this.snapshot.user });
222
+ return true;
170
223
  }
171
224
  async clearSession() {
225
+ this.invalidateSession();
226
+ await this.removeStoredRefreshToken(this.sessionGeneration);
227
+ }
228
+ invalidateSession() {
229
+ this.sessionGeneration += 1;
230
+ this.refreshPromise = undefined;
172
231
  this.accessToken = null;
173
232
  this.refreshToken = null;
174
- await this.storage.removeRefreshToken();
175
233
  this.updateSnapshot({ status: "anonymous", user: null });
176
234
  }
235
+ sessionMatches(generation, refreshToken) {
236
+ return (this.sessionGeneration === generation &&
237
+ (refreshToken === undefined || this.refreshToken === refreshToken));
238
+ }
239
+ removeStoredRefreshToken(generation) {
240
+ return this.mutateStorage(async () => {
241
+ if (this.sessionGeneration === generation) {
242
+ await this.storage.removeRefreshToken();
243
+ }
244
+ });
245
+ }
246
+ mutateStorage(mutation) {
247
+ const result = this.storageMutation.catch(() => undefined).then(mutation);
248
+ this.storageMutation = result;
249
+ return result;
250
+ }
177
251
  updateSnapshot(snapshot) {
178
252
  this.snapshot = snapshot;
179
253
  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({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@authara/react-native",
3
- "version": "0.0.0",
3
+ "version": "0.1.1",
4
4
  "description": "Official React Native client, hooks, and native-provider buttons for Authara",
5
5
  "keywords": [
6
6
  "authentication",