2019-07-28 15:54:39 +02:00
|
|
|
package providers
|
|
|
|
|
|
|
|
import (
|
2020-05-05 17:53:33 +02:00
|
|
|
"context"
|
2020-12-12 22:57:32 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-07-28 15:54:39 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
2020-09-29 18:44:42 +02:00
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
|
2020-12-12 22:57:32 +02:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/ginkgo/extensions/table"
|
2020-05-25 14:08:04 +02:00
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-07-28 15:54:39 +02:00
|
|
|
)
|
|
|
|
|
2020-12-12 22:57:32 +02:00
|
|
|
const (
|
|
|
|
keycloakAccessToken = "eyJKeycloak.eyJAccess.Token"
|
|
|
|
keycloakUserinfoPath = "/api/v3/user"
|
|
|
|
|
|
|
|
// Userinfo Test Cases
|
|
|
|
tcUIStandard = "userinfo-standard"
|
|
|
|
tcUIFail = "userinfo-fail"
|
|
|
|
tcUISingleGroup = "userinfo-single-group"
|
|
|
|
tcUIMissingEmail = "userinfo-missing-email"
|
|
|
|
tcUIMissingGroups = "userinfo-missing-groups"
|
|
|
|
)
|
|
|
|
|
|
|
|
func testKeycloakProvider(backend *httptest.Server) (*KeycloakProvider, error) {
|
2019-07-28 15:54:39 +02:00
|
|
|
p := NewKeycloakProvider(
|
|
|
|
&ProviderData{
|
|
|
|
ProviderName: "",
|
|
|
|
LoginURL: &url.URL{},
|
|
|
|
RedeemURL: &url.URL{},
|
|
|
|
ProfileURL: &url.URL{},
|
|
|
|
ValidateURL: &url.URL{},
|
|
|
|
Scope: ""})
|
|
|
|
|
2020-12-12 22:57:32 +02:00
|
|
|
if backend != nil {
|
|
|
|
bURL, err := url.Parse(backend.URL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
hostname := bURL.Host
|
2019-07-28 15:54:39 +02:00
|
|
|
|
|
|
|
updateURL(p.Data().LoginURL, hostname)
|
|
|
|
updateURL(p.Data().RedeemURL, hostname)
|
|
|
|
updateURL(p.Data().ProfileURL, hostname)
|
|
|
|
updateURL(p.Data().ValidateURL, hostname)
|
|
|
|
}
|
|
|
|
|
2020-12-12 22:57:32 +02:00
|
|
|
return p, nil
|
|
|
|
}
|
2019-07-28 15:54:39 +02:00
|
|
|
|
2020-12-12 22:57:32 +02:00
|
|
|
func testKeycloakBackend() *httptest.Server {
|
2019-07-28 15:54:39 +02:00
|
|
|
return httptest.NewServer(http.HandlerFunc(
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
2020-12-12 22:57:32 +02:00
|
|
|
if r.URL.Path != keycloakUserinfoPath {
|
2019-07-28 15:54:39 +02:00
|
|
|
w.WriteHeader(404)
|
2020-12-12 22:57:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
switch r.URL.Query().Get("testcase") {
|
|
|
|
case tcUIStandard:
|
|
|
|
w.WriteHeader(200)
|
|
|
|
_, err = w.Write([]byte(`
|
|
|
|
{
|
|
|
|
"email": "michael.bland@gsa.gov",
|
|
|
|
"groups": [
|
|
|
|
"test-grp1",
|
|
|
|
"test-grp2"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`))
|
|
|
|
case tcUIFail:
|
|
|
|
w.WriteHeader(500)
|
|
|
|
case tcUISingleGroup:
|
|
|
|
w.WriteHeader(200)
|
|
|
|
_, err = w.Write([]byte(`
|
|
|
|
{
|
|
|
|
"email": "michael.bland@gsa.gov",
|
|
|
|
"groups": ["test-grp1"]
|
|
|
|
}
|
|
|
|
`))
|
|
|
|
case tcUIMissingEmail:
|
|
|
|
w.WriteHeader(200)
|
|
|
|
_, err = w.Write([]byte(`
|
|
|
|
{
|
|
|
|
"groups": [
|
|
|
|
"test-grp1",
|
|
|
|
"test-grp2"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`))
|
|
|
|
case tcUIMissingGroups:
|
2019-07-28 15:54:39 +02:00
|
|
|
w.WriteHeader(200)
|
2020-12-12 22:57:32 +02:00
|
|
|
_, err = w.Write([]byte(`
|
|
|
|
{
|
|
|
|
"email": "michael.bland@gsa.gov"
|
|
|
|
}
|
|
|
|
`))
|
|
|
|
default:
|
|
|
|
w.WriteHeader(404)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2019-07-28 15:54:39 +02:00
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2020-12-12 22:57:32 +02:00
|
|
|
var _ = Describe("Keycloak Provider Tests", func() {
|
|
|
|
var p *KeycloakProvider
|
|
|
|
var b *httptest.Server
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
b = testKeycloakBackend()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
p, err = testKeycloakProvider(b)
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
|
|
|
b.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
Context("EnrichSession", func() {
|
|
|
|
type enrichSessionTableInput struct {
|
|
|
|
testcase string
|
|
|
|
expectedError error
|
|
|
|
expectedEmail string
|
|
|
|
expectedGroups []string
|
|
|
|
}
|
|
|
|
|
|
|
|
DescribeTable("should return expected results",
|
|
|
|
func(in enrichSessionTableInput) {
|
|
|
|
var err error
|
|
|
|
p.ValidateURL, err = url.Parse(
|
|
|
|
fmt.Sprintf("%s%s?testcase=%s", b.URL, keycloakUserinfoPath, in.testcase),
|
|
|
|
)
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
|
|
|
session := &sessions.SessionState{AccessToken: keycloakAccessToken}
|
|
|
|
err = p.EnrichSession(context.Background(), session)
|
|
|
|
|
|
|
|
if in.expectedError != nil {
|
|
|
|
Expect(err).To(Equal(in.expectedError))
|
|
|
|
} else {
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
}
|
|
|
|
|
|
|
|
Expect(session.Email).To(Equal(in.expectedEmail))
|
|
|
|
|
|
|
|
if in.expectedGroups != nil {
|
|
|
|
Expect(session.Groups).To(Equal(in.expectedGroups))
|
|
|
|
} else {
|
|
|
|
Expect(session.Groups).To(BeNil())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Entry("email and multiple groups", enrichSessionTableInput{
|
|
|
|
testcase: tcUIStandard,
|
|
|
|
expectedError: nil,
|
|
|
|
expectedEmail: "michael.bland@gsa.gov",
|
|
|
|
expectedGroups: []string{"test-grp1", "test-grp2"},
|
|
|
|
}),
|
|
|
|
Entry("email and single group", enrichSessionTableInput{
|
|
|
|
testcase: tcUISingleGroup,
|
|
|
|
expectedError: nil,
|
|
|
|
expectedEmail: "michael.bland@gsa.gov",
|
|
|
|
expectedGroups: []string{"test-grp1"},
|
|
|
|
}),
|
|
|
|
Entry("email and no groups", enrichSessionTableInput{
|
|
|
|
testcase: tcUIMissingGroups,
|
|
|
|
expectedError: nil,
|
|
|
|
expectedEmail: "michael.bland@gsa.gov",
|
|
|
|
expectedGroups: nil,
|
|
|
|
}),
|
|
|
|
Entry("missing email", enrichSessionTableInput{
|
|
|
|
testcase: tcUIMissingEmail,
|
|
|
|
expectedError: errors.New(
|
|
|
|
"unable to extract email from userinfo endpoint: type assertion to string failed"),
|
|
|
|
expectedEmail: "",
|
|
|
|
expectedGroups: []string{"test-grp1", "test-grp2"},
|
|
|
|
}),
|
|
|
|
Entry("request failure", enrichSessionTableInput{
|
|
|
|
testcase: tcUIFail,
|
|
|
|
expectedError: errors.New(`unexpected status "500": `),
|
|
|
|
expectedEmail: "",
|
|
|
|
expectedGroups: nil,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-07-28 15:54:39 +02:00
|
|
|
func TestKeycloakProviderDefaults(t *testing.T) {
|
2020-12-12 22:57:32 +02:00
|
|
|
p, err := testKeycloakProvider(nil)
|
|
|
|
assert.NoError(t, err)
|
2019-07-28 15:54:39 +02:00
|
|
|
assert.NotEqual(t, nil, p)
|
|
|
|
assert.Equal(t, "Keycloak", p.Data().ProviderName)
|
|
|
|
assert.Equal(t, "https://keycloak.org/oauth/authorize",
|
|
|
|
p.Data().LoginURL.String())
|
|
|
|
assert.Equal(t, "https://keycloak.org/oauth/token",
|
|
|
|
p.Data().RedeemURL.String())
|
|
|
|
assert.Equal(t, "https://keycloak.org/api/v3/user",
|
|
|
|
p.Data().ValidateURL.String())
|
|
|
|
assert.Equal(t, "api", p.Data().Scope)
|
|
|
|
}
|
|
|
|
|
2020-05-25 14:08:04 +02:00
|
|
|
func TestNewKeycloakProvider(t *testing.T) {
|
|
|
|
g := NewWithT(t)
|
|
|
|
|
|
|
|
// Test that defaults are set when calling for a new provider with nothing set
|
|
|
|
providerData := NewKeycloakProvider(&ProviderData{}).Data()
|
|
|
|
g.Expect(providerData.ProviderName).To(Equal("Keycloak"))
|
|
|
|
g.Expect(providerData.LoginURL.String()).To(Equal("https://keycloak.org/oauth/authorize"))
|
|
|
|
g.Expect(providerData.RedeemURL.String()).To(Equal("https://keycloak.org/oauth/token"))
|
|
|
|
g.Expect(providerData.ProfileURL.String()).To(Equal(""))
|
|
|
|
g.Expect(providerData.ValidateURL.String()).To(Equal("https://keycloak.org/api/v3/user"))
|
|
|
|
g.Expect(providerData.Scope).To(Equal("api"))
|
|
|
|
}
|
|
|
|
|
2019-07-28 15:54:39 +02:00
|
|
|
func TestKeycloakProviderOverrides(t *testing.T) {
|
|
|
|
p := NewKeycloakProvider(
|
|
|
|
&ProviderData{
|
|
|
|
LoginURL: &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "example.com",
|
|
|
|
Path: "/oauth/auth"},
|
|
|
|
RedeemURL: &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "example.com",
|
|
|
|
Path: "/oauth/token"},
|
|
|
|
ValidateURL: &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "example.com",
|
|
|
|
Path: "/api/v3/user"},
|
|
|
|
Scope: "profile"})
|
|
|
|
assert.NotEqual(t, nil, p)
|
|
|
|
assert.Equal(t, "Keycloak", 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/api/v3/user",
|
|
|
|
p.Data().ValidateURL.String())
|
|
|
|
assert.Equal(t, "profile", p.Data().Scope)
|
|
|
|
}
|