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

Auth.assertValidToken: always remove old token when force == true. #354

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
8 changes: 3 additions & 5 deletions lib/src/main/java/io/ably/lib/rest/Auth.java
Original file line number Diff line number Diff line change
Expand Up @@ -948,11 +948,9 @@ public TokenDetails assertValidToken() throws AblyException {
private TokenDetails assertValidToken(TokenParams params, AuthOptions options, boolean force) throws AblyException {
Log.i("Auth.assertValidToken()", "");
if(tokenDetails != null) {
if(tokenDetails.expires == 0 || tokenValid(tokenDetails)) {
if (!force) {
Log.i("Auth.assertValidToken()", "using cached token; expires = " + tokenDetails.expires);
return tokenDetails;
}
if(!force && (tokenDetails.expires == 0 || tokenValid(tokenDetails))) {
Log.i("Auth.assertValidToken()", "using cached token; expires = " + tokenDetails.expires);
return tokenDetails;
} else {
/* expired, so remove */
Log.i("Auth.assertValidToken()", "deleting expired token");
Expand Down
41 changes: 41 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 @@ -1717,6 +1717,47 @@ public void auth_token_request_json_omitted_defaults() {
}
}

/**
* Verify that renewing the token when useTokenAuth is true doesn't use the old (expired) token.
*/
@Test
public void auth_renew_token_bearer_auth() {
try {
ClientOptions opts = createOptions(testVars.keys[0].keyStr);
opts.useTokenAuth = true;
opts.defaultTokenParams = new TokenParams() {{
ttl = 100;
}};
AblyRest ably = new AblyRest(opts);

// Any request will issue a new token with the defaultTokenParams and use it.

ably.channels.get("test").history(null);
TokenDetails oldToken = ably.auth.getTokenDetails();

// Sleep until old token expires, then ensure it did.

Thread.sleep(110);
ClientOptions optsWithOldToken = createOptions();
optsWithOldToken.tokenDetails = oldToken;
AblyRest ablyWithOldToken = new AblyRest(optsWithOldToken);
try {
ablyWithOldToken.channels.get("test").history(null);
fail("expected old token to be expired already");
} catch(AblyException e) {}

// The library should now renew the token using the key.

ably.channels.get("test").history(null);
TokenDetails newToken = ably.auth.getTokenDetails();

assertNotEquals(oldToken.token, newToken.token);
} catch (Exception e) {
e.printStackTrace();
fail("Unexpected exception");
}
}

private static TokenServer tokenServer;
private static SessionHandlerNanoHTTPD nanoHTTPD;

Expand Down