diff options
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/auth/github/github.go | 45 | ||||
-rw-r--r-- | pkg/form/auth.go | 3 |
2 files changed, 47 insertions, 1 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 +} diff --git a/pkg/form/auth.go b/pkg/form/auth.go index b2ee3ae8..65f5eac0 100644 --- a/pkg/form/auth.go +++ b/pkg/form/auth.go @@ -11,7 +11,7 @@ import ( type Authentication struct { ID int64 - Type int `binding:"Range(2,5)"` + Type int `binding:"Range(2,6)"` Name string `binding:"Required;MaxSize(30)"` Host string Port int @@ -41,6 +41,7 @@ type Authentication struct { TLS bool SkipVerify bool PAMServiceName string + GithubApiEndpoint string `binding:Required;Url` } func (f *Authentication) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { |