Skip to content

Commit 6a773fb

Browse files
committed
check that issued credentials work in token endpoint in hydra integration test
1 parent 4a7dc3e commit 6a773fb

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

diode-server/auth/server_hydra_integration_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"testing"
1818
"time"
1919

20+
"github.com/golang-jwt/jwt/v5"
2021
"github.com/stretchr/testify/require"
2122
testcontainers "github.com/testcontainers/testcontainers-go"
2223
"github.com/testcontainers/testcontainers-go/wait"
@@ -193,13 +194,59 @@ func TestServerHydraIntegration(t *testing.T) {
193194
// verify that we saw all 9 clients
194195
require.Equal(t, 9, len(seen))
195196
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)
196227
}
197228

198229
type authTestClient struct {
199230
endpoint string
200231
token string
201232
}
202233

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+
203250
func (c *authTestClient) listClients(t *testing.T, pageToken string, pageSize int) auth.ListClientsResponse {
204251
u, err := url.Parse(c.endpoint + "/clients")
205252
require.NoError(t, err)

0 commit comments

Comments
 (0)