-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
139 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
frontend/openchat-shared/src/utils/emailSignInSessionStorage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import type { AuthClientStorage } from "@dfinity/auth-client"; | ||
import { ECDSAKeyIdentity } from "@dfinity/identity"; | ||
|
||
const KEY_STORAGE_EMAIL_LINK = "email_link"; | ||
const KEY_STORAGE_EMAIL_LINK_CONTEXT = "email_link_context"; | ||
|
||
export type EmailSignInSession = EmailSignInContext & { | ||
key: ECDSAKeyIdentity; | ||
}; | ||
|
||
export type EmailSignInContext = { | ||
email: string; | ||
userKey: Uint8Array; | ||
expiration: bigint; | ||
}; | ||
|
||
export async function storeEmailSignInSession( | ||
storage: AuthClientStorage, | ||
session: EmailSignInSession, | ||
): Promise<void> { | ||
await storage.set(KEY_STORAGE_EMAIL_LINK, session.key.getKeyPair()); | ||
|
||
await storage.set( | ||
KEY_STORAGE_EMAIL_LINK_CONTEXT, | ||
JSON.stringify({ | ||
email: session.email, | ||
userKey: Array.from(session.userKey), | ||
expiration: session.expiration.toString(), | ||
}), | ||
); | ||
} | ||
|
||
export async function getEmailSignInSession( | ||
storage: AuthClientStorage, | ||
): Promise<EmailSignInSession | undefined> { | ||
const keyPair = (await storage.get(KEY_STORAGE_EMAIL_LINK)) as CryptoKeyPair | null; | ||
if (keyPair == null) return undefined; | ||
const contextString = (await storage.get(KEY_STORAGE_EMAIL_LINK_CONTEXT)) as string | null; | ||
if (contextString == null) return undefined; | ||
|
||
const key = await ECDSAKeyIdentity.fromKeyPair(keyPair); | ||
const context = JSON.parse(contextString); | ||
|
||
return { | ||
key, | ||
email: context.email, | ||
userKey: Uint8Array.from(context.userKey), | ||
expiration: BigInt(context.expiration), | ||
}; | ||
} | ||
|
||
export async function removeEmailSignInSession(storage: AuthClientStorage): Promise<void> { | ||
storage.remove(KEY_STORAGE_EMAIL_LINK); | ||
storage.remove(KEY_STORAGE_EMAIL_LINK_CONTEXT); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters