Skip to content

Commit

Permalink
refactor token generation
Browse files Browse the repository at this point in the history
  • Loading branch information
bprize15 committed Nov 20, 2024
1 parent 3b80ff2 commit 144af14
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/mskcc/cbio/oncokb/domain/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
44 changes: 30 additions & 14 deletions src/main/java/org/mskcc/cbio/oncokb/domain/TokenKey.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/org/mskcc/cbio/oncokb/domain/TokenKeyTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 144af14

Please sign in to comment.