@@ -23,49 +23,84 @@ type AzureProvider struct {
2323
2424var _ Provider = (*AzureProvider)(nil)
2525
26+const (
27+ azureProviderName = "Azure"
28+ azureDefaultScope = "openid"
29+)
30+
31+var (
32+ // Default Login URL for Azure.
33+ // Pre-parsed URL of https://login.microsoftonline.com/common/oauth2/authorize.
34+ azureDefaultLoginURL = &url.URL{
35+ Scheme: "https",
36+ Host: "login.microsoftonline.com",
37+ Path: "/common/oauth2/authorize",
38+ }
39+
40+ // Default Redeem URL for Azure.
41+ // Pre-parsed URL of https://login.microsoftonline.com/common/oauth2/token.
42+ azureDefaultRedeemURL = &url.URL{
43+ Scheme: "https",
44+ Host: "login.microsoftonline.com",
45+ Path: "/common/oauth2/token",
46+ }
47+
48+ // Default Profile URL for Azure.
49+ // Pre-parsed URL of https://graph.microsoft.com/v1.0/me.
50+ azureDefaultProfileURL = &url.URL{
51+ Scheme: "https",
52+ Host: "graph.microsoft.com",
53+ Path: "/v1.0/me",
54+ }
55+
56+ // Default ProtectedResource URL for Azure.
57+ // Pre-parsed URL of https://graph.microsoft.com.
58+ azureDefaultProtectResourceURL = &url.URL{
59+ Scheme: "https",
60+ Host: "graph.microsoft.com",
61+ }
62+)
63+
2664// NewAzureProvider initiates a new AzureProvider
2765func NewAzureProvider(p *ProviderData) *AzureProvider {
28− p.ProviderName = "Azure"
66+ p.setProviderDefaults(providerDefaults{
67+ name: azureProviderName,
68+ loginURL: azureDefaultLoginURL,
69+ redeemURL: azureDefaultRedeemURL,
70+ profileURL: azureDefaultProfileURL,
71+ validateURL: nil,
72+ scope: azureDefaultScope,
73+ })
2974
30− if p.ProfileURL == nil || p.ProfileURL.String() == "" {
31− p.ProfileURL = &url.URL{
32− Scheme: "https",
33− Host: "graph.microsoft.com",
34− Path: "/v1.0/me",
35− }
36− }
3775 if p.ProtectedResource == nil || p.ProtectedResource.String() == "" {
38− p.ProtectedResource = &url.URL{
39− Scheme: "https",
40− Host: "graph.microsoft.com",
41− }
42− }
43− if p.Scope == "" {
44− p.Scope = "openid"
76+ p.ProtectedResource = azureDefaultProtectResourceURL
4577 }
4678
47− return &AzureProvider{ProviderData: p}
79+ return &AzureProvider{
80+ ProviderData: p,
81+ Tenant: "common",
82+ }
4883}
4984
5085// Configure defaults the AzureProvider configuration options
5186func (p *AzureProvider) Configure(tenant string) {
52− p.Tenant = tenant
53− if tenant == "" {
54− p.Tenant = "common"
87+ if tenant == "" || tenant == "common" {
88+ // tenant is empty or default, remain on the default "common" tenant
89+ return
5590 }
5691
57− if p.LoginURL == nil || p.LoginURL.String() == "" {
58− p.LoginURL = &url.URL{
59− Scheme: "https",
60− Host: "login.microsoftonline.com",
61− Path: "/" + p.Tenant + "/oauth2/authorize"}
62− }
63− if p.RedeemURL == nil || p.RedeemURL.String() == "" {
64− p.RedeemURL = &url.URL{
92+ // Specific tennant specified, override the Login and RedeemURLs
93+ p.Tenant = tenant
94+ overrideTenantURL(p.LoginURL, azureDefaultLoginURL, tenant, "authorize")
95+ overrideTenantURL(p.RedeemURL, azureDefaultRedeemURL, tenant, "token")
96+}
97+
98+func overrideTenantURL(current, defaultURL *url.URL, tenant, path string) {
99+ if current == nil || current.String() == "" || current.String() == defaultURL.String() {
100+ *current = url.URL{
65101 Scheme: "https",
66102 Host: "login.microsoftonline.com",
67− Path: "/" + p.Tenant + "/oauth2/token",
68− }
103+ Path: "/" + tenant + "/oauth2/" + path}
69104 }
70105}
71106