provider.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2020 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 github
  5. import (
  6. "strings"
  7. "gogs.io/gogs/internal/auth"
  8. )
  9. // Provider contains configuration of a PAM authentication provider.
  10. type Provider struct {
  11. config *Config
  12. }
  13. // NewProvider creates a new PAM authentication provider.
  14. func NewProvider(cfg *Config) auth.Provider {
  15. return &Provider{
  16. config: cfg,
  17. }
  18. }
  19. func (p *Provider) Authenticate(login, password string) (*auth.ExternalAccount, error) {
  20. fullname, email, website, location, err := p.config.doAuth(login, password)
  21. if err != nil {
  22. if strings.Contains(err.Error(), "401") {
  23. return nil, auth.ErrBadCredentials{Args: map[string]interface{}{"login": login}}
  24. }
  25. return nil, err
  26. }
  27. return &auth.ExternalAccount{
  28. Login: login,
  29. Name: login,
  30. FullName: fullname,
  31. Email: email,
  32. Location: location,
  33. Website: website,
  34. }, nil
  35. }
  36. func (p *Provider) Config() interface{} {
  37. return p.config
  38. }
  39. func (p *Provider) HasTLS() bool {
  40. return true
  41. }
  42. func (p *Provider) UseTLS() bool {
  43. return true
  44. }
  45. func (p *Provider) SkipTLSVerify() bool {
  46. return p.config.SkipVerify
  47. }