github.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package github
  7. import (
  8. "context"
  9. "crypto/tls"
  10. "fmt"
  11. "net/http"
  12. "strings"
  13. "github.com/google/go-github/github"
  14. )
  15. // Authenticate auth via GitHub
  16. func Authenticate(apiEndpoint, login, passwd string) (name string, email string, website string, location string, _ error) {
  17. tp := github.BasicAuthTransport{
  18. Username: strings.TrimSpace(login),
  19. Password: strings.TrimSpace(passwd),
  20. Transport: &http.Transport{
  21. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  22. },
  23. }
  24. client, err := github.NewEnterpriseClient(apiEndpoint, apiEndpoint, tp.Client())
  25. if err != nil {
  26. return "", "", "", "", fmt.Errorf("create new client: %v", err)
  27. }
  28. user, _, err := client.Users.Get(context.Background(), "")
  29. if err != nil {
  30. return "", "", "", "", fmt.Errorf("get user info: %v", err)
  31. }
  32. if user.Name != nil {
  33. name = *user.Name
  34. }
  35. if user.Email != nil {
  36. email = *user.Email
  37. } else {
  38. email = login + "+github@local"
  39. }
  40. if user.HTMLURL != nil {
  41. website = strings.ToLower(*user.HTMLURL)
  42. }
  43. if user.Location != nil {
  44. location = strings.ToUpper(*user.Location)
  45. }
  46. return name, email, website, location, nil
  47. }