Skip to content

Commit

Permalink
Updated sandbox GetJWTAuthParams and CreateJwt method as per review c…
Browse files Browse the repository at this point in the history
…omments
  • Loading branch information
sacOO7 committed Sep 6, 2024
1 parent cbd39b6 commit 1ca24af
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions ablytest/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,14 @@ func (app *Sandbox) URL(paths ...string) string {
// Source code for the same => https://github.com/ably/echoserver/blob/main/app.js
var CREATE_JWT_URL string = "https://echo.ably.io/createJWT"

// Returns authParams, required for authUrl as a mode of auth
// GetJwtAuthParams constructs the authentication parameters required for JWT creation.
// Required when authUrl is chosen as a mode of auth
//
// Parameters:
// - expiresIn: The duration until the JWT expires.
// - invalid: A boolean flag indicating whether to use an invalid key secret.
//
// Returns: A url.Values object containing the authentication parameters.
func (app *Sandbox) GetJwtAuthParams(expiresIn time.Duration, invalid bool) url.Values {
key, secret := app.KeyParts()
authParams := url.Values{}
Expand All @@ -275,7 +282,15 @@ func (app *Sandbox) GetJwtAuthParams(expiresIn time.Duration, invalid bool) url.
return authParams
}

// Returns JWT with given expiry
// CreateJwt generates a JWT with the specified expiration time.
//
// Parameters:
// - expiresIn: The duration until the JWT expires.
// - invalid: A boolean flag indicating whether to use an invalid key secret.
//
// Returns:
// - A string containing the generated JWT.
// - An error if the JWT creation fails.
func (app *Sandbox) CreateJwt(expiresIn time.Duration, invalid bool) (string, error) {
u, err := url.Parse(CREATE_JWT_URL)
if err != nil {
Expand All @@ -288,12 +303,17 @@ func (app *Sandbox) CreateJwt(expiresIn time.Duration, invalid bool) (string, er
}
res, err := app.client.Do(req)
if err != nil {
res.Body.Close()
return "", fmt.Errorf("client: error making http request: %s", err)
}
defer res.Body.Close()
resBody, err := io.ReadAll(res.Body)
if err != nil {
return "", fmt.Errorf("client: could not read response body: %s", err)
}
if res.StatusCode != 200 {
return "", fmt.Errorf("non-success response received: %v:%s", res.StatusCode, resBody)
}
return string(resBody), nil
}

Expand Down

0 comments on commit 1ca24af

Please sign in to comment.