webauthn.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package model // import "miniflux.app/v2/internal/model"
  4. import (
  5. "database/sql/driver"
  6. "encoding/hex"
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "time"
  11. "github.com/go-webauthn/webauthn/webauthn"
  12. )
  13. // WebAuthnSession handles marshalling / unmarshalling session data
  14. type WebAuthnSession struct {
  15. *webauthn.SessionData
  16. }
  17. func (s WebAuthnSession) Value() (driver.Value, error) {
  18. return json.Marshal(s)
  19. }
  20. func (s *WebAuthnSession) Scan(value any) error {
  21. b, ok := value.([]byte)
  22. if !ok {
  23. return errors.New("type assertion to []byte failed")
  24. }
  25. return json.Unmarshal(b, &s)
  26. }
  27. func (s WebAuthnSession) String() string {
  28. if s.SessionData == nil {
  29. return "{}"
  30. }
  31. return fmt.Sprintf("{Challenge: %s, UserID: %x}", s.Challenge, s.UserID)
  32. }
  33. type WebAuthnCredential struct {
  34. Credential webauthn.Credential
  35. Name string
  36. AddedOn *time.Time
  37. LastSeenOn *time.Time
  38. Handle []byte
  39. }
  40. func (s WebAuthnCredential) HandleEncoded() string {
  41. return hex.EncodeToString(s.Handle)
  42. }