DefaultBearerTokenProvider implements BearerTokenProvider { protected String generateTokenKey(int size) { if (size < 0) { throw new IllegalArgumentException("Token key size is less than 0"); } int count = (int)Math.ceil((double)size / 8); byte[] buffer = new byte[count * 8]; for (int i = 0; i < count; i++) { BigEndianCodec.putLong(buffer, i * 8, SecureRandomUtil.nextLong()); } StringBundler sb = new StringBundler(size); for (int i = 0; i < size; i++) { sb.append(Integer.toHexString(0xFF & buffer[i])); } return sb.toString(); }  @Override boolean isValid(AccessToken accessToken); @Override boolean isValid(RefreshToken refreshToken); @Override void onBeforeCreate(AccessToken accessToken); @Override void onBeforeCreate(RefreshToken refreshToken);  }
@Test public void testGenerateTokenKey() { Assert.assertEquals( "", _defaultBearerTokenProvider.generateTokenKey(0)); Assert.assertEquals( _TOKEN_KEY_STRING_32_BYTES_HEX.substring(0, 4), _defaultBearerTokenProvider.generateTokenKey(2)); Assert.assertEquals( _TOKEN_KEY_STRING_32_BYTES_HEX.substring(0, 16), _defaultBearerTokenProvider.generateTokenKey(8)); Assert.assertEquals( _TOKEN_KEY_STRING_32_BYTES_HEX.substring(0, 20), _defaultBearerTokenProvider.generateTokenKey(10)); Assert.assertEquals( _TOKEN_KEY_STRING_32_BYTES_HEX + _TOKEN_KEY_STRING_32_BYTES_HEX, _defaultBearerTokenProvider.generateTokenKey(64)); }
@Test(expected = IllegalArgumentException.class) public void testGenerateTokenKeyWithNegativeKeySize() { _defaultBearerTokenProvider.generateTokenKey(-1); }