authelia/internal/handlers/webauthn.go

117 lines
3.9 KiB
Go
Raw Normal View History

package handlers
import (
"net/url"
"strings"
"github.com/go-webauthn/webauthn/protocol"
"github.com/go-webauthn/webauthn/webauthn"
"github.com/authelia/authelia/v4/internal/middlewares"
"github.com/authelia/authelia/v4/internal/model"
2023-02-16 19:40:40 +00:00
"github.com/authelia/authelia/v4/internal/random"
)
2023-02-19 01:40:57 +00:00
func getWebauthnUserByRPID(ctx *middlewares.AutheliaCtx, username, displayname string, rpid string) (user *model.WebauthnUser, err error) {
2023-02-16 19:40:40 +00:00
if user, err = ctx.Providers.StorageProvider.LoadWebauthnUser(ctx, rpid, username); err != nil {
return nil, err
}
if user == nil {
user = &model.WebauthnUser{
RPID: rpid,
Username: username,
UserID: ctx.Providers.Random.StringCustom(64, random.CharSetASCII),
2023-02-19 01:40:57 +00:00
DisplayName: displayname,
2023-02-16 19:40:40 +00:00
}
2023-02-11 15:47:03 +00:00
2023-02-16 19:40:40 +00:00
if err = ctx.Providers.StorageProvider.SaveWebauthnUser(ctx, *user); err != nil {
return nil, err
}
2023-02-19 01:40:57 +00:00
} else {
user.DisplayName = displayname
}
if user.DisplayName == "" {
user.DisplayName = user.Username
}
2023-02-14 02:53:57 +00:00
if user.Devices, err = ctx.Providers.StorageProvider.LoadWebauthnDevicesByUsername(ctx, rpid, user.Username); err != nil {
return nil, err
}
return user, nil
}
func newWebauthn(ctx *middlewares.AutheliaCtx) (w *webauthn.WebAuthn, err error) {
var (
2023-02-11 15:47:03 +00:00
origin *url.URL
)
2023-02-11 15:47:03 +00:00
if origin, err = ctx.GetOrigin(); err != nil {
return nil, err
}
config := &webauthn.Config{
2023-02-19 00:48:35 +00:00
RPID: origin.Hostname(),
RPDisplayName: ctx.Configuration.Webauthn.DisplayName,
RPOrigins: []string{origin.String()},
AttestationPreference: ctx.Configuration.Webauthn.ConveyancePreference,
AuthenticatorSelection: protocol.AuthenticatorSelection{
AuthenticatorAttachment: protocol.CrossPlatform,
RequireResidentKey: protocol.ResidentKeyNotRequired(),
2023-02-19 00:48:35 +00:00
ResidentKey: protocol.ResidentKeyRequirementDiscouraged,
UserVerification: ctx.Configuration.Webauthn.UserVerification,
},
Debug: false,
EncodeUserIDAsString: true,
Timeouts: webauthn.TimeoutsConfig{
Login: webauthn.TimeoutConfig{
Enforce: true,
Timeout: ctx.Configuration.Webauthn.Timeout,
TimeoutUVD: ctx.Configuration.Webauthn.Timeout,
},
Registration: webauthn.TimeoutConfig{
Enforce: true,
Timeout: ctx.Configuration.Webauthn.Timeout,
TimeoutUVD: ctx.Configuration.Webauthn.Timeout,
},
},
}
ctx.Logger.Tracef("Creating new Webauthn RP instance with ID %s and Origins %s", config.RPID, strings.Join(config.RPOrigins, ", "))
return webauthn.New(config)
}
2023-02-18 04:36:58 +00:00
func webauthnCredentialCreationIsDiscoverable(ctx *middlewares.AutheliaCtx, response *protocol.ParsedCredentialCreationData) (discoverable bool) {
if value, ok := response.ClientExtensionResults["credProps"]; ok {
switch credentialProperties := value.(type) {
case map[string]any:
var v any
if v, ok = credentialProperties["rk"]; ok {
if discoverable, ok = v.(bool); ok {
ctx.Logger.WithFields(map[string]any{"discoverable": discoverable}).Trace("Determined Credential Discoverability via Client Extension Results")
return discoverable
} else {
ctx.Logger.WithFields(map[string]any{"discoverable": false}).Trace("Assuming Credential Discoverability is false as the 'rk' field for the 'credProps' extension in the Client Extension Results was not a boolean")
}
} else {
ctx.Logger.WithFields(map[string]any{"discoverable": false}).Trace("Assuming Credential Discoverability is false as the 'rk' field for the 'credProps' extension was missing from the Client Extension Results")
}
return false
default:
ctx.Logger.WithFields(map[string]any{"discoverable": false}).Trace("Assuming Credential Discoverability is false as the 'credProps' extension in the Client Extension Results does not appear to be a dictionary")
return false
}
}
ctx.Logger.WithFields(map[string]any{"discoverable": false}).Trace("Assuming Credential Discoverability is false as the 'credProps' extension is missing from the Client Extension Results")
return false
}