2022-03-06 05:47:40 +00:00
|
|
|
package model
|
2022-03-03 11:20:43 +00:00
|
|
|
|
|
|
|
import (
|
2022-10-20 02:16:36 +00:00
|
|
|
"database/sql"
|
2022-12-23 04:00:23 +00:00
|
|
|
"encoding/base64"
|
2022-03-03 11:20:43 +00:00
|
|
|
"encoding/hex"
|
2022-11-14 02:19:18 +00:00
|
|
|
"encoding/json"
|
2022-03-03 11:20:43 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-03-03 23:46:38 +00:00
|
|
|
"github.com/go-webauthn/webauthn/protocol"
|
|
|
|
"github.com/go-webauthn/webauthn/webauthn"
|
2022-03-03 11:20:43 +00:00
|
|
|
"github.com/google/uuid"
|
2022-12-23 04:00:23 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2022-03-03 11:20:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
attestationTypeFIDOU2F = "fido-u2f"
|
|
|
|
)
|
|
|
|
|
|
|
|
// WebauthnUser is an object to represent a user for the Webauthn lib.
|
|
|
|
type WebauthnUser struct {
|
|
|
|
Username string
|
|
|
|
DisplayName string
|
|
|
|
Devices []WebauthnDevice
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasFIDOU2F returns true if the user has any attestation type `fido-u2f` devices.
|
|
|
|
func (w WebauthnUser) HasFIDOU2F() bool {
|
|
|
|
for _, c := range w.Devices {
|
|
|
|
if c.AttestationType == attestationTypeFIDOU2F {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// WebAuthnID implements the webauthn.User interface.
|
|
|
|
func (w WebauthnUser) WebAuthnID() []byte {
|
|
|
|
return []byte(w.Username)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WebAuthnName implements the webauthn.User interface.
|
|
|
|
func (w WebauthnUser) WebAuthnName() string {
|
|
|
|
return w.Username
|
|
|
|
}
|
|
|
|
|
|
|
|
// WebAuthnDisplayName implements the webauthn.User interface.
|
|
|
|
func (w WebauthnUser) WebAuthnDisplayName() string {
|
|
|
|
return w.DisplayName
|
|
|
|
}
|
|
|
|
|
|
|
|
// WebAuthnIcon implements the webauthn.User interface.
|
|
|
|
func (w WebauthnUser) WebAuthnIcon() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// WebAuthnCredentials implements the webauthn.User interface.
|
|
|
|
func (w WebauthnUser) WebAuthnCredentials() (credentials []webauthn.Credential) {
|
|
|
|
credentials = make([]webauthn.Credential, len(w.Devices))
|
|
|
|
|
|
|
|
var credential webauthn.Credential
|
|
|
|
|
|
|
|
for i, device := range w.Devices {
|
|
|
|
aaguid, err := device.AAGUID.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
credential = webauthn.Credential{
|
|
|
|
ID: device.KID.Bytes(),
|
|
|
|
PublicKey: device.PublicKey,
|
|
|
|
AttestationType: device.AttestationType,
|
2023-02-14 02:53:57 +00:00
|
|
|
Flags: webauthn.CredentialFlags{
|
|
|
|
UserPresent: device.Present,
|
|
|
|
UserVerified: device.Verified,
|
|
|
|
BackupEligible: device.BackupEligible,
|
|
|
|
BackupState: device.BackupState,
|
|
|
|
},
|
2022-03-03 11:20:43 +00:00
|
|
|
Authenticator: webauthn.Authenticator{
|
|
|
|
AAGUID: aaguid,
|
|
|
|
SignCount: device.SignCount,
|
|
|
|
CloneWarning: device.CloneWarning,
|
2023-02-14 02:53:57 +00:00
|
|
|
Attachment: protocol.AuthenticatorAttachment(device.Attachment),
|
2022-03-03 11:20:43 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
transports := strings.Split(device.Transport, ",")
|
|
|
|
credential.Transport = []protocol.AuthenticatorTransport{}
|
|
|
|
|
|
|
|
for _, t := range transports {
|
|
|
|
if t == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
credential.Transport = append(credential.Transport, protocol.AuthenticatorTransport(t))
|
|
|
|
}
|
|
|
|
|
|
|
|
credentials[i] = credential
|
|
|
|
}
|
|
|
|
|
|
|
|
return credentials
|
|
|
|
}
|
|
|
|
|
|
|
|
// WebAuthnCredentialDescriptors decodes the users credentials into protocol.CredentialDescriptor's.
|
|
|
|
func (w WebauthnUser) WebAuthnCredentialDescriptors() (descriptors []protocol.CredentialDescriptor) {
|
|
|
|
credentials := w.WebAuthnCredentials()
|
|
|
|
|
|
|
|
descriptors = make([]protocol.CredentialDescriptor, len(credentials))
|
|
|
|
|
|
|
|
for i, credential := range credentials {
|
|
|
|
descriptors[i] = credential.Descriptor()
|
|
|
|
}
|
|
|
|
|
|
|
|
return descriptors
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewWebauthnDeviceFromCredential creates a WebauthnDevice from a webauthn.Credential.
|
2023-02-14 02:53:57 +00:00
|
|
|
func NewWebauthnDeviceFromCredential(rpid, username, displayname string, credential *webauthn.Credential) (device WebauthnDevice) {
|
2022-03-03 11:20:43 +00:00
|
|
|
transport := make([]string, len(credential.Transport))
|
|
|
|
|
|
|
|
for i, t := range credential.Transport {
|
|
|
|
transport[i] = string(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
device = WebauthnDevice{
|
|
|
|
RPID: rpid,
|
|
|
|
Username: username,
|
|
|
|
CreatedAt: time.Now(),
|
2023-02-14 02:53:57 +00:00
|
|
|
DisplayName: displayname,
|
2022-03-03 11:20:43 +00:00
|
|
|
KID: NewBase64(credential.ID),
|
|
|
|
AttestationType: credential.AttestationType,
|
2023-02-14 02:53:57 +00:00
|
|
|
Attachment: string(credential.Authenticator.Attachment),
|
|
|
|
Transport: strings.Join(transport, ","),
|
2022-03-03 11:20:43 +00:00
|
|
|
SignCount: credential.Authenticator.SignCount,
|
|
|
|
CloneWarning: credential.Authenticator.CloneWarning,
|
2023-02-14 02:53:57 +00:00
|
|
|
Discoverable: false,
|
|
|
|
Present: credential.Flags.UserPresent,
|
|
|
|
Verified: credential.Flags.UserVerified,
|
|
|
|
BackupEligible: credential.Flags.BackupEligible,
|
|
|
|
BackupState: credential.Flags.BackupState,
|
|
|
|
PublicKey: credential.PublicKey,
|
2022-03-03 11:20:43 +00:00
|
|
|
}
|
|
|
|
|
2022-11-19 06:42:03 +00:00
|
|
|
aaguid, err := uuid.Parse(hex.EncodeToString(credential.Authenticator.AAGUID))
|
|
|
|
if err == nil && aaguid.ID() != 0 {
|
|
|
|
device.AAGUID = uuid.NullUUID{Valid: true, UUID: aaguid}
|
|
|
|
}
|
2022-03-03 11:20:43 +00:00
|
|
|
|
|
|
|
return device
|
|
|
|
}
|
|
|
|
|
2022-12-12 01:21:27 +00:00
|
|
|
// WebauthnDeviceJSON represents a Webauthn Device in the JSON format.
|
|
|
|
type WebauthnDeviceJSON struct {
|
|
|
|
ID int `json:"id"`
|
|
|
|
CreatedAt time.Time `json:"created_at"`
|
|
|
|
LastUsedAt *time.Time `json:"last_used_at,omitempty"`
|
|
|
|
RPID string `json:"rpid"`
|
2023-02-14 02:53:57 +00:00
|
|
|
DisplayName string `json:"displayname"`
|
2022-12-12 01:21:27 +00:00
|
|
|
KID []byte `json:"kid"`
|
2023-02-14 02:53:57 +00:00
|
|
|
AAGUID string `json:"aaguid,omitempty"`
|
|
|
|
Attachment string `json:"attachment"`
|
2022-12-12 01:21:27 +00:00
|
|
|
AttestationType string `json:"attestation_type"`
|
|
|
|
Transports []string `json:"transports"`
|
|
|
|
SignCount uint32 `json:"sign_count"`
|
|
|
|
CloneWarning bool `json:"clone_warning"`
|
2023-02-14 02:53:57 +00:00
|
|
|
Discoverable bool `json:"discoverable"`
|
|
|
|
Present bool `json:"present"`
|
|
|
|
Verified bool `json:"verified"`
|
|
|
|
BackupEligible bool `json:"backup_eligible"`
|
|
|
|
BackupState bool `json:"backup_state"`
|
|
|
|
PublicKey []byte `json:"public_key"`
|
2022-12-12 01:21:27 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 11:20:43 +00:00
|
|
|
// WebauthnDevice represents a Webauthn Device in the database storage.
|
|
|
|
type WebauthnDevice struct {
|
2022-11-19 06:42:03 +00:00
|
|
|
ID int `db:"id"`
|
|
|
|
CreatedAt time.Time `db:"created_at"`
|
|
|
|
LastUsedAt sql.NullTime `db:"last_used_at"`
|
|
|
|
RPID string `db:"rpid"`
|
|
|
|
Username string `db:"username"`
|
2023-02-14 02:53:57 +00:00
|
|
|
DisplayName string `db:"displayname"`
|
2022-11-19 06:42:03 +00:00
|
|
|
KID Base64 `db:"kid"`
|
2023-02-14 02:53:57 +00:00
|
|
|
AAGUID uuid.NullUUID `db:"aaguid"`
|
2022-11-19 06:42:03 +00:00
|
|
|
AttestationType string `db:"attestation_type"`
|
2023-02-14 02:53:57 +00:00
|
|
|
Attachment string `db:"attachment"`
|
2022-11-19 06:42:03 +00:00
|
|
|
Transport string `db:"transport"`
|
|
|
|
SignCount uint32 `db:"sign_count"`
|
|
|
|
CloneWarning bool `db:"clone_warning"`
|
2023-02-14 02:53:57 +00:00
|
|
|
Discoverable bool `db:"discoverable"`
|
|
|
|
Present bool `db:"present"`
|
|
|
|
Verified bool `db:"verified"`
|
|
|
|
BackupEligible bool `db:"backup_eligible"`
|
|
|
|
BackupState bool `db:"backup_state"`
|
|
|
|
PublicKey []byte `db:"public_key"`
|
2022-11-14 02:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON returns the WebauthnDevice in a JSON friendly manner.
|
|
|
|
func (w *WebauthnDevice) MarshalJSON() (data []byte, err error) {
|
2022-12-12 01:21:27 +00:00
|
|
|
o := WebauthnDeviceJSON{
|
2022-11-14 02:19:18 +00:00
|
|
|
ID: w.ID,
|
|
|
|
CreatedAt: w.CreatedAt,
|
|
|
|
RPID: w.RPID,
|
2023-02-14 02:53:57 +00:00
|
|
|
DisplayName: w.DisplayName,
|
2022-11-14 02:19:18 +00:00
|
|
|
KID: w.KID.data,
|
|
|
|
AttestationType: w.AttestationType,
|
2023-02-14 02:53:57 +00:00
|
|
|
Attachment: w.Attachment,
|
2022-11-14 02:19:18 +00:00
|
|
|
Transports: []string{},
|
|
|
|
SignCount: w.SignCount,
|
|
|
|
CloneWarning: w.CloneWarning,
|
2023-02-14 02:53:57 +00:00
|
|
|
Discoverable: w.Discoverable,
|
|
|
|
Present: w.Present,
|
|
|
|
Verified: w.Verified,
|
|
|
|
BackupEligible: w.BackupEligible,
|
|
|
|
BackupState: w.BackupState,
|
|
|
|
PublicKey: w.PublicKey,
|
2022-11-14 02:19:18 +00:00
|
|
|
}
|
|
|
|
|
2022-11-19 06:42:03 +00:00
|
|
|
if w.AAGUID.Valid {
|
|
|
|
o.AAGUID = w.AAGUID.UUID.String()
|
|
|
|
}
|
|
|
|
|
2022-11-14 02:19:18 +00:00
|
|
|
if w.Transport != "" {
|
|
|
|
o.Transports = strings.Split(w.Transport, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.LastUsedAt.Valid {
|
|
|
|
o.LastUsedAt = &w.LastUsedAt.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(o)
|
2022-03-03 11:20:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateSignInInfo adjusts the values of the WebauthnDevice after a sign in.
|
2022-12-23 04:00:23 +00:00
|
|
|
func (d *WebauthnDevice) UpdateSignInInfo(config *webauthn.Config, now time.Time, signCount uint32) {
|
|
|
|
d.LastUsedAt = sql.NullTime{Time: now, Valid: true}
|
2022-03-03 11:20:43 +00:00
|
|
|
|
2022-12-23 04:00:23 +00:00
|
|
|
d.SignCount = signCount
|
2022-03-03 11:20:43 +00:00
|
|
|
|
2022-12-23 04:00:23 +00:00
|
|
|
if d.RPID != "" {
|
2022-03-03 11:20:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-23 04:00:23 +00:00
|
|
|
switch d.AttestationType {
|
2022-03-03 11:20:43 +00:00
|
|
|
case attestationTypeFIDOU2F:
|
2023-01-07 03:21:27 +00:00
|
|
|
d.RPID = config.RPOrigins[0]
|
2022-03-03 11:20:43 +00:00
|
|
|
default:
|
2022-12-23 04:00:23 +00:00
|
|
|
d.RPID = config.RPID
|
2022-03-03 11:20:43 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-23 04:00:23 +00:00
|
|
|
|
|
|
|
func (d *WebauthnDevice) LastUsed() *time.Time {
|
|
|
|
if d.LastUsedAt.Valid {
|
|
|
|
return &d.LastUsedAt.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalYAML marshals this model into YAML.
|
|
|
|
func (d *WebauthnDevice) MarshalYAML() (any, error) {
|
|
|
|
o := WebauthnDeviceData{
|
|
|
|
CreatedAt: d.CreatedAt,
|
|
|
|
LastUsedAt: d.LastUsed(),
|
|
|
|
RPID: d.RPID,
|
|
|
|
Username: d.Username,
|
2023-02-14 02:53:57 +00:00
|
|
|
DisplayName: d.DisplayName,
|
2022-12-23 04:00:23 +00:00
|
|
|
KID: d.KID.String(),
|
2023-02-14 02:53:57 +00:00
|
|
|
AAGUID: d.AAGUID.UUID.String(),
|
2022-12-23 04:00:23 +00:00
|
|
|
AttestationType: d.AttestationType,
|
2023-02-14 02:53:57 +00:00
|
|
|
Attachment: d.Attachment,
|
2022-12-23 04:00:23 +00:00
|
|
|
Transport: d.Transport,
|
|
|
|
SignCount: d.SignCount,
|
|
|
|
CloneWarning: d.CloneWarning,
|
2023-02-14 02:53:57 +00:00
|
|
|
Present: d.Present,
|
|
|
|
Verified: d.Verified,
|
|
|
|
BackupEligible: d.BackupEligible,
|
|
|
|
BackupState: d.BackupState,
|
|
|
|
PublicKey: base64.StdEncoding.EncodeToString(d.PublicKey),
|
2022-12-23 04:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return yaml.Marshal(o)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML unmarshalls YAML into this model.
|
|
|
|
func (d *WebauthnDevice) UnmarshalYAML(value *yaml.Node) (err error) {
|
|
|
|
o := &WebauthnDeviceData{}
|
|
|
|
|
|
|
|
if err = value.Decode(o); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.PublicKey, err = base64.StdEncoding.DecodeString(o.PublicKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var aaguid uuid.UUID
|
|
|
|
|
|
|
|
if aaguid, err = uuid.Parse(o.AAGUID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if aaguid.ID() != 0 {
|
|
|
|
d.AAGUID = uuid.NullUUID{Valid: true, UUID: aaguid}
|
|
|
|
}
|
|
|
|
|
|
|
|
var kid []byte
|
|
|
|
|
|
|
|
if kid, err = base64.StdEncoding.DecodeString(o.KID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.KID = NewBase64(kid)
|
|
|
|
|
|
|
|
d.CreatedAt = o.CreatedAt
|
|
|
|
d.RPID = o.RPID
|
|
|
|
d.Username = o.Username
|
2023-02-14 02:53:57 +00:00
|
|
|
d.DisplayName = o.DisplayName
|
2022-12-23 04:00:23 +00:00
|
|
|
d.AttestationType = o.AttestationType
|
2023-02-14 02:53:57 +00:00
|
|
|
d.Attachment = o.Attachment
|
2022-12-23 04:00:23 +00:00
|
|
|
d.Transport = o.Transport
|
|
|
|
d.SignCount = o.SignCount
|
|
|
|
d.CloneWarning = o.CloneWarning
|
2023-02-14 02:53:57 +00:00
|
|
|
d.Discoverable = o.Discoverable
|
|
|
|
d.Present = o.Present
|
|
|
|
d.Verified = o.Verified
|
|
|
|
d.BackupEligible = o.BackupEligible
|
|
|
|
d.BackupState = o.BackupState
|
2022-12-23 04:00:23 +00:00
|
|
|
|
|
|
|
if o.LastUsedAt != nil {
|
|
|
|
d.LastUsedAt = sql.NullTime{Valid: true, Time: *o.LastUsedAt}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WebauthnDeviceData represents a Webauthn Device in the database storage.
|
|
|
|
type WebauthnDeviceData struct {
|
|
|
|
CreatedAt time.Time `yaml:"created_at"`
|
|
|
|
LastUsedAt *time.Time `yaml:"last_used_at"`
|
|
|
|
RPID string `yaml:"rpid"`
|
|
|
|
Username string `yaml:"username"`
|
2023-02-14 02:53:57 +00:00
|
|
|
DisplayName string `yaml:"displayname"`
|
2022-12-23 04:00:23 +00:00
|
|
|
KID string `yaml:"kid"`
|
2023-02-14 02:53:57 +00:00
|
|
|
AAGUID string `yaml:"aaguid"`
|
2022-12-23 04:00:23 +00:00
|
|
|
AttestationType string `yaml:"attestation_type"`
|
2023-02-14 02:53:57 +00:00
|
|
|
Attachment string `yaml:"attachment"`
|
2022-12-23 04:00:23 +00:00
|
|
|
Transport string `yaml:"transport"`
|
|
|
|
SignCount uint32 `yaml:"sign_count"`
|
|
|
|
CloneWarning bool `yaml:"clone_warning"`
|
2023-02-14 02:53:57 +00:00
|
|
|
Discoverable bool `yaml:"discoverable"`
|
|
|
|
Present bool `yaml:"present"`
|
|
|
|
Verified bool `yaml:"verified"`
|
|
|
|
BackupEligible bool `yaml:"backup_eligible"`
|
|
|
|
BackupState bool `yaml:"backup_state"`
|
|
|
|
PublicKey string `yaml:"public_key"`
|
2022-12-23 04:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WebauthnDeviceExport represents a WebauthnDevice export file.
|
|
|
|
type WebauthnDeviceExport struct {
|
|
|
|
WebauthnDevices []WebauthnDevice `yaml:"webauthn_devices"`
|
|
|
|
}
|