1
0

auth.go 2.4 KB

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