config.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. "context"
  7. "crypto/tls"
  8. "net/http"
  9. "strings"
  10. "github.com/google/go-github/github"
  11. "github.com/pkg/errors"
  12. )
  13. // Config contains configuration for GitHub authentication.
  14. //
  15. // ⚠️ WARNING: Change to the field name must preserve the INI key name for backward compatibility.
  16. type Config struct {
  17. // the GitHub service endpoint, e.g. https://api.github.com/.
  18. APIEndpoint string
  19. SkipVerify bool
  20. }
  21. func (c *Config) doAuth(login, password string) (fullname, email, location, website string, err error) {
  22. tp := github.BasicAuthTransport{
  23. Username: strings.TrimSpace(login),
  24. Password: strings.TrimSpace(password),
  25. Transport: &http.Transport{
  26. TLSClientConfig: &tls.Config{InsecureSkipVerify: c.SkipVerify},
  27. },
  28. }
  29. client, err := github.NewEnterpriseClient(c.APIEndpoint, c.APIEndpoint, tp.Client())
  30. if err != nil {
  31. return "", "", "", "", errors.Wrap(err, "create new client")
  32. }
  33. user, _, err := client.Users.Get(context.Background(), "")
  34. if err != nil {
  35. return "", "", "", "", errors.Wrap(err, "get user info")
  36. }
  37. if user.Name != nil {
  38. fullname = *user.Name
  39. }
  40. if user.Email != nil {
  41. email = *user.Email
  42. } else {
  43. email = login + "+github@local"
  44. }
  45. if user.Location != nil {
  46. location = strings.ToUpper(*user.Location)
  47. }
  48. if user.HTMLURL != nil {
  49. website = strings.ToLower(*user.HTMLURL)
  50. }
  51. return fullname, email, location, website, nil
  52. }