2015-11-09 10:28:34 +02:00
|
|
|
package providers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
2017-10-23 18:23:46 +02:00
|
|
|
|
2019-05-05 14:33:13 +02:00
|
|
|
"github.com/pusher/oauth2_proxy/pkg/apis/sessions"
|
2017-10-23 18:23:46 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-11-09 10:28:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func testAzureProvider(hostname string) *AzureProvider {
|
|
|
|
p := NewAzureProvider(
|
|
|
|
&ProviderData{
|
|
|
|
ProviderName: "",
|
|
|
|
LoginURL: &url.URL{},
|
|
|
|
RedeemURL: &url.URL{},
|
|
|
|
ProfileURL: &url.URL{},
|
|
|
|
ValidateURL: &url.URL{},
|
|
|
|
ProtectedResource: &url.URL{},
|
|
|
|
Scope: ""})
|
|
|
|
if hostname != "" {
|
|
|
|
updateURL(p.Data().LoginURL, hostname)
|
|
|
|
updateURL(p.Data().RedeemURL, hostname)
|
|
|
|
updateURL(p.Data().ProfileURL, hostname)
|
|
|
|
updateURL(p.Data().ValidateURL, hostname)
|
|
|
|
updateURL(p.Data().ProtectedResource, hostname)
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAzureProviderDefaults(t *testing.T) {
|
|
|
|
p := testAzureProvider("")
|
|
|
|
assert.NotEqual(t, nil, p)
|
|
|
|
p.Configure("")
|
|
|
|
assert.Equal(t, "Azure", p.Data().ProviderName)
|
|
|
|
assert.Equal(t, "common", p.Tenant)
|
|
|
|
assert.Equal(t, "https://login.microsoftonline.com/common/oauth2/authorize",
|
|
|
|
p.Data().LoginURL.String())
|
|
|
|
assert.Equal(t, "https://login.microsoftonline.com/common/oauth2/token",
|
|
|
|
p.Data().RedeemURL.String())
|
|
|
|
assert.Equal(t, "https://graph.windows.net/me?api-version=1.6",
|
|
|
|
p.Data().ProfileURL.String())
|
|
|
|
assert.Equal(t, "https://graph.windows.net",
|
|
|
|
p.Data().ProtectedResource.String())
|
|
|
|
assert.Equal(t, "",
|
|
|
|
p.Data().ValidateURL.String())
|
|
|
|
assert.Equal(t, "openid", p.Data().Scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAzureProviderOverrides(t *testing.T) {
|
|
|
|
p := NewAzureProvider(
|
|
|
|
&ProviderData{
|
|
|
|
LoginURL: &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "example.com",
|
|
|
|
Path: "/oauth/auth"},
|
|
|
|
RedeemURL: &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "example.com",
|
|
|
|
Path: "/oauth/token"},
|
|
|
|
ProfileURL: &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "example.com",
|
|
|
|
Path: "/oauth/profile"},
|
|
|
|
ValidateURL: &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "example.com",
|
|
|
|
Path: "/oauth/tokeninfo"},
|
|
|
|
ProtectedResource: &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "example.com"},
|
|
|
|
Scope: "profile"})
|
|
|
|
assert.NotEqual(t, nil, p)
|
|
|
|
assert.Equal(t, "Azure", p.Data().ProviderName)
|
|
|
|
assert.Equal(t, "https://example.com/oauth/auth",
|
|
|
|
p.Data().LoginURL.String())
|
|
|
|
assert.Equal(t, "https://example.com/oauth/token",
|
|
|
|
p.Data().RedeemURL.String())
|
|
|
|
assert.Equal(t, "https://example.com/oauth/profile",
|
|
|
|
p.Data().ProfileURL.String())
|
|
|
|
assert.Equal(t, "https://example.com/oauth/tokeninfo",
|
|
|
|
p.Data().ValidateURL.String())
|
|
|
|
assert.Equal(t, "https://example.com",
|
|
|
|
p.Data().ProtectedResource.String())
|
|
|
|
assert.Equal(t, "profile", p.Data().Scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAzureSetTenant(t *testing.T) {
|
|
|
|
p := testAzureProvider("")
|
|
|
|
p.Configure("example")
|
|
|
|
assert.Equal(t, "Azure", p.Data().ProviderName)
|
|
|
|
assert.Equal(t, "example", p.Tenant)
|
|
|
|
assert.Equal(t, "https://login.microsoftonline.com/example/oauth2/authorize",
|
|
|
|
p.Data().LoginURL.String())
|
|
|
|
assert.Equal(t, "https://login.microsoftonline.com/example/oauth2/token",
|
|
|
|
p.Data().RedeemURL.String())
|
|
|
|
assert.Equal(t, "https://graph.windows.net/me?api-version=1.6",
|
|
|
|
p.Data().ProfileURL.String())
|
|
|
|
assert.Equal(t, "https://graph.windows.net",
|
|
|
|
p.Data().ProtectedResource.String())
|
|
|
|
assert.Equal(t, "",
|
|
|
|
p.Data().ValidateURL.String())
|
|
|
|
assert.Equal(t, "openid", p.Data().Scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAzureBackend(payload string) *httptest.Server {
|
|
|
|
path := "/me"
|
|
|
|
query := "api-version=1.6"
|
|
|
|
|
|
|
|
return httptest.NewServer(http.HandlerFunc(
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
2018-11-29 16:26:41 +02:00
|
|
|
if r.URL.Path != path || r.URL.RawQuery != query {
|
2015-11-09 10:28:34 +02:00
|
|
|
w.WriteHeader(404)
|
|
|
|
} else if r.Header.Get("Authorization") != "Bearer imaginary_access_token" {
|
|
|
|
w.WriteHeader(403)
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(200)
|
|
|
|
w.Write([]byte(payload))
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAzureProviderGetEmailAddress(t *testing.T) {
|
|
|
|
b := testAzureBackend(`{ "mail": "user@windows.net" }`)
|
|
|
|
defer b.Close()
|
|
|
|
|
2016-06-29 09:00:08 +02:00
|
|
|
bURL, _ := url.Parse(b.URL)
|
|
|
|
p := testAzureProvider(bURL.Host)
|
2015-11-09 10:28:34 +02:00
|
|
|
|
2019-05-05 14:33:13 +02:00
|
|
|
session := &sessions.SessionState{AccessToken: "imaginary_access_token"}
|
2015-11-09 10:28:34 +02:00
|
|
|
email, err := p.GetEmailAddress(session)
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
assert.Equal(t, "user@windows.net", email)
|
|
|
|
}
|
2016-06-29 09:00:08 +02:00
|
|
|
|
|
|
|
func TestAzureProviderGetEmailAddressMailNull(t *testing.T) {
|
|
|
|
b := testAzureBackend(`{ "mail": null, "otherMails": ["user@windows.net", "altuser@windows.net"] }`)
|
|
|
|
defer b.Close()
|
|
|
|
|
|
|
|
bURL, _ := url.Parse(b.URL)
|
|
|
|
p := testAzureProvider(bURL.Host)
|
|
|
|
|
2019-05-05 14:33:13 +02:00
|
|
|
session := &sessions.SessionState{AccessToken: "imaginary_access_token"}
|
2016-06-29 09:00:08 +02:00
|
|
|
email, err := p.GetEmailAddress(session)
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
assert.Equal(t, "user@windows.net", email)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAzureProviderGetEmailAddressGetUserPrincipalName(t *testing.T) {
|
|
|
|
b := testAzureBackend(`{ "mail": null, "otherMails": [], "userPrincipalName": "user@windows.net" }`)
|
|
|
|
defer b.Close()
|
|
|
|
|
|
|
|
bURL, _ := url.Parse(b.URL)
|
|
|
|
p := testAzureProvider(bURL.Host)
|
|
|
|
|
2019-05-05 14:33:13 +02:00
|
|
|
session := &sessions.SessionState{AccessToken: "imaginary_access_token"}
|
2016-06-29 09:00:08 +02:00
|
|
|
email, err := p.GetEmailAddress(session)
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
assert.Equal(t, "user@windows.net", email)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAzureProviderGetEmailAddressFailToGetEmailAddress(t *testing.T) {
|
|
|
|
b := testAzureBackend(`{ "mail": null, "otherMails": [], "userPrincipalName": null }`)
|
|
|
|
defer b.Close()
|
|
|
|
|
|
|
|
bURL, _ := url.Parse(b.URL)
|
|
|
|
p := testAzureProvider(bURL.Host)
|
|
|
|
|
2019-05-05 14:33:13 +02:00
|
|
|
session := &sessions.SessionState{AccessToken: "imaginary_access_token"}
|
2016-06-29 09:00:08 +02:00
|
|
|
email, err := p.GetEmailAddress(session)
|
|
|
|
assert.Equal(t, "type assertion to string failed", err.Error())
|
|
|
|
assert.Equal(t, "", email)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAzureProviderGetEmailAddressEmptyUserPrincipalName(t *testing.T) {
|
|
|
|
b := testAzureBackend(`{ "mail": null, "otherMails": [], "userPrincipalName": "" }`)
|
|
|
|
defer b.Close()
|
|
|
|
|
|
|
|
bURL, _ := url.Parse(b.URL)
|
|
|
|
p := testAzureProvider(bURL.Host)
|
|
|
|
|
2019-05-05 14:33:13 +02:00
|
|
|
session := &sessions.SessionState{AccessToken: "imaginary_access_token"}
|
2016-06-29 09:00:08 +02:00
|
|
|
email, err := p.GetEmailAddress(session)
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
assert.Equal(t, "", email)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAzureProviderGetEmailAddressIncorrectOtherMails(t *testing.T) {
|
|
|
|
b := testAzureBackend(`{ "mail": null, "otherMails": "", "userPrincipalName": null }`)
|
|
|
|
defer b.Close()
|
|
|
|
|
|
|
|
bURL, _ := url.Parse(b.URL)
|
|
|
|
p := testAzureProvider(bURL.Host)
|
|
|
|
|
2019-05-05 14:33:13 +02:00
|
|
|
session := &sessions.SessionState{AccessToken: "imaginary_access_token"}
|
2016-06-29 09:00:08 +02:00
|
|
|
email, err := p.GetEmailAddress(session)
|
|
|
|
assert.Equal(t, "type assertion to string failed", err.Error())
|
|
|
|
assert.Equal(t, "", email)
|
2017-03-29 15:36:38 +02:00
|
|
|
}
|