Documentation
¶
Index ¶
- Variables
- type AttestationPreference
- type BeginRegistrationOptions
- type ClientData
- type Config
- type CredentialParameter
- type LoginData
- type LoginResult
- type ParsedAuthData
- type PublicKeyCredential
- type PublicKeyCredentialAssertion
- type PublicKeyCredentialDescriptor
- type PublicKeyCredentialRequestOptions
- type RegistrationData
- type RegistrationResult
- type RelyingPartyEntity
- type UserEntity
- type UserVerificationRequirement
- type ValidationOutput
- type WebAuthn
- func (w *WebAuthn) BeginLogin(allowedCredentialIDs []string) (*PublicKeyCredentialRequestOptions, error)
- func (w *WebAuthn) BeginRegistration(user UserEntity) (navigator *BeginRegistrationOptions, err error)
- func (w *WebAuthn) FinishLogin(data *LoginData) (*LoginResult, error)
- func (w *WebAuthn) FinishRegistration(data RegistrationData) (*RegistrationResult, error)
- func (w *WebAuthn) ParseAuthenticatorData(authDataBytes []byte) (*ParsedAuthData, error)
- func (w *WebAuthn) ValidateLoginData(c *LoginData) (out ValidationOutput, err error)
Constants ¶
This section is empty.
Variables ¶
var ( ErrNilConfig = errors.New("config cannot be nil") ErrNilInstance = errors.New("webauthn instance is not initialized") ErrAttestationNotSupported = errors.New("unsupported attestation preference requested") ErrInvalidUserVerification = errors.New("invalid user verification preference in config") ErrInvalidRPOrigins = errors.New("invalid RP origins") ErrInvalidRPOrigin = errors.New("invalid RP origin") ErrEmptyRPID = errors.New("RP ID cannot be empty") ErrEmptyRPDisplayName = errors.New("RP display name cannot be empty") ErrInvalidTimeout = errors.New("timeout must be greater than 0") ErrParsingOrigin = errors.New("error parsing origin") ErrOriginNotAllowed = errors.New("origin not allowed") ErrFailedUnmarshalClientData = errors.New("failed to unmarshal client data") ErrTypeNotWebauthnGet = errors.New("client data type is not webauthn.get") ErrTypeNotWebauthnCreate = errors.New("client data type is not webauthn.create") ErrFailedDecodeAuthData = errors.New("failed to decode authenticator data") ErrFailedParseAuthData = errors.New("failed to parse authenticator data") ErrFailedParseClientData = errors.New("failed to parse client data") ErrFailedDecodeClientData = errors.New("failed to decode client data") ErrRPIDHashMismatch = errors.New("RP ID hash mismatch") ErrUserPresentFlagNotSet = errors.New("flag User Present not set") ErrUserVerifiedFlagNotSet = errors.New("flag User Verified not set") ErrFailedDecodeSignature = errors.New("failed to decode signature") ErrSignatureVerification = errors.New("signature verification error") ErrInvalidSignature = errors.New("invalid signature") ErrSignatureCountMismatch = errors.New("signature count mismatch") ErrGeneratingChallenge = errors.New("error generating challenge") ErrFailedDecodeAttestationObject = errors.New("failed to decode attestation object") ErrUnsupportedAttestationFormat = errors.New("unsupported attestation format received") ErrMissingPublicKey = errors.New("missing public key") ErrInvalidPublicKey = errors.New("invalid public key format") ErrMissingCredentialID = errors.New("missing credential ID") ErrAuthDataTooShort = errors.New("auth data too short, expected at least 37 bytes") ErrAuthDataTooShortAttested = errors.New("auth data too short for attested credential data header") ErrAAGUIDToUUID = errors.New("error converting AAGUID to UUID") ErrAuthDataTooShortCredentialID = errors.New("auth data too short for credential ID") ErrParsingCOSEKey = errors.New("error parsing COSE key data") ErrATFlagButNoData = errors.New("AT flag set, but no data remains for public key") ErrEDFlagButNoData = errors.New("ED flag set, but no data remains after parsing previous parts") ErrFailedDecodeExtensionData = errors.New("failed to decode extension data") ErrFailedUnmarshalPublicKeyCredential = errors.New("failed to unmarshal public key credential") ErrFailedUnmarshalPublicKeyCredentialAssertion = errors.New("failed to unmarshal public key credential assertion") )
Functions ¶
This section is empty.
Types ¶
type AttestationPreference ¶
type AttestationPreference string
AttestationPreference defines the level of attestation requested.
const ( AttestationNone AttestationPreference = "none" AttestationIndirect AttestationPreference = "indirect" AttestationPacked AttestationPreference = "packed" )
func (AttestationPreference) IsValid ¶
func (ap AttestationPreference) IsValid() bool
IsValid checks if the AttestationPreference is one of the defined constants.
type BeginRegistrationOptions ¶
type BeginRegistrationOptions struct {
Challenge string `json:"challenge"`
RP RelyingPartyEntity `json:"rp"`
User UserEntity `json:"user"`
PubKeyCredParams []CredentialParameter `json:"pubKeyCredParams"`
Timeout uint32 `json:"timeout"`
Attestation AttestationPreference `json:"attestation"`
UserVerification UserVerificationRequirement `json:"userVerification,omitempty"`
}
BeginRegistrationOptions holds options for navigator.credentials.create()
type ClientData ¶
type ClientData struct {
Type string `json:"type"`
Challenge string `json:"challenge"`
RPOrigin string `json:"origin"`
}
ClientData represents the common structure of client data in both registration and login
func (*ClientData) ParseWithB64 ¶
func (c *ClientData) ParseWithB64(jsonData string) (b64 []byte, err error)
ParseWithB64 parses client data JSON and also returns the base64 encoded version
type Config ¶
type Config struct {
RPID string // Relying Party ID (e.g., "example.com")
RPDisplayName string // Relying Party display name (e.g., "Example Corp")
RPOrigins []string // Allowed origins for RP assertions (e.g., ["https://example.com", "https://login.example.com:2137"])
Timeout uint32 // Default timeout for operations (milliseconds)
UserVerification UserVerificationRequirement // Default User Verification Requirement
Attestation AttestationPreference // Default Attestation Preference
Debug bool // Enable debug logging
}
Config holds the configuration for the WebAuthn library. Ensure RPOrigin(s) are set correctly for security checks.
type CredentialParameter ¶
CredentialParameter defines a credential parameter
type LoginResult ¶
type LoginResult struct {
NewSignCount uint32 `json:"newSignCount"`
UserVerified bool `json:"userVerified"`
}
LoginResult holds the successful result of an authentication (login) ceremony.
type ParsedAuthData ¶
type ParsedAuthData struct {
RPIDHash []byte
Flags byte
SignCount uint32
AAGUID uuid.UUID // Present if AT flag is set
CredentialID []byte // Present if AT flag is set
CredentialPubKeyBytes []byte
Extensions map[string]any // Present if ED flag is set
}
ParsedAuthData holds the structured information from the authenticator data.
type PublicKeyCredential ¶
type PublicKeyCredential struct {
ID string `json:"id"`
AttestationObject string `json:"attestationObject"`
ClientDataJSON string `json:"clientDataJSON"`
// contains filtered or unexported fields
}
func (*PublicKeyCredential) ClientData ¶
func (pkc *PublicKeyCredential) ClientData() (c *ClientData)
func (*PublicKeyCredential) Parse ¶
func (pkc *PublicKeyCredential) Parse(data []byte) (err error)
type PublicKeyCredentialAssertion ¶
type PublicKeyCredentialAssertion struct {
// Matches PublicKeyCredential structure from client Assertion
ID string `json:"id"`
Type string `json:"type"`
AuthenticatorData string `json:"authenticatorData"`
ClientDataJSON string `json:"clientDataJSON"`
Signature string `json:"signature"`
UserHandle string `json:"userHandle"`
// contains filtered or unexported fields
}
func (*PublicKeyCredentialAssertion) GetChallenge ¶
func (p *PublicKeyCredentialAssertion) GetChallenge() string
func (*PublicKeyCredentialAssertion) Parse ¶
func (p *PublicKeyCredentialAssertion) Parse(data []byte) (err error)
type PublicKeyCredentialDescriptor ¶
PublicKeyCredentialDescriptor defines allowed credentials for login
type PublicKeyCredentialRequestOptions ¶
type PublicKeyCredentialRequestOptions struct {
Challenge string `json:"challenge"`
Timeout uint32 `json:"timeout"`
RPID string `json:"rpId"`
AllowCredentials []PublicKeyCredentialDescriptor `json:"allowCredentials"`
UserVerification UserVerificationRequirement `json:"userVerification"`
}
PublicKeyCredentialRequestOptions holds options for navigator.credentials.get()
type RegistrationData ¶
type RegistrationData struct {
ClientDataJSON string `json:"clientDataJSON"`
AttestationObject string
}
RegistrationData holds the inputs for completing a registration ceremony.
type RegistrationResult ¶
type RegistrationResult struct {
CredentialID string
PublicKey []byte
AAGUID string
AuthenticatorName string
SignCount uint32
Extensions map[string]any // optional
}
RegistrationResult holds the successful result of a registration ceremony.
type RelyingPartyEntity ¶
type UserEntity ¶
type UserEntity struct {
ID []byte `json:"id"`
Name string `json:"name"`
DisplayName string `json:"displayName"`
}
UserEntity represents the user entity
type UserVerificationRequirement ¶
type UserVerificationRequirement string
UserVerificationRequirement defines the requirement level for user verification.
const ( UVRequired UserVerificationRequirement = "required" UVPreferred UserVerificationRequirement = "preferred" UVDiscouraged UserVerificationRequirement = "discouraged" )
func (UserVerificationRequirement) IsValid ¶
func (uv UserVerificationRequirement) IsValid() bool
IsValid checks if the UserVerificationRequirement is one of the defined constants.
type ValidationOutput ¶
type ValidationOutput struct {
NewSignCount uint32 `json:"newSignCount"`
UserVerified bool `json:"userVerified"`
}
ValidationOutput holds results from the internal validateAssertion method.
type WebAuthn ¶
type WebAuthn struct {
Config *Config
// contains filtered or unexported fields
}
WebAuthn struct holds the configuration and manages WebAuthn operations.
func New ¶
New creates a new WebAuthn instance with the provided configuration. It preparses and validates the RPOrigins.
func (*WebAuthn) BeginLogin ¶
func (w *WebAuthn) BeginLogin(allowedCredentialIDs []string) (*PublicKeyCredentialRequestOptions, error)
BeginLogin generates options for the login process using a pre-generated challenge. Returns options (with base64url challenge) or an error.
func (*WebAuthn) BeginRegistration ¶
func (w *WebAuthn) BeginRegistration(user UserEntity) (navigator *BeginRegistrationOptions, err error)
BeginRegistration starts the WebAuthn registration process It generates a challenge (as bytes) and returns options including the challenge encoded as a base64url string. Attestation preference is passed as a parameter. User verification preference is taken from the WebAuthn configuration. FLOW: 1. pass data
func (*WebAuthn) FinishLogin ¶
func (w *WebAuthn) FinishLogin(data *LoginData) (*LoginResult, error)
FinishLogin completes the WebAuthn login process.
func (*WebAuthn) FinishRegistration ¶
func (w *WebAuthn) FinishRegistration(data RegistrationData) (*RegistrationResult, error)
FinishRegistration completes the WebAuthn registration process FLOW 1: pass data
func (*WebAuthn) ParseAuthenticatorData ¶
func (w *WebAuthn) ParseAuthenticatorData(authDataBytes []byte) (*ParsedAuthData, error)
ParseAuthenticatorData returns the parsed data structure or an error
func (*WebAuthn) ValidateLoginData ¶
func (w *WebAuthn) ValidateLoginData(c *LoginData) (out ValidationOutput, err error)
ValidateLoginData performs the core cryptographic verification of an assertion.
