diff --git a/src/main/java/org/mskcc/cbio/oncokb/domain/Token.java b/src/main/java/org/mskcc/cbio/oncokb/domain/Token.java index fa6c51606..009f19b9a 100644 --- a/src/main/java/org/mskcc/cbio/oncokb/domain/Token.java +++ b/src/main/java/org/mskcc/cbio/oncokb/domain/Token.java @@ -39,11 +39,11 @@ public class Token implements Serializable { @NotNull @Column(name = "current_usage", nullable = false) - private Integer currentUsage; + private Integer currentUsage = 0; @NotNull @Column(name = "renewable", nullable = false) - private Boolean renewable; + private Boolean renewable = true; @Convert(converter = TokenKeyConverter.class) @Column(name = "new_token") diff --git a/src/main/java/org/mskcc/cbio/oncokb/domain/TokenKey.java b/src/main/java/org/mskcc/cbio/oncokb/domain/TokenKey.java index f364fe8ae..ec3b54045 100644 --- a/src/main/java/org/mskcc/cbio/oncokb/domain/TokenKey.java +++ b/src/main/java/org/mskcc/cbio/oncokb/domain/TokenKey.java @@ -1,19 +1,19 @@ package org.mskcc.cbio.oncokb.domain; import java.io.Serializable; -import java.nio.ByteBuffer; import java.security.SecureRandom; import java.util.zip.CRC32; +import org.apache.commons.lang3.StringUtils; import org.mskcc.cbio.oncokb.domain.enumeration.TokenType; -import io.seruco.encoding.base62.Base62; - public class TokenKey implements Serializable { public static int TOKEN_CHAR_LENGTH = 30; public static int CHECKSUM_CHAR_LENGTH = 6; + private static String BASE62_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + private TokenType tokenType; private String token; @@ -24,24 +24,40 @@ public static TokenKey generate(TokenType type) { TokenKey tokenKey = new TokenKey(); tokenKey.setTokenType(type); - Base62 base62 = Base62.createInstance(); - SecureRandom secureRandom = new SecureRandom(); + CRC32 crc32 = new CRC32(); - byte[] bytes = new byte[24]; - secureRandom.nextBytes(bytes); - String token = new String(base62.encode(bytes)); + String token = generateToken(); tokenKey.setToken(token); - CRC32 crc32 = new CRC32(); - crc32.update(bytes); - ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES); - buffer.putLong(crc32.getValue()); - String checksum = new String(base62.encode(buffer.array())); - tokenKey.setChecksum(checksum.substring(checksum.length() - TokenKey.CHECKSUM_CHAR_LENGTH)); + crc32.update(token.getBytes()); + String base62Checksum = toBase62(crc32.getValue()); + if (base62Checksum.length() < CHECKSUM_CHAR_LENGTH) { + base62Checksum = StringUtils.repeat('0', CHECKSUM_CHAR_LENGTH - base62Checksum.length()); + } + tokenKey.setChecksum(base62Checksum); return tokenKey; } + private static String generateToken() { + SecureRandom secureRandom = new SecureRandom(); + StringBuilder token = new StringBuilder(); + for (int i = 0; i < TOKEN_CHAR_LENGTH; i++) { + token.append(BASE62_CHARS.charAt(secureRandom.nextInt(BASE62_CHARS.length()))); + } + return token.toString(); + } + + public static String toBase62(long val) { + StringBuffer sb = new StringBuffer(); + while(val > 0) { + int remainder = (int) (val % 62); + val = val / 62; + sb.insert(0, BASE62_CHARS.charAt((int) remainder)); + } + return sb.toString(); + } + public boolean validateChecksum() { return false; } diff --git a/src/test/java/org/mskcc/cbio/oncokb/domain/TokenKeyTest.java b/src/test/java/org/mskcc/cbio/oncokb/domain/TokenKeyTest.java new file mode 100644 index 000000000..c4a25a1da --- /dev/null +++ b/src/test/java/org/mskcc/cbio/oncokb/domain/TokenKeyTest.java @@ -0,0 +1,31 @@ +package org.mskcc.cbio.oncokb.domain; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.nio.ByteBuffer; + +import org.apache.commons.lang3.StringUtils; +import org.junit.Test; + +public class TokenKeyTest { + @Test + public void toBase62() { + String test = "d"; + assertThat(TokenKey.toBase62(stringToLong(test))).isEqualTo("1c"); + + test = "hello"; + assertThat(TokenKey.toBase62(stringToLong(test))).isEqualTo("7tQLFHz"); + + test = "oncokb"; + assertThat(TokenKey.toBase62(122519905332066L)).isEqualTo("Yn1xclvu"); + } + + private long stringToLong(String str) { + if (str.length() < Long.BYTES) { + str = StringUtils.repeat('\0', Long.BYTES - str.length()) + str; + } + + ByteBuffer buffer = ByteBuffer.wrap(str.getBytes()); + return buffer.getLong(0); + } +}