@@ -6,6 +6,7 @@ package oauth2
6
6
7
7
import (
8
8
"context"
9
+ "encoding/json"
9
10
"errors"
10
11
"fmt"
11
12
"io/ioutil"
@@ -71,6 +72,62 @@ func TestAuthCodeURL_Optional(t *testing.T) {
71
72
}
72
73
}
73
74
75
+ func TestAuthCodeURL_UserInfoClaims (t * testing.T ) {
76
+ claimSet := & ClaimSet {}
77
+ claimSet .AddVoluntaryClaim (UserInfoClaim , "email" )
78
+ conf := & Config {
79
+ ClientID : "CLIENT_ID" ,
80
+ Endpoint : Endpoint {
81
+ AuthURL : "/auth-url" ,
82
+ TokenURL : "/token-url" ,
83
+ },
84
+ ClaimSet : claimSet ,
85
+ }
86
+ url := conf .AuthCodeURL ("" )
87
+ const want = "/auth-url?claims=%7B%22userinfo%22%3A%7B%22email%22%3Anull%7D%7D&client_id=CLIENT_ID&response_type=code"
88
+ if got := url ; got != want {
89
+ t .Fatalf ("got auth code = %q; want %q" , got , want )
90
+ }
91
+ }
92
+
93
+ func TestAuthCodeURL_IdTokenClaims (t * testing.T ) {
94
+ claimSet := & ClaimSet {}
95
+ claimSet .AddClaimWithValues (IdTokenClaim , "name" , false , "nameValue1" , "nameValue2" )
96
+ conf := & Config {
97
+ ClientID : "CLIENT_ID" ,
98
+ Endpoint : Endpoint {
99
+ AuthURL : "/auth-url" ,
100
+ TokenURL : "/token-url" ,
101
+ },
102
+ ClaimSet : claimSet ,
103
+ }
104
+ url := conf .AuthCodeURL ("" )
105
+ const want = "/auth-url?claims=%7B%22id_token%22%3A%7B%22name%22%3A%7B%22values%22%3A%5B%22nameValue1%22%2C%22nameValue2%22%5D%7D%7D%7D&client_id=CLIENT_ID&response_type=code"
106
+ if got := url ; got != want {
107
+ t .Fatalf ("got auth code = %q; want %q" , got , want )
108
+ }
109
+ }
110
+
111
+ func TestAuthCodeURL_MultipleClaims (t * testing.T ) {
112
+ claimSet := & ClaimSet {}
113
+ claimSet .AddVoluntaryClaim (UserInfoClaim , "email" )
114
+ claimSet .AddClaimWithValue (IdTokenClaim , "email" , true , "emailValue" )
115
+ claimSet .AddClaimWithValues (IdTokenClaim , "name" , false , "nameValue1" , "nameValue2" )
116
+ conf := & Config {
117
+ ClientID : "CLIENT_ID" ,
118
+ Endpoint : Endpoint {
119
+ AuthURL : "/auth-url" ,
120
+ TokenURL : "/token-url" ,
121
+ },
122
+ ClaimSet : claimSet ,
123
+ }
124
+ url := conf .AuthCodeURL ("" )
125
+ const want = "/auth-url?claims=%7B%22id_token%22%3A%7B%22email%22%3A%7B%22essential%22%3Atrue%2C%22value%22%3A%22emailValue%22%7D%2C%22name%22%3A%7B%22values%22%3A%5B%22nameValue1%22%2C%22nameValue2%22%5D%7D%7D%2C%22userinfo%22%3A%7B%22email%22%3Anull%7D%7D&client_id=CLIENT_ID&response_type=code"
126
+ if got := url ; got != want {
127
+ t .Fatalf ("got auth code = %q; want %q" , got , want )
128
+ }
129
+ }
130
+
74
131
func TestURLUnsafeClientConfig (t * testing.T ) {
75
132
ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
76
133
if got , want := r .Header .Get ("Authorization" ), "Basic Q0xJRU5UX0lEJTNGJTNGOkNMSUVOVF9TRUNSRVQlM0YlM0Y=" ; got != want {
@@ -231,10 +288,10 @@ func TestExchangeRequest_JSONResponse(t *testing.T) {
231
288
func TestExtraValueRetrieval (t * testing.T ) {
232
289
values := url.Values {}
233
290
kvmap := map [string ]string {
234
- "scope" : "user" , "token_type" : "bearer" , "expires_in" : "86400.92" ,
291
+ "scope" : "user" , "token_type" : "bearer" , "expires_in" : "86400.92" ,
235
292
"server_time" : "1443571905.5606415" , "referer_ip" : "10.0.0.1" ,
236
- "etag" : "\" afZYj912P4alikMz_P11982\" " , "request_id" : "86400" ,
237
- "untrimmed" : " untrimmed " ,
293
+ "etag" : "\" afZYj912P4alikMz_P11982\" " , "request_id" : "86400" ,
294
+ "untrimmed" : " untrimmed " ,
238
295
}
239
296
for key , value := range kvmap {
240
297
values .Set (key , value )
@@ -548,3 +605,78 @@ func TestConfigClientWithToken(t *testing.T) {
548
605
t .Error (err )
549
606
}
550
607
}
608
+
609
+ func TestClaimSet_AddVoluntaryClaim (t * testing.T ) {
610
+ claimSet := & ClaimSet {}
611
+ claimSet .AddVoluntaryClaim (UserInfoClaim , "name" )
612
+
613
+ body , err := json .Marshal (claimSet )
614
+ if err != nil {
615
+ t .Error (err )
616
+ }
617
+
618
+ expected := `{"userinfo":{"name":null}}`
619
+ if string (body ) != expected {
620
+ t .Errorf ("Claims request parameter = %q; want %q" , string (body ), expected )
621
+ }
622
+ }
623
+
624
+ func TestClaimSet_AddClaimWithValue_Essential (t * testing.T ) {
625
+ claimSet := & ClaimSet {}
626
+ claimSet .AddClaimWithValue (UserInfoClaim , "name" , true , "nameValue" )
627
+
628
+ body , err := json .Marshal (claimSet )
629
+ if err != nil {
630
+ t .Error (err )
631
+ }
632
+
633
+ expected := `{"userinfo":{"name":{"essential":true,"value":"nameValue"}}}`
634
+ if string (body ) != expected {
635
+ t .Errorf ("Claims request parameter = %q; want %q" , string (body ), expected )
636
+ }
637
+ }
638
+
639
+ func TestClaimSet_AddClaimWithValue_Voluntary (t * testing.T ) {
640
+ claimSet := & ClaimSet {}
641
+ claimSet .AddClaimWithValue (UserInfoClaim , "name" , false , "nameValue" )
642
+
643
+ body , err := json .Marshal (claimSet )
644
+ if err != nil {
645
+ t .Error (err )
646
+ }
647
+
648
+ expected := `{"userinfo":{"name":{"value":"nameValue"}}}`
649
+ if string (body ) != expected {
650
+ t .Errorf ("Claims request parameter = %q; want %q" , string (body ), expected )
651
+ }
652
+ }
653
+
654
+ func TestClaimSet_AddClaimWithValues_Essential (t * testing.T ) {
655
+ claimSet := & ClaimSet {}
656
+ claimSet .AddClaimWithValues (IdTokenClaim , "email" , true , "emailValue" , "mailValue" )
657
+
658
+ body , err := json .Marshal (claimSet )
659
+ if err != nil {
660
+ t .Error (err )
661
+ }
662
+
663
+ expected := `{"id_token":{"email":{"essential":true,"values":["emailValue","mailValue"]}}}`
664
+ if string (body ) != expected {
665
+ t .Errorf ("Claims request parameter = %q; want %q" , string (body ), expected )
666
+ }
667
+ }
668
+
669
+ func TestClaimSet_AddClaimWithValues_Voluntary (t * testing.T ) {
670
+ claimSet := & ClaimSet {}
671
+ claimSet .AddClaimWithValues (IdTokenClaim , "email" , false , "emailValue" , "mailValue" )
672
+
673
+ body , err := json .Marshal (claimSet )
674
+ if err != nil {
675
+ t .Error (err )
676
+ }
677
+
678
+ expected := `{"id_token":{"email":{"values":["emailValue","mailValue"]}}}`
679
+ if string (body ) != expected {
680
+ t .Errorf ("Claims request parameter = %q; want %q" , string (body ), expected )
681
+ }
682
+ }
0 commit comments