Repository navigation
Expand file tree
/
Copy pathoidc_test.go
More file actions
98 lines (77 loc) · 2.8 KB
/
Copy pathoidc_test.go
File metadata and controls
98 lines (77 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package oidc
import (
"encoding/json"
"io/ioutil"
"net/http/httptest"
"net/url"
"testing"
gooidc "github.com/coreos/go-oidc"
"github.com/stretchr/testify/assert"
"golang.org/x/oauth2"
"github.com/argoproj/argo-cd/server/settings/oidc"
)
func TestInferGrantType(t *testing.T) {
for _, path := range []string{"dex", "okta", "auth0", "onelogin"} {
t.Run(path, func(t *testing.T) {
rawConfig, err := ioutil.ReadFile("testdata/" + path + ".json")
assert.NoError(t, err)
var config OIDCConfiguration
err = json.Unmarshal(rawConfig, &config)
assert.NoError(t, err)
grantType := InferGrantType(&config)
assert.Equal(t, GrantTypeAuthorizationCode, grantType)
var noCodeResponseTypes []string
for _, supportedResponseType := range config.ResponseTypesSupported {
if supportedResponseType != ResponseTypeCode {
noCodeResponseTypes = append(noCodeResponseTypes, supportedResponseType)
}
}
config.ResponseTypesSupported = noCodeResponseTypes
grantType = InferGrantType(&config)
assert.Equal(t, GrantTypeImplicit, grantType)
})
}
}
func TestIDTokenClaims(t *testing.T) {
oauth2Config := &oauth2.Config{
ClientID: "DUMMY_OIDC_PROVIDER",
ClientSecret: "0987654321",
Endpoint: oauth2.Endpoint{AuthURL: "https://argocd-dev.onelogin.com/oidc/auth", TokenURL: "https://argocd-dev.onelogin.com/oidc/token"},
Scopes: []string{"oidc", "profile", "groups"},
RedirectURL: "https://argocd-dev.io/redirect_url",
}
var opts []oauth2.AuthCodeOption
requestedClaims := make(map[string]*oidc.Claim)
opts = AppendClaimsAuthenticationRequestParameter(opts, requestedClaims)
assert.Len(t, opts, 0)
requestedClaims["groups"] = &oidc.Claim{Essential: true}
opts = AppendClaimsAuthenticationRequestParameter(opts, requestedClaims)
assert.Len(t, opts, 1)
authCodeURL, err := url.Parse(oauth2Config.AuthCodeURL("TEST", opts...))
assert.NoError(t, err)
values, err := url.ParseQuery(authCodeURL.RawQuery)
assert.NoError(t, err)
assert.Equal(t, "{\"id_token\":{\"groups\":{\"essential\":true}}}", values.Get("claims"))
}
type fakeProvider struct {
}
func (p *fakeProvider) Endpoint() (*oauth2.Endpoint, error) {
return &oauth2.Endpoint{}, nil
}
func (p *fakeProvider) ParseConfig() (*OIDCConfiguration, error) {
return nil, nil
}
func (p *fakeProvider) Verify(_, _ string) (*gooidc.IDToken, error) {
return nil, nil
}
func TestHandleCallback(t *testing.T) {
app := ClientApp{provider: &fakeProvider{}}
req := httptest.NewRequest("GET", "http://example.com/foo", nil)
req.Form = url.Values{
"error": []string{"login-failed"},
"error_description": []string{"<script>alert('hello')</script>"},
}
w := httptest.NewRecorder()
app.HandleCallback(w, req)
assert.Equal(t, "login-failed: <script>alert('hello')</script>\n", w.Body.String())
}