aboutsummaryrefslogtreecommitdiff
path: root/pkg/auth/github/github.go
diff options
context:
space:
mode:
authorUnknwon <u@gogs.io>2018-12-18 16:46:50 -0500
committerUnknwon <u@gogs.io>2018-12-18 16:46:50 -0500
commit657ea2686fd076c4baacc5293694c94f97c4fec3 (patch)
tree172b201112c29b42799612f3c5ef26e49a58e95e /pkg/auth/github/github.go
parent311df9c521f05cdca880152e73dbed47afb74cde (diff)
auth: coding style and glitches fixes for GitHub login source (#5340)
Diffstat (limited to 'pkg/auth/github/github.go')
-rw-r--r--pkg/auth/github/github.go41
1 files changed, 23 insertions, 18 deletions
diff --git a/pkg/auth/github/github.go b/pkg/auth/github/github.go
index 2a8d8b48..a06608a3 100644
--- a/pkg/auth/github/github.go
+++ b/pkg/auth/github/github.go
@@ -1,9 +1,12 @@
+// Copyright 2018 The Gogs Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
package github
import (
"context"
"crypto/tls"
- "errors"
"fmt"
"net/http"
"strings"
@@ -11,35 +14,37 @@ import (
"github.com/google/go-github/github"
)
-func GITHUBAuth(apiEndpoint, userName, passwd string) (string, string, string, string, string, error) {
- tr := &http.Transport{
- TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
- }
+func Authenticate(apiEndpoint, login, passwd string) (name string, email string, website string, location string, _ error) {
tp := github.BasicAuthTransport{
- Username: strings.TrimSpace(userName),
- Password: strings.TrimSpace(passwd),
- Transport: tr,
+ Username: strings.TrimSpace(login),
+ Password: strings.TrimSpace(passwd),
+ Transport: &http.Transport{
+ TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
+ },
}
client, err := github.NewEnterpriseClient(apiEndpoint, apiEndpoint, tp.Client())
if err != nil {
- return "", "", "", "", "", errors.New("Authentication failure: GitHub Api Endpoint can not be reached")
+ return "", "", "", "", fmt.Errorf("create new client: %v", err)
}
- ctx := context.Background()
- user, _, err := client.Users.Get(ctx, "")
- if err != nil || user == nil {
- fmt.Println(err)
- msg := fmt.Sprintf("Authentication failure! Github Api Endpoint authticated failed! User %s", userName)
- return "", "", "", "", "", errors.New(msg)
+ user, _, err := client.Users.Get(context.Background(), "")
+ if err != nil {
+ return "", "", "", "", fmt.Errorf("get user info: %v", err)
}
- var website = ""
+ if user.Name != nil {
+ name = *user.Name
+ }
+ if user.Email != nil {
+ email = *user.Email
+ } else {
+ email = login + "+github@local"
+ }
if user.HTMLURL != nil {
website = strings.ToLower(*user.HTMLURL)
}
- var location = ""
if user.Location != nil {
location = strings.ToUpper(*user.Location)
}
- return *user.Login, *user.Name, *user.Email, website, location, nil
+ return name, email, website, location, nil
}