2016-02-17 12:19:52 +00:00
|
|
|
package providers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2019-05-24 16:08:48 +00:00
|
|
|
"github.com/pusher/oauth2_proxy/pkg/logger"
|
2019-05-05 12:33:13 +00:00
|
|
|
"github.com/pusher/oauth2_proxy/pkg/apis/sessions"
|
2019-05-24 15:55:12 +00:00
|
|
|
"github.com/pusher/oauth2_proxy/pkg/requests"
|
2016-02-17 12:19:52 +00:00
|
|
|
)
|
|
|
|
|
2018-12-20 10:37:59 +00:00
|
|
|
// GitLabProvider represents an GitLab based Identity Provider
|
2016-02-17 12:19:52 +00:00
|
|
|
type GitLabProvider struct {
|
|
|
|
*ProviderData
|
|
|
|
}
|
|
|
|
|
2018-12-20 10:37:59 +00:00
|
|
|
// 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",
|
2017-11-06 17:01:48 +00:00
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
2018-12-20 10:37:59 +00:00
|
|
|
// GetEmailAddress returns the Account email address
|
2019-05-05 12:33:13 +00:00
|
|
|
func (p *GitLabProvider) GetEmailAddress(s *sessions.SessionState) (string, error) {
|
2016-02-17 12:19:52 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest("GET",
|
|
|
|
p.ValidateURL.String()+"?access_token="+s.AccessToken, nil)
|
|
|
|
if err != nil {
|
2019-02-10 16:37:45 +00:00
|
|
|
logger.Printf("failed building request %s", err)
|
2016-02-17 12:19:52 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2019-05-24 15:55:12 +00:00
|
|
|
json, err := requests.Request(req)
|
2016-02-17 12:19:52 +00:00
|
|
|
if err != nil {
|
2019-02-10 16:37:45 +00:00
|
|
|
logger.Printf("failed making request %s", err)
|
2016-02-17 12:19:52 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return json.Get("email").String()
|
|
|
|
}
|