5
5
"fmt"
6
6
"net/http"
7
7
"net/http/httptest"
8
- "net/url"
9
8
"testing"
10
9
11
10
"github.com/gavv/httpexpect"
@@ -26,22 +25,30 @@ var (
26
25
clientSecret = "11111111"
27
26
28
27
plainChallenge = "ThisIsAFourtyThreeCharactersLongStringThing"
29
- s256Challenge = "s256test"
30
- // echo s256test | sha256 | base64 | tr '/+' '_-'
31
- s256ChallengeHash = "W6YWc_4yHwYN-cGDgGmOMHF3l7KDy7VcRjf7q2FVF-o="
28
+ s256Challenge = "s256tests256tests256tests256tests256tests256test"
29
+ // sha2562 := sha256.Sum256([]byte(s256Challenge))
30
+ // fmt.Printf(base64.URLEncoding.EncodeToString(sha2562[:]))
31
+ s256ChallengeHash = "To2Xqv01cm16bC9Sf7KRRS8CO2SFss_HSMQOr3sdCDE="
32
32
)
33
33
34
34
func init () {
35
35
manager = manage .NewDefaultManager ()
36
36
manager .MustTokenStorage (store .NewMemoryTokenStore ())
37
37
}
38
38
39
- func clientStore (domain string ) oauth2.ClientStore {
39
+ func clientStore (domain string , public bool ) oauth2.ClientStore {
40
40
clientStore := store .NewClientStore ()
41
+ var secret string
42
+ if public {
43
+ secret = ""
44
+ } else {
45
+ secret = clientSecret
46
+ }
41
47
clientStore .Set (clientID , & models.Client {
42
48
ID : clientID ,
43
- Secret : clientSecret ,
49
+ Secret : secret ,
44
50
Domain : domain ,
51
+ Public : public ,
45
52
})
46
53
return clientStore
47
54
}
@@ -95,7 +102,7 @@ func TestAuthorizeCode(t *testing.T) {
95
102
}))
96
103
defer csrv .Close ()
97
104
98
- manager .MapClientStorage (clientStore (csrv .URL ))
105
+ manager .MapClientStorage (clientStore (csrv .URL , true ))
99
106
srv = server .NewDefaultServer (manager )
100
107
srv .SetUserAuthorizationHandler (func (w http.ResponseWriter , r * http.Request ) (userID string , err error ) {
101
108
userID = "000000"
@@ -107,7 +114,7 @@ func TestAuthorizeCode(t *testing.T) {
107
114
WithQuery ("client_id" , clientID ).
108
115
WithQuery ("scope" , "all" ).
109
116
WithQuery ("state" , "123" ).
110
- WithQuery ("redirect_uri" , url . QueryEscape ( csrv .URL + "/oauth2" ) ).
117
+ WithQuery ("redirect_uri" , csrv .URL + "/oauth2" ).
111
118
Expect ().Status (http .StatusOK )
112
119
}
113
120
@@ -134,7 +141,7 @@ func TestAuthorizeCodeWithChallengePlain(t *testing.T) {
134
141
WithFormField ("grant_type" , "authorization_code" ).
135
142
WithFormField ("client_id" , clientID ).
136
143
WithFormField ("code" , code ).
137
- WithBasicAuth ("code_verifier" , "testchallenge" ).
144
+ WithFormField ("code_verifier" , plainChallenge ).
138
145
Expect ().
139
146
Status (http .StatusOK ).
140
147
JSON ().Object ()
@@ -146,19 +153,20 @@ func TestAuthorizeCodeWithChallengePlain(t *testing.T) {
146
153
}))
147
154
defer csrv .Close ()
148
155
149
- manager .MapClientStorage (clientStore (csrv .URL ))
156
+ manager .MapClientStorage (clientStore (csrv .URL , true ))
150
157
srv = server .NewDefaultServer (manager )
151
158
srv .SetUserAuthorizationHandler (func (w http.ResponseWriter , r * http.Request ) (userID string , err error ) {
152
159
userID = "000000"
153
160
return
154
161
})
162
+ srv .SetClientInfoHandler (server .ClientFormHandler )
155
163
156
164
e .GET ("/authorize" ).
157
165
WithQuery ("response_type" , "code" ).
158
166
WithQuery ("client_id" , clientID ).
159
167
WithQuery ("scope" , "all" ).
160
168
WithQuery ("state" , "123" ).
161
- WithQuery ("redirect_uri" , url . QueryEscape ( csrv .URL + "/oauth2" ) ).
169
+ WithQuery ("redirect_uri" , csrv .URL + "/oauth2" ).
162
170
WithQuery ("code_challenge" , plainChallenge ).
163
171
Expect ().Status (http .StatusOK )
164
172
}
@@ -186,7 +194,7 @@ func TestAuthorizeCodeWithChallengeS256(t *testing.T) {
186
194
WithFormField ("grant_type" , "authorization_code" ).
187
195
WithFormField ("client_id" , clientID ).
188
196
WithFormField ("code" , code ).
189
- WithBasicAuth ("code_verifier" , s256Challenge ).
197
+ WithFormField ("code_verifier" , s256Challenge ).
190
198
Expect ().
191
199
Status (http .StatusOK ).
192
200
JSON ().Object ()
@@ -198,19 +206,20 @@ func TestAuthorizeCodeWithChallengeS256(t *testing.T) {
198
206
}))
199
207
defer csrv .Close ()
200
208
201
- manager .MapClientStorage (clientStore (csrv .URL ))
209
+ manager .MapClientStorage (clientStore (csrv .URL , true ))
202
210
srv = server .NewDefaultServer (manager )
203
211
srv .SetUserAuthorizationHandler (func (w http.ResponseWriter , r * http.Request ) (userID string , err error ) {
204
212
userID = "000000"
205
213
return
206
214
})
215
+ srv .SetClientInfoHandler (server .ClientFormHandler )
207
216
208
217
e .GET ("/authorize" ).
209
218
WithQuery ("response_type" , "code" ).
210
219
WithQuery ("client_id" , clientID ).
211
220
WithQuery ("scope" , "all" ).
212
221
WithQuery ("state" , "123" ).
213
- WithQuery ("redirect_uri" , url . QueryEscape ( csrv .URL + "/oauth2" ) ).
222
+ WithQuery ("redirect_uri" , csrv .URL + "/oauth2" ).
214
223
WithQuery ("code_challenge" , s256ChallengeHash ).
215
224
WithQuery ("code_challenge_method" , "S256" ).
216
225
Expect ().Status (http .StatusOK )
@@ -226,7 +235,7 @@ func TestImplicit(t *testing.T) {
226
235
csrv = httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {}))
227
236
defer csrv .Close ()
228
237
229
- manager .MapClientStorage (clientStore (csrv .URL ))
238
+ manager .MapClientStorage (clientStore (csrv .URL , false ))
230
239
srv = server .NewDefaultServer (manager )
231
240
srv .SetUserAuthorizationHandler (func (w http.ResponseWriter , r * http.Request ) (userID string , err error ) {
232
241
userID = "000000"
@@ -238,7 +247,7 @@ func TestImplicit(t *testing.T) {
238
247
WithQuery ("client_id" , clientID ).
239
248
WithQuery ("scope" , "all" ).
240
249
WithQuery ("state" , "123" ).
241
- WithQuery ("redirect_uri" , url . QueryEscape ( csrv .URL + "/oauth2" ) ).
250
+ WithQuery ("redirect_uri" , csrv .URL + "/oauth2" ).
242
251
Expect ().Status (http .StatusOK )
243
252
}
244
253
@@ -249,7 +258,7 @@ func TestPasswordCredentials(t *testing.T) {
249
258
defer tsrv .Close ()
250
259
e := httpexpect .New (t , tsrv .URL )
251
260
252
- manager .MapClientStorage (clientStore ("" ))
261
+ manager .MapClientStorage (clientStore ("" , false ))
253
262
srv = server .NewDefaultServer (manager )
254
263
srv .SetPasswordAuthorizationHandler (func (ctx context.Context , clientID , username , password string ) (userID string , err error ) {
255
264
if username == "admin" && password == "123456" {
@@ -282,7 +291,7 @@ func TestClientCredentials(t *testing.T) {
282
291
defer tsrv .Close ()
283
292
e := httpexpect .New (t , tsrv .URL )
284
293
285
- manager .MapClientStorage (clientStore ("" ))
294
+ manager .MapClientStorage (clientStore ("" , false ))
286
295
287
296
srv = server .NewDefaultServer (manager )
288
297
srv .SetClientInfoHandler (server .ClientFormHandler )
@@ -372,7 +381,7 @@ func TestRefreshing(t *testing.T) {
372
381
}))
373
382
defer csrv .Close ()
374
383
375
- manager .MapClientStorage (clientStore (csrv .URL ))
384
+ manager .MapClientStorage (clientStore (csrv .URL , true ))
376
385
srv = server .NewDefaultServer (manager )
377
386
srv .SetUserAuthorizationHandler (func (w http.ResponseWriter , r * http.Request ) (userID string , err error ) {
378
387
userID = "000000"
@@ -384,7 +393,7 @@ func TestRefreshing(t *testing.T) {
384
393
WithQuery ("client_id" , clientID ).
385
394
WithQuery ("scope" , "all" ).
386
395
WithQuery ("state" , "123" ).
387
- WithQuery ("redirect_uri" , url . QueryEscape ( csrv .URL + "/oauth2" ) ).
396
+ WithQuery ("redirect_uri" , csrv .URL + "/oauth2" ).
388
397
Expect ().Status (http .StatusOK )
389
398
}
390
399
0 commit comments