@@ -3,25 +3,26 @@ defmodule OpenIDConnectTest do
3
3
import OpenIDConnect.Fixtures
4
4
import OpenIDConnect
5
5
6
+ @ redirect_uri "https://localhost/redirect_uri"
7
+
6
8
@ config % {
7
9
discovery_document_uri: nil ,
8
10
client_id: "CLIENT_ID" ,
9
11
client_secret: "CLIENT_SECRET" ,
10
- redirect_uri: "https://localhost/redirect_uri" ,
11
12
response_type: "code id_token token" ,
12
13
scope: "openid email profile"
13
14
}
14
15
15
- describe "authorization_uri/2 " do
16
+ describe "authorization_uri/3 " do
16
17
test "generates authorization url with scope and response_type as binaries" do
17
18
{ _bypass , uri } = start_fixture ( "google" )
18
19
config = % { @ config | discovery_document_uri: uri }
19
20
20
- assert authorization_uri ( config ) ==
21
+ assert authorization_uri ( config , @ redirect_uri ) ==
21
22
{ :ok ,
22
23
"https://accounts.google.com/o/oauth2/v2/auth?" <>
23
24
"client_id=CLIENT_ID" <>
24
- "&redirect_uri=https%3A%2F%2Flocalhost%2Fredirect_uri " <>
25
+ "&redirect_uri=#{ URI . encode_www_form ( @ redirect_uri ) } " <>
25
26
"&response_type=code+id_token+token" <>
26
27
"&scope=openid+email+profile" }
27
28
end
@@ -30,11 +31,11 @@ defmodule OpenIDConnectTest do
30
31
{ _bypass , uri } = start_fixture ( "google" )
31
32
config = % { @ config | discovery_document_uri: uri , scope: [ "openid" , "email" , "profile" ] }
32
33
33
- assert authorization_uri ( config ) ==
34
+ assert authorization_uri ( config , @ redirect_uri ) ==
34
35
{ :ok ,
35
36
"https://accounts.google.com/o/oauth2/v2/auth?" <>
36
37
"client_id=CLIENT_ID" <>
37
- "&redirect_uri=https%3A%2F%2Flocalhost%2Fredirect_uri " <>
38
+ "&redirect_uri=#{ URI . encode_www_form ( @ redirect_uri ) } " <>
38
39
"&response_type=code+id_token+token" <>
39
40
"&scope=openid+email+profile" }
40
41
end
@@ -48,11 +49,11 @@ defmodule OpenIDConnectTest do
48
49
response_type: [ "code" , "id_token" , "token" ]
49
50
}
50
51
51
- assert authorization_uri ( config ) ==
52
+ assert authorization_uri ( config , @ redirect_uri ) ==
52
53
{ :ok ,
53
54
"https://accounts.google.com/o/oauth2/v2/auth?" <>
54
55
"client_id=CLIENT_ID" <>
55
- "&redirect_uri=https%3A%2F%2Flocalhost%2Fredirect_uri " <>
56
+ "&redirect_uri=#{ URI . encode_www_form ( @ redirect_uri ) } " <>
56
57
"&response_type=code+id_token+token" <>
57
58
"&scope=openid+email+profile" }
58
59
end
@@ -61,37 +62,37 @@ defmodule OpenIDConnectTest do
61
62
{ _bypass , uri } = start_fixture ( "google" )
62
63
63
64
config = % { @ config | discovery_document_uri: uri , scope: nil }
64
- assert authorization_uri ( config ) == { :error , :invalid_scope }
65
+ assert authorization_uri ( config , @ redirect_uri ) == { :error , :invalid_scope }
65
66
66
67
config = % { @ config | discovery_document_uri: uri , scope: "" }
67
- assert authorization_uri ( config ) == { :error , :invalid_scope }
68
+ assert authorization_uri ( config , @ redirect_uri ) == { :error , :invalid_scope }
68
69
69
70
config = % { @ config | discovery_document_uri: uri , scope: [ ] }
70
- assert authorization_uri ( config ) == { :error , :invalid_scope }
71
+ assert authorization_uri ( config , @ redirect_uri ) == { :error , :invalid_scope }
71
72
end
72
73
73
74
test "returns error on empty response_type" do
74
75
{ _bypass , uri } = start_fixture ( "google" )
75
76
76
77
config = % { @ config | discovery_document_uri: uri , response_type: nil }
77
- assert authorization_uri ( config ) == { :error , :invalid_response_type }
78
+ assert authorization_uri ( config , @ redirect_uri ) == { :error , :invalid_response_type }
78
79
79
80
config = % { @ config | discovery_document_uri: uri , response_type: "" }
80
- assert authorization_uri ( config ) == { :error , :invalid_response_type }
81
+ assert authorization_uri ( config , @ redirect_uri ) == { :error , :invalid_response_type }
81
82
82
83
config = % { @ config | discovery_document_uri: uri , response_type: [ ] }
83
- assert authorization_uri ( config ) == { :error , :invalid_response_type }
84
+ assert authorization_uri ( config , @ redirect_uri ) == { :error , :invalid_response_type }
84
85
end
85
86
86
87
test "adds optional params" do
87
88
{ _bypass , uri } = start_fixture ( "google" )
88
89
config = % { @ config | discovery_document_uri: uri }
89
90
90
- assert authorization_uri ( config , % { "state" => "foo" } ) ==
91
+ assert authorization_uri ( config , @ redirect_uri , % { "state" => "foo" } ) ==
91
92
{ :ok ,
92
93
"https://accounts.google.com/o/oauth2/v2/auth?" <>
93
94
"client_id=CLIENT_ID" <>
94
- "&redirect_uri=https%3A%2F%2Flocalhost%2Fredirect_uri " <>
95
+ "&redirect_uri=#{ URI . encode_www_form ( @ redirect_uri ) } " <>
95
96
"&response_type=code+id_token+token" <>
96
97
"&scope=openid+email+profile" <>
97
98
"&state=foo" }
@@ -101,11 +102,11 @@ defmodule OpenIDConnectTest do
101
102
{ _bypass , uri } = start_fixture ( "google" )
102
103
config = % { @ config | discovery_document_uri: uri }
103
104
104
- assert authorization_uri ( config , % { client_id: "foo" } ) ==
105
+ assert authorization_uri ( config , @ redirect_uri , % { client_id: "foo" } ) ==
105
106
{ :ok ,
106
107
"https://accounts.google.com/o/oauth2/v2/auth?" <>
107
108
"client_id=foo" <>
108
- "&redirect_uri=https%3A%2F%2Flocalhost%2Fredirect_uri " <>
109
+ "&redirect_uri=#{ URI . encode_www_form ( @ redirect_uri ) } " <>
109
110
"&response_type=code+id_token+token" <>
110
111
"&scope=openid+email+profile" }
111
112
end
@@ -117,7 +118,7 @@ defmodule OpenIDConnectTest do
117
118
118
119
config = % { @ config | discovery_document_uri: uri }
119
120
120
- assert authorization_uri ( config , % { client_id: "foo" } ) ==
121
+ assert authorization_uri ( config , @ redirect_uri , % { client_id: "foo" } ) ==
121
122
{ :error , % Mint.TransportError { reason: :econnrefused } }
122
123
end
123
124
end
@@ -187,8 +188,14 @@ defmodule OpenIDConnectTest do
187
188
{ _bypass , uri } = start_fixture ( "google" , % { token_endpoint: token_endpoint } )
188
189
config = % { @ config | discovery_document_uri: uri }
189
190
190
- assert fetch_tokens ( config , % { code: "1234" , id_token: "abcd" } ) ==
191
- { :ok , token_response_attrs }
191
+ params = % {
192
+ grant_type: "authorization_code" ,
193
+ redirect_uri: @ redirect_uri ,
194
+ code: "1234" ,
195
+ id_token: "abcd"
196
+ }
197
+
198
+ assert fetch_tokens ( config , params ) == { :ok , token_response_attrs }
192
199
193
200
assert_receive { :req , body }
194
201
@@ -198,7 +205,7 @@ defmodule OpenIDConnectTest do
198
205
"&code=1234" <>
199
206
"&grant_type=authorization_code" <>
200
207
"&id_token=abcd" <>
201
- "&redirect_uri=https%3A%2F%2Flocalhost%2Fredirect_uri "
208
+ "&redirect_uri=#{ URI . encode_www_form ( @ redirect_uri ) } "
202
209
end
203
210
204
211
test "allows to override the default params" do
@@ -221,15 +228,50 @@ defmodule OpenIDConnectTest do
221
228
{ _bypass , uri } = start_fixture ( "google" , % { token_endpoint: token_endpoint } )
222
229
config = % { @ config | discovery_document_uri: uri }
223
230
224
- fetch_tokens ( config , % { client_id: "foo" } )
231
+ fetch_tokens ( config , % {
232
+ client_id: "foo" ,
233
+ grant_type: "authorization_code" ,
234
+ redirect_uri: @ redirect_uri
235
+ } )
225
236
226
237
assert_receive { :req , body }
227
238
228
239
assert body ==
229
240
"client_id=foo" <>
230
241
"&client_secret=CLIENT_SECRET" <>
231
242
"&grant_type=authorization_code" <>
232
- "&redirect_uri=https%3A%2F%2Flocalhost%2Fredirect_uri"
243
+ "&redirect_uri=#{ URI . encode_www_form ( @ redirect_uri ) } "
244
+ end
245
+
246
+ test "allows to use refresh_token grant type" do
247
+ bypass = Bypass . open ( )
248
+ test_pid = self ( )
249
+
250
+ token_response_attrs = % {
251
+ "access_token" => "ACCESS_TOKEN" ,
252
+ "id_token" => "ID_TOKEN" ,
253
+ "refresh_token" => "REFRESH_TOKEN"
254
+ }
255
+
256
+ Bypass . expect_once ( bypass , "POST" , "/token" , fn conn ->
257
+ { :ok , body , conn } = Plug.Conn . read_body ( conn )
258
+ send ( test_pid , { :req , body } )
259
+ Plug.Conn . resp ( conn , 200 , Jason . encode! ( token_response_attrs ) )
260
+ end )
261
+
262
+ token_endpoint = "http://localhost:#{ bypass . port } /token"
263
+ { _bypass , uri } = start_fixture ( "google" , % { token_endpoint: token_endpoint } )
264
+ config = % { @ config | discovery_document_uri: uri }
265
+
266
+ fetch_tokens ( config , % { grant_type: "refresh_token" , refresh_token: "foo" } )
267
+
268
+ assert_receive { :req , body }
269
+
270
+ assert body ==
271
+ "client_id=CLIENT_ID" <>
272
+ "&client_secret=CLIENT_SECRET" <>
273
+ "&grant_type=refresh_token" <>
274
+ "&refresh_token=foo"
233
275
end
234
276
235
277
test "returns error when token endpoint is not available" do
@@ -238,8 +280,9 @@ defmodule OpenIDConnectTest do
238
280
token_endpoint = "http://localhost:#{ bypass . port } /token"
239
281
{ _bypass , uri } = start_fixture ( "google" , % { token_endpoint: token_endpoint } )
240
282
config = % { @ config | discovery_document_uri: uri }
283
+ params = % { grant_type: "authorization_code" , redirect_uri: @ redirect_uri }
241
284
242
- assert fetch_tokens ( config , % { client_id: "foo" } ) ==
285
+ assert fetch_tokens ( config , params ) ==
243
286
{ :error , % Mint.TransportError { reason: :econnrefused } }
244
287
end
245
288
@@ -254,14 +297,21 @@ defmodule OpenIDConnectTest do
254
297
{ _bypass , uri } = start_fixture ( "google" , % { token_endpoint: token_endpoint } )
255
298
config = % { @ config | discovery_document_uri: uri }
256
299
257
- assert fetch_tokens ( config , % { client_id: "foo" } ) ==
300
+ assert fetch_tokens ( config , % { } ) ==
258
301
{ :error , { 401 , "{\" error\" :\" unauthorized\" }" } }
259
302
end
260
303
261
304
test "returns error when real provider token endpoint is responded with invalid code" do
262
305
{ _bypass , uri } = start_fixture ( "google" )
263
306
config = % { @ config | discovery_document_uri: uri }
264
- assert { :error , { 401 , resp } } = fetch_tokens ( config , % { code: "foo" } )
307
+
308
+ assert { :error , { 401 , resp } } =
309
+ fetch_tokens ( config , % {
310
+ grant_type: "authorization_code" ,
311
+ redirect_uri: @ redirect_uri ,
312
+ code: "foo"
313
+ } )
314
+
265
315
resp_json = Jason . decode! ( resp )
266
316
267
317
assert resp_json == % {
@@ -272,7 +322,14 @@ defmodule OpenIDConnectTest do
272
322
for provider <- [ "auth0" , "okta" , "onelogin" ] do
273
323
{ _bypass , uri } = start_fixture ( provider )
274
324
config = % { @ config | discovery_document_uri: uri }
275
- assert { :error , { status , _resp } } = fetch_tokens ( config , % { code: "foo" } )
325
+
326
+ assert { :error , { status , _resp } } =
327
+ fetch_tokens ( config , % {
328
+ grant_type: "authorization_code" ,
329
+ redirect_uri: @ redirect_uri ,
330
+ code: "foo"
331
+ } )
332
+
276
333
assert status in 400 .. 499
277
334
end
278
335
end
@@ -284,7 +341,13 @@ defmodule OpenIDConnectTest do
284
341
285
342
config = % { @ config | discovery_document_uri: uri }
286
343
287
- assert fetch_tokens ( config , % { code: "foo" } ) ==
344
+ params = % {
345
+ grant_type: "authorization_code" ,
346
+ redirect_uri: @ redirect_uri ,
347
+ code: "foo"
348
+ }
349
+
350
+ assert fetch_tokens ( config , params ) ==
288
351
{ :error , % Mint.TransportError { reason: :econnrefused } }
289
352
end
290
353
end
0 commit comments