-
-
Notifications
You must be signed in to change notification settings - Fork 97
Add API support for Passkeys #1327
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
122 changes: 122 additions & 0 deletions
122
Auth/src/commonMain/kotlin/io/github/jan/supabase/auth/passkey/AuthPasskeyApi.kt
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,122 @@ | ||
| package io.github.jan.supabase.auth.passkey | ||
|
|
||
| import io.github.jan.supabase.auth.api.AuthenticatedSupabaseApi | ||
| import io.github.jan.supabase.auth.user.UserSession | ||
| import io.github.jan.supabase.safeBody | ||
| import kotlinx.serialization.json.Json | ||
| import kotlinx.serialization.json.buildJsonObject | ||
| import kotlinx.serialization.json.put | ||
| import kotlinx.serialization.json.putJsonObject | ||
|
|
||
| /** | ||
| * Interface for interacting with the Supabase Passkey API | ||
| */ | ||
| interface AuthPasskeyApi { | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
|
|
||
| /** | ||
| * Start passkey registration for the current authenticated user. | ||
| */ | ||
| suspend fun startRegistration(): PasskeyRegistrationResponse | ||
|
|
||
| /** | ||
| * Verify passkey registration with the credential response. | ||
| * @param challengeId Challenge ID from startRegistration | ||
| * @param credential Serialized credential | ||
| */ | ||
| suspend fun verifyRegistration(challengeId: String, credential: String): PasskeyRegistrationVerifyResponse | ||
|
|
||
| /** | ||
| * Start passkey authentication. | ||
| * @param builder Extra parameters like a captcha token | ||
| */ | ||
| suspend fun startAuthentication(builder: PasskeyAuthenticationBuilder.() -> Unit = {}): PasskeyAuthenticationOptionsResponse | ||
|
|
||
| /** | ||
| * Verify passkey authentication and create a session. | ||
| * @param challengeId Challenge ID from startAuthentication | ||
| * @param credential Serialized credential | ||
| */ | ||
| suspend fun verifyAuthentication(challengeId: String, credential: String): UserSession | ||
|
|
||
| /** | ||
| * List all passkeys for the current user. | ||
| */ | ||
| suspend fun list(): List<PasskeyListItem> | ||
|
|
||
| /** | ||
| * Update a passkey. | ||
| * @param passkeyId UUID of the passkey to delete | ||
| */ | ||
| suspend fun delete(passkeyId: String) | ||
|
|
||
| /** | ||
| * Delete a passkey. | ||
| * @param passkeyId UUID of the passkey to update | ||
| * @param friendlyName New friendly name (max 120 chars) | ||
| */ | ||
| suspend fun update(passkeyId: String, friendlyName: String): PasskeyListItem | ||
|
|
||
| } | ||
|
|
||
| internal class AuthPasskeyApiImpl( | ||
| private val api: AuthenticatedSupabaseApi, | ||
| private val saveSession: suspend (UserSession) -> Unit, | ||
| ): AuthPasskeyApi { | ||
|
|
||
| override suspend fun startRegistration(): PasskeyRegistrationResponse { | ||
| val result = api.post("registration/options") | ||
| return result.safeBody() | ||
| } | ||
|
|
||
| override suspend fun verifyRegistration( | ||
| challengeId: String, | ||
| credential: String | ||
| ): PasskeyRegistrationVerifyResponse { | ||
| return api.postJson("registration/verify", buildJsonObject { | ||
| put("challenge_id", challengeId) | ||
| put("credential", Json.decodeFromString(credential)) | ||
| }).safeBody() | ||
| } | ||
|
|
||
| override suspend fun startAuthentication(builder: PasskeyAuthenticationBuilder.() -> Unit): PasskeyAuthenticationOptionsResponse { | ||
| return api.postJson("authentication/options", buildJsonObject { | ||
| val builder = PasskeyAuthenticationBuilder().apply(builder) | ||
| builder.captchaToken?.let { | ||
| putJsonObject("gotrue_meta_security") { | ||
| put("captcha_token", it) | ||
| } | ||
| } | ||
| }).safeBody() | ||
| } | ||
|
|
||
| override suspend fun verifyAuthentication( | ||
| challengeId: String, | ||
| credential: String | ||
| ): UserSession { | ||
| return api.postJson("authentication/verify", buildJsonObject { | ||
| put("challenge_id", challengeId) | ||
| put("credential", Json.decodeFromString(credential)) | ||
| }).safeBody<UserSession>().also { | ||
| saveSession(it) | ||
| } | ||
| } | ||
|
|
||
| override suspend fun list(): List<PasskeyListItem> { | ||
| return api.get("").safeBody() | ||
| } | ||
|
|
||
| override suspend fun delete(passkeyId: String) { | ||
| api.delete(passkeyId) | ||
| } | ||
|
|
||
| override suspend fun update( | ||
| passkeyId: String, | ||
| friendlyName: String | ||
| ): PasskeyListItem { | ||
| return api.patchJson(passkeyId, buildJsonObject { | ||
| put("friendly_name", friendlyName) | ||
| }).safeBody() | ||
| } | ||
|
|
||
|
|
||
| } | ||
9 changes: 9 additions & 0 deletions
9
...src/commonMain/kotlin/io/github/jan/supabase/auth/passkey/PasskeyAuthenticationBuilder.kt
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,9 @@ | ||
| package io.github.jan.supabase.auth.passkey | ||
|
|
||
| /** | ||
| * Builder for [AuthPasskeyApi.startAuthentication] | ||
| * @param captchaToken An optional captcha token for the authentication | ||
| */ | ||
| data class PasskeyAuthenticationBuilder( | ||
| var captchaToken: String? = null | ||
| ) |
20 changes: 20 additions & 0 deletions
20
...onMain/kotlin/io/github/jan/supabase/auth/passkey/PasskeyAuthenticationOptionsResponse.kt
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,20 @@ | ||
| package io.github.jan.supabase.auth.passkey | ||
|
|
||
| import io.github.jan.supabase.serializer.UnixTimestampSerializer | ||
| import kotlinx.serialization.SerialName | ||
| import kotlinx.serialization.Serializable | ||
| import kotlinx.serialization.json.JsonObject | ||
| import kotlin.time.Instant | ||
|
|
||
| /** | ||
| * The response for [AuthPasskeyApi.startAuthentication] | ||
| * @param challengeId The challenge id | ||
| * @param options Server options | ||
| * @param expiresAt When the authentication session expires | ||
| */ | ||
| @Serializable | ||
| data class PasskeyAuthenticationOptionsResponse( | ||
| @SerialName("challenge_id") val challengeId: String, | ||
| val options: JsonObject, | ||
| @SerialName("expires_at") @Serializable(UnixTimestampSerializer::class) val expiresAt: Instant | ||
| ) |
20 changes: 20 additions & 0 deletions
20
Auth/src/commonMain/kotlin/io/github/jan/supabase/auth/passkey/PasskeyListItem.kt
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,20 @@ | ||
| package io.github.jan.supabase.auth.passkey | ||
|
|
||
| import kotlinx.serialization.SerialName | ||
| import kotlinx.serialization.Serializable | ||
| import kotlin.time.Instant | ||
|
|
||
| /** | ||
| * A passkey item for [AuthPasskeyApi.list] | ||
| * @param id The uuid for the passkey | ||
| * @param friendlyName The friendly name of the passkey | ||
| * @param createdAt When the passkey was created at | ||
| * @param lastUsedAt When the passkey was last used at | ||
| */ | ||
| @Serializable | ||
| data class PasskeyListItem( | ||
| val id: String, | ||
| @SerialName("friendly_name") val friendlyName: String? = null, | ||
| @SerialName("created_at") val createdAt: Instant, | ||
| @SerialName("last_used_at") val lastUsedAt: Instant? = null | ||
| ) |
19 changes: 19 additions & 0 deletions
19
.../src/commonMain/kotlin/io/github/jan/supabase/auth/passkey/PasskeyRegistrationResponse.kt
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,19 @@ | ||
| package io.github.jan.supabase.auth.passkey | ||
|
|
||
| import kotlinx.serialization.SerialName | ||
| import kotlinx.serialization.Serializable | ||
| import kotlinx.serialization.json.JsonObject | ||
| import kotlin.time.Instant | ||
|
|
||
| /** | ||
| * Response for [AuthPasskeyApi.startRegistration] | ||
| * @param challengeId The challenge id | ||
| * @param options The server options | ||
| * @param expiresAt When the registration expires at | ||
| */ | ||
| @Serializable | ||
| data class PasskeyRegistrationResponse( | ||
| @SerialName("challenge_id") val challengeId: String, | ||
| val options: JsonObject, | ||
| @SerialName("expires_at") val expiresAt: Instant | ||
| ) |
18 changes: 18 additions & 0 deletions
18
...ommonMain/kotlin/io/github/jan/supabase/auth/passkey/PasskeyRegistrationVerifyResponse.kt
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,18 @@ | ||
| package io.github.jan.supabase.auth.passkey | ||
|
|
||
| import kotlinx.serialization.SerialName | ||
| import kotlinx.serialization.Serializable | ||
| import kotlin.time.Instant | ||
|
|
||
| /** | ||
| * Response for [AuthPasskeyApi.verifyRegistration] | ||
| * @param id The uuid of the passkey | ||
| * @param friendlyName The friendly name of the passkey | ||
| * @param createdAt When the passkey was created at | ||
| */ | ||
| @Serializable | ||
| data class PasskeyRegistrationVerifyResponse( | ||
| val id: String, | ||
| @SerialName("friendly_name") val friendlyName: String? = null, | ||
| @SerialName("created_at") val createdAt: Instant | ||
| ) |
Oops, something went wrong.
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.