Skip to content

Commit

Permalink
Make hashed token ids url safe (#42651)
Browse files Browse the repository at this point in the history
This commit changes the way token ids are hashed so that the output is
url safe without requiring encoding. This follows the pattern that we
use for document ids that are autogenerated, see UUIDs and the
associated classes for additional details.
  • Loading branch information
jaymode committed May 30, 2019
1 parent 64e9c88 commit f7abb1a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -360,14 +360,14 @@ public boolean verify(SecureString text, char[] hash) {
public char[] hash(SecureString text) {
MessageDigest md = MessageDigests.sha256();
md.update(CharArrays.toUtf8Bytes(text.getChars()));
return Base64.getEncoder().encodeToString(md.digest()).toCharArray();
return Base64.getUrlEncoder().withoutPadding().encodeToString(md.digest()).toCharArray();
}

@Override
public boolean verify(SecureString text, char[] hash) {
MessageDigest md = MessageDigests.sha256();
md.update(CharArrays.toUtf8Bytes(text.getChars()));
return CharArrays.constantTimeEquals(Base64.getEncoder().encodeToString(md.digest()).toCharArray(), hash);
return CharArrays.constantTimeEquals(Base64.getUrlEncoder().withoutPadding().encodeToString(md.digest()).toCharArray(), hash);
}
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public final class TokenService {
TimeValue.MINUS_ONE, Property.NodeScope);

static final String TOKEN_DOC_TYPE = "token";
private static final int HASHED_TOKEN_LENGTH = 44;
private static final int HASHED_TOKEN_LENGTH = 43;
// UUIDs are 16 bytes encoded base64 without padding, therefore the length is (16 / 3) * 4 + ((16 % 3) * 8 + 5) / 6 chars
private static final int TOKEN_LENGTH = 22;
private static final String TOKEN_DOC_ID_PREFIX = TOKEN_DOC_TYPE + "_";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@
import org.junit.Before;
import org.junit.BeforeClass;

import javax.crypto.SecretKey;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.time.Clock;
import java.time.Instant;
Expand All @@ -70,8 +73,6 @@
import java.util.HashMap;
import java.util.Map;

import javax.crypto.SecretKey;

import static java.time.Clock.systemUTC;
import static org.elasticsearch.repositories.ESBlobStoreTestCase.randomBytes;
import static org.elasticsearch.test.ClusterServiceUtils.setState;
Expand Down Expand Up @@ -722,6 +723,11 @@ public void testCannotValidateTokenIfLicenseDoesNotAllowTokens() throws Exceptio
assertThat(authToken, Matchers.nullValue());
}

public void testHashedTokenIsUrlSafe() throws Exception {
final String hashedId = TokenService.hashTokenString(UUIDs.randomBase64UUID());
assertEquals(hashedId, URLEncoder.encode(hashedId, StandardCharsets.UTF_8.name()));
}

private TokenService createTokenService(Settings settings, Clock clock) throws GeneralSecurityException {
return new TokenService(settings, clock, client, licenseState, securityMainIndex, securityTokensIndex, clusterService);
}
Expand Down

0 comments on commit f7abb1a

Please sign in to comment.