github.go 1.2 KB

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