From 46cda88805a38f3638a12a30f2443aaee760fb06 Mon Sep 17 00:00:00 2001 From: bprize15 Date: Tue, 19 Nov 2024 10:18:42 -0500 Subject: [PATCH] update token model and db to support github token format --- .jhipster/Token.json | 4 ++ jhipster-jdl.jdl | 3 +- .../org/mskcc/cbio/oncokb/domain/Token.java | 70 ++++++++++++++++++- .../mskcc/cbio/oncokb/domain/TokenKey.java | 41 +++++++++++ .../oncokb/domain/enumeration/TokenType.java | 16 +++++ ...18184124_added_field_newToken_to_Token.xml | 8 +++ .../resources/config/liquibase/master.xml | 1 + 7 files changed, 140 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/mskcc/cbio/oncokb/domain/TokenKey.java create mode 100644 src/main/java/org/mskcc/cbio/oncokb/domain/enumeration/TokenType.java create mode 100644 src/main/resources/config/liquibase/changelog/20241118184124_added_field_newToken_to_Token.xml diff --git a/.jhipster/Token.json b/.jhipster/Token.json index f1b13b58a..f95832f3f 100644 --- a/.jhipster/Token.json +++ b/.jhipster/Token.json @@ -36,6 +36,10 @@ "fieldName": "renewable", "fieldType": "Boolean", "fieldValidateRules": ["required"] + }, + { + "fieldName": "newToken", + "fieldType": "String" } ], "changelogDate": "20190823204705", diff --git a/jhipster-jdl.jdl b/jhipster-jdl.jdl index d33ddd22d..f1947c306 100644 --- a/jhipster-jdl.jdl +++ b/jhipster-jdl.jdl @@ -4,7 +4,8 @@ entity Token { expiration Instant, usageLimit Integer, currentUsage Integer required, - renewable Boolean required + renewable Boolean required, + newToken String } entity TokenStats { 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 ad58f95aa..fa6c51606 100644 --- a/src/main/java/org/mskcc/cbio/oncokb/domain/Token.java +++ b/src/main/java/org/mskcc/cbio/oncokb/domain/Token.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import org.hibernate.annotations.Type; +import org.mskcc.cbio.oncokb.domain.enumeration.TokenType; import javax.persistence.*; import javax.validation.constraints.*; @@ -38,11 +39,15 @@ public class Token implements Serializable { @NotNull @Column(name = "current_usage", nullable = false) - private Integer currentUsage = 0; + private Integer currentUsage; @NotNull @Column(name = "renewable", nullable = false) - private Boolean renewable = true; + private Boolean renewable; + + @Convert(converter = TokenKeyConverter.class) + @Column(name = "new_token") + private TokenKey newToken; @ManyToOne @JsonIgnoreProperties(value = "tokens", allowSetters = true) @@ -135,6 +140,19 @@ public void setRenewable(Boolean renewable) { this.renewable = renewable; } + public TokenKey getNewToken() { + return newToken; + } + + public Token newToken(TokenKey newToken) { + this.newToken = newToken; + return this; + } + + public void setNewToken(TokenKey newToken) { + this.newToken = newToken; + } + public User getUser() { return user; } @@ -176,6 +194,54 @@ public String toString() { ", usageLimit=" + getUsageLimit() + ", currentUsage=" + getCurrentUsage() + ", renewable='" + isRenewable() + "'" + + ", newToken='" + getNewToken() + "'" + "}"; } } + +class TokenKeyConverter implements AttributeConverter { + @Override + public String convertToDatabaseColumn(TokenKey tokenKey) { + if (tokenKey.getTokenType() == null || tokenKey.getToken() == null || tokenKey.getChecksum() == null) { + return null; + } + if (tokenKey.getToken().length() != TokenKey.TOKEN_CHAR_LENGTH || tokenKey.getChecksum().length() != TokenKey.CHECKSUM_CHAR_LENGTH) { + return null; + } + return tokenKey.getTokenType().getType() + "_" + tokenKey.getToken() + tokenKey.getChecksum(); + } + + @Override + public TokenKey convertToEntityAttribute(String dbData) { + if (dbData == null) { + return null; + } + String[] parts = dbData.split("_"); + if (parts.length != 2) { + return null; + } + + + TokenKey tokenKey = new TokenKey(); + String type = parts[0]; + if (type.equals(TokenType.SERVICE.getType())) { + tokenKey.setTokenType(TokenType.SERVICE); + } else if (type.equals(TokenType.USER.getType())) { + tokenKey.setTokenType(TokenType.USER); + } else { + return null; + } + + String tokenAndChecksum = parts[1]; + if (tokenAndChecksum.length() == TokenKey.TOKEN_CHAR_LENGTH + TokenKey.CHECKSUM_CHAR_LENGTH) { + String token = tokenAndChecksum.substring(0, TokenKey.TOKEN_CHAR_LENGTH); + String checksum = tokenAndChecksum.substring(TokenKey.TOKEN_CHAR_LENGTH); + tokenKey.setToken(token); + tokenKey.setChecksum(checksum); + } else { + return null; + } + + return tokenKey; + } +} \ No newline at end of file diff --git a/src/main/java/org/mskcc/cbio/oncokb/domain/TokenKey.java b/src/main/java/org/mskcc/cbio/oncokb/domain/TokenKey.java new file mode 100644 index 000000000..0c1f59519 --- /dev/null +++ b/src/main/java/org/mskcc/cbio/oncokb/domain/TokenKey.java @@ -0,0 +1,41 @@ +package org.mskcc.cbio.oncokb.domain; + +import java.io.Serializable; + +import org.mskcc.cbio.oncokb.domain.enumeration.TokenType; + +public class TokenKey implements Serializable { + public static int TOKEN_CHAR_LENGTH = 30; + + public static int CHECKSUM_CHAR_LENGTH = 6; + + private TokenType tokenType; + + private String token; + + private String checksum; + + public TokenType getTokenType() { + return tokenType; + } + + public void setTokenType(TokenType tokenType) { + this.tokenType = tokenType; + } + + public String getToken() { + return token; + } + + public void setToken(String token) { + this.token = token; + } + + public String getChecksum() { + return checksum; + } + + public void setChecksum(String checksum) { + this.checksum = checksum; + } +} diff --git a/src/main/java/org/mskcc/cbio/oncokb/domain/enumeration/TokenType.java b/src/main/java/org/mskcc/cbio/oncokb/domain/enumeration/TokenType.java new file mode 100644 index 000000000..b54ae5dcf --- /dev/null +++ b/src/main/java/org/mskcc/cbio/oncokb/domain/enumeration/TokenType.java @@ -0,0 +1,16 @@ +package org.mskcc.cbio.oncokb.domain.enumeration; + +public enum TokenType { + USER("ocku"), + SERVICE("okbs"); + + String type; + + TokenType(String type) { + this.type = type; + } + + public String getType() { + return this.type; + } +} diff --git a/src/main/resources/config/liquibase/changelog/20241118184124_added_field_newToken_to_Token.xml b/src/main/resources/config/liquibase/changelog/20241118184124_added_field_newToken_to_Token.xml new file mode 100644 index 000000000..dac25f73e --- /dev/null +++ b/src/main/resources/config/liquibase/changelog/20241118184124_added_field_newToken_to_Token.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/src/main/resources/config/liquibase/master.xml b/src/main/resources/config/liquibase/master.xml index b90a7fbb7..68ea5b5a5 100644 --- a/src/main/resources/config/liquibase/master.xml +++ b/src/main/resources/config/liquibase/master.xml @@ -27,6 +27,7 @@ +