2020-12-02 16:00:04 +00:00
|
|
|
package webauthn
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-04-25 08:01:17 +00:00
|
|
|
"context"
|
2020-12-02 16:00:04 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/duo-labs/webauthn/protocol"
|
|
|
|
"github.com/duo-labs/webauthn/webauthn"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/logging"
|
2020-12-02 16:00:04 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/api/http"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
2020-12-02 16:00:04 +00:00
|
|
|
)
|
|
|
|
|
2022-02-14 16:22:30 +00:00
|
|
|
type Config struct {
|
2022-04-25 08:01:17 +00:00
|
|
|
DisplayName string
|
|
|
|
ExternalSecure bool
|
2020-12-02 16:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type webUser struct {
|
2021-01-15 08:32:59 +00:00
|
|
|
*domain.Human
|
2020-12-17 15:22:27 +00:00
|
|
|
accountName string
|
2020-12-02 16:00:04 +00:00
|
|
|
credentials []webauthn.Credential
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *webUser) WebAuthnID() []byte {
|
|
|
|
return []byte(u.AggregateID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *webUser) WebAuthnName() string {
|
2020-12-17 15:22:27 +00:00
|
|
|
if u.accountName != "" {
|
|
|
|
return u.accountName
|
|
|
|
}
|
2021-01-15 08:32:59 +00:00
|
|
|
return u.GetUsername()
|
2020-12-02 16:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *webUser) WebAuthnDisplayName() string {
|
2022-04-25 08:01:17 +00:00
|
|
|
if u.DisplayName != "" {
|
|
|
|
return u.DisplayName
|
|
|
|
}
|
|
|
|
return u.GetUsername()
|
2020-12-02 16:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *webUser) WebAuthnIcon() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *webUser) WebAuthnCredentials() []webauthn.Credential {
|
|
|
|
return u.credentials
|
|
|
|
}
|
|
|
|
|
2022-04-25 08:01:17 +00:00
|
|
|
func (w *Config) BeginRegistration(ctx context.Context, user *domain.Human, accountName string, authType domain.AuthenticatorAttachment, userVerification domain.UserVerificationRequirement, isLoginUI bool, webAuthNs ...*domain.WebAuthNToken) (*domain.WebAuthNToken, error) {
|
|
|
|
webAuthNServer, err := w.serverFromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-02 16:00:04 +00:00
|
|
|
creds := WebAuthNsToCredentials(webAuthNs)
|
|
|
|
existing := make([]protocol.CredentialDescriptor, len(creds))
|
|
|
|
for i, cred := range creds {
|
|
|
|
existing[i] = protocol.CredentialDescriptor{
|
|
|
|
Type: protocol.PublicKeyCredentialType,
|
|
|
|
CredentialID: cred.ID,
|
|
|
|
}
|
|
|
|
}
|
2022-04-25 08:01:17 +00:00
|
|
|
credentialOptions, sessionData, err := webAuthNServer.BeginRegistration(
|
2020-12-02 16:00:04 +00:00
|
|
|
&webUser{
|
2021-01-15 08:32:59 +00:00
|
|
|
Human: user,
|
2020-12-17 15:22:27 +00:00
|
|
|
accountName: accountName,
|
2020-12-02 16:00:04 +00:00
|
|
|
credentials: creds,
|
|
|
|
},
|
|
|
|
webauthn.WithAuthenticatorSelection(protocol.AuthenticatorSelection{
|
2021-01-15 08:32:59 +00:00
|
|
|
UserVerification: UserVerificationFromDomain(userVerification),
|
|
|
|
AuthenticatorAttachment: AuthenticatorAttachmentFromDomain(authType),
|
2020-12-02 16:00:04 +00:00
|
|
|
}),
|
|
|
|
webauthn.WithConveyancePreference(protocol.PreferNoAttestation),
|
|
|
|
webauthn.WithExclusions(existing),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, caos_errs.ThrowInternal(err, "WEBAU-bM8sd", "Errors.User.WebAuthN.BeginRegisterFailed")
|
|
|
|
}
|
|
|
|
cred, err := json.Marshal(credentialOptions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, caos_errs.ThrowInternal(err, "WEBAU-D7cus", "Errors.User.WebAuthN.MarshalError")
|
|
|
|
}
|
2021-01-15 08:32:59 +00:00
|
|
|
return &domain.WebAuthNToken{
|
2020-12-02 16:00:04 +00:00
|
|
|
Challenge: sessionData.Challenge,
|
|
|
|
CredentialCreationData: cred,
|
|
|
|
AllowedCredentialIDs: sessionData.AllowedCredentialIDs,
|
2021-01-15 08:32:59 +00:00
|
|
|
UserVerification: UserVerificationToDomain(sessionData.UserVerification),
|
2020-12-02 16:00:04 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-04-25 08:01:17 +00:00
|
|
|
func (w *Config) FinishRegistration(ctx context.Context, user *domain.Human, webAuthN *domain.WebAuthNToken, tokenName string, credData []byte, isLoginUI bool) (*domain.WebAuthNToken, error) {
|
2020-12-02 16:00:04 +00:00
|
|
|
if webAuthN == nil {
|
|
|
|
return nil, caos_errs.ThrowInternal(nil, "WEBAU-5M9so", "Errors.User.WebAuthN.NotFound")
|
|
|
|
}
|
|
|
|
credentialData, err := protocol.ParseCredentialCreationResponseBody(bytes.NewReader(credData))
|
|
|
|
if err != nil {
|
2022-03-24 13:00:24 +00:00
|
|
|
e := *err.(*protocol.Error)
|
|
|
|
logging.WithFields("error", e).Error("webauthn credential could not be parsed")
|
2020-12-02 16:00:04 +00:00
|
|
|
return nil, caos_errs.ThrowInternal(err, "WEBAU-sEr8c", "Errors.User.WebAuthN.ErrorOnParseCredential")
|
|
|
|
}
|
|
|
|
sessionData := WebAuthNToSessionData(webAuthN)
|
2022-04-25 08:01:17 +00:00
|
|
|
webAuthNServer, err := w.serverFromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
credential, err := webAuthNServer.CreateCredential(
|
2020-12-02 16:00:04 +00:00
|
|
|
&webUser{
|
2021-01-15 08:32:59 +00:00
|
|
|
Human: user,
|
2020-12-02 16:00:04 +00:00
|
|
|
},
|
2021-02-08 10:30:30 +00:00
|
|
|
sessionData,
|
|
|
|
credentialData)
|
2020-12-02 16:00:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, caos_errs.ThrowInternal(err, "WEBAU-3Vb9s", "Errors.User.WebAuthN.CreateCredentialFailed")
|
|
|
|
}
|
|
|
|
|
|
|
|
webAuthN.KeyID = credential.ID
|
|
|
|
webAuthN.PublicKey = credential.PublicKey
|
|
|
|
webAuthN.AttestationType = credential.AttestationType
|
|
|
|
webAuthN.AAGUID = credential.Authenticator.AAGUID
|
|
|
|
webAuthN.SignCount = credential.Authenticator.SignCount
|
|
|
|
webAuthN.WebAuthNTokenName = tokenName
|
|
|
|
return webAuthN, nil
|
|
|
|
}
|
|
|
|
|
2022-04-25 08:01:17 +00:00
|
|
|
func (w *Config) BeginLogin(ctx context.Context, user *domain.Human, userVerification domain.UserVerificationRequirement, isLoginUI bool, webAuthNs ...*domain.WebAuthNToken) (*domain.WebAuthNLogin, error) {
|
|
|
|
webAuthNServer, err := w.serverFromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
assertion, sessionData, err := webAuthNServer.BeginLogin(&webUser{
|
2021-01-15 08:32:59 +00:00
|
|
|
Human: user,
|
2020-12-02 16:00:04 +00:00
|
|
|
credentials: WebAuthNsToCredentials(webAuthNs),
|
2021-01-15 08:32:59 +00:00
|
|
|
}, webauthn.WithUserVerification(UserVerificationFromDomain(userVerification)))
|
2020-12-02 16:00:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, caos_errs.ThrowInternal(err, "WEBAU-4G8sw", "Errors.User.WebAuthN.BeginLoginFailed")
|
|
|
|
}
|
|
|
|
cred, err := json.Marshal(assertion)
|
|
|
|
if err != nil {
|
|
|
|
return nil, caos_errs.ThrowInternal(err, "WEBAU-2M0s9", "Errors.User.WebAuthN.MarshalError")
|
|
|
|
}
|
2021-01-15 08:32:59 +00:00
|
|
|
return &domain.WebAuthNLogin{
|
2020-12-02 16:00:04 +00:00
|
|
|
Challenge: sessionData.Challenge,
|
|
|
|
CredentialAssertionData: cred,
|
|
|
|
AllowedCredentialIDs: sessionData.AllowedCredentialIDs,
|
|
|
|
UserVerification: userVerification,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-04-25 08:01:17 +00:00
|
|
|
func (w *Config) FinishLogin(ctx context.Context, user *domain.Human, webAuthN *domain.WebAuthNLogin, credData []byte, isLoginUI bool, webAuthNs ...*domain.WebAuthNToken) ([]byte, uint32, error) {
|
2020-12-02 16:00:04 +00:00
|
|
|
assertionData, err := protocol.ParseCredentialRequestResponseBody(bytes.NewReader(credData))
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, caos_errs.ThrowInternal(err, "WEBAU-ADgv4", "Errors.User.WebAuthN.ValidateLoginFailed")
|
|
|
|
}
|
|
|
|
webUser := &webUser{
|
2021-01-15 08:32:59 +00:00
|
|
|
Human: user,
|
2020-12-02 16:00:04 +00:00
|
|
|
credentials: WebAuthNsToCredentials(webAuthNs),
|
|
|
|
}
|
2022-04-25 08:01:17 +00:00
|
|
|
webAuthNServer, err := w.serverFromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
credential, err := webAuthNServer.ValidateLogin(webUser, WebAuthNLoginToSessionData(webAuthN), assertionData)
|
2020-12-02 16:00:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, caos_errs.ThrowInternal(err, "WEBAU-3M9si", "Errors.User.WebAuthN.ValidateLoginFailed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if credential.Authenticator.CloneWarning {
|
|
|
|
return credential.ID, credential.Authenticator.SignCount, caos_errs.ThrowInternal(err, "WEBAU-4M90s", "Errors.User.WebAuthN.CloneWarning")
|
|
|
|
}
|
|
|
|
return credential.ID, credential.Authenticator.SignCount, nil
|
|
|
|
}
|
2022-04-25 08:01:17 +00:00
|
|
|
|
|
|
|
func (w *Config) serverFromContext(ctx context.Context) (*webauthn.WebAuthn, error) {
|
|
|
|
host := authz.GetInstance(ctx).RequestedDomain()
|
|
|
|
return webauthn.New(&webauthn.Config{
|
|
|
|
RPDisplayName: w.DisplayName,
|
|
|
|
RPID: host,
|
|
|
|
RPOrigin: http.BuildOrigin(host, w.ExternalSecure),
|
|
|
|
})
|
|
|
|
}
|