github.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package github
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "errors"
  6. "fmt"
  7. "net/http"
  8. "strings"
  9. "github.com/google/go-github/github"
  10. )
  11. func GITHUBAuth(apiEndpoint, userName, passwd string) (string, string, string, string, string, error) {
  12. tr := &http.Transport{
  13. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  14. }
  15. tp := github.BasicAuthTransport{
  16. Username: strings.TrimSpace(userName),
  17. Password: strings.TrimSpace(passwd),
  18. Transport: tr,
  19. }
  20. client, err := github.NewEnterpriseClient(apiEndpoint, apiEndpoint, tp.Client())
  21. if err != nil {
  22. return "", "", "", "", "", errors.New("Authentication failure: GitHub Api Endpoint can not be reached")
  23. }
  24. ctx := context.Background()
  25. user, _, err := client.Users.Get(ctx, "")
  26. if err != nil || user == nil {
  27. fmt.Println(err)
  28. msg := fmt.Sprintf("Authentication failure! Github Api Endpoint authticated failed! User %s", userName)
  29. return "", "", "", "", "", errors.New(msg)
  30. }
  31. var website = ""
  32. if user.HTMLURL != nil {
  33. website = strings.ToLower(*user.HTMLURL)
  34. }
  35. var location = ""
  36. if user.Location != nil {
  37. location = strings.ToUpper(*user.Location)
  38. }
  39. return *user.Login, *user.Name, *user.Email, website, location, nil
  40. }