@@ -17,6 +17,7 @@ import (
17
17
"testing"
18
18
"time"
19
19
20
+ "github.com/golang-jwt/jwt/v5"
20
21
"github.com/stretchr/testify/require"
21
22
testcontainers "github.com/testcontainers/testcontainers-go"
22
23
"github.com/testcontainers/testcontainers-go/wait"
@@ -193,13 +194,59 @@ func TestServerHydraIntegration(t *testing.T) {
193
194
// verify that we saw all 9 clients
194
195
require .Equal (t , 9 , len (seen ))
195
196
require .Equal (t , 5 , pages )
197
+
198
+ tokenClientInfo := client .createClient (t , "test-client-token-auth" , ingestClientScope )
199
+
200
+ // call the token endpoint with the credentials and verify that a token comes back ...
201
+ resp := client .getToken (t , tokenClientInfo .ClientID , tokenClientInfo .ClientSecret , ingestClientScope )
202
+ defer func () {
203
+ _ = resp .Body .Close ()
204
+ }()
205
+ require .Equal (t , http .StatusOK , resp .StatusCode )
206
+
207
+ var tokenResult struct {
208
+ AccessToken string `json:"access_token"`
209
+ }
210
+ err = json .NewDecoder (resp .Body ).Decode (& tokenResult )
211
+ require .NoError (t , err )
212
+ require .NotEmpty (t , tokenResult .AccessToken )
213
+ accessToken := tokenResult .AccessToken
214
+ require .NotEmpty (t , accessToken )
215
+
216
+ token , _ , err := jwt .NewParser ().ParseUnverified (accessToken , jwt.MapClaims {})
217
+ require .NoError (t , err )
218
+ claims , ok := token .Claims .(jwt.MapClaims )
219
+ require .True (t , ok )
220
+ scopeClaim , ok := claims ["scope" ]
221
+ require .True (t , ok )
222
+ require .Equal (t , ingestClientScope , scopeClaim )
223
+
224
+ // try to use the credentials to create a token with a different scope ...
225
+ resp = client .getToken (t , tokenClientInfo .ClientID , tokenClientInfo .ClientSecret , "netbox:read" )
226
+ require .Equal (t , http .StatusBadRequest , resp .StatusCode )
196
227
}
197
228
198
229
type authTestClient struct {
199
230
endpoint string
200
231
token string
201
232
}
202
233
234
+ func (c * authTestClient ) getToken (t * testing.T , clientID string , clientSecret string , scope string ) * http.Response {
235
+ data := url.Values {}
236
+ data .Set ("grant_type" , "client_credentials" )
237
+ data .Set ("client_id" , clientID )
238
+ data .Set ("client_secret" , clientSecret )
239
+ data .Set ("scope" , scope )
240
+ req , err := http .NewRequest (http .MethodPost , c .endpoint + "/token" , strings .NewReader (data .Encode ()))
241
+ require .NoError (t , err )
242
+ req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
243
+
244
+ client := & http.Client {}
245
+ resp , err := client .Do (req )
246
+ require .NoError (t , err )
247
+ return resp
248
+ }
249
+
203
250
func (c * authTestClient ) listClients (t * testing.T , pageToken string , pageSize int ) auth.ListClientsResponse {
204
251
u , err := url .Parse (c .endpoint + "/clients" )
205
252
require .NoError (t , err )
0 commit comments