webauthn.go 1023 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package passkey
  2. import (
  3. "context"
  4. "github.com/0xJacky/Nginx-UI/settings"
  5. "github.com/go-webauthn/webauthn/protocol"
  6. "github.com/go-webauthn/webauthn/webauthn"
  7. "github.com/uozi-tech/cosy/logger"
  8. )
  9. var instance *webauthn.WebAuthn
  10. func Init(ctx context.Context) {
  11. options := settings.WebAuthnSettings
  12. if !Enabled() {
  13. logger.Debug("WebAuthn settings are not configured")
  14. return
  15. }
  16. requireResidentKey := true
  17. var err error
  18. instance, err = webauthn.New(&webauthn.Config{
  19. RPDisplayName: options.RPDisplayName,
  20. RPID: options.RPID,
  21. RPOrigins: options.RPOrigins,
  22. AuthenticatorSelection: protocol.AuthenticatorSelection{
  23. RequireResidentKey: &requireResidentKey,
  24. UserVerification: "required",
  25. },
  26. })
  27. if err != nil {
  28. logger.Fatal(err)
  29. }
  30. }
  31. func Enabled() bool {
  32. options := settings.WebAuthnSettings
  33. if options.RPDisplayName == "" || options.RPID == "" || len(options.RPOrigins) == 0 {
  34. return false
  35. }
  36. return true
  37. }
  38. func GetInstance() *webauthn.WebAuthn {
  39. return instance
  40. }