github.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. "fmt"
  11. "net/http"
  12. "strings"
  13. "github.com/google/go-github/github"
  14. )
  15. func Authenticate(apiEndpoint, login, passwd string) (name string, email string, website string, location string, _ error) {
  16. tp := github.BasicAuthTransport{
  17. Username: strings.TrimSpace(login),
  18. Password: strings.TrimSpace(passwd),
  19. Transport: &http.Transport{
  20. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  21. },
  22. }
  23. client, err := github.NewEnterpriseClient(apiEndpoint, apiEndpoint, tp.Client())
  24. if err != nil {
  25. return "", "", "", "", fmt.Errorf("create new client: %v", err)
  26. }
  27. user, _, err := client.Users.Get(context.Background(), "")
  28. if err != nil {
  29. return "", "", "", "", fmt.Errorf("get user info: %v", err)
  30. }
  31. if user.Name != nil {
  32. name = *user.Name
  33. }
  34. if user.Email != nil {
  35. email = *user.Email
  36. } else {
  37. email = login + "+github@local"
  38. }
  39. if user.HTMLURL != nil {
  40. website = strings.ToLower(*user.HTMLURL)
  41. }
  42. if user.Location != nil {
  43. location = strings.ToUpper(*user.Location)
  44. }
  45. return name, email, website, location, nil
  46. }