-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add endpoints - authenticate [passkeys] #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
matteyu
wants to merge
52
commits into
development
Choose a base branch
from
arc-937/auth-passkeys
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
52 commits
Select commit
Hold shift + click to select a range
564f163
feat: add passkeys registration, auth, verify routes
matteyu edc5e1e
add auth methods for passkeys
matteyu 4655f75
handle merge conflicts
matteyu 91b3475
add missing userprofile fields
matteyu 1746dbd
fix imports
matteyu f11d5f0
add deps for erd
matteyu d1d19ce
skip erd generate on vercel
matteyu f7ba311
swtich to npx
matteyu 5caa432
comment out prisma erd generator
matteyu a49eaff
refactor passkeys
matteyu fde81bd
change to buffer.from
matteyu 2951926
add exported types from prisma
matteyu 88d195f
dry functions for challenge validations
matteyu 1e08ccf
fix merge conflicts
matteyu e11ff0b
add challenge purpose authentication
matteyu 7f49d71
fix merge conflicts
matteyu 47adcc9
merge conflicts
matteyu 6e8a885
fix merge conflicts
matteyu f284f19
add apple callback
matteyu b0e5316
fix merge conflict
matteyu 19f1040
refactor passkeys
matteyu bf147c6
remove unused auth
matteyu b6530a9
modify test page
matteyu df6ed2f
switch to otp
matteyu e184777
add refresh token test
matteyu c26f941
Merge branch 'development' of github.com:arconnectio/embed-api into a…
matteyu 2c461d1
add back readme
matteyu d253050
revert back context session update
matteyu 73cbec6
fix input errors
matteyu 995ee31
remake init migration
matteyu 0bcf1a6
resolve merge confilicts
matteyu e152969
add back custom access token migration
matteyu 9a7ee95
rename init migration
matteyu 4a6a618
remove countryCode from context
matteyu 8cd440f
fix context errors
matteyu 72cf369
Merge branch 'development' of github.com:arconnectio/embed-api into a…
matteyu 8c3a390
add index
matteyu 845aa21
fix passkey logic
matteyu bdfc880
implement authentication verification
matteyu ba37c16
add session metadata
matteyu fe976e0
modifications for UI integration
matteyu bb53dfc
fix migration files
matteyu a3dff5e
fix reuse body in req for custom token
matteyu d4cdabe
enable for iframe
matteyu c1f0fe2
fix merge conflicts
matteyu 224925c
use supabase auth for token generation
matteyu ac6f774
fix user trigger
matteyu 70153a5
cleanup crossauth
matteyu cfad026
fix session data structure
matteyu 0c4438b
restore x-custom-auth
matteyu 626ecf3
fix merge conflicts
matteyu 34b3e8d
modify passkey appraoch
matteyu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions
16
prisma/migrations/20250122083115_add_passkeys/migration.sql
This file contains hidden or 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,16 @@ | ||
| -- CreateEnum | ||
| CREATE TYPE "WebAuthnDeviceType" AS ENUM ('SINGLE_DEVICE', 'MULTI_DEVICE'); | ||
|
|
||
| -- CreateEnum | ||
| CREATE TYPE "WebAuthnBackupState" AS ENUM ('NOT_BACKED_UP', 'BACKED_UP'); | ||
|
|
||
| -- AlterTable | ||
| ALTER TABLE "AuthMethods" ADD COLUMN "aaguid" VARCHAR(255) DEFAULT '00000000-0000-0000-0000-000000000000', | ||
| ADD COLUMN "backupState" "WebAuthnBackupState" NOT NULL DEFAULT 'NOT_BACKED_UP', | ||
| ADD COLUMN "deviceType" "WebAuthnDeviceType" NOT NULL DEFAULT 'SINGLE_DEVICE', | ||
| ADD COLUMN "publicKey" BYTEA, | ||
| ADD COLUMN "signCount" INTEGER, | ||
| ADD COLUMN "transports" TEXT[]; | ||
|
|
||
| -- AlterTable | ||
| ALTER TABLE "Challenges" ADD COLUMN "usedAt" TIMESTAMP(3); |
This file contains hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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,10 @@ | ||
| import { publicProcedure } from "@/server/trpc" | ||
| import { handleGoogleCallback } from "@/services/auth" | ||
|
|
||
| export const googleRoutes = { | ||
| handleGoogleCallback: publicProcedure.query(async () => { | ||
| const user = await handleGoogleCallback() | ||
| return { user } | ||
| }), | ||
| // Add any other google auth related routes here | ||
| } |
This file contains hidden or 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,125 @@ | ||
| // src/server/routers/passkeys.ts | ||
matteyu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import { | ||
| generateRegistrationOptions, | ||
| verifyRegistrationResponse, | ||
| } from "@simplewebauthn/server"; | ||
| import { supabase } from "@/lib/supabaseClient"; | ||
| import { TRPCError } from "@trpc/server"; | ||
| import { z } from "zod"; | ||
| import { publicProcedure } from "@/server/trpc"; | ||
| import { | ||
| relyingPartyID, | ||
| relyingPartyName, | ||
| relyingPartyOrigin, | ||
| } from "@/services/webauthnConfig"; | ||
|
|
||
| function stringToUint8Array(str: string): Uint8Array { | ||
| return new TextEncoder().encode(str); | ||
matteyu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| export const passkeysRoutes = { | ||
| startRegistration: publicProcedure | ||
| .input( | ||
| z.object({ | ||
| userId: z.string(), | ||
matteyu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| userEmail: z.string(), | ||
| }) | ||
| ) | ||
| .mutation(async ({ input }) => { | ||
| const { userId, userEmail } = input; | ||
|
|
||
| // Generate registration options | ||
| const options = await generateRegistrationOptions({ | ||
| rpName: relyingPartyName, | ||
| rpID: relyingPartyID, | ||
| userID: stringToUint8Array(userId), | ||
| userName: userEmail, | ||
| attestationType: "direct", | ||
| authenticatorSelection: { | ||
| residentKey: "preferred", | ||
| userVerification: "preferred", | ||
| authenticatorAttachment: "platform", | ||
| }, | ||
| }); | ||
|
|
||
| // Store challenge in the database | ||
| const { error } = await supabase.from("Challenges").insert({ | ||
| type: "SIGNATURE", | ||
| purpose: "ACCOUNT_RECOVERY", | ||
matteyu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| value: options.challenge, | ||
| user_id: userId, | ||
| }); | ||
|
|
||
| if (error) { | ||
| throw new TRPCError({ | ||
| code: "INTERNAL_SERVER_ERROR", | ||
| message: error.message, | ||
| }); | ||
| } | ||
|
|
||
| return options; | ||
| }), | ||
| verifyRegistration: publicProcedure | ||
| .input( | ||
| z.object({ | ||
| userId: z.string(), | ||
| attestationResponse: z.any(), | ||
| }) | ||
| ) | ||
| .mutation(async ({ input }) => { | ||
| const { userId, attestationResponse } = input; | ||
|
|
||
matteyu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Retrieve the challenge | ||
| const { data: challenge, error: challengeError } = await supabase | ||
| .from("Challenges") | ||
| .select("*") | ||
| .eq("user_id", userId) | ||
| .order("created_at", { ascending: false }) | ||
| .limit(1) | ||
matteyu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .single(); | ||
|
|
||
matteyu marked this conversation as resolved.
Show resolved
Hide resolved
matteyu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (challengeError || !challenge) { | ||
| throw new TRPCError({ | ||
| code: "NOT_FOUND", | ||
| message: "Challenge not found", | ||
| }); | ||
| } | ||
|
|
||
| // Verify the response | ||
| const verification = await verifyRegistrationResponse({ | ||
| response: attestationResponse, | ||
| expectedChallenge: challenge.value, | ||
| expectedOrigin: relyingPartyOrigin, | ||
| expectedRPID: relyingPartyID, | ||
| }); | ||
|
|
||
| if (!verification.verified) { | ||
| throw new TRPCError({ | ||
| code: "FORBIDDEN", | ||
| message: "Verification failed", | ||
| }); | ||
| } | ||
|
|
||
| // Save the credential | ||
| const { error: saveError } = await supabase.from("AuthMethods").insert({ | ||
matteyu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| user_id: userId, | ||
| provider_id: verification.registrationInfo?.credential.id, | ||
| public_key: verification.registrationInfo?.credential.publicKey, | ||
| sign_count: verification.registrationInfo?.credential.counter, | ||
| provider_label: `Passkey created ${new Date().toLocaleString()}`, | ||
| provider_type: "PASSKEYS", | ||
| }); | ||
|
|
||
| if (saveError) { | ||
| throw new TRPCError({ | ||
| code: "INTERNAL_SERVER_ERROR", | ||
| message: saveError.message, | ||
| }); | ||
| } | ||
|
|
||
| // Delete the challenge | ||
| await supabase.from("Challenges").delete().eq("id", challenge.id); | ||
|
|
||
| return { verified: true }; | ||
| }), | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.