Created
March 26, 2018 22:49
-
-
Save briandilley/038d09c95c8cdf210a844b36e5b17423 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.flipagram.core.auth; | |
import org.joda.time.DateTime; | |
import com.fasterxml.jackson.annotation.JsonIgnore; | |
import com.flipagram.lib.joda.JodaUtil; | |
import com.flipagram.lib.model.AbstractModelObject; | |
import com.flipagram.lib.model.Fid; | |
import com.flipagram.lib.model.JsonBackedModelObject; | |
import com.flipagram.lib.util.StringUtil; | |
/** | |
* An access token. | |
*/ | |
@SuppressWarnings("serial") | |
public class AccessToken | |
extends AbstractModelObject | |
implements JsonBackedModelObject<Fid> { | |
public static final String ACCESS_TOKEN_ENC_KEY = "This is the encryption key, keep it secret"; | |
private Fid id; | |
private Fid userId; | |
private Fid apiKeyId; | |
private Fid deviceId; | |
private DateTime dateCreated; | |
private DateTime dateExpires; | |
private String version; | |
/** | |
* Creates an {@link AccessToken} from the given token. | |
* @param token the token | |
* @return the token | |
*/ | |
public static AccessToken fromToken(String token) { | |
String[] parts = token.split(":"); | |
if (parts.length!=7) { | |
throw new IllegalArgumentException("Expected 7 parts"); | |
} | |
AccessToken ret = new AccessToken(); | |
ret.version = parts[0].length()>0 ? parts[0] : null; | |
ret.id = new Fid(Long.parseLong(parts[1])); | |
ret.apiKeyId = new Fid(parts[2]); | |
ret.deviceId = parts[3].length()>0 ? new Fid(parts[3]) : null; | |
ret.userId = parts[4].length()>0 ? new Fid(parts[4]) : null; | |
ret.dateCreated = JodaUtil.forMillis(Long.parseLong(parts[5])); | |
ret.dateExpires = JodaUtil.forMillis(Long.parseLong(parts[6])); | |
return ret; | |
} | |
/** | |
* Creates an {@link AccessToken} from the given encoded token. | |
* @param encodedToken the encoded token | |
* @return the token | |
*/ | |
public static AccessToken fromEncodedToken(String encodedToken) { | |
if (!StringUtil.isEmpty(encodedToken)) { | |
return fromToken(StringUtil.aesDecodeString( | |
encodedToken, ACCESS_TOKEN_ENC_KEY)); | |
} | |
return null; | |
} | |
@Override | |
public Fid getId() { | |
return id; | |
} | |
@Override | |
public void setId(Fid id) { | |
this.id = id; | |
} | |
/** | |
* @return the userId | |
*/ | |
public Fid getUserId() { | |
return userId; | |
} | |
/** | |
* @param userId the userId to set | |
*/ | |
public void setUserId(Fid userId) { | |
this.userId = userId; | |
} | |
/** | |
* @return the apiKeyId | |
*/ | |
public Fid getApiKeyId() { | |
return apiKeyId; | |
} | |
/** | |
* @param apiKeyId the apiKeyId to set | |
*/ | |
public void setApiKeyId(Fid apiKeyId) { | |
this.apiKeyId = apiKeyId; | |
} | |
/** | |
* @return the deviceId | |
*/ | |
public Fid getDeviceId() { | |
return deviceId; | |
} | |
/** | |
* @param deviceId the deviceId | |
*/ | |
public void setDeviceId(Fid deviceId) { | |
this.deviceId = deviceId; | |
} | |
/** | |
* Returns a decoded version of the token. | |
* @return the token | |
*/ | |
@JsonIgnore | |
public String getDecodedToken() { | |
String buf = new StringBuilder() | |
.append(version!=null ? version : "").append(":") | |
.append(id!=null ? id.asLong() : null).append(":") | |
.append(apiKeyId).append(":") | |
.append(deviceId!=null ? deviceId : "").append(":") | |
.append(userId!=null ? userId : "").append(":") | |
.append(dateCreated!=null ? dateCreated.getMillis() : 0).append(":") | |
.append(dateExpires!=null ? dateExpires.getMillis() : 0) | |
.toString(); | |
return buf; | |
} | |
/** | |
* Returns an encoded version of the token. | |
* @return the token | |
*/ | |
@JsonIgnore | |
public String getToken() { | |
return StringUtil.aesEncodeString( | |
getDecodedToken(), ACCESS_TOKEN_ENC_KEY); | |
} | |
/** | |
* @return the dateCreated | |
*/ | |
public DateTime getDateCreated() { | |
return dateCreated; | |
} | |
/** | |
* @param dateCreated the dateCreated to set | |
*/ | |
public void setDateCreated(DateTime dateCreated) { | |
this.dateCreated = dateCreated; | |
} | |
/** | |
* @return the dateExpires | |
*/ | |
public DateTime getDateExpires() { | |
return dateExpires; | |
} | |
/** | |
* @param dateExpires the dateExpires to set | |
*/ | |
public void setDateExpires(DateTime dateExpires) { | |
this.dateExpires = dateExpires; | |
} | |
@Override | |
public String getVersion() { | |
return version; | |
} | |
@Override | |
public void setVersion(String version) { | |
this.version = version; | |
} | |
@Override | |
public String toString() { | |
return getToken(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment