Skip to content

Commit

Permalink
Merge pull request #353 from ably/349-omit-ttl
Browse files Browse the repository at this point in the history
Omit TTL in TokenRequest as JSON if unset.
  • Loading branch information
tcard authored Sep 29, 2017
2 parents b3c8862 + cbc8893 commit 73cb76d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/src/main/java/io/ably/lib/rest/Auth.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ public static class TokenParams {
* is successful, the TTL of the returned token will be less
* than or equal to this value depending on application settings
* and the attributes of the issuing key.
*
* 0 means Ably will set it to the default value.
*/
public long ttl;

Expand Down Expand Up @@ -433,7 +435,14 @@ public static TokenRequest fromJson(String json) {
* Convert a TokenParams into a JSON object.
*/
public JsonObject asJsonElement() {
return (JsonObject)Serialisation.gson.toJsonTree(this);
JsonObject o = (JsonObject)Serialisation.gson.toJsonTree(this);
if (this.ttl == 0) {
o.remove("ttl");
}
if (this.capability != null && this.capability.isEmpty()) {
o.remove("capability");
}
return o;
}

/**
Expand Down
26 changes: 26 additions & 0 deletions lib/src/test/java/io/ably/lib/test/rest/RestAuthTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,32 @@ public void auth_json_interop() {
}
}

/**
* Verify TokenRequest as JSON TTL and capability are omitted if not explicitly set to nonzero.
* Spec: RSA5, RSA6
*/
@Test
public void auth_token_request_json_omitted_defaults() {
AblyRest ably;
TokenRequest tokenRequest;
try {
for (String cap : new String[] {null, ""}) {
ClientOptions opts = createOptions(testVars.keys[0].keyStr);
opts.clientId = "Test client id";
ably = new AblyRest(opts);
tokenRequest = ably.auth.createTokenRequest(new TokenParams() {{
capability = cap;
}}, null);
String serialisedTokenRequest = tokenRequest.asJson();
assertTrue("Verify token request has no ttl", !serialisedTokenRequest.contains("ttl"));
assertTrue("Verify token request has no capability", !serialisedTokenRequest.contains("capability"));
}
} catch (AblyException e) {
e.printStackTrace();
fail("Unexpected exception");
}
}

private static TokenServer tokenServer;
private static SessionHandlerNanoHTTPD nanoHTTPD;

Expand Down

0 comments on commit 73cb76d

Please sign in to comment.