oauth2_proxy/providers/gitlab.go

62 lines
1.4 KiB
Go
Raw Permalink Normal View History

2016-02-17 12:19:52 +00:00
package providers
import (
"log"
"net/http"
"net/url"
2018-11-27 11:45:05 +00:00
"github.com/pusher/oauth2_proxy/api"
2016-02-17 12:19:52 +00:00
)
// GitLabProvider represents an GitLab based Identity Provider
2016-02-17 12:19:52 +00:00
type GitLabProvider struct {
*ProviderData
}
// NewGitLabProvider initiates a new GitLabProvider
2016-02-17 12:19:52 +00:00
func NewGitLabProvider(p *ProviderData) *GitLabProvider {
p.ProviderName = "GitLab"
if p.LoginURL == nil || p.LoginURL.String() == "" {
p.LoginURL = &url.URL{
Scheme: "https",
Host: "gitlab.com",
Path: "/oauth/authorize",
}
}
if p.RedeemURL == nil || p.RedeemURL.String() == "" {
p.RedeemURL = &url.URL{
Scheme: "https",
Host: "gitlab.com",
Path: "/oauth/token",
}
}
if p.ValidateURL == nil || p.ValidateURL.String() == "" {
p.ValidateURL = &url.URL{
Scheme: "https",
Host: "gitlab.com",
Path: "/api/v4/user",
2016-02-17 12:19:52 +00:00
}
}
if p.Scope == "" {
2017-09-12 21:42:07 +00:00
p.Scope = "read_user"
2016-02-17 12:19:52 +00:00
}
return &GitLabProvider{ProviderData: p}
}
// GetEmailAddress returns the Account email address
2016-02-17 12:19:52 +00:00
func (p *GitLabProvider) GetEmailAddress(s *SessionState) (string, error) {
req, err := http.NewRequest("GET",
p.ValidateURL.String()+"?access_token="+s.AccessToken, nil)
if err != nil {
log.Printf("failed building request %s", err)
return "", err
}
json, err := api.Request(req)
if err != nil {
log.Printf("failed making request %s", err)
return "", err
}
return json.Get("email").String()
}