@@ -22,7 +22,17 @@ import (
2222func TestNewDeviceRequestWithPublicClient (t * testing.T ) {
2323 ctrl := gomock .NewController (t )
2424 store := internal .NewMockStorage (ctrl )
25- client := & DefaultClient {ID : "client_id" }
25+ deviceClient := & DefaultClient {ID : "client_id" }
26+ deviceClient .Public = true
27+ deviceClient .Scopes = []string {"17" , "42" }
28+ deviceClient .Audience = []string {"aud2" }
29+ deviceClient .GrantTypes = []string {"urn:ietf:params:oauth:grant-type:device_code" }
30+
31+ authCodeClient := & DefaultClient {ID : "client_id_2" }
32+ authCodeClient .Public = true
33+ authCodeClient .Scopes = []string {"17" , "42" }
34+ authCodeClient .GrantTypes = []string {"authorization_code" }
35+
2636 defer ctrl .Finish ()
2737 config := & Config {ScopeStrategy : ExactScopeStrategy , AudienceMatchingStrategy : DefaultAudienceMatchingStrategy }
2838 fosite := & Fosite {Store : store , Config : config }
@@ -63,40 +73,30 @@ func TestNewDeviceRequestWithPublicClient(t *testing.T) {
6373 },
6474 method : "POST" ,
6575 mock : func () {
66- store .EXPECT ().GetClient (gomock .Any (), gomock .Eq ("client_id" )).Return (client , nil )
67- client .Public = true
68- client .Scopes = []string {"17" , "42" }
69- client .GrantTypes = []string {"urn:ietf:params:oauth:grant-type:device_code" }
76+ store .EXPECT ().GetClient (gomock .Any (), gomock .Eq ("client_id" )).Return (deviceClient , nil )
7077 },
7178 expectedError : ErrInvalidScope ,
7279 }, {
7380 description : "fails because audience not allowed" ,
7481 form : url.Values {
7582 "client_id" : {"client_id" },
7683 "scope" : {"17 42" },
77- "audience" : {"aud " },
84+ "audience" : {"random_aud " },
7885 },
7986 method : "POST" ,
8087 mock : func () {
81- store .EXPECT ().GetClient (gomock .Any (), gomock .Eq ("client_id" )).Return (client , nil )
82- client .Public = true
83- client .Scopes = []string {"17" , "42" }
84- client .Audience = []string {"aud2" }
85- client .GrantTypes = []string {"urn:ietf:params:oauth:grant-type:device_code" }
88+ store .EXPECT ().GetClient (gomock .Any (), gomock .Eq ("client_id" )).Return (deviceClient , nil )
8689 },
8790 expectedError : ErrInvalidRequest ,
8891 }, {
8992 description : "fails because it doesn't have the proper grant" ,
9093 form : url.Values {
91- "client_id" : {"client_id " },
94+ "client_id" : {"client_id_2 " },
9295 "scope" : {"17 42" },
9396 },
9497 method : "POST" ,
9598 mock : func () {
96- store .EXPECT ().GetClient (gomock .Any (), gomock .Eq ("client_id" )).Return (client , nil )
97- client .Public = true
98- client .Scopes = []string {"17" , "42" }
99- client .GrantTypes = []string {"authorization_code" }
99+ store .EXPECT ().GetClient (gomock .Any (), gomock .Eq ("client_id_2" )).Return (authCodeClient , nil )
100100 },
101101 expectedError : ErrInvalidGrant ,
102102 }, {
@@ -107,10 +107,7 @@ func TestNewDeviceRequestWithPublicClient(t *testing.T) {
107107 },
108108 method : "POST" ,
109109 mock : func () {
110- store .EXPECT ().GetClient (gomock .Any (), gomock .Eq ("client_id" )).Return (client , nil )
111- client .Public = true
112- client .Scopes = []string {"17" , "42" }
113- client .GrantTypes = []string {"urn:ietf:params:oauth:grant-type:device_code" }
110+ store .EXPECT ().GetClient (gomock .Any (), gomock .Eq ("client_id" )).Return (deviceClient , nil )
114111 },
115112 }} {
116113 t .Run (fmt .Sprintf ("case=%d description=%s" , k , c .description ), func (t * testing.T ) {
@@ -123,10 +120,8 @@ func TestNewDeviceRequestWithPublicClient(t *testing.T) {
123120 }
124121
125122 ar , err := fosite .NewDeviceRequest (context .Background (), r )
126- if c .expectedError != nil {
127- assert .EqualError (t , err , c .expectedError .Error ())
128- } else {
129- require .NoError (t , err )
123+ require .ErrorIs (t , err , c .expectedError )
124+ if c .expectedError == nil {
130125 assert .NotNil (t , ar .GetRequestedAt ())
131126 }
132127 })
@@ -141,15 +136,21 @@ func TestNewDeviceRequestWithClientAuthn(t *testing.T) {
141136 defer ctrl .Finish ()
142137 config := & Config {ClientSecretsHasher : hasher , ScopeStrategy : ExactScopeStrategy , AudienceMatchingStrategy : DefaultAudienceMatchingStrategy }
143138 fosite := & Fosite {Store : store , Config : config }
139+
140+ client .Public = false
141+ client .Secret = []byte ("client_secret" )
142+ client .Scopes = []string {"foo" , "bar" }
143+ client .GrantTypes = []string {"urn:ietf:params:oauth:grant-type:device_code" }
144+
144145 for k , c := range []struct {
145146 header http.Header
146147 form url.Values
147148 method string
148149 expectedError error
149150 mock func ()
150151 expect DeviceRequester
152+ description string
151153 }{
152- // No client authn provided
153154 {
154155 form : url.Values {
155156 "client_id" : {"client_id" },
@@ -159,14 +160,26 @@ func TestNewDeviceRequestWithClientAuthn(t *testing.T) {
159160 method : "POST" ,
160161 mock : func () {
161162 store .EXPECT ().GetClient (gomock .Any (), gomock .Eq ("client_id" )).Return (client , nil )
162- client .Public = false
163- client .Secret = []byte ("client_secret" )
164- client .Scopes = []string {"foo" , "bar" }
165- client .GrantTypes = []string {"urn:ietf:params:oauth:grant-type:device_code" }
166163 hasher .EXPECT ().Compare (gomock .Any (), gomock .Any (), gomock .Any ()).Return (errors .New ("" ))
167164 },
165+ description : "Should failed becaue no client authn provided." ,
166+ },
167+ {
168+ form : url.Values {
169+ "client_id" : {"client_id2" },
170+ "scope" : {"foo bar" },
171+ },
172+ header : http.Header {
173+ "Authorization" : {basicAuth ("client_id" , "client_secret" )},
174+ },
175+ expectedError : ErrInvalidRequest ,
176+ method : "POST" ,
177+ mock : func () {
178+ store .EXPECT ().GetClient (gomock .Any (), gomock .Eq ("client_id" )).Return (client , nil )
179+ hasher .EXPECT ().Compare (gomock .Any (), gomock .Eq ([]byte ("client_secret" )), gomock .Eq ([]byte ("client_secret" ))).Return (nil )
180+ },
181+ description : "should fail because different client is used in authn than in form" ,
168182 },
169- // success
170183 {
171184 form : url.Values {
172185 "client_id" : {"client_id" },
@@ -178,15 +191,12 @@ func TestNewDeviceRequestWithClientAuthn(t *testing.T) {
178191 method : "POST" ,
179192 mock : func () {
180193 store .EXPECT ().GetClient (gomock .Any (), gomock .Eq ("client_id" )).Return (client , nil )
181- client .Public = false
182- client .Secret = []byte ("client_secret" )
183- client .Scopes = []string {"foo" , "bar" }
184- client .GrantTypes = []string {"urn:ietf:params:oauth:grant-type:device_code" }
185194 hasher .EXPECT ().Compare (gomock .Any (), gomock .Eq ([]byte ("client_secret" )), gomock .Eq ([]byte ("client_secret" ))).Return (nil )
186195 },
196+ description : "should succeed" ,
187197 },
188198 } {
189- t .Run (fmt .Sprintf ("case=%d" , k ), func (t * testing.T ) {
199+ t .Run (fmt .Sprintf ("case=%d description=%s " , k , c . description ), func (t * testing.T ) {
190200 c .mock ()
191201 r := & http.Request {
192202 Header : c .header ,
@@ -196,11 +206,9 @@ func TestNewDeviceRequestWithClientAuthn(t *testing.T) {
196206 }
197207
198208 req , err := fosite .NewDeviceRequest (context .Background (), r )
199- if c .expectedError != nil {
200- assert .EqualError (t , err , c .expectedError .Error ())
201- } else {
202- require .NoError (t , err )
203- assert .NotNil (t , req .GetRequestedAt ())
209+ require .ErrorIs (t , err , c .expectedError )
210+ if c .expectedError == nil {
211+ assert .NotZero (t , req .GetRequestedAt ())
204212 }
205213 })
206214 }
0 commit comments