webauthn.go 992 B

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