-
-
Notifications
You must be signed in to change notification settings - Fork 367
/
validator_test.go
274 lines (265 loc) · 6.81 KB
/
validator_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
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package openid
import (
"context"
"fmt"
"net/url"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ory/fosite"
"github.com/ory/fosite/token/jwt"
)
func TestValidatePrompt(t *testing.T) {
config := &fosite.Config{
MinParameterEntropy: fosite.MinParameterEntropy,
}
var j = &DefaultStrategy{
Signer: &jwt.DefaultSigner{
GetPrivateKey: func(_ context.Context) (interface{}, error) {
return key, nil
}},
Config: &fosite.Config{
MinParameterEntropy: fosite.MinParameterEntropy,
},
}
v := NewOpenIDConnectRequestValidator(j, config)
var genIDToken = func(c jwt.IDTokenClaims) string {
s, _, err := j.Generate(context.TODO(), c.ToMapClaims(), jwt.NewHeaders())
require.NoError(t, err)
return s
}
for k, tc := range []struct {
d string
prompt string
redirectURL string
isPublic bool
expectErr bool
idTokenHint string
s *DefaultSession
}{
{
d: "should fail because prompt=none should not work together with public clients and http non-localhost",
prompt: "none",
isPublic: true,
expectErr: true,
redirectURL: "http://foo-bar/",
s: &DefaultSession{
Subject: "foo",
Claims: &jwt.IDTokenClaims{
Subject: "foo",
RequestedAt: time.Now().UTC(),
AuthTime: time.Now().UTC().Add(-time.Minute),
},
},
},
{
d: "should pass because prompt=none works for public clients and http localhost",
prompt: "none",
isPublic: true,
expectErr: false,
redirectURL: "http://localhost/",
s: &DefaultSession{
Subject: "foo",
Claims: &jwt.IDTokenClaims{
Subject: "foo",
RequestedAt: time.Now().UTC(),
AuthTime: time.Now().UTC().Add(-time.Minute),
},
},
},
{
d: "should pass",
prompt: "none",
isPublic: true,
expectErr: false,
redirectURL: "https://foo-bar/",
s: &DefaultSession{
Subject: "foo",
Claims: &jwt.IDTokenClaims{
Subject: "foo",
RequestedAt: time.Now().UTC(),
AuthTime: time.Now().UTC().Add(-time.Minute),
},
},
},
{
d: "should fail because prompt=none requires an auth time being set",
prompt: "none",
isPublic: false,
expectErr: true,
s: &DefaultSession{
Subject: "foo",
Claims: &jwt.IDTokenClaims{
Subject: "foo",
RequestedAt: time.Now().UTC(),
},
},
},
{
d: "should fail because prompt=none and auth time is recent (after requested at)",
prompt: "none",
isPublic: false,
expectErr: true,
s: &DefaultSession{
Subject: "foo",
Claims: &jwt.IDTokenClaims{
Subject: "foo",
RequestedAt: time.Now().UTC().Add(-time.Minute),
AuthTime: time.Now().UTC(),
},
},
},
{
d: "should pass because prompt=none and auth time is in the past (before requested at)",
prompt: "none",
isPublic: false,
expectErr: false,
s: &DefaultSession{
Subject: "foo",
Claims: &jwt.IDTokenClaims{
Subject: "foo",
RequestedAt: time.Now().UTC(),
AuthTime: time.Now().UTC().Add(-time.Minute),
},
},
},
{
d: "should fail because prompt=none can not be used together with other prompts",
prompt: "none login",
isPublic: false,
expectErr: true,
s: &DefaultSession{
Subject: "foo",
Claims: &jwt.IDTokenClaims{
Subject: "foo",
RequestedAt: time.Now().UTC(),
AuthTime: time.Now().UTC(),
},
},
},
{
d: "should fail because prompt=foo is an unknown value",
prompt: "foo",
isPublic: false,
expectErr: true,
s: &DefaultSession{
Subject: "foo",
Claims: &jwt.IDTokenClaims{
Subject: "foo",
RequestedAt: time.Now().UTC(),
AuthTime: time.Now().UTC(),
},
},
},
{
d: "should pass because requesting consent and login works with public clients",
prompt: "login consent",
isPublic: true,
expectErr: false,
s: &DefaultSession{
Subject: "foo",
Claims: &jwt.IDTokenClaims{
Subject: "foo",
RequestedAt: time.Now().UTC().Add(-time.Second * 5),
AuthTime: time.Now().UTC().Add(-time.Second),
},
},
},
{
d: "should pass because requesting consent and login works with confidential clients",
prompt: "login consent",
isPublic: false,
expectErr: false,
s: &DefaultSession{
Subject: "foo",
Claims: &jwt.IDTokenClaims{
Subject: "foo",
RequestedAt: time.Now().UTC().Add(-time.Second * 5),
AuthTime: time.Now().UTC().Add(-time.Second),
},
},
},
{
d: "should fail subject from ID token does not match subject from session",
prompt: "login",
isPublic: false,
expectErr: true,
s: &DefaultSession{
Subject: "foo",
Claims: &jwt.IDTokenClaims{
Subject: "foo",
RequestedAt: time.Now().UTC(),
AuthTime: time.Now().UTC().Add(-time.Second),
},
},
idTokenHint: genIDToken(jwt.IDTokenClaims{
Subject: "bar",
RequestedAt: time.Now(),
ExpiresAt: time.Now().Add(time.Hour),
}),
},
{
d: "should pass subject from ID token matches subject from session",
prompt: "",
isPublic: false,
expectErr: false,
s: &DefaultSession{
Subject: "foo",
Claims: &jwt.IDTokenClaims{
Subject: "foo",
RequestedAt: time.Now().UTC(),
AuthTime: time.Now().UTC().Add(-time.Second),
},
},
idTokenHint: genIDToken(jwt.IDTokenClaims{
Subject: "foo",
RequestedAt: time.Now(),
ExpiresAt: time.Now().Add(time.Hour),
}),
},
{
d: "should pass subject from ID token matches subject from session even though id token is expired",
prompt: "",
isPublic: false,
expectErr: false,
s: &DefaultSession{
Subject: "foo",
Claims: &jwt.IDTokenClaims{
Subject: "foo",
RequestedAt: time.Now().UTC(),
AuthTime: time.Now().UTC().Add(-time.Second),
ExpiresAt: time.Now().UTC().Add(-time.Second),
},
},
idTokenHint: genIDToken(jwt.IDTokenClaims{
Subject: "foo",
RequestedAt: time.Now(),
ExpiresAt: time.Now().Add(time.Hour),
}),
},
} {
t.Run(fmt.Sprintf("case=%d/description=%s", k, tc.d), func(t *testing.T) {
t.Logf("%s", tc.idTokenHint)
err := v.ValidatePrompt(context.TODO(), &fosite.AuthorizeRequest{
Request: fosite.Request{
Form: url.Values{"prompt": {tc.prompt}, "id_token_hint": {tc.idTokenHint}},
Client: &fosite.DefaultClient{Public: tc.isPublic},
Session: tc.s,
},
RedirectURI: parse(tc.redirectURL),
})
if tc.expectErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}
func parse(u string) *url.URL {
o, _ := url.Parse(u)
return o
}