Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

feat: login via accounts passkeys

httpjamesm5bc3e48876e9
diff --git a/apps/accounts/public/images/ente-circular.png b/apps/accounts/public/images/ente-circular.png
new file mode 100644
index 0000000000..49043f9340
Binary files /dev/null and b/apps/accounts/public/images/ente-circular.png differ
diff --git a/apps/accounts/src/pages/passkeys/flow/index.tsx b/apps/accounts/src/pages/passkeys/flow/index.tsx
new file mode 100644
index 0000000000..73562aa56a
--- /dev/null
+++ b/apps/accounts/src/pages/passkeys/flow/index.tsx
@@ -0,0 +1,132 @@
+import { CenteredFlex } from '@ente/shared/components/Container';
+import FormPaper from '@ente/shared/components/Form/FormPaper';
+import { Box, Typography } from '@mui/material';
+import InfoIcon from '@mui/icons-material/Info';
+import Image from 'next/image';
+import { useEffect } from 'react';
+import {
+ BeginPasskeyAuthenticationResponse,
+ beginPasskeyAuthentication,
+ finishPasskeyAuthentication,
+} from 'services/passkeysService';
+import { logError } from '@ente/shared/sentry';
+import _sodium from 'libsodium-wrappers';
+
+const PasskeysFlow = () => {
+ const init = async () => {
+ // get passkeySessionID from the query params
+ const searchParams = new URLSearchParams(window.location.search);
+ const passkeySessionID = searchParams.get('passkeySessionID');
+
+ let beginData: BeginPasskeyAuthenticationResponse;
+
+ try {
+ beginData = await beginAuthentication(passkeySessionID);
+ } catch (e) {
+ logError(e, "Couldn't begin passkey authentication");
+ return;
+ }
+
+ let credential: Credential;
+
+ try {
+ credential = await getCredential(beginData.options.publicKey);
+ } catch (e) {
+ logError(e, "Couldn't get credential");
+ return;
+ }
+
+ let finishData;
+
+ try {
+ finishData = await finishAuthentication(
+ credential,
+ passkeySessionID,
+ beginData.ceremonySessionID
+ );
+ } catch (e) {
+ logError(e, "Couldn't finish passkey authentication");
+ return;
+ }
+
+ const encodedResponse = _sodium.to_base64(JSON.stringify(finishData));
+ console.log(encodedResponse);
+ };
+
+ const beginAuthentication = async (sessionId: string) => {
+ const data = await beginPasskeyAuthentication(sessionId);
+ return data;
+ };
+
+ const getCredential = async (publicKey: any): Promise<Credential> => {
+ publicKey.challenge = _sodium.from_base64(
+ publicKey.challenge,
+ _sodium.base64_variants.URLSAFE_NO_PADDING
+ );
+ publicKey.allowCredentials?.forEach(function (listItem) {
+ listItem.id = _sodium.from_base64(
+ listItem.id,
+ _sodium.base64_variants.URLSAFE_NO_PADDING
+ );
+ });
+
+ const credential = await navigator.credentials.get({
+ publicKey,
+ });
+
+ return credential;
+ };
+
+ const finishAuthentication = async (
+ credential: Credential,
+ sessionId: string,
+ ceremonySessionId: string
+ ) => {
+ const data = await finishPasskeyAuthentication(
+ credential,
+ sessionId,
+ ceremonySessionId
+ );
+ return data;
+ };
+
+ useEffect(() => {
+ init();
+ }, []);
+
+ return (
+ <>
+ <Box
+ display="flex"
+ justifyContent="center"
+ alignItems="center"
+ height="100%">
+ <Box maxWidth="30rem">
+ <FormPaper
+ style={{
+ padding: '1rem',
+ }}>
+ <InfoIcon />
+ <Typography fontWeight="bold" variant="h4">
+ Login with Passkey
+ </Typography>
+ <Typography marginTop="1rem">
+ Follow the steps from your browser to continue
+ logging into Ente
+ </Typography>
+ <CenteredFlex marginTop="1rem">
+ <Image
+ alt="ente Logo Circular"
+ height={150}
+ width={150}
+ src="/images/ente-circular.png"
+ />
+ </CenteredFlex>
+ </FormPaper>
+ </Box>
+ </Box>
+ </>
+ );
+};
+
+export default PasskeysFlow;
diff --git a/apps/accounts/src/services/passkeysService.ts b/apps/accounts/src/services/passkeysService.ts
index 6ca9052a07..94f4040b22 100644
--- a/apps/accounts/src/services/passkeysService.ts
+++ b/apps/accounts/src/services/passkeysService.ts
@@ -126,3 +126,86 @@ export const finishPasskeyRegistration = async (
throw e;
}
};
+
+export interface BeginPasskeyAuthenticationResponse {
+ ceremonySessionID: string;
+ options: Options;
+}
+interface Options {
+ publicKey: PublicKeyCredentialRequestOptions;
+}
+
+export const beginPasskeyAuthentication = async (
+ sessionId: string
+): Promise<BeginPasskeyAuthenticationResponse> => {
+ try {
+ const data = await HTTPService.post(
+ `${ENDPOINT}/users/two-factor/passkeys/begin`,
+ {
+ sessionID: sessionId,
+ }
+ );
+
+ return data.data;
+ } catch (e) {
+ logError(e, 'begin passkey authentication failed');
+ throw e;
+ }
+};
+
+export const finishPasskeyAuthentication = async (
+ credential: Credential,
+ sessionId: string,
+ ceremonySessionId: string
+) => {
+ try {
+ const data = await HTTPService.post(
+ `${ENDPOINT}/users/two-factor/passkeys/finish`,
+ {
+ id: credential.id,
+ rawId: _sodium.to_base64(
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-ignore
+ new Uint8Array(credential.rawId),
+ _sodium.base64_variants.URLSAFE_NO_PADDING
+ ),
+ type: credential.type,
+ response: {
+ authenticatorData: _sodium.to_base64(
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-ignore
+ new Uint8Array(credential.response.authenticatorData),
+ _sodium.base64_variants.URLSAFE_NO_PADDING
+ ),
+ clientDataJSON: _sodium.to_base64(
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-ignore
+ new Uint8Array(credential.response.clientDataJSON),
+ _sodium.base64_variants.URLSAFE_NO_PADDING
+ ),
+ signature: _sodium.to_base64(
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-ignore
+ new Uint8Array(credential.response.signature),
+ _sodium.base64_variants.URLSAFE_NO_PADDING
+ ),
+ userHandle: _sodium.to_base64(
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-ignore
+ new Uint8Array(credential.response.userHandle),
+ _sodium.base64_variants.URLSAFE_NO_PADDING
+ ),
+ },
+ },
+ {
+ sessionID: sessionId,
+ ceremonySessionID: ceremonySessionId,
+ }
+ );
+
+ return data.data;
+ } catch (e) {
+ logError(e, 'finish passkey authentication failed');
+ throw e;
+ }
+};
diff --git a/apps/accounts/src/styles/global.css b/apps/accounts/src/styles/global.css
index 2dace242c1..8f926ea083 100644
--- a/apps/accounts/src/styles/global.css
+++ b/apps/accounts/src/styles/global.css
@@ -33,7 +33,8 @@
html,
body {
- height: 100%;
+ min-height: 100vh;
+ height: 100vh;
flex: 1;
display: flex;
flex-direction: column;
@@ -43,6 +44,8 @@ body {
flex: 1;
display: flex;
flex-direction: column;
+ min-height: 100vh;
+ height: 100vh;
}
.pswp__button--custom {
diff --git a/packages/accounts/pages/credentials.tsx b/packages/accounts/pages/credentials.tsx
index 12bdabf428..51f4db900b 100644
--- a/packages/accounts/pages/credentials.tsx
+++ b/packages/accounts/pages/credentials.tsx
@@ -148,9 +148,26 @@ export default function Credentials({
token,
id,
twoFactorSessionID,
+ passkeySessionID,
} = await loginViaSRP(srpAttributes, kek);
setIsFirstLogin(true);
- if (twoFactorSessionID) {
+ if (passkeySessionID) {
+ const sessionKeyAttributes =
+ await cryptoWorker.generateKeyAndEncryptToB64(kek);
+ setKey(
+ SESSION_KEYS.KEY_ENCRYPTION_KEY,
+ sessionKeyAttributes
+ );
+ const user = getData(LS_KEYS.USER);
+ setData(LS_KEYS.USER, {
+ ...user,
+ passkeySessionID,
+ isTwoFactorEnabled: true,
+ isTwoFactorPasskeysEnabled: true,
+ });
+ window.location.href = `${process.env.NEXT_PUBLIC_ACCOUNTS_ENDPOINT}/passkeys/flow?passkeySessionID=${passkeySessionID}`;
+ throw Error(CustomError.PASSKEYS_TWO_FACTOR_ENABLED);
+ } else if (twoFactorSessionID) {
const sessionKeyAttributes =
await cryptoWorker.generateKeyAndEncryptToB64(kek);
setKey(
diff --git a/packages/accounts/types/user.ts b/packages/accounts/types/user.ts
index df293e3aab..fc216194ba 100644
--- a/packages/accounts/types/user.ts
+++ b/packages/accounts/types/user.ts
@@ -6,6 +6,7 @@ export interface UserVerificationResponse {
encryptedToken?: string;
token?: string;
twoFactorSessionID: string;
+ passkeySessionID: string;
srpM2?: string;
}
diff --git a/packages/shared/error/index.ts b/packages/shared/error/index.ts
index d9a35ab7ac..ca3f969a16 100644
--- a/packages/shared/error/index.ts
+++ b/packages/shared/error/index.ts
@@ -82,6 +82,7 @@ export const CustomError = {
PROCESSING_FAILED: 'processing failed',
EXPORT_RECORD_JSON_PARSING_FAILED: 'export record json parsing failed',
TWO_FACTOR_ENABLED: 'two factor enabled',
+ PASSKEYS_TWO_FACTOR_ENABLED: 'passkeys two factor enabled',
CLIENT_ERROR: 'client error',
ServerError: 'server error',
FILE_NOT_FOUND: 'file not found',