2015-11-09 10:28:34 +02:00
|
|
|
package providers
|
|
|
|
|
|
|
|
import (
|
2019-08-29 16:37:25 +02:00
|
|
|
"bytes"
|
2020-05-05 17:53:33 +02:00
|
|
|
"context"
|
2015-11-09 10:28:34 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2019-08-29 15:32:01 +02:00
|
|
|
"time"
|
2018-11-29 16:26:41 +02:00
|
|
|
|
|
|
|
"github.com/bitly/go-simplejson"
|
2020-03-29 15:54:36 +02:00
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/pkg/apis/sessions"
|
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
|
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/pkg/requests"
|
2015-11-09 10:28:34 +02:00
|
|
|
)
|
|
|
|
|
2018-12-20 12:37:59 +02:00
|
|
|
// AzureProvider represents an Azure based Identity Provider
|
2015-11-09 10:28:34 +02:00
|
|
|
type AzureProvider struct {
|
|
|
|
*ProviderData
|
|
|
|
Tenant string
|
|
|
|
}
|
|
|
|
|
2020-05-05 17:53:33 +02:00
|
|
|
var _ Provider = (*AzureProvider)(nil)
|
|
|
|
|
2020-05-25 14:08:04 +02:00
|
|
|
const (
|
|
|
|
azureProviderName = "Azure"
|
|
|
|
azureDefaultScope = "openid"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Default Login URL for Azure.
|
|
|
|
// Pre-parsed URL of https://login.microsoftonline.com/common/oauth2/authorize.
|
|
|
|
azureDefaultLoginURL = &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "login.microsoftonline.com",
|
|
|
|
Path: "/common/oauth2/authorize",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default Redeem URL for Azure.
|
|
|
|
// Pre-parsed URL of https://login.microsoftonline.com/common/oauth2/token.
|
|
|
|
azureDefaultRedeemURL = &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "login.microsoftonline.com",
|
|
|
|
Path: "/common/oauth2/token",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default Profile URL for Azure.
|
|
|
|
// Pre-parsed URL of https://graph.microsoft.com/v1.0/me.
|
|
|
|
azureDefaultProfileURL = &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "graph.microsoft.com",
|
|
|
|
Path: "/v1.0/me",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default ProtectedResource URL for Azure.
|
|
|
|
// Pre-parsed URL of https://graph.microsoft.com.
|
|
|
|
azureDefaultProtectResourceURL = &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "graph.microsoft.com",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-12-20 12:37:59 +02:00
|
|
|
// NewAzureProvider initiates a new AzureProvider
|
2015-11-09 10:28:34 +02:00
|
|
|
func NewAzureProvider(p *ProviderData) *AzureProvider {
|
2020-05-25 14:08:04 +02:00
|
|
|
p.setProviderDefaults(providerDefaults{
|
|
|
|
name: azureProviderName,
|
|
|
|
loginURL: azureDefaultLoginURL,
|
|
|
|
redeemURL: azureDefaultRedeemURL,
|
|
|
|
profileURL: azureDefaultProfileURL,
|
|
|
|
validateURL: nil,
|
|
|
|
scope: azureDefaultScope,
|
|
|
|
})
|
2015-11-09 10:28:34 +02:00
|
|
|
|
|
|
|
if p.ProtectedResource == nil || p.ProtectedResource.String() == "" {
|
2020-05-25 14:08:04 +02:00
|
|
|
p.ProtectedResource = azureDefaultProtectResourceURL
|
2015-11-09 10:28:34 +02:00
|
|
|
}
|
|
|
|
|
2020-05-25 14:08:04 +02:00
|
|
|
return &AzureProvider{
|
|
|
|
ProviderData: p,
|
|
|
|
Tenant: "common",
|
|
|
|
}
|
2015-11-09 10:28:34 +02:00
|
|
|
}
|
|
|
|
|
2018-12-20 12:37:59 +02:00
|
|
|
// Configure defaults the AzureProvider configuration options
|
2015-11-09 10:28:34 +02:00
|
|
|
func (p *AzureProvider) Configure(tenant string) {
|
2020-05-25 14:08:04 +02:00
|
|
|
if tenant == "" || tenant == "common" {
|
|
|
|
// tenant is empty or default, remain on the default "common" tenant
|
|
|
|
return
|
2015-11-09 10:28:34 +02:00
|
|
|
}
|
|
|
|
|
2020-05-25 14:08:04 +02:00
|
|
|
// Specific tennant specified, override the Login and RedeemURLs
|
|
|
|
p.Tenant = tenant
|
|
|
|
overrideTenantURL(p.LoginURL, azureDefaultLoginURL, tenant, "authorize")
|
|
|
|
overrideTenantURL(p.RedeemURL, azureDefaultRedeemURL, tenant, "token")
|
|
|
|
}
|
|
|
|
|
|
|
|
func overrideTenantURL(current, defaultURL *url.URL, tenant, path string) {
|
|
|
|
if current == nil || current.String() == "" || current.String() == defaultURL.String() {
|
|
|
|
*current = url.URL{
|
2015-11-09 10:28:34 +02:00
|
|
|
Scheme: "https",
|
|
|
|
Host: "login.microsoftonline.com",
|
2020-05-25 14:08:04 +02:00
|
|
|
Path: "/" + tenant + "/oauth2/" + path}
|
2015-11-09 10:28:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 17:53:33 +02:00
|
|
|
func (p *AzureProvider) Redeem(ctx context.Context, redirectURL, code string) (s *sessions.SessionState, err error) {
|
2019-08-29 15:32:01 +02:00
|
|
|
if code == "" {
|
|
|
|
err = errors.New("missing code")
|
|
|
|
return
|
|
|
|
}
|
2020-02-15 15:44:39 +02:00
|
|
|
clientSecret, err := p.GetClientSecret()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-29 15:32:01 +02:00
|
|
|
params := url.Values{}
|
|
|
|
params.Add("redirect_uri", redirectURL)
|
|
|
|
params.Add("client_id", p.ClientID)
|
2020-02-15 15:44:39 +02:00
|
|
|
params.Add("client_secret", clientSecret)
|
2019-08-29 15:32:01 +02:00
|
|
|
params.Add("code", code)
|
|
|
|
params.Add("grant_type", "authorization_code")
|
|
|
|
if p.ProtectedResource != nil && p.ProtectedResource.String() != "" {
|
|
|
|
params.Add("resource", p.ProtectedResource.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
var jsonResponse struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
|
|
|
ExpiresOn int64 `json:"expires_on,string"`
|
|
|
|
IDToken string `json:"id_token"`
|
|
|
|
}
|
2020-07-03 20:27:25 +02:00
|
|
|
|
|
|
|
err = requests.New(p.RedeemURL.String()).
|
|
|
|
WithContext(ctx).
|
|
|
|
WithMethod("POST").
|
|
|
|
WithBody(bytes.NewBufferString(params.Encode())).
|
|
|
|
SetHeader("Content-Type", "application/x-www-form-urlencoded").
|
2020-07-06 18:42:26 +02:00
|
|
|
Do().
|
2020-07-03 20:27:25 +02:00
|
|
|
UnmarshalInto(&jsonResponse)
|
2019-08-29 15:32:01 +02:00
|
|
|
if err != nil {
|
2020-07-03 20:27:25 +02:00
|
|
|
return nil, err
|
2019-08-29 15:32:01 +02:00
|
|
|
}
|
2019-08-29 16:37:25 +02:00
|
|
|
|
2020-05-30 09:53:38 +02:00
|
|
|
created := time.Now()
|
|
|
|
expires := time.Unix(jsonResponse.ExpiresOn, 0)
|
2019-08-29 15:32:01 +02:00
|
|
|
s = &sessions.SessionState{
|
|
|
|
AccessToken: jsonResponse.AccessToken,
|
|
|
|
IDToken: jsonResponse.IDToken,
|
2020-05-30 09:53:38 +02:00
|
|
|
CreatedAt: &created,
|
|
|
|
ExpiresOn: &expires,
|
2019-08-29 15:32:01 +02:00
|
|
|
RefreshToken: jsonResponse.RefreshToken,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-29 16:26:41 +02:00
|
|
|
func getAzureHeader(accessToken string) http.Header {
|
2015-11-09 10:28:34 +02:00
|
|
|
header := make(http.Header)
|
2018-11-29 16:26:41 +02:00
|
|
|
header.Set("Authorization", fmt.Sprintf("Bearer %s", accessToken))
|
2015-11-09 10:28:34 +02:00
|
|
|
return header
|
|
|
|
}
|
|
|
|
|
2016-06-29 09:00:08 +02:00
|
|
|
func getEmailFromJSON(json *simplejson.Json) (string, error) {
|
2017-03-29 15:36:38 +02:00
|
|
|
var email string
|
2016-06-29 09:00:08 +02:00
|
|
|
var err error
|
2017-03-29 15:36:38 +02:00
|
|
|
|
2016-06-29 09:00:08 +02:00
|
|
|
email, err = json.Get("mail").String()
|
2017-03-29 15:36:38 +02:00
|
|
|
|
2016-06-29 09:00:08 +02:00
|
|
|
if err != nil || email == "" {
|
|
|
|
otherMails, otherMailsErr := json.Get("otherMails").Array()
|
2017-03-29 15:36:38 +02:00
|
|
|
if len(otherMails) > 0 {
|
2016-06-29 09:00:08 +02:00
|
|
|
email = otherMails[0].(string)
|
|
|
|
}
|
|
|
|
err = otherMailsErr
|
|
|
|
}
|
2017-03-29 15:36:38 +02:00
|
|
|
|
2016-06-29 09:00:08 +02:00
|
|
|
return email, err
|
2017-03-29 15:36:38 +02:00
|
|
|
}
|
2016-06-29 09:00:08 +02:00
|
|
|
|
2018-12-20 12:37:59 +02:00
|
|
|
// GetEmailAddress returns the Account email address
|
2020-05-05 17:53:33 +02:00
|
|
|
func (p *AzureProvider) GetEmailAddress(ctx context.Context, s *sessions.SessionState) (string, error) {
|
2016-06-29 09:00:08 +02:00
|
|
|
var email string
|
|
|
|
var err error
|
2017-03-29 15:36:38 +02:00
|
|
|
|
2015-11-09 10:28:34 +02:00
|
|
|
if s.AccessToken == "" {
|
|
|
|
return "", errors.New("missing access token")
|
|
|
|
}
|
|
|
|
|
2020-07-03 20:27:25 +02:00
|
|
|
json, err := requests.New(p.ProfileURL.String()).
|
|
|
|
WithContext(ctx).
|
|
|
|
WithHeaders(getAzureHeader(s.AccessToken)).
|
2020-07-06 18:42:26 +02:00
|
|
|
Do().
|
2020-07-03 20:27:25 +02:00
|
|
|
UnmarshalJSON()
|
2015-11-09 10:28:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2016-06-29 09:00:08 +02:00
|
|
|
email, err = getEmailFromJSON(json)
|
|
|
|
if err == nil && email != "" {
|
|
|
|
return email, err
|
|
|
|
}
|
|
|
|
|
|
|
|
email, err = json.Get("userPrincipalName").String()
|
|
|
|
if err != nil {
|
2020-08-10 12:44:08 +02:00
|
|
|
logger.Errorf("failed making request %s", err)
|
2016-06-29 09:00:08 +02:00
|
|
|
return "", err
|
|
|
|
}
|
2017-03-29 15:36:38 +02:00
|
|
|
|
2016-06-29 09:00:08 +02:00
|
|
|
if email == "" {
|
2020-08-10 12:44:08 +02:00
|
|
|
logger.Errorf("failed to get email address")
|
2016-06-29 09:00:08 +02:00
|
|
|
return "", err
|
|
|
|
}
|
2017-03-29 15:36:38 +02:00
|
|
|
|
2016-06-29 09:00:08 +02:00
|
|
|
return email, err
|
2015-11-09 10:28:34 +02:00
|
|
|
}
|