Skip to content

Commit

Permalink
fix: add types for assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
J0 committed Sep 19, 2024
1 parent 41c22fb commit 2586cac
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2452,14 +2452,14 @@ export default class GoTrueClient {
await this._notifyAllSubscribers('MFA_CHALLENGE_VERIFIED', data)
return { data, error }
} else if ('factorType' in params && params.factorType === 'webauthn') {
// Single Step enroll
// TODO: Replace the placeholder
const { data, error } = await _request(
this.fetch,
'POST',
`${this.url}/factors/verify`,
{
body: {
use_multi_step: params.useMultiStep,
factorType: params.factorType,
},
headers: this.headers,
Expand Down
174 changes: 168 additions & 6 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -844,13 +844,17 @@ export type MFAVerifyTOTPParams = {
// Declared as a separate type to allow for future changes
export type MFAVerifyPhoneParams = MFAVerifyTOTPParams

export type MFAVerifyWebAuthnParams = {
/** The type of factor being enrolled. */
factorType: 'webauthn'
export type MFAVerifyWebAuthnParams =
| {
/** The type of factor being enrolled. */
factorType: 'webauthn'
}
| {
// TODO: define the type for this
factorId: string

/** Have the Auth client library handle the browser-authenticator interaction for you */
useMultiStep?: boolean
}
credential: Object
}

export type MFAEnrollParams = MFAEnrollTOTPParams | MFAEnrollPhoneParams | MFAEnrollWebAuthnParams

Expand Down Expand Up @@ -1244,3 +1248,161 @@ export type SignOut = {
*/
scope?: 'global' | 'local' | 'others'
}

/**
* Available only in secure contexts.
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/AuthenticatorAssertionResponse)
*/
export interface AuthenticatorAssertionResponse extends AuthenticatorResponse {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/AuthenticatorAssertionResponse/authenticatorData) */
readonly authenticatorData: ArrayBuffer
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/AuthenticatorAssertionResponse/signature) */
readonly signature: ArrayBuffer
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/AuthenticatorAssertionResponse/userHandle) */
readonly userHandle: ArrayBuffer | null
}

/**
* Available only in secure contexts.
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/AuthenticatorAttestationResponse)
*/
export interface AuthenticatorAttestationResponse extends AuthenticatorResponse {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/AuthenticatorAttestationResponse/attestationObject) */
readonly attestationObject: ArrayBuffer
getAuthenticatorData(): ArrayBuffer
getPublicKey(): ArrayBuffer | null
getPublicKeyAlgorithm(): COSEAlgorithmIdentifier
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/AuthenticatorAttestationResponse/getTransports) */
getTransports(): string[]
}

export interface AuthenticationExtensionsClientInputs {
appid?: string
credProps?: boolean
hmacCreateSecret?: boolean
}

export interface AuthenticationExtensionsClientOutputs {
appid?: boolean
credProps?: CredentialPropertiesOutput
hmacCreateSecret?: boolean
}

export interface AuthenticatorSelectionCriteria {
authenticatorAttachment?: AuthenticatorAttachment
requireResidentKey?: boolean
residentKey?: ResidentKeyRequirement
userVerification?: UserVerificationRequirement
}

/**
* Available only in secure contexts.
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/PublicKeyCredential)
*/
export interface PublicKeyCredential extends Credential {
readonly authenticatorAttachment: string | null
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/PublicKeyCredential/rawId) */
readonly rawId: ArrayBuffer
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/PublicKeyCredential/response) */
readonly response: AuthenticatorResponse
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/PublicKeyCredential/getClientExtensionResults) */
getClientExtensionResults(): AuthenticationExtensionsClientOutputs
}

export interface PublicKeyCredentialCreationOptions {
attestation?: AttestationConveyancePreference
authenticatorSelection?: AuthenticatorSelectionCriteria
challenge: BufferSource
excludeCredentials?: PublicKeyCredentialDescriptor[]
extensions?: AuthenticationExtensionsClientInputs
pubKeyCredParams: PublicKeyCredentialParameters[]
rp: PublicKeyCredentialRpEntity
timeout?: number
user: PublicKeyCredentialUserEntity
}

export interface PublicKeyCredentialDescriptor {
id: BufferSource
transports?: AuthenticatorTransport[]
type: PublicKeyCredentialType
}

export interface PublicKeyCredentialParameters {
alg: COSEAlgorithmIdentifier
type: PublicKeyCredentialType
}

export interface PublicKeyCredentialRequestOptions {
allowCredentials?: PublicKeyCredentialDescriptor[]
challenge: BufferSource
extensions?: AuthenticationExtensionsClientInputs
rpId?: string
timeout?: number
userVerification?: UserVerificationRequirement
}

export interface PublicKeyCredentialUserEntity extends PublicKeyCredentialEntity {
displayName: string
id: BufferSource
}

/**
* Available only in secure contexts.
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/AuthenticatorResponse)
*/
export interface AuthenticatorResponse {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/AuthenticatorResponse/clientDataJSON) */
readonly clientDataJSON: ArrayBuffer
}

export interface CredentialPropertiesOutput {
rk?: boolean
}

/**
* Available only in secure contexts.
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Credential)
*/
export interface Credential {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Credential/id) */
readonly id: string
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Credential/type) */
readonly type: string
}

export interface PublicKeyCredentialRpEntity extends PublicKeyCredentialEntity {
id?: string
}

export interface PublicKeyCredentialEntity {
name: string
}

export type AttestationConveyancePreference = 'direct' | 'enterprise' | 'indirect' | 'none'
export type AuthenticatorTransport = 'ble' | 'hybrid' | 'internal' | 'nfc' | 'usb'
export type COSEAlgorithmIdentifier = number
export type UserVerificationRequirement = 'discouraged' | 'preferred' | 'required'
export type AuthenticatorAttachment = 'cross-platform' | 'platform'
export type ResidentKeyRequirement = 'discouraged' | 'preferred' | 'required'
export type BufferSource = ArrayBufferView | ArrayBuffer
export type PublicKeyCredentialType = 'public-key'
export type AlgorithmIdentifier = Algorithm | string
export type KeyUsage =
| 'decrypt'
| 'deriveBits'
| 'deriveKey'
| 'encrypt'
| 'sign'
| 'unwrapKey'
| 'verify'
| 'wrapKey'
export type KeyFormat = 'jwk' | 'pkcs8' | 'raw' | 'spki'
export type KeyType = 'private' | 'public' | 'secret'
export type HashAlgorithmIdentifier = AlgorithmIdentifier
export type NamedCurve = string
export type BigInteger = Uint8Array

0 comments on commit 2586cac

Please sign in to comment.