opinionated-vertx/src/de/pzzz/vertx/oauth/OAuthToken.java

59 lines
1.3 KiB
Java
Executable file

package de.pzzz.vertx.oauth;
import java.io.Serializable;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* Represents an OAuth2 Token as received from IAM via client credentials flow.
*/
public class OAuthToken implements Serializable {
private static final long serialVersionUID = -1712414758315937247L;
private static final Integer EXPIRATION_OFFSET = 30;
@JsonProperty("access_token")
private String token;
@JsonProperty("token_type")
private String type;
@JsonProperty("expires_in")
private Integer expiresIn;
@JsonIgnore
private final Date issued;
public OAuthToken() {
issued = new Date();
}
public boolean isValid() {
int diffSeconds = (int) Math.ceil((new Date().getTime() - issued.getTime())/1000f);
return diffSeconds < (expiresIn - EXPIRATION_OFFSET);
}
public String getToken() {
return token;
}
public void setToken(final String token) {
this.token = token;
}
public String getType() {
return type;
}
public void setType(final String type) {
this.type = type;
}
public Integer getExpiresIn() {
return expiresIn;
}
public void setExpiresIn(final Integer expiresIn) {
this.expiresIn = expiresIn;
}
}