@mediaviz/react-native-sdk 1.9.0-dev.116
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/LICENSE +21 -0
- package/dist/react.cjs +404 -0
- package/dist/react.d.cts +48 -0
- package/dist/react.d.ts +48 -0
- package/dist/react.esm.js +400 -0
- package/dist/sdk.cjs +3032 -0
- package/dist/sdk.d.cts +650 -0
- package/dist/sdk.d.ts +650 -0
- package/dist/sdk.esm.d.ts +650 -0
- package/dist/sdk.esm.js +2986 -0
- package/package.json +60 -0
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
import require$$0 from 'react';
|
|
2
|
+
|
|
3
|
+
function getDefaultExportFromCjs (x) {
|
|
4
|
+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
var storage;
|
|
8
|
+
var hasRequiredStorage;
|
|
9
|
+
|
|
10
|
+
function requireStorage () {
|
|
11
|
+
if (hasRequiredStorage) return storage;
|
|
12
|
+
hasRequiredStorage = 1;
|
|
13
|
+
|
|
14
|
+
// Token persistence.
|
|
15
|
+
//
|
|
16
|
+
// Access and refresh tokens are kept under separate keys because
|
|
17
|
+
// expo-secure-store caps a single value at 2048 bytes and two JWTs in one blob
|
|
18
|
+
// can cross that. Keychain has no such cap, so it stores one JSON entry.
|
|
19
|
+
|
|
20
|
+
const ACCESS_KEY = 'mediaviz.accessToken';
|
|
21
|
+
const REFRESH_KEY = 'mediaviz.refreshToken';
|
|
22
|
+
|
|
23
|
+
/** Non-persistent default. Tokens are lost when the app is killed. */
|
|
24
|
+
class MemoryTokenStore {
|
|
25
|
+
constructor() {
|
|
26
|
+
this._tokens = { accessToken: null, refreshToken: null };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async load() {
|
|
30
|
+
return { ...this._tokens };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async save(tokens) {
|
|
34
|
+
this._tokens = normalize(tokens);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async clear() {
|
|
38
|
+
this._tokens = { accessToken: null, refreshToken: null };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Backed by expo-secure-store (Keychain on iOS, Keystore on Android).
|
|
44
|
+
* @param {{getItemAsync: Function, setItemAsync: Function, deleteItemAsync: Function}} SecureStore
|
|
45
|
+
*/
|
|
46
|
+
function createSecureTokenStore(SecureStore, options = {}) {
|
|
47
|
+
requireMethods(SecureStore, ['getItemAsync', 'setItemAsync', 'deleteItemAsync'], 'expo-secure-store');
|
|
48
|
+
const accessKey = options.accessKey ?? ACCESS_KEY;
|
|
49
|
+
const refreshKey = options.refreshKey ?? REFRESH_KEY;
|
|
50
|
+
const opts = options.secureStoreOptions;
|
|
51
|
+
return {
|
|
52
|
+
async load() {
|
|
53
|
+
const [accessToken, refreshToken] = await Promise.all([
|
|
54
|
+
SecureStore.getItemAsync(accessKey, opts),
|
|
55
|
+
SecureStore.getItemAsync(refreshKey, opts),
|
|
56
|
+
]);
|
|
57
|
+
return { accessToken: accessToken ?? null, refreshToken: refreshToken ?? null };
|
|
58
|
+
},
|
|
59
|
+
async save(tokens) {
|
|
60
|
+
const { accessToken, refreshToken } = normalize(tokens);
|
|
61
|
+
await Promise.all([
|
|
62
|
+
writeOrDelete(SecureStore, accessKey, accessToken, opts),
|
|
63
|
+
writeOrDelete(SecureStore, refreshKey, refreshToken, opts),
|
|
64
|
+
]);
|
|
65
|
+
},
|
|
66
|
+
async clear() {
|
|
67
|
+
await Promise.all([
|
|
68
|
+
SecureStore.deleteItemAsync(accessKey, opts),
|
|
69
|
+
SecureStore.deleteItemAsync(refreshKey, opts),
|
|
70
|
+
]);
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Backed by react-native-keychain.
|
|
77
|
+
* @param {{getGenericPassword: Function, setGenericPassword: Function, resetGenericPassword: Function}} Keychain
|
|
78
|
+
*/
|
|
79
|
+
function createKeychainTokenStore(Keychain, options = {}) {
|
|
80
|
+
requireMethods(Keychain, ['getGenericPassword', 'setGenericPassword', 'resetGenericPassword'], 'react-native-keychain');
|
|
81
|
+
const opts = { service: options.service ?? 'ai.mediaviz.tokens', ...options.keychainOptions };
|
|
82
|
+
return {
|
|
83
|
+
async load() {
|
|
84
|
+
const entry = await Keychain.getGenericPassword(opts);
|
|
85
|
+
if (!entry || !entry.password) return { accessToken: null, refreshToken: null };
|
|
86
|
+
return normalize(JSON.parse(entry.password));
|
|
87
|
+
},
|
|
88
|
+
async save(tokens) {
|
|
89
|
+
await Keychain.setGenericPassword('mediaviz', JSON.stringify(normalize(tokens)), opts);
|
|
90
|
+
},
|
|
91
|
+
async clear() {
|
|
92
|
+
await Keychain.resetGenericPassword(opts);
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// helpers
|
|
98
|
+
function normalize(tokens) {
|
|
99
|
+
// The OAuth client emits snake_case (access_token); MediaViz getters emit
|
|
100
|
+
// camelCase. Both reach this store, so both are accepted.
|
|
101
|
+
const t = tokens ?? {};
|
|
102
|
+
return {
|
|
103
|
+
accessToken: t.accessToken ?? t.access_token ?? null,
|
|
104
|
+
refreshToken: t.refreshToken ?? t.refresh_token ?? null,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function writeOrDelete(SecureStore, key, value, opts) {
|
|
109
|
+
// A null token must erase the key, not persist the string "null".
|
|
110
|
+
return value ? SecureStore.setItemAsync(key, value, opts) : SecureStore.deleteItemAsync(key, opts);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function requireMethods(mod, names, label) {
|
|
114
|
+
for (const name of names) {
|
|
115
|
+
if (typeof mod?.[name] !== 'function') {
|
|
116
|
+
throw new TypeError(`Expected the ${label} module; it has no ${name}()`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
storage = { MemoryTokenStore, createSecureTokenStore, createKeychainTokenStore };
|
|
122
|
+
return storage;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
var authSession;
|
|
126
|
+
var hasRequiredAuthSession;
|
|
127
|
+
|
|
128
|
+
function requireAuthSession () {
|
|
129
|
+
if (hasRequiredAuthSession) return authSession;
|
|
130
|
+
hasRequiredAuthSession = 1;
|
|
131
|
+
|
|
132
|
+
// Authorization Code + PKCE on device.
|
|
133
|
+
//
|
|
134
|
+
// The app injects openAuthSession (expo-web-browser's openAuthSessionAsync, or
|
|
135
|
+
// anything with the same contract), keeping this file free of native
|
|
136
|
+
// dependencies. The verifier never leaves this function, so there is nothing to
|
|
137
|
+
// persist across the redirect the way a web app must.
|
|
138
|
+
|
|
139
|
+
class AuthSessionError extends Error {
|
|
140
|
+
constructor(code, message) {
|
|
141
|
+
super(message);
|
|
142
|
+
this.name = 'AuthSessionError';
|
|
143
|
+
this.code = code;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Runs the full interactive login and leaves *mv* authenticated.
|
|
149
|
+
* @param {object} mv - a MediaViz client
|
|
150
|
+
* @param {{openAuthSession: Function, redirectUri: string, state?: string, sessionOptions?: object}} options
|
|
151
|
+
* @returns {Promise<object>} the token response
|
|
152
|
+
*/
|
|
153
|
+
async function startAuthSession(mv, options) {
|
|
154
|
+
const { openAuthSession, redirectUri, state: requestedState, sessionOptions } = options ?? {};
|
|
155
|
+
if (typeof openAuthSession !== 'function') {
|
|
156
|
+
throw new TypeError('startAuthSession requires openAuthSession(url, redirectUri, options)');
|
|
157
|
+
}
|
|
158
|
+
if (!redirectUri) {
|
|
159
|
+
throw new TypeError('startAuthSession requires redirectUri');
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const { url, state, code_verifier: codeVerifier } = await mv.getAuthorizationUrl(requestedState);
|
|
163
|
+
const result = await openAuthSession(url, redirectUri, sessionOptions);
|
|
164
|
+
|
|
165
|
+
const type = result?.type;
|
|
166
|
+
if (type === 'cancel' || type === 'dismiss') {
|
|
167
|
+
throw new AuthSessionError('cancelled', 'The user dismissed the sign-in screen.');
|
|
168
|
+
}
|
|
169
|
+
if (type !== 'success' || !result?.url) {
|
|
170
|
+
throw new AuthSessionError('failed', `Sign-in did not complete (result type: ${type ?? 'none'}).`);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const params = parseRedirectUrl(result.url);
|
|
174
|
+
if (params.error) {
|
|
175
|
+
throw new AuthSessionError(params.error, params.error_description ?? `Authorization failed: ${params.error}`);
|
|
176
|
+
}
|
|
177
|
+
// Rejecting a mismatched state is the CSRF defence the whole flow rests on —
|
|
178
|
+
// it must fail closed, including when the server returns no state at all.
|
|
179
|
+
if (params.state !== state) {
|
|
180
|
+
throw new AuthSessionError('state_mismatch', 'Authorization state did not match the request; discarding the response.');
|
|
181
|
+
}
|
|
182
|
+
if (!params.code) {
|
|
183
|
+
throw new AuthSessionError('no_code', 'Authorization redirect carried no code.');
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return mv.handleCallback(params.code, codeVerifier);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Extracts query and fragment params from a redirect URL.
|
|
191
|
+
*
|
|
192
|
+
* Hand-rolled because React Native's URL polyfill mis-parses custom schemes
|
|
193
|
+
* (mediaviz://callback) — the very shape every native redirect URI takes.
|
|
194
|
+
* @param {string} url
|
|
195
|
+
* @returns {Record<string, string>}
|
|
196
|
+
*/
|
|
197
|
+
function parseRedirectUrl(url) {
|
|
198
|
+
const out = {};
|
|
199
|
+
const text = String(url ?? '');
|
|
200
|
+
const queryStart = text.indexOf('?');
|
|
201
|
+
const hashStart = text.indexOf('#');
|
|
202
|
+
if (queryStart !== -1) {
|
|
203
|
+
const end = hashStart > queryStart ? hashStart : text.length;
|
|
204
|
+
assign(out, text.slice(queryStart + 1, end));
|
|
205
|
+
}
|
|
206
|
+
if (hashStart !== -1) assign(out, text.slice(hashStart + 1));
|
|
207
|
+
return out;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// helpers
|
|
211
|
+
function assign(out, segment) {
|
|
212
|
+
for (const pair of segment.split('&')) {
|
|
213
|
+
if (!pair) continue;
|
|
214
|
+
const eq = pair.indexOf('=');
|
|
215
|
+
const key = decode(eq === -1 ? pair : pair.slice(0, eq));
|
|
216
|
+
if (!key) continue;
|
|
217
|
+
out[key] = eq === -1 ? '' : decode(pair.slice(eq + 1));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function decode(value) {
|
|
222
|
+
try {
|
|
223
|
+
return decodeURIComponent(value.replace(/\+/g, ' '));
|
|
224
|
+
} catch {
|
|
225
|
+
// A malformed escape must not take down the whole redirect parse; the
|
|
226
|
+
// caller's state/code checks still reject anything unusable.
|
|
227
|
+
return value;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
authSession = { startAuthSession, parseRedirectUrl, AuthSessionError };
|
|
232
|
+
return authSession;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
var session;
|
|
236
|
+
var hasRequiredSession;
|
|
237
|
+
|
|
238
|
+
function requireSession () {
|
|
239
|
+
if (hasRequiredSession) return session;
|
|
240
|
+
hasRequiredSession = 1;
|
|
241
|
+
|
|
242
|
+
// The authenticated-session lifecycle, independent of React.
|
|
243
|
+
//
|
|
244
|
+
// Everything stateful about being signed in — rehydrating persisted tokens,
|
|
245
|
+
// keeping the store in step with refresh-token rotation, running the
|
|
246
|
+
// interactive login, clearing on sign-out — lives here so it can be driven and
|
|
247
|
+
// tested without a renderer. react.js is a thin binding over this.
|
|
248
|
+
//
|
|
249
|
+
// The MediaViz class is injected because this module is copied *into* the
|
|
250
|
+
// generated package: it cannot name its own host, and MediaViz.js is ESM while
|
|
251
|
+
// this file is CommonJS.
|
|
252
|
+
|
|
253
|
+
const { MemoryTokenStore } = requireStorage();
|
|
254
|
+
const { startAuthSession } = requireAuthSession();
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* @param {{MediaViz: Function, config?: object, store?: object, openAuthSession?: Function, onTokens?: Function}} options
|
|
258
|
+
*/
|
|
259
|
+
function createSession(options) {
|
|
260
|
+
const { MediaViz, config, store, openAuthSession, onTokens } = options ?? {};
|
|
261
|
+
if (typeof MediaViz !== 'function') throw new TypeError('createSession requires the MediaViz class');
|
|
262
|
+
|
|
263
|
+
const tokenStore = store ?? new MemoryTokenStore();
|
|
264
|
+
const emit = (tokens) => {
|
|
265
|
+
if (onTokens) onTokens(tokens ?? null);
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
const client = new MediaViz({
|
|
269
|
+
...config,
|
|
270
|
+
onTokenRefresh: (tokens) => {
|
|
271
|
+
emit(tokens);
|
|
272
|
+
// Fire-and-forget: the refresh already succeeded and the retry is waiting
|
|
273
|
+
// on this callback, so awaiting the write would stall the request. A
|
|
274
|
+
// failed write is not worth failing the call over — the next refresh
|
|
275
|
+
// rewrites it.
|
|
276
|
+
Promise.resolve(tokenStore.save(tokens)).catch(() => {});
|
|
277
|
+
},
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
return {
|
|
281
|
+
client,
|
|
282
|
+
store: tokenStore,
|
|
283
|
+
|
|
284
|
+
/** Loads persisted tokens into the client. Returns them, or null if none. */
|
|
285
|
+
async restore() {
|
|
286
|
+
const tokens = await tokenStore.load().catch(() => null);
|
|
287
|
+
if (!tokens || !tokens.accessToken) return null;
|
|
288
|
+
client.setTokens(tokens.accessToken, tokens.refreshToken ?? null);
|
|
289
|
+
emit(tokens);
|
|
290
|
+
return tokens;
|
|
291
|
+
},
|
|
292
|
+
|
|
293
|
+
async signIn(overrides = {}) {
|
|
294
|
+
const tokens = await startAuthSession(client, {
|
|
295
|
+
openAuthSession: overrides.openAuthSession ?? openAuthSession,
|
|
296
|
+
redirectUri: overrides.redirectUri ?? config?.redirectUri,
|
|
297
|
+
state: overrides.state,
|
|
298
|
+
sessionOptions: overrides.sessionOptions,
|
|
299
|
+
});
|
|
300
|
+
await tokenStore.save(tokens);
|
|
301
|
+
emit(tokens);
|
|
302
|
+
return tokens;
|
|
303
|
+
},
|
|
304
|
+
|
|
305
|
+
async signOut() {
|
|
306
|
+
client.setTokens(null, null);
|
|
307
|
+
emit(null);
|
|
308
|
+
await tokenStore.clear();
|
|
309
|
+
},
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
session = { createSession };
|
|
314
|
+
return session;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
var react;
|
|
318
|
+
var hasRequiredReact;
|
|
319
|
+
|
|
320
|
+
function requireReact () {
|
|
321
|
+
if (hasRequiredReact) return react;
|
|
322
|
+
hasRequiredReact = 1;
|
|
323
|
+
|
|
324
|
+
// React bindings, reachable at "<pkg>/react".
|
|
325
|
+
//
|
|
326
|
+
// Deliberately outside the package barrel: this is the only file that requires
|
|
327
|
+
// react, so keeping it on a subpath leaves the core SDK importable from plain
|
|
328
|
+
// modules, background tasks, and tests with no React in scope. All the actual
|
|
329
|
+
// lifecycle logic lives in session.js; this file only mirrors it into state.
|
|
330
|
+
|
|
331
|
+
const React = require$$0;
|
|
332
|
+
|
|
333
|
+
const { createSession } = requireSession();
|
|
334
|
+
|
|
335
|
+
const MediaVizContext = React.createContext(null);
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Provides an authenticated MediaViz client to the tree.
|
|
339
|
+
*
|
|
340
|
+
* Pass the MediaViz class itself as a prop — see session.js for why it cannot
|
|
341
|
+
* be imported here.
|
|
342
|
+
*/
|
|
343
|
+
function MediaVizProvider(props) {
|
|
344
|
+
const { MediaViz, config, store, openAuthSession, children } = props;
|
|
345
|
+
|
|
346
|
+
const [ready, setReady] = React.useState(false);
|
|
347
|
+
const [accessToken, setAccessToken] = React.useState(null);
|
|
348
|
+
|
|
349
|
+
const session = React.useMemo(
|
|
350
|
+
() =>
|
|
351
|
+
createSession({
|
|
352
|
+
MediaViz,
|
|
353
|
+
config,
|
|
354
|
+
store,
|
|
355
|
+
openAuthSession,
|
|
356
|
+
onTokens: (tokens) => setAccessToken(tokens?.accessToken ?? tokens?.access_token ?? null),
|
|
357
|
+
}),
|
|
358
|
+
[MediaViz, config, store, openAuthSession]
|
|
359
|
+
);
|
|
360
|
+
|
|
361
|
+
React.useEffect(() => {
|
|
362
|
+
let cancelled = false;
|
|
363
|
+
// `ready` gates the tree on the one-time keychain read, so it must flip even
|
|
364
|
+
// when there is nothing stored or the read fails.
|
|
365
|
+
session.restore().finally(() => {
|
|
366
|
+
if (!cancelled) setReady(true);
|
|
367
|
+
});
|
|
368
|
+
return () => {
|
|
369
|
+
cancelled = true;
|
|
370
|
+
};
|
|
371
|
+
}, [session]);
|
|
372
|
+
|
|
373
|
+
const signIn = React.useCallback((overrides) => session.signIn(overrides), [session]);
|
|
374
|
+
const signOut = React.useCallback(() => session.signOut(), [session]);
|
|
375
|
+
|
|
376
|
+
const value = React.useMemo(
|
|
377
|
+
() => ({ client: session.client, ready, signedIn: Boolean(accessToken), signIn, signOut }),
|
|
378
|
+
[session, ready, accessToken, signIn, signOut]
|
|
379
|
+
);
|
|
380
|
+
|
|
381
|
+
return React.createElement(MediaVizContext.Provider, { value }, children);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
function useMediaViz() {
|
|
385
|
+
const value = React.useContext(MediaVizContext);
|
|
386
|
+
if (!value) throw new Error('useMediaViz must be used inside a <MediaVizProvider>');
|
|
387
|
+
return value;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
react = { MediaVizProvider, useMediaViz, MediaVizContext };
|
|
391
|
+
return react;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
var reactExports = requireReact();
|
|
395
|
+
var _react = /*@__PURE__*/getDefaultExportFromCjs(reactExports);
|
|
396
|
+
|
|
397
|
+
// Auto-generated — do not edit
|
|
398
|
+
const { MediaVizProvider, useMediaViz, MediaVizContext } = _react;
|
|
399
|
+
|
|
400
|
+
export { MediaVizContext, MediaVizProvider, useMediaViz };
|