Skip to content

Commit

Permalink
update token model and db to support github token format
Browse files Browse the repository at this point in the history
  • Loading branch information
bprize15 committed Nov 19, 2024
1 parent a863842 commit 46cda88
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .jhipster/Token.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
"fieldName": "renewable",
"fieldType": "Boolean",
"fieldValidateRules": ["required"]
},
{
"fieldName": "newToken",
"fieldType": "String"
}
],
"changelogDate": "20190823204705",
Expand Down
3 changes: 2 additions & 1 deletion jhipster-jdl.jdl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ entity Token {
expiration Instant,
usageLimit Integer,
currentUsage Integer required,
renewable Boolean required
renewable Boolean required,
newToken String
}

entity TokenStats {
Expand Down
70 changes: 68 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 @@ -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.*;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -176,6 +194,54 @@ public String toString() {
", usageLimit=" + getUsageLimit() +
", currentUsage=" + getCurrentUsage() +
", renewable='" + isRenewable() + "'" +
", newToken='" + getNewToken() + "'" +
"}";
}
}

class TokenKeyConverter implements AttributeConverter<TokenKey, String> {
@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;
}
}
41 changes: 41 additions & 0 deletions src/main/java/org/mskcc/cbio/oncokb/domain/TokenKey.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-3.9.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.9.xsd">
<changeSet author="preiserb (generated)" id="1731955350576-1">
<addColumn tableName="token">
<column name="new_token" type="varchar(255)"/>
</addColumn>
</changeSet>
</databaseChangeLog>
1 change: 1 addition & 0 deletions src/main/resources/config/liquibase/master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<include file="config/liquibase/changelog/20191230201137_added_entity_constraints_UserMails.xml" relativeToChangelogFile="false"/>
<include file="config/liquibase/changelog/20210927165533_added_entity_constraints_CompanyDomain.xml" relativeToChangelogFile="false"/>
<include file="config/liquibase/changelog/20210927165433_added_entity_constraints_Company.xml" relativeToChangelogFile="false"/>
<include file="config/liquibase/changelog/20241118184124_added_field_newToken_to_Token.xml" relativeToChangelogFile="false"/>
<!-- jhipster-needle-liquibase-add-constraints-changelog - JHipster will add liquibase constraints changelogs here -->
<!-- jhipster-needle-liquibase-add-incremental-changelog - JHipster will add incremental liquibase changelogs here -->
</databaseChangeLog>

0 comments on commit 46cda88

Please sign in to comment.