auth.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE and LICENSE.gogs file.
  4. package auth
  5. import (
  6. "fmt"
  7. "github.com/pkg/errors"
  8. "gogs.io/gogs/internal/errutil"
  9. )
  10. type Type int
  11. // Note: New type must append to the end of list to maintain backward compatibility.
  12. const (
  13. None Type = iota
  14. Plain // 1
  15. LDAP // 2
  16. SMTP // 3
  17. PAM // 4
  18. DLDAP // 5
  19. GitHub // 6
  20. Mock Type = 999
  21. )
  22. // Name returns the human-readable name for given authentication type.
  23. func Name(typ Type) string {
  24. return map[Type]string{
  25. LDAP: "LDAP (via BindDN)",
  26. DLDAP: "LDAP (simple auth)", // Via direct bind
  27. SMTP: "SMTP",
  28. PAM: "PAM",
  29. GitHub: "GitHub",
  30. }[typ]
  31. }
  32. var _ errutil.NotFound = (*ErrBadCredentials)(nil)
  33. type ErrBadCredentials struct {
  34. Args errutil.Args
  35. }
  36. // IsErrBadCredentials returns true if the underlying error has the type
  37. // ErrBadCredentials.
  38. func IsErrBadCredentials(err error) bool {
  39. return errors.As(err, &ErrBadCredentials{})
  40. }
  41. func (err ErrBadCredentials) Error() string {
  42. return fmt.Sprintf("bad credentials: %v", err.Args)
  43. }
  44. func (ErrBadCredentials) NotFound() bool {
  45. return true
  46. }
  47. // ExternalAccount contains queried information returned by an authenticate provider
  48. // for an external account.
  49. type ExternalAccount struct {
  50. // REQUIRED: The login to be used for authenticating against the provider.
  51. Login string
  52. // REQUIRED: The username of the account.
  53. Name string
  54. // The full name of the account.
  55. FullName string
  56. // The email address of the account.
  57. Email string
  58. // The public email address of the account.
  59. PublicEmail string
  60. // The location of the account.
  61. Location string
  62. // The website of the account.
  63. Website string
  64. // Whether the user should be prompted as a site admin.
  65. Admin bool
  66. }
  67. // Provider defines an authenticate provider which provides ability to authentication against
  68. // an external identity provider and query external account information.
  69. type Provider interface {
  70. // Authenticate performs authentication against an external identity provider
  71. // using given credentials and returns queried information of the external account.
  72. Authenticate(login, password string) (*ExternalAccount, error)
  73. // Config returns the underlying configuration of the authenticate provider.
  74. Config() any
  75. // HasTLS returns true if the authenticate provider supports TLS.
  76. HasTLS() bool
  77. // UseTLS returns true if the authenticate provider is configured to use TLS.
  78. UseTLS() bool
  79. // SkipTLSVerify returns true if the authenticate provider is configured to skip TLS verify.
  80. SkipTLSVerify() bool
  81. }