aboutsummaryrefslogtreecommitdiff
path: root/pkg/auth
diff options
context:
space:
mode:
authorhaixunlu <luhaixun@gmail.com>2018-12-18 12:49:30 -0800
committer无闻 <u@gogs.io>2018-12-18 15:49:30 -0500
commit311df9c521f05cdca880152e73dbed47afb74cde (patch)
treef3b9159e6dc23e6659e1da5939ff7f09d7a32c16 /pkg/auth
parentff93d9dbda5cebe90d86e4b7dfb2c6b8642970ce (diff)
auth: add new authentication source: GitHub, including GitHub Enterprise (#5340)
* Add new Authentication Source: GitHub, including GitHub Enterprise. * Add vendor dependencies.
Diffstat (limited to 'pkg/auth')
-rw-r--r--pkg/auth/github/github.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/pkg/auth/github/github.go b/pkg/auth/github/github.go
new file mode 100644
index 00000000..2a8d8b48
--- /dev/null
+++ b/pkg/auth/github/github.go
@@ -0,0 +1,45 @@
+package github
+
+import (
+ "context"
+ "crypto/tls"
+ "errors"
+ "fmt"
+ "net/http"
+ "strings"
+
+ "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},
+ }
+ tp := github.BasicAuthTransport{
+ Username: strings.TrimSpace(userName),
+ Password: strings.TrimSpace(passwd),
+ Transport: tr,
+ }
+ client, err := github.NewEnterpriseClient(apiEndpoint, apiEndpoint, tp.Client())
+ if err != nil {
+ return "", "", "", "", "", errors.New("Authentication failure: GitHub Api Endpoint can not be reached")
+ }
+ 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)
+ }
+
+ var website = ""
+ 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
+}