@evolu/common 6.0.1-preview.22 → 6.0.1-preview.23

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.
@@ -6,7 +6,6 @@ import { StoreSubscribe } from "../Store.js";
6
6
  import { TimeDep } from "../Time.js";
7
7
  import { Mnemonic } from "../Type.js";
8
8
  import { CreateDbWorkerDep, DbConfig } from "./Db.js";
9
- import { LocalAuthDep } from "./LocalAuth.js";
10
9
  import { AppOwner } from "./Owner.js";
11
10
  import { FlushSyncDep, ReloadAppDep } from "./Platform.js";
12
11
  import { ProtocolError, ProtocolUnsupportedVersionError } from "./Protocol.js";
@@ -364,7 +363,7 @@ export interface Evolu<S extends EvoluSchema = EvoluSchema> {
364
363
  }
365
364
  /** Represents errors that can occur in Evolu. */
366
365
  export type EvoluError = ProtocolError | ProtocolUnsupportedVersionError | SqliteError | SymmetricCryptoDecryptError | TimestampError | TransferableError;
367
- export type EvoluDeps = ConsoleDep & CreateDbWorkerDep & LocalAuthDep & Partial<FlushSyncDep> & RandomBytesDep & ReloadAppDep & TimeDep;
366
+ export type EvoluDeps = ConsoleDep & CreateDbWorkerDep & Partial<FlushSyncDep> & RandomBytesDep & ReloadAppDep & TimeDep;
368
367
  /**
369
368
  * Creates an {@link Evolu} instance for a platform configured with the specified
370
369
  * {@link EvoluSchema} and optional {@link EvoluConfig} providing a typed
@@ -1 +1 @@
1
- {"version":3,"file":"Evolu.d.ts","sourceRoot":"","sources":["../../../src/Evolu/Evolu.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,cAAc,EAAE,2BAA2B,EAAE,MAAM,cAAc,CAAC;AAE3E,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAGhD,OAAO,EAA0B,WAAW,EAAe,MAAM,cAAc,CAAC;AAChF,OAAO,EAAe,cAAc,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,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S,WAAW,EACpB,QAAQ,cAAc,CAAC,CAAC,CAAC,SAAS,KAAK,GAAG,CAAC,GAAG,cAAc,CAAC,CAAC,CAAC,EAC/D,SAAS,WAAW,KACnB,KAAK,CAAC,CAAC,CAaT,CAAC;AAwaJ,eAAO,MAAM,WAAW,GAAI,CAAC,SAAS,GAAG,EACvC,eAAe,UAAU,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,EACtD,UAAU,UAAU,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,KAChD,KAAK,CAAC,CAAC,CAeT,CAAC"}
@@ -1,23 +1,25 @@
1
1
  import { RandomBytesDep } from "../Crypto.js";
2
2
  import { Mnemonic } from "../Type.js";
3
3
  import { AppOwner, OwnerId } from "./Owner.js";
4
- /** @experimental */
4
+ /**
5
+ * Local authentication and authorization system for Evolu.
6
+ * This is API is subject to change and not recommended for production use.
7
+ *
8
+ * @experimental
9
+ */
5
10
  export interface LocalAuth {
6
11
  /** Logs in with the given owner ID, or loads the target owner if not provided. */
7
- login: (ownerId?: OwnerId, options?: LocalAuthOptions & {
8
- reloadNeeded?: boolean;
9
- }) => Promise<AuthResult | null>;
12
+ login: (ownerId: OwnerId, options?: LocalAuthOptions) => Promise<AuthResult | null>;
10
13
  /** Registers a new owner with the given username. */
11
14
  register: (username: string, options?: LocalAuthOptions & {
12
15
  mnemonic?: Mnemonic | null;
13
16
  }) => Promise<AuthResult | null>;
14
17
  /** Unregisters an owner with the given owner ID. */
15
18
  unregister: (ownerId: OwnerId, options?: LocalAuthOptions) => Promise<void>;
19
+ /** Gets the current owner (last logged in or last registered owner) */
20
+ getOwner: (options?: LocalAuthOptions) => Promise<AuthResult | null>;
16
21
  /** Lists all registered owner ids with associated usernames. */
17
- getProfiles: (options?: LocalAuthOptionsValues) => Promise<Array<{
18
- ownerId: OwnerId;
19
- username: string;
20
- }>>;
22
+ getProfiles: (options?: LocalAuthOptionsValues) => Promise<Array<AuthList>>;
21
23
  /** Clears all owners and metadata from the local auth. */
22
24
  clearAll: (options?: LocalAuthOptions) => Promise<void>;
23
25
  }
@@ -50,11 +52,17 @@ export declare const AUTH_METAKEY_LAST_OWNER = "_last_owner";
50
52
  export declare const AUTH_METAKEY_OWNER_NAMES = "_owner_names";
51
53
  export declare const AUTH_DEFAULT_OPTIONS: LocalAuthOptions;
52
54
  export interface AuthResult {
53
- /** The owner created during registration. */
55
+ /** The app owner created during registration. */
54
56
  readonly owner: AppOwner | undefined;
55
57
  /** The name provided by the user during registration. */
56
58
  readonly username: string;
57
59
  }
60
+ export interface AuthList {
61
+ /** The app owner ID. */
62
+ readonly ownerId: OwnerId;
63
+ /** The name provided by the user during registration. */
64
+ readonly username: string;
65
+ }
58
66
  export interface LocalAuthOptions {
59
67
  /**
60
68
  * Native: Namespaces the stored entry. Defaults to the bundle identifier
@@ -1 +1 @@
1
- {"version":3,"file":"LocalAuth.d.ts","sourceRoot":"","sources":["../../../src/Evolu/LocalAuth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EACL,QAAQ,EAKR,OAAO,EAER,MAAM,YAAY,CAAC;AAEpB,oBAAoB;AACpB,MAAM,WAAW,SAAS;IACxB,kFAAkF;IAClF,KAAK,EAAE,CACL,OAAO,CAAC,EAAE,OAAO,EACjB,OAAO,CAAC,EAAE,gBAAgB,GAAG;QAAE,YAAY,CAAC,EAAE,OAAO,CAAA;KAAE,KACpD,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC,CAAC;IAEhC,qDAAqD;IACrD,QAAQ,EAAE,CACR,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE,gBAAgB,GAAG;QAAE,QAAQ,CAAC,EAAE,QAAQ,GAAG,IAAI,CAAA;KAAE,KACxD,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC,CAAC;IAEh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AAE,aAAa,CAAC;IACtC,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC;IACjC,QAAQ,CAAC,aAAa,EAAE,aAAa,CAAC;IACtC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B;AAED,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,QAAQ,EAAE,eAAe,CAAC;CACpC;AAED,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,QAAQ,EAAE,eAAe,CAAC;CACpC;AAED;;;GAGG;AACH,MAAM,MAAM,aAAa,GACrB,eAAe,GACf,WAAW,GACX,UAAU,GACV,kBAAkB,GAClB,UAAU,CAAC;AAEf,4EAA4E;AAC5E,MAAM,MAAM,cAAc,GACtB,UAAU,GACV,iBAAiB,GACjB,4BAA4B,CAAC;AAEjC;;;GAGG;AACH,MAAM,MAAM,aAAa,GACrB,uBAAuB,GACvB,oBAAoB,GACpB,aAAa,GACb,gBAAgB,GAChB,MAAM,CAAC"}
@@ -7,23 +7,20 @@ import { createAppOwner, createOwnerSecret, mnemonicToOwnerSecret, OwnerEncrypti
7
7
  export const createLocalAuth = (deps) => {
8
8
  const setLastOwnerId = async (id, options) => {
9
9
  await deps.secureStorage.setItem(AUTH_METAKEY_LAST_OWNER, id, {
10
- ...AUTH_DEFAULT_OPTIONS,
11
- ...options,
10
+ ...buildAuthOptions(options),
12
11
  accessControl: "none",
13
12
  });
14
13
  };
15
14
  const getLastOwnerId = async (options) => {
16
15
  const item = await deps.secureStorage.getItem(AUTH_METAKEY_LAST_OWNER, {
17
- ...AUTH_DEFAULT_OPTIONS,
18
- ...options,
16
+ ...buildAuthOptions(options),
19
17
  accessControl: "none",
20
18
  });
21
19
  return item?.value;
22
20
  };
23
21
  const getOwnerNames = async (options) => {
24
22
  const item = await deps.secureStorage.getItem(AUTH_METAKEY_OWNER_NAMES, {
25
- ...AUTH_DEFAULT_OPTIONS,
26
- ...options,
23
+ ...buildAuthOptions(options),
27
24
  accessControl: "none",
28
25
  });
29
26
  let names = {};
@@ -36,23 +33,20 @@ export const createLocalAuth = (deps) => {
36
33
  const names = await getOwnerNames(options);
37
34
  names[id] = username;
38
35
  await deps.secureStorage.setItem(AUTH_METAKEY_OWNER_NAMES, JSON.stringify(names), {
39
- ...AUTH_DEFAULT_OPTIONS,
40
- ...options,
36
+ ...buildAuthOptions(options),
41
37
  accessControl: "none",
42
38
  });
43
39
  };
44
40
  const deleteOwnerName = async (id, options) => {
45
41
  const { [id]: _, ...names } = await getOwnerNames(options);
46
42
  await deps.secureStorage.setItem(AUTH_METAKEY_OWNER_NAMES, JSON.stringify(names), {
47
- ...AUTH_DEFAULT_OPTIONS,
48
- ...options,
43
+ ...buildAuthOptions(options),
49
44
  accessControl: "none",
50
45
  });
51
46
  };
52
47
  const getOwnerIds = async (options) => {
53
48
  const items = await deps.secureStorage.getAllItems({
54
- ...AUTH_DEFAULT_OPTIONS,
55
- ...options,
49
+ ...buildAuthOptions(options),
56
50
  includeValues: false,
57
51
  });
58
52
  return items
@@ -61,42 +55,39 @@ export const createLocalAuth = (deps) => {
61
55
  i.key !== AUTH_METAKEY_OWNER_NAMES)
62
56
  .map((i) => i.key);
63
57
  };
64
- const clearAuthStore = (options) => deps.secureStorage.clearService({
65
- ...AUTH_DEFAULT_OPTIONS,
66
- ...options,
67
- });
58
+ const clearAuthStore = (options) => deps.secureStorage.clearService(buildAuthOptions(options));
59
+ const buildAuthOptions = (options, username) => {
60
+ const newOptions = {
61
+ ...AUTH_DEFAULT_OPTIONS,
62
+ ...(username && { webAuthnUsername: username }),
63
+ ...options,
64
+ };
65
+ return {
66
+ ...newOptions,
67
+ authenticationPrompt: {
68
+ title: replaceMessageTokens(newOptions.authenticationPrompt?.title ?? "", username),
69
+ cancel: replaceMessageTokens(newOptions.authenticationPrompt?.cancel ?? "", username),
70
+ subtitle: replaceMessageTokens(newOptions.authenticationPrompt?.subtitle ?? "", username),
71
+ description: replaceMessageTokens(newOptions.authenticationPrompt?.description ?? "", username),
72
+ },
73
+ };
74
+ };
75
+ const replaceMessageTokens = (text, username) => {
76
+ if (!username)
77
+ return text;
78
+ return text.replace("|USERNAME|", username);
79
+ };
68
80
  return {
69
81
  login: async (ownerId, options) => {
70
- // Use either specified owner or the last owner used during registration/login.
71
- const targetOwnerId = ownerId ?? (await getLastOwnerId(options));
72
- if (!targetOwnerId)
73
- return null;
74
82
  // Lookup the associated username
75
83
  const names = await getOwnerNames(options);
76
- const username = names[targetOwnerId] ?? "";
77
- // If a reload is needed, avoid authentication, it needs to be handled on next page load.
84
+ const username = names[ownerId] ?? "";
85
+ // Currently a reload is needed. This avoids authentication
86
+ // it needs to be handled on next page load.
78
87
  // We set the last owner so we know what the target is.
79
88
  // It is the applications's responsibility to reload and trigger login.
80
- if (options?.reloadNeeded) {
81
- await setLastOwnerId(targetOwnerId, options);
82
- return { owner: undefined, username };
83
- }
84
- // Retrieve and decrypt the owner (this will trigger device authentication)
85
- const account = await deps.secureStorage.getItem(targetOwnerId, {
86
- ...AUTH_DEFAULT_OPTIONS,
87
- ...options,
88
- });
89
- if (!account?.value)
90
- return null;
91
- // Unserialize the values (TODO: save these as base64 instead of json serializing)
92
- const result = JSON.parse(account.value);
93
- const writeKey = OwnerWriteKey.orThrow(new Uint8Array(Object.values(result.owner.writeKey)));
94
- const encryptionKey = OwnerEncryptionKey.orThrow(new Uint8Array(Object.values(result.owner.encryptionKey)));
95
- const owner = { ...result.owner, writeKey, encryptionKey };
96
- // Update the last owner for future login attempts
97
- await setLastOwnerId(targetOwnerId, options);
98
- // Return the owner and associated username
99
- return { owner, username };
89
+ await setLastOwnerId(ownerId, options);
90
+ return { owner: undefined, username };
100
91
  },
101
92
  register: async (username, options) => {
102
93
  // Create an owner with a new secret or use specified mnemonic
@@ -106,11 +97,7 @@ export const createLocalAuth = (deps) => {
106
97
  // Store owner, associated username, and update last owner
107
98
  await Promise.all([
108
99
  // setOwnerItem
109
- deps.secureStorage.setItem(owner.id, JSON.stringify({ owner }), {
110
- ...AUTH_DEFAULT_OPTIONS,
111
- webAuthnUsername: username,
112
- ...options,
113
- }),
100
+ deps.secureStorage.setItem(owner.id, JSON.stringify({ owner }), buildAuthOptions(options, username)),
114
101
  setOwnerName(owner.id, username, options),
115
102
  setLastOwnerId(owner.id, options),
116
103
  ]);
@@ -121,10 +108,7 @@ export const createLocalAuth = (deps) => {
121
108
  // Delete the owner and associated username
122
109
  await Promise.all([
123
110
  // deleteOwnerItem
124
- deps.secureStorage.deleteItem(ownerId, {
125
- ...AUTH_DEFAULT_OPTIONS,
126
- ...options,
127
- }),
111
+ deps.secureStorage.deleteItem(ownerId, buildAuthOptions(options)),
128
112
  deleteOwnerName(ownerId, options),
129
113
  ]);
130
114
  // If the owner was the last owner then set to
@@ -137,6 +121,26 @@ export const createLocalAuth = (deps) => {
137
121
  }
138
122
  }
139
123
  },
124
+ getOwner: async (options) => {
125
+ const ownerId = await getLastOwnerId(options);
126
+ if (!ownerId)
127
+ return null;
128
+ const names = await getOwnerNames(options);
129
+ const username = names[ownerId] ?? "";
130
+ // Retrieve and decrypt the owner (this will trigger device authentication)
131
+ const account = await deps.secureStorage.getItem(ownerId, buildAuthOptions(options, username));
132
+ if (!account?.value)
133
+ return null;
134
+ // Unserialize the values (TODO: save these as base64 instead of json serializing)
135
+ const result = JSON.parse(account.value);
136
+ const writeKey = OwnerWriteKey.orThrow(new Uint8Array(Object.values(result.owner.writeKey)));
137
+ const encryptionKey = OwnerEncryptionKey.orThrow(new Uint8Array(Object.values(result.owner.encryptionKey)));
138
+ const owner = { ...result.owner, writeKey, encryptionKey };
139
+ // Update the last owner for future login attempts
140
+ await setLastOwnerId(ownerId, options);
141
+ // Return the owner and associated username
142
+ return { owner, username };
143
+ },
140
144
  getProfiles: async (options) => {
141
145
  // Get all owner ids and associated usernames
142
146
  const [ids, names] = await Promise.all([
@@ -166,6 +170,6 @@ export const AUTH_DEFAULT_OPTIONS = {
166
170
  iosSynchronizable: true,
167
171
  webAuthnUsername: "Evolu User",
168
172
  authenticationPrompt: {
169
- title: "Authenticate to unlock your session",
173
+ title: "Authenticate as |USERNAME|",
170
174
  },
171
175
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evolu/common",
3
- "version": "6.0.1-preview.22",
3
+ "version": "6.0.1-preview.23",
4
4
  "description": "TypeScript library and local-first framework",
5
5
  "keywords": [
6
6
  "evolu",
@@ -25,7 +25,6 @@ import {
25
25
  import { IntentionalNever } from "../Types.js";
26
26
  import { CreateDbWorkerDep, DbConfig, defaultDbConfig } from "./Db.js";
27
27
  import { applyPatches } from "./Diff.js";
28
- import { LocalAuthDep } from "./LocalAuth.js";
29
28
  import { AppOwner } from "./Owner.js";
30
29
  import { FlushSyncDep, ReloadAppDep } from "./Platform.js";
31
30
  import { ProtocolError, ProtocolUnsupportedVersionError } from "./Protocol.js";
@@ -453,7 +452,6 @@ interface InternalEvoluInstance<S extends EvoluSchema = EvoluSchema>
453
452
 
454
453
  export type EvoluDeps = ConsoleDep &
455
454
  CreateDbWorkerDep &
456
- LocalAuthDep &
457
455
  Partial<FlushSyncDep> &
458
456
  RandomBytesDep &
459
457
  ReloadAppDep &
@@ -10,12 +10,17 @@ import {
10
10
  OwnerWriteKey,
11
11
  } from "./Owner.js";
12
12
 
13
- /** @experimental */
13
+ /**
14
+ * Local authentication and authorization system for Evolu.
15
+ * This is API is subject to change and not recommended for production use.
16
+ *
17
+ * @experimental
18
+ */
14
19
  export interface LocalAuth {
15
20
  /** Logs in with the given owner ID, or loads the target owner if not provided. */
16
21
  login: (
17
- ownerId?: OwnerId,
18
- options?: LocalAuthOptions & { reloadNeeded?: boolean },
22
+ ownerId: OwnerId,
23
+ options?: LocalAuthOptions,
19
24
  ) => Promise<AuthResult | null>;
20
25
 
21
26
  /** Registers a new owner with the given username. */
@@ -27,10 +32,11 @@ export interface LocalAuth {
27
32
  /** Unregisters an owner with the given owner ID. */
28
33
  unregister: (ownerId: OwnerId, options?: LocalAuthOptions) => Promise<void>;
29
34
 
35
+ /** Gets the current owner (last logged in or last registered owner) */
36
+ getOwner: (options?: LocalAuthOptions) => Promise<AuthResult | null>;
37
+
30
38
  /** Lists all registered owner ids with associated usernames. */
31
- getProfiles: (
32
- options?: LocalAuthOptionsValues,
33
- ) => Promise<Array<{ ownerId: OwnerId; username: string }>>;
39
+ getProfiles: (options?: LocalAuthOptionsValues) => Promise<Array<AuthList>>;
34
40
 
35
41
  /** Clears all owners and metadata from the local auth. */
36
42
  clearAll: (options?: LocalAuthOptions) => Promise<void>;
@@ -79,8 +85,7 @@ export const createLocalAuth = (
79
85
  options?: LocalAuthOptions,
80
86
  ): Promise<void> => {
81
87
  await deps.secureStorage.setItem(AUTH_METAKEY_LAST_OWNER, id, {
82
- ...AUTH_DEFAULT_OPTIONS,
83
- ...options,
88
+ ...buildAuthOptions(options),
84
89
  accessControl: "none",
85
90
  });
86
91
  };
@@ -89,8 +94,7 @@ export const createLocalAuth = (
89
94
  options?: LocalAuthOptions,
90
95
  ): Promise<OwnerId | undefined> => {
91
96
  const item = await deps.secureStorage.getItem(AUTH_METAKEY_LAST_OWNER, {
92
- ...AUTH_DEFAULT_OPTIONS,
93
- ...options,
97
+ ...buildAuthOptions(options),
94
98
  accessControl: "none",
95
99
  });
96
100
  return item?.value as OwnerId;
@@ -100,8 +104,7 @@ export const createLocalAuth = (
100
104
  options?: LocalAuthOptions,
101
105
  ): Promise<Record<OwnerId, string>> => {
102
106
  const item = await deps.secureStorage.getItem(AUTH_METAKEY_OWNER_NAMES, {
103
- ...AUTH_DEFAULT_OPTIONS,
104
- ...options,
107
+ ...buildAuthOptions(options),
105
108
  accessControl: "none",
106
109
  });
107
110
  let names: Record<OwnerId, string> = {};
@@ -122,8 +125,7 @@ export const createLocalAuth = (
122
125
  AUTH_METAKEY_OWNER_NAMES,
123
126
  JSON.stringify(names),
124
127
  {
125
- ...AUTH_DEFAULT_OPTIONS,
126
- ...options,
128
+ ...buildAuthOptions(options),
127
129
  accessControl: "none",
128
130
  },
129
131
  );
@@ -138,8 +140,7 @@ export const createLocalAuth = (
138
140
  AUTH_METAKEY_OWNER_NAMES,
139
141
  JSON.stringify(names),
140
142
  {
141
- ...AUTH_DEFAULT_OPTIONS,
142
- ...options,
143
+ ...buildAuthOptions(options),
143
144
  accessControl: "none",
144
145
  },
145
146
  );
@@ -149,8 +150,7 @@ export const createLocalAuth = (
149
150
  options?: LocalAuthOptions,
150
151
  ): Promise<Array<OwnerId>> => {
151
152
  const items = await deps.secureStorage.getAllItems({
152
- ...AUTH_DEFAULT_OPTIONS,
153
- ...options,
153
+ ...buildAuthOptions(options),
154
154
  includeValues: false,
155
155
  });
156
156
  return items
@@ -164,51 +164,44 @@ export const createLocalAuth = (
164
164
  };
165
165
 
166
166
  const clearAuthStore = (options?: LocalAuthOptions): Promise<void> =>
167
- deps.secureStorage.clearService({
167
+ deps.secureStorage.clearService(buildAuthOptions(options));
168
+
169
+ const buildAuthOptions = (
170
+ options?: LocalAuthOptions,
171
+ username?: string,
172
+ ): LocalAuthOptions => {
173
+ const newOptions: LocalAuthOptions = {
168
174
  ...AUTH_DEFAULT_OPTIONS,
175
+ ...(username && { webAuthnUsername: username }),
169
176
  ...options,
170
- });
177
+ };
178
+ return {
179
+ ...newOptions,
180
+ authenticationPrompt: {
181
+ title: replaceMessageTokens(newOptions.authenticationPrompt?.title ?? "", username),
182
+ cancel: replaceMessageTokens(newOptions.authenticationPrompt?.cancel ?? "", username),
183
+ subtitle: replaceMessageTokens(newOptions.authenticationPrompt?.subtitle ?? "", username),
184
+ description: replaceMessageTokens(newOptions.authenticationPrompt?.description ?? "", username),
185
+ },
186
+ };
187
+ };
188
+
189
+ const replaceMessageTokens = (text: string, username?: string): string => {
190
+ if (!username) return text;
191
+ return text.replace("|USERNAME|", username);
192
+ };
171
193
 
172
194
  return {
173
195
  login: async (ownerId, options) => {
174
- // Use either specified owner or the last owner used during registration/login.
175
- const targetOwnerId = ownerId ?? (await getLastOwnerId(options));
176
- if (!targetOwnerId) return null;
177
-
178
196
  // Lookup the associated username
179
197
  const names = await getOwnerNames(options);
180
- const username = names[targetOwnerId] ?? "";
181
-
182
- // If a reload is needed, avoid authentication, it needs to be handled on next page load.
198
+ const username = names[ownerId] ?? "";
199
+ // Currently a reload is needed. This avoids authentication
200
+ // it needs to be handled on next page load.
183
201
  // We set the last owner so we know what the target is.
184
202
  // It is the applications's responsibility to reload and trigger login.
185
- if (options?.reloadNeeded) {
186
- await setLastOwnerId(targetOwnerId, options);
187
- return { owner: undefined, username };
188
- }
189
-
190
- // Retrieve and decrypt the owner (this will trigger device authentication)
191
- const account = await deps.secureStorage.getItem(targetOwnerId, {
192
- ...AUTH_DEFAULT_OPTIONS,
193
- ...options,
194
- });
195
- if (!account?.value) return null;
196
-
197
- // Unserialize the values (TODO: save these as base64 instead of json serializing)
198
- const result = JSON.parse(account.value) as { owner: AppOwner };
199
- const writeKey = OwnerWriteKey.orThrow(
200
- new Uint8Array(Object.values(result.owner.writeKey)),
201
- );
202
- const encryptionKey = OwnerEncryptionKey.orThrow(
203
- new Uint8Array(Object.values(result.owner.encryptionKey)),
204
- );
205
- const owner: AppOwner = { ...result.owner, writeKey, encryptionKey };
206
-
207
- // Update the last owner for future login attempts
208
- await setLastOwnerId(targetOwnerId, options);
209
-
210
- // Return the owner and associated username
211
- return { owner, username };
203
+ await setLastOwnerId(ownerId, options);
204
+ return { owner: undefined, username };
212
205
  },
213
206
 
214
207
  register: async (username, options) => {
@@ -222,11 +215,11 @@ export const createLocalAuth = (
222
215
  // Store owner, associated username, and update last owner
223
216
  await Promise.all([
224
217
  // setOwnerItem
225
- deps.secureStorage.setItem(owner.id, JSON.stringify({ owner }), {
226
- ...AUTH_DEFAULT_OPTIONS,
227
- webAuthnUsername: username,
228
- ...options,
229
- }),
218
+ deps.secureStorage.setItem(
219
+ owner.id,
220
+ JSON.stringify({ owner }),
221
+ buildAuthOptions(options, username),
222
+ ),
230
223
  setOwnerName(owner.id, username, options),
231
224
  setLastOwnerId(owner.id, options),
232
225
  ]);
@@ -239,10 +232,7 @@ export const createLocalAuth = (
239
232
  // Delete the owner and associated username
240
233
  await Promise.all([
241
234
  // deleteOwnerItem
242
- deps.secureStorage.deleteItem(ownerId, {
243
- ...AUTH_DEFAULT_OPTIONS,
244
- ...options,
245
- }),
235
+ deps.secureStorage.deleteItem(ownerId, buildAuthOptions(options)),
246
236
  deleteOwnerName(ownerId, options),
247
237
  ]);
248
238
 
@@ -257,6 +247,37 @@ export const createLocalAuth = (
257
247
  }
258
248
  },
259
249
 
250
+ getOwner: async (options) => {
251
+ const ownerId = await getLastOwnerId(options);
252
+ if (!ownerId) return null;
253
+
254
+ const names = await getOwnerNames(options);
255
+ const username = names[ownerId] ?? "";
256
+
257
+ // Retrieve and decrypt the owner (this will trigger device authentication)
258
+ const account = await deps.secureStorage.getItem(
259
+ ownerId,
260
+ buildAuthOptions(options, username),
261
+ );
262
+ if (!account?.value) return null;
263
+
264
+ // Unserialize the values (TODO: save these as base64 instead of json serializing)
265
+ const result = JSON.parse(account.value) as { owner: AppOwner };
266
+ const writeKey = OwnerWriteKey.orThrow(
267
+ new Uint8Array(Object.values(result.owner.writeKey)),
268
+ );
269
+ const encryptionKey = OwnerEncryptionKey.orThrow(
270
+ new Uint8Array(Object.values(result.owner.encryptionKey)),
271
+ );
272
+ const owner: AppOwner = { ...result.owner, writeKey, encryptionKey };
273
+
274
+ // Update the last owner for future login attempts
275
+ await setLastOwnerId(ownerId, options);
276
+
277
+ // Return the owner and associated username
278
+ return { owner, username };
279
+ },
280
+
260
281
  getProfiles: async (options) => {
261
282
  // Get all owner ids and associated usernames
262
283
  const [ids, names] = await Promise.all([
@@ -289,17 +310,24 @@ export const AUTH_DEFAULT_OPTIONS: LocalAuthOptions = {
289
310
  iosSynchronizable: true,
290
311
  webAuthnUsername: "Evolu User",
291
312
  authenticationPrompt: {
292
- title: "Authenticate to unlock your session",
313
+ title: "Authenticate as |USERNAME|",
293
314
  },
294
315
  };
295
316
 
296
317
  export interface AuthResult {
297
- /** The owner created during registration. */
318
+ /** The app owner created during registration. */
298
319
  readonly owner: AppOwner | undefined;
299
320
  /** The name provided by the user during registration. */
300
321
  readonly username: string;
301
322
  }
302
323
 
324
+ export interface AuthList {
325
+ /** The app owner ID. */
326
+ readonly ownerId: OwnerId;
327
+ /** The name provided by the user during registration. */
328
+ readonly username: string;
329
+ }
330
+
303
331
  /* Types below based off of react-native-sensitive-info */
304
332
 
305
333
  export interface LocalAuthOptions {