This repository has been archived by the owner on Sep 5, 2022. It is now read-only.
forked from coreos/go-oidc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoidc_test.go
299 lines (289 loc) · 7.65 KB
/
oidc_test.go
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package oidc
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
)
const (
// at_hash value and access_token returned by Google.
googleAccessTokenHash = "piwt8oCH-K2D9pXlaS1Y-w"
googleAccessToken = "ya29.CjHSA1l5WUn8xZ6HanHFzzdHdbXm-14rxnC7JHch9eFIsZkQEGoWzaYG4o7k5f6BnPLj"
googleSigningAlg = RS256
// following values computed by own algo for regression testing
computed384TokenHash = "_ILKVQjbEzFKNJjUKC2kz9eReYi0A9Of"
computed512TokenHash = "Spa_APgwBrarSeQbxI-rbragXho6dqFpH5x9PqaPfUI"
)
type accessTokenTest struct {
name string
tok *IDToken
accessToken string
verifier func(err error) error
}
func (a accessTokenTest) run(t *testing.T) {
err := a.tok.VerifyAccessToken(a.accessToken)
result := a.verifier(err)
if result != nil {
t.Error(result)
}
}
func TestAccessTokenVerification(t *testing.T) {
newToken := func(alg, atHash string) *IDToken {
return &IDToken{sigAlgorithm: alg, AccessTokenHash: atHash}
}
assertNil := func(err error) error {
if err != nil {
return fmt.Errorf("want nil error, got %v", err)
}
return nil
}
assertMsg := func(msg string) func(err error) error {
return func(err error) error {
if err == nil {
return fmt.Errorf("expected error, got success")
}
if err.Error() != msg {
return fmt.Errorf("bad error message, %q, (want %q)", err.Error(), msg)
}
return nil
}
}
tests := []accessTokenTest{
{
"goodRS256",
newToken(googleSigningAlg, googleAccessTokenHash),
googleAccessToken,
assertNil,
},
{
"goodES384",
newToken("ES384", computed384TokenHash),
googleAccessToken,
assertNil,
},
{
"goodPS512",
newToken("PS512", computed512TokenHash),
googleAccessToken,
assertNil,
},
{
"badRS256",
newToken("RS256", computed512TokenHash),
googleAccessToken,
assertMsg("access token hash does not match value in ID token"),
},
{
"nohash",
newToken("RS256", ""),
googleAccessToken,
assertMsg("id token did not have an access token hash"),
},
{
"badSignAlgo",
newToken("none", "xxx"),
googleAccessToken,
assertMsg(`oidc: unsupported signing algorithm "none"`),
},
}
for _, test := range tests {
t.Run(test.name, test.run)
}
}
func TestNewProvider(t *testing.T) {
tests := []struct {
name string
data string
trailingSlash bool
wantAuthURL string
wantTokenURL string
wantUserInfoURL string
wantAlgorithms []string
wantErr bool
}{
{
name: "basic_case",
data: `{
"issuer": "ISSUER",
"authorization_endpoint": "https://example.com/auth",
"token_endpoint": "https://example.com/token",
"jwks_uri": "https://example.com/keys",
"id_token_signing_alg_values_supported": ["RS256"]
}`,
wantAuthURL: "https://example.com/auth",
wantTokenURL: "https://example.com/token",
wantAlgorithms: []string{"RS256"},
},
{
name: "additional_algorithms",
data: `{
"issuer": "ISSUER",
"authorization_endpoint": "https://example.com/auth",
"token_endpoint": "https://example.com/token",
"jwks_uri": "https://example.com/keys",
"id_token_signing_alg_values_supported": ["RS256", "RS384", "ES256"]
}`,
wantAuthURL: "https://example.com/auth",
wantTokenURL: "https://example.com/token",
wantAlgorithms: []string{"RS256", "RS384", "ES256"},
},
{
name: "unsupported_algorithms",
data: `{
"issuer": "ISSUER",
"authorization_endpoint": "https://example.com/auth",
"token_endpoint": "https://example.com/token",
"jwks_uri": "https://example.com/keys",
"id_token_signing_alg_values_supported": [
"RS256", "RS384", "ES256", "HS256", "none"
]
}`,
wantAuthURL: "https://example.com/auth",
wantTokenURL: "https://example.com/token",
wantAlgorithms: []string{"RS256", "RS384", "ES256"},
},
{
name: "mismatched_issuer",
data: `{
"issuer": "https://example.com",
"authorization_endpoint": "https://example.com/auth",
"token_endpoint": "https://example.com/token",
"jwks_uri": "https://example.com/keys",
"id_token_signing_alg_values_supported": ["RS256"]
}`,
wantErr: true,
},
{
name: "issuer_with_trailing_slash",
data: `{
"issuer": "ISSUER",
"authorization_endpoint": "https://example.com/auth",
"token_endpoint": "https://example.com/token",
"jwks_uri": "https://example.com/keys",
"id_token_signing_alg_values_supported": ["RS256"]
}`,
trailingSlash: true,
wantAuthURL: "https://example.com/auth",
wantTokenURL: "https://example.com/token",
wantAlgorithms: []string{"RS256"},
},
{
// Test case taken directly from:
// https://accounts.google.com/.well-known/openid-configuration
name: "google",
wantAuthURL: "https://accounts.google.com/o/oauth2/v2/auth",
wantTokenURL: "https://oauth2.googleapis.com/token",
wantUserInfoURL: "https://openidconnect.googleapis.com/v1/userinfo",
wantAlgorithms: []string{"RS256"},
data: `{
"issuer": "ISSUER",
"authorization_endpoint": "https://accounts.google.com/o/oauth2/v2/auth",
"device_authorization_endpoint": "https://oauth2.googleapis.com/device/code",
"token_endpoint": "https://oauth2.googleapis.com/token",
"userinfo_endpoint": "https://openidconnect.googleapis.com/v1/userinfo",
"revocation_endpoint": "https://oauth2.googleapis.com/revoke",
"jwks_uri": "https://www.googleapis.com/oauth2/v3/certs",
"response_types_supported": [
"code",
"token",
"id_token",
"code token",
"code id_token",
"token id_token",
"code token id_token",
"none"
],
"subject_types_supported": [
"public"
],
"id_token_signing_alg_values_supported": [
"RS256"
],
"scopes_supported": [
"openid",
"email",
"profile"
],
"token_endpoint_auth_methods_supported": [
"client_secret_post",
"client_secret_basic"
],
"claims_supported": [
"aud",
"email",
"email_verified",
"exp",
"family_name",
"given_name",
"iat",
"iss",
"locale",
"name",
"picture",
"sub"
],
"code_challenge_methods_supported": [
"plain",
"S256"
],
"grant_types_supported": [
"authorization_code",
"refresh_token",
"urn:ietf:params:oauth:grant-type:device_code",
"urn:ietf:params:oauth:grant-type:jwt-bearer"
]
}`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var issuer string
hf := func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/.well-known/openid-configuration" {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, strings.ReplaceAll(test.data, "ISSUER", issuer))
}
s := httptest.NewServer(http.HandlerFunc(hf))
defer s.Close()
issuer = s.URL
if test.trailingSlash {
issuer += "/"
}
p, err := NewProvider(ctx, issuer)
if err != nil {
if !test.wantErr {
t.Errorf("NewProvider() failed: %v", err)
}
return
}
if test.wantErr {
t.Fatalf("NewProvider(): expected error")
}
if p.authURL != test.wantAuthURL {
t.Errorf("NewProvider() unexpected authURL value, got=%s, want=%s",
p.authURL, test.wantAuthURL)
}
if p.tokenURL != test.wantTokenURL {
t.Errorf("NewProvider() unexpected tokenURL value, got=%s, want=%s",
p.tokenURL, test.wantTokenURL)
}
if p.userInfoURL != test.wantUserInfoURL {
t.Errorf("NewProvider() unexpected userInfoURL value, got=%s, want=%s",
p.userInfoURL, test.wantUserInfoURL)
}
if !reflect.DeepEqual(p.algorithms, test.wantAlgorithms) {
t.Errorf("NewProvider() unexpected algorithms value, got=%s, want=%s",
p.algorithms, test.wantAlgorithms)
}
})
}
}