oauth2_proxy/providers/azure.go

131 lines
2.8 KiB
Go
Raw Normal View History

2015-11-09 08:28:34 +00:00
package providers
import (
"errors"
"fmt"
"net/http"
"net/url"
2018-11-29 14:26:41 +00:00
"github.com/bitly/go-simplejson"
2019-05-05 12:33:13 +00:00
"github.com/pusher/oauth2_proxy/pkg/apis/sessions"
2019-06-15 09:33:29 +00:00
"github.com/pusher/oauth2_proxy/pkg/logger"
2019-05-24 15:55:12 +00:00
"github.com/pusher/oauth2_proxy/pkg/requests"
2015-11-09 08:28:34 +00:00
)
// AzureProvider represents an Azure based Identity Provider
2015-11-09 08:28:34 +00:00
type AzureProvider struct {
*ProviderData
Tenant string
}
// NewAzureProvider initiates a new AzureProvider
2015-11-09 08:28:34 +00:00
func NewAzureProvider(p *ProviderData) *AzureProvider {
p.ProviderName = "Azure"
if p.ProfileURL == nil || p.ProfileURL.String() == "" {
p.ProfileURL = &url.URL{
Scheme: "https",
Host: "graph.windows.net",
Path: "/me",
RawQuery: "api-version=1.6",
}
}
if p.ProtectedResource == nil || p.ProtectedResource.String() == "" {
p.ProtectedResource = &url.URL{
Scheme: "https",
Host: "graph.windows.net",
}
}
if p.Scope == "" {
p.Scope = "openid"
}
return &AzureProvider{ProviderData: p}
}
// Configure defaults the AzureProvider configuration options
2015-11-09 08:28:34 +00:00
func (p *AzureProvider) Configure(tenant string) {
p.Tenant = tenant
if tenant == "" {
p.Tenant = "common"
}
if p.LoginURL == nil || p.LoginURL.String() == "" {
p.LoginURL = &url.URL{
Scheme: "https",
Host: "login.microsoftonline.com",
Path: "/" + p.Tenant + "/oauth2/authorize"}
}
if p.RedeemURL == nil || p.RedeemURL.String() == "" {
p.RedeemURL = &url.URL{
Scheme: "https",
Host: "login.microsoftonline.com",
Path: "/" + p.Tenant + "/oauth2/token",
}
}
}
2018-11-29 14:26:41 +00:00
func getAzureHeader(accessToken string) http.Header {
2015-11-09 08:28:34 +00:00
header := make(http.Header)
2018-11-29 14:26:41 +00:00
header.Set("Authorization", fmt.Sprintf("Bearer %s", accessToken))
2015-11-09 08:28:34 +00:00
return header
}
func getEmailFromJSON(json *simplejson.Json) (string, error) {
2017-03-29 13:36:38 +00:00
var email string
var err error
2017-03-29 13:36:38 +00:00
email, err = json.Get("mail").String()
2017-03-29 13:36:38 +00:00
if err != nil || email == "" {
otherMails, otherMailsErr := json.Get("otherMails").Array()
2017-03-29 13:36:38 +00:00
if len(otherMails) > 0 {
email = otherMails[0].(string)
}
err = otherMailsErr
}
2017-03-29 13:36:38 +00:00
return email, err
2017-03-29 13:36:38 +00:00
}
// GetEmailAddress returns the Account email address
2019-05-05 12:33:13 +00:00
func (p *AzureProvider) GetEmailAddress(s *sessions.SessionState) (string, error) {
var email string
var err error
2017-03-29 13:36:38 +00:00
2015-11-09 08:28:34 +00:00
if s.AccessToken == "" {
return "", errors.New("missing access token")
}
req, err := http.NewRequest("GET", p.ProfileURL.String(), nil)
if err != nil {
return "", err
}
req.Header = getAzureHeader(s.AccessToken)
2019-05-24 15:55:12 +00:00
json, err := requests.Request(req)
2015-11-09 08:28:34 +00:00
if err != nil {
return "", err
}
email, err = getEmailFromJSON(json)
if err == nil && email != "" {
return email, err
}
email, err = json.Get("userPrincipalName").String()
2017-03-29 13:36:38 +00:00
if err != nil {
logger.Printf("failed making request %s", err)
return "", err
}
2017-03-29 13:36:38 +00:00
if email == "" {
logger.Printf("failed to get email address")
return "", err
}
2017-03-29 13:36:38 +00:00
return email, err
2015-11-09 08:28:34 +00:00
}