Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omit TTL in TokenRequest as JSON if unset. #353

Merged
merged 1 commit into from
Sep 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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