github.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 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. "errors"
  11. "fmt"
  12. "net/http"
  13. "strings"
  14. "github.com/google/go-github/github"
  15. )
  16. func GITHUBAuth(apiEndpoint, userName, passwd string) (string, string, string, string, string, error) {
  17. tr := &http.Transport{
  18. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  19. }
  20. tp := github.BasicAuthTransport{
  21. Username: strings.TrimSpace(userName),
  22. Password: strings.TrimSpace(passwd),
  23. Transport: tr,
  24. }
  25. client, err := github.NewEnterpriseClient(apiEndpoint, apiEndpoint, tp.Client())
  26. if err != nil {
  27. return "", "", "", "", "", errors.New("Authentication failure: GitHub Api Endpoint can not be reached")
  28. }
  29. ctx := context.Background()
  30. user, _, err := client.Users.Get(ctx, "")
  31. if err != nil || user == nil {
  32. fmt.Println(err)
  33. msg := fmt.Sprintf("Authentication failure! Github Api Endpoint authticated failed! User %s", userName)
  34. return "", "", "", "", "", errors.New(msg)
  35. }
  36. var website = ""
  37. if user.HTMLURL != nil {
  38. website = strings.ToLower(*user.HTMLURL)
  39. }
  40. var location = ""
  41. if user.Location != nil {
  42. location = strings.ToUpper(*user.Location)
  43. }
  44. return *user.Login, *user.Name, *user.Email, website, location, nil
  45. }