auth.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 file.
  4. package auth
  5. import (
  6. "fmt"
  7. "gogs.io/gogs/internal/errutil"
  8. )
  9. type Type int
  10. // Note: New type must append to the end of list to maintain backward compatibility.
  11. const (
  12. None Type = iota
  13. Plain // 1
  14. LDAP // 2
  15. SMTP // 3
  16. PAM // 4
  17. DLDAP // 5
  18. GitHub // 6
  19. )
  20. // Name returns the human-readable name for given authentication type.
  21. func Name(typ Type) string {
  22. return map[Type]string{
  23. LDAP: "LDAP (via BindDN)",
  24. DLDAP: "LDAP (simple auth)", // Via direct bind
  25. SMTP: "SMTP",
  26. PAM: "PAM",
  27. GitHub: "GitHub",
  28. }[typ]
  29. }
  30. var _ errutil.NotFound = (*ErrBadCredentials)(nil)
  31. type ErrBadCredentials struct {
  32. Args errutil.Args
  33. }
  34. func IsErrBadCredentials(err error) bool {
  35. _, ok := err.(ErrBadCredentials)
  36. return ok
  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() interface{}
  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. }