Skip to content

Commit

Permalink
fix: update to working
Browse files Browse the repository at this point in the history
  • Loading branch information
J0 committed Jul 30, 2024
1 parent 51af7b6 commit d6b8069
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 40 deletions.
17 changes: 4 additions & 13 deletions src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2346,6 +2346,7 @@ export default class GoTrueClient {
body: {
friendly_name: params.friendlyName,
factor_type: params.factorType,
web_authn: params.webAuthn,
},
headers: this.headers,
jwt: sessionData?.session?.access_token,
Expand Down Expand Up @@ -2384,16 +2385,6 @@ export default class GoTrueClient {
}
}

// TODO: find less hacky way
private hasPublicKey(
params: MFAVerifyParams
): params is { factorId: string; challengeId: string; publicKey: WebAuthnResponse } {
return (
(params as { factorId: string; challengeId: string; publicKey: WebAuthnResponse })
.publicKey !== undefined
)
}

/**
* {@see GoTrueMFAApi#verify}
*/
Expand All @@ -2406,13 +2397,13 @@ export default class GoTrueClient {
return { data: null, error: sessionError }
}
let response: { data: any; error: any } = { data: null, error: null }
if (this.hasPublicKey(params)) {
if (params.webAuthn) {
response = await _request(
this.fetch,
'POST',
`${this.url}/factors/${params.factorId}/verify`,
{
body: { challenge_id: params.challengeId, ...params?.publicKey },
body: { challenge_id: params.challengeId, web_authn: params.webAuthn },
headers: this.headers,
jwt: sessionData?.session?.access_token,
}
Expand All @@ -2430,7 +2421,6 @@ export default class GoTrueClient {
)
}
const { data, error } = response
console.log(data)
if (error) {
return { data: null, error }
}
Expand Down Expand Up @@ -2469,6 +2459,7 @@ export default class GoTrueClient {
'POST',
`${this.url}/factors/${params.factorId}/challenge`,
{
body: { web_authn: params.webAuthn },
headers: this.headers,
jwt: sessionData?.session?.access_token,
}
Expand Down
62 changes: 35 additions & 27 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,14 +805,26 @@ export type GenerateLinkType =
| 'email_change_current'
| 'email_change_new'

export type MFAEnrollParams = {
/** The type of factor being enrolled. */
factorType: 'totp' | 'webauthn'
/** Domain which the user is enrolled with. */
issuer?: string
/** Human readable name assigned to the factor. */
friendlyName?: string
}
export type MFAEnrollParams =
| {
/** The type of factor being enrolled. */
factorType: 'totp'
/** Domain which the user is enrolled with. */
issuer?: string
/** Human readable name assigned to the factor. */
friendlyName?: string
}
| {
/** The type of factor being enrolled. */
factorType: 'webauthn'
/** Domain which the user is enrolled with. */
issuer?: string
/** Human readable name assigned to the factor. */
friendlyName?: string

/** WebAuthn specific parameters*/
webAuthn?: Object
}

export type MFAUnenrollParams = {
/** ID of the factor being unenrolled. */
Expand All @@ -835,31 +847,26 @@ export type WebAuthnResponse = {
authenticatorAttachment: string
}

export type MFAVerifyParams =
| {
/** ID of the factor being verified. Returned in enroll(). */
factorId: string

/** ID of the challenge being verified. Returned in challenge(). */
challengeId: string
export type MFAVerifyParams = {
/** ID of the factor being verified. Returned in enroll(). */
factorId: string

/** Verification code provided by the user. */
code: string
}
| {
/** ID of the factor being verified. Returned in enroll(). */
factorId: string
/** ID of the challenge being verified. Returned in challenge(). */
challengeId: string

/** ID of the challenge being verified. Returned in challenge(). */
challengeId: string
/** Verification code provided by the user. */
code?: string

/** Webauthn Response */
publicKey: WebAuthnResponse
}
/** Webauthn Response */
// TODO: narrow this type
webAuthn?: Object
}

export type MFAChallengeParams = {
/** ID of the factor to be challenged. Returned in enroll(). */
factorId: string
// TODO: narrow this type
webAuthn?: Object
}

export type MFAChallengeAndVerifyParams = {
Expand Down Expand Up @@ -929,6 +936,7 @@ export type AuthMFAEnrollResponse =
public_key_credential_request_options: Object
factor_id: string
challenge_id: string
friendly_name?: string
}
error: null
}
Expand Down Expand Up @@ -960,7 +968,7 @@ export type AuthMFAChallengeResponse =
}
| {
data: {
// TODO: make thismore specific
// TODO: make the type bound tighter specific
public_key_credential_request_options: Object
id: string
}
Expand Down

0 comments on commit d6b8069

Please sign in to comment.