Skip to content

Commit

Permalink
add new token generation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bprize15 committed Nov 19, 2024
1 parent a50971f commit 3b80ff2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/main/java/org/mskcc/cbio/oncokb/domain/TokenKey.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
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.mskcc.cbio.oncokb.domain.enumeration.TokenType;

import io.seruco.encoding.base62.Base62;

public class TokenKey implements Serializable {
public static int TOKEN_CHAR_LENGTH = 30;

Expand All @@ -15,6 +20,32 @@ public class TokenKey implements Serializable {

private String checksum;

public static TokenKey generate(TokenType type) {
TokenKey tokenKey = new TokenKey();
tokenKey.setTokenType(type);

Base62 base62 = Base62.createInstance();
SecureRandom secureRandom = new SecureRandom();

byte[] bytes = new byte[24];
secureRandom.nextBytes(bytes);
String token = new String(base62.encode(bytes));
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));

return tokenKey;
}

public boolean validateChecksum() {
return false;
}

public TokenType getTokenType() {
return tokenType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.mskcc.cbio.oncokb.domain.Authority;
import org.mskcc.cbio.oncokb.domain.Token;
import org.mskcc.cbio.oncokb.domain.TokenKey;
import org.mskcc.cbio.oncokb.domain.User;
import org.mskcc.cbio.oncokb.domain.enumeration.TokenType;
import org.mskcc.cbio.oncokb.repository.UserRepository;
import org.mskcc.cbio.oncokb.security.AuthoritiesConstants;
import org.mskcc.cbio.oncokb.security.SecurityUtils;
Expand Down Expand Up @@ -74,6 +76,7 @@ private Token getNewToken(Set<Authority> authorities, Optional<Instant> definedE
token.setExpiration(expirationTime);
}
token.setToken(UUID.randomUUID());
token.setNewToken(TokenKey.generate(TokenType.USER));
return token;
}

Expand Down

0 comments on commit 3b80ff2

Please sign in to comment.