You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.go 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package webauthn
  2. import (
  3. "fmt"
  4. "net/url"
  5. "github.com/duo-labs/webauthn/protocol"
  6. )
  7. var defaultTimeout = 60000
  8. // WebAuthn is the primary interface of this package and contains the request handlers that should be called.
  9. type WebAuthn struct {
  10. Config *Config
  11. }
  12. // The config values required for proper
  13. type Config struct {
  14. RPDisplayName string
  15. RPID string
  16. RPOrigin string
  17. RPIcon string
  18. // Defaults for generating options
  19. AttestationPreference protocol.ConveyancePreference
  20. AuthenticatorSelection protocol.AuthenticatorSelection
  21. Timeout int
  22. Debug bool
  23. }
  24. // Validate that the config flags in Config are properly set
  25. func (config *Config) validate() error {
  26. if len(config.RPDisplayName) == 0 {
  27. return fmt.Errorf("Missing RPDisplayName")
  28. }
  29. if len(config.RPID) == 0 {
  30. return fmt.Errorf("Missing RPID")
  31. }
  32. _, err := url.Parse(config.RPID)
  33. if err != nil {
  34. return fmt.Errorf("RPID not valid URI: %+v", err)
  35. }
  36. if config.Timeout == 0 {
  37. config.Timeout = defaultTimeout
  38. }
  39. if config.RPOrigin == "" {
  40. config.RPOrigin = config.RPID
  41. } else {
  42. u, err := url.Parse(config.RPOrigin)
  43. if err != nil {
  44. return fmt.Errorf("RPOrigin not valid URL: %+v", err)
  45. }
  46. config.RPOrigin = protocol.FullyQualifiedOrigin(u)
  47. }
  48. return nil
  49. }
  50. // Create a new WebAuthn object given the proper config flags
  51. func New(config *Config) (*WebAuthn, error) {
  52. if err := config.validate(); err != nil {
  53. return nil, fmt.Errorf("Configuration error: %+v", err)
  54. }
  55. return &WebAuthn{
  56. config,
  57. }, nil
  58. }