Skip to content

Commit

Permalink
Added sandbox methods for jwtAuthParams and creatingJWT
Browse files Browse the repository at this point in the history
  • Loading branch information
sacOO7 committed Sep 4, 2024
1 parent 8159c05 commit 52e16c3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
13 changes: 3 additions & 10 deletions ably/realtime_conn_spec_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3027,17 +3027,10 @@ func TestRealtimeConn_RTC8a_ExplicitAuthorizeWhileConnected(t *testing.T) {
app := ablytest.MustSandbox(nil)
defer safeclose(t, app)

key, secret := app.KeyParts()
authParams := url.Values{}
authParams.Add("environment", app.Environment)
authParams.Add("returnType", "jwt")
authParams.Add("keyName", key)
authParams.Add("keySecret", secret)

rec, optn := ablytest.NewHttpRecorder()
rest, err := ably.NewREST(
ably.WithAuthURL("https://echo.ably.io/createJWT"),
ably.WithAuthParams(authParams),
ably.WithAuthURL(ablytest.CREATE_JWT_URL),
ably.WithAuthParams(app.GetJwtAuthParams(30*time.Second)),
ably.WithEnvironment(app.Environment),
ably.WithKey(""),
optn[0],
Expand All @@ -3052,7 +3045,7 @@ func TestRealtimeConn_RTC8a_ExplicitAuthorizeWhileConnected(t *testing.T) {

// first request is jwt request
jwtRequest := rec.Request(0).URL
assert.Equal(t, "echo.ably.io/createJWT", jwtRequest.Host+jwtRequest.Path)
assert.Equal(t, ablytest.CREATE_JWT_URL, "https://"+jwtRequest.Host+jwtRequest.Path)
// response is jwt token
jwtResponse, err := io.ReadAll(rec.Response(0).Body)
assert.NoError(t, err)
Expand Down
37 changes: 37 additions & 0 deletions ablytest/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,43 @@ func (app *Sandbox) URL(paths ...string) string {
return "https://" + app.Environment + "-rest.ably.io/" + path.Join(paths...)
}

// 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
func (app *Sandbox) GetJwtAuthParams(expiresIn time.Duration) url.Values {
key, secret := app.KeyParts()
authParams := url.Values{}
authParams.Add("environment", app.Environment)
authParams.Add("returnType", "jwt")
authParams.Add("keyName", key)
authParams.Add("keySecret", secret)
authParams.Add("expiresIn", fmt.Sprint(expiresIn.Milliseconds()))
return authParams
}

// Returns JWT with given expiry
func (app *Sandbox) CreateJwt(expiresIn time.Duration) (string, error) {
u, err := url.Parse(CREATE_JWT_URL)
if err != nil {
return "", err
}
u.RawQuery = app.GetJwtAuthParams(expiresIn).Encode()
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return "", fmt.Errorf("client: could not create request: %s", err)
}
res, err := app.client.Do(req)
if err != nil {
return "", fmt.Errorf("client: error making http request: %s", err)
}
resBody, err := io.ReadAll(res.Body)
if err != nil {
return "", fmt.Errorf("client: could not read response body: %s", err)
}
return string(resBody), nil
}

func NewHTTPClient() *http.Client {
const timeout = time.Minute
return &http.Client{
Expand Down

0 comments on commit 52e16c3

Please sign in to comment.