Skip to content
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

feat: add with setters for appid related extensions #11

Merged
merged 1 commit into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion protocol/attestation_u2f.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/go-webauthn/webauthn/protocol/webauthncose"
)

var u2fAttestationKey = "fido-u2f"
var u2fAttestationKey = CredentialTypeFIDOU2F

func init() {
RegisterAttestationFormat(u2fAttestationKey, verifyU2FFormat)
Expand Down
10 changes: 7 additions & 3 deletions protocol/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,11 @@ func (ppkc ParsedPublicKeyCredential) GetAppID(authExt AuthenticationExtensions,

// If the credential does not have the correct attestation type it is assumed to NOT be a fido-u2f credential.
// https://w3c.github.io/webauthn/#sctn-fido-u2f-attestation
if credentialAttestationType != "fido-u2f" {
if credentialAttestationType != CredentialTypeFIDOU2F {
return "", nil
}

if clientValue, ok = ppkc.ClientExtensionResults["appid"]; !ok {
if clientValue, ok = ppkc.ClientExtensionResults[ExtensionAppID]; !ok {
return "", nil
}

Expand All @@ -214,7 +214,7 @@ func (ppkc ParsedPublicKeyCredential) GetAppID(authExt AuthenticationExtensions,
return "", nil
}

if value, ok = authExt["appid"]; !ok {
if value, ok = authExt[ExtensionAppID]; !ok {
return "", ErrBadRequest.WithDetails("Session Data does not have an appid but Client Output indicates it should be set")
}

Expand All @@ -224,3 +224,7 @@ func (ppkc ParsedPublicKeyCredential) GetAppID(authExt AuthenticationExtensions,

return appID, nil
}

const (
CredentialTypeFIDOU2F = "fido-u2f"
)
5 changes: 5 additions & 0 deletions protocol/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ package protocol
// (https://www.w3.org/TR/webauthn/#sctn-defined-extensions).

type AuthenticationExtensionsClientOutputs map[string]interface{}

const (
ExtensionAppID = "appid"
ExtensionAppIDExclude = "appidExclude"
)
3 changes: 3 additions & 0 deletions protocol/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ type CredentialDescriptor struct {
CredentialID []byte `json:"id"`
// The authenticator transports that can be used
Transport []AuthenticatorTransport `json:"transports,omitempty"`

// The AttestationType from the Credential. Used internally only.
AttestationType string `json:"-"`
}

// CredentialParameter is the credential type and algorithm
Expand Down
10 changes: 10 additions & 0 deletions webauthn/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ type Credential struct {
Authenticator Authenticator
}

// ToCredentialDescriptor converts a Credential into a protocol.CredentialDescriptor.
func (c Credential) ToCredentialDescriptor() (descriptor protocol.CredentialDescriptor) {
return protocol.CredentialDescriptor{
CredentialID: c.ID,
Transport: c.Transport,
Type: protocol.PublicKeyCredentialType,
AttestationType: c.AttestationType,
}
}

// MakeNewCredential will return a credential pointer on successful validation of a registration response
func MakeNewCredential(c *protocol.ParsedCredentialCreationData) (*Credential, error) {
newCredential := &Credential{
Expand Down
16 changes: 16 additions & 0 deletions webauthn/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ func WithAssertionExtensions(extensions protocol.AuthenticationExtensions) Login
}
}

// WithAppIdExtension automatically includes the specified appid if the AllowedCredentials contains a credential
// with the type `fido-u2f`.
func WithAppIdExtension(appid string) LoginOption {
return func(cco *protocol.PublicKeyCredentialRequestOptions) {
for _, credential := range cco.AllowedCredentials {
if credential.AttestationType == protocol.CredentialTypeFIDOU2F {
if cco.Extensions == nil {
cco.Extensions = map[string]interface{}{}
}

cco.Extensions[protocol.ExtensionAppID] = appid
}
}
}
}

// Take the response from the client and validate it against the user credentials and stored session data
func (webauthn *WebAuthn) FinishLogin(user User, session SessionData, response *http.Request) (*Credential, error) {
parsedResponse, err := protocol.ParseCredentialRequestResponse(response)
Expand Down
16 changes: 16 additions & 0 deletions webauthn/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ func WithExtensions(extension protocol.AuthenticationExtensions) RegistrationOpt
}
}

// WithAppIdExcludeExtension automatically includes the specified appid if the CredentialExcludeList contains a credential
// with the type `fido-u2f`.
func WithAppIdExcludeExtension(appid string) RegistrationOption {
return func(cco *protocol.PublicKeyCredentialCreationOptions) {
for _, credential := range cco.CredentialExcludeList {
if credential.AttestationType == protocol.CredentialTypeFIDOU2F {
if cco.Extensions == nil {
cco.Extensions = map[string]interface{}{}
}

cco.Extensions[protocol.ExtensionAppIDExclude] = appid
}
}
}
}

// Take the response from the authenticator and client and verify the credential against the user's credentials and
// session data.
func (webauthn *WebAuthn) FinishRegistration(user User, session SessionData, response *http.Request) (*Credential, error) {
Expand Down