8000 Handle current time in seconds as long by ahus1 · Pull Request #11611 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -364,17 +364,17 @@ public String getCorsAllowedHeaders() {
}

@Override
public void setNotBefore(int notBefore) {
public void setNotBefore(long notBefore) {
delegate.setNotBefore(notBefore);
}

@Override
public int getNotBefore() {
public long getNotBefore() {
return delegate.getNotBefore();
}

@Override
public void updateNotBefore(int notBefore) {
public void updateNotBefore(long notBefore) {
delegate.setNotBefore(notBefore);
getPublicKeyLocator().reset(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public class KeycloakDeployment {
protected int registerNodePeriod;
protected boolean turnOffChangeSessionIdOnLogin;

protected volatile int notBefore;
protected volatile long notBefore;
protected int tokenMinimumTimeToLive;
protected int minTimeBetweenJwksRequests;
protected int publicKeyCacheTtl;
Expand Down Expand Up @@ -462,15 +462,15 @@ public void setExposeToken(boolean exposeToken) {
this.exposeToken = exposeToken;
}

public int getNotBefore() {
public long getNotBefore() {
return notBefore;
}

public void setNotBefore(int notBefore) {
public void setNotBefore(long notBefore) {
this.notBefore = notBefore;
}

public void updateNotBefore(int notBefore) {
public void updateNotBefore(long notBefore) {
this.notBefore = notBefore;
getPublicKeyLocator().reset(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ protected boolean sendUnregistrationEvent(KeycloakDeployment deployment) {

public static class NodeRegistrationContext {

private final Integer lastRegistrationTime;
private final Long lastRegistrationTime;
// deployment instance used for registration request
private final KeycloakDeployment resolvedDeployment;

public NodeRegistrationContext(Integer lastRegTime, KeycloakDeployment deployment) {
public NodeRegistrationContext(Long lastRegTime, KeycloakDeployment deployment) {
this.lastRegistrationTime = lastRegTime;
this.resolvedDeployment = deployment;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ protected JsonWebToken createRequestToken(String clientId, String realmInfoUrl)
reqToken.subject(clientId);
reqToken.audience(realmInfoUrl);

int now = Time.currentTime();
reqToken.issuedAt(now);
reqToken.expiration(now + this.tokenTimeout);
reqToken.notBefore(now);
long now = Time.currentTime();
reqToken.iat(now);
reqToken.exp(now + this.tokenTimeout);
reqToken.nbf(now);

return reqToken;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ protected JsonWebToken createRequestToken(String clientId, String realmInfoUrl)
reqToken.subject(clientId);
reqToken.audience(realmInfoUrl);

int now = Time.currentTime();
reqToken.issuedAt(now);
long now = Time.currentTime();
reqToken.iat(now);
// the same as in KEYCLOAK-2986, JWTClientCredentialsProvider's timeout field
reqToken.expiration(now + 10);
reqToken.notBefore(now);
reqToken.exp(now + 10);
reqToken.nbf(now);
return reqToken;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ public class JWKPublicKeyLocator implements PublicKeyLocator {

private Map<String, PublicKey> currentKeys = new ConcurrentHashMap<>();

private volatile int lastRequestTime = 0;
private volatile long lastRequestTime = 0;

@Override
public PublicKey getPublicKey(String kid, KeycloakDeployment deployment) {
int minTimeBetweenRequests = deployment.getMinTimeBetweenJwksRequests();
int publicKeyCacheTtl = deployment.getPublicKeyCacheTtl();
int currentTime = Time.currentTime();
long currentTime = Time.currentTime();

// Check if key is in cache.
PublicKey publicKey = lookupCachedKey(publicKeyCacheTtl, currentTime, kid);
Expand Down Expand Up @@ -81,7 +81,7 @@ public void reset(KeycloakDeployment deployment) {
}


private PublicKey lookupCachedKey(int publicKeyCacheTtl, int currentTime, String kid) {
private PublicKey lookupCachedKey(int publicKeyCacheTtl, long currentTime, String kid) {
if (lastRequestTime + publicKeyCacheTtl > currentTime && kid != null) {
return currentKeys.get(kid);
} else {
Expand Down
10BC8
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ public void sameIssuedAtAsNotBeforeIsActiveKEYCLOAK10013() {

TokenMetadataRepresentation token = new TokenMetadataRepresentation();
token.setActive(true);
token.issuedAt(4999);
token.iat(4999L);

RefreshableKeycloakSecurityContext sut = new RefreshableKeycloakSecurityContext(keycloakDeployment,null,null,token,null, null, null);

assertFalse(sut.isActive());

token.issuedAt(5000);
token.iat(5000L);
assertTrue(sut.isActive());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ public String getTokenString() {
}

public String getTokenString(long minValidity, TimeUnit unit) throws VerificationException, IOException, ServerRequest.HttpFailure {
long expires = ((long) token.getExpiration()) * 1000 - unit.toMillis(minValidity);
long expires = token.getExp() * 1000 - unit.toMillis(minValidity);
if (expires < System.currentTimeMillis()) {
refreshToken();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class SamlDescriptorPublicKeyLocator implements KeyLocator, Iterable<Publ

private final HttpClient client;

private volatile int lastRequestTime = 0;
private volatile long lastRequestTime = 0;

public SamlDescriptorPublicKeyLocator(String descriptorUrl, int minTimeBetweenDescriptorRequests, int cacheEntryTtl, HttpClient httpClient) {
this.minTimeBetweenDescriptorRequests = minTimeBetweenDescriptorRequests <= 0
Expand All @@ -93,7 +93,7 @@ public Key getKey(String kid) throws KeyManagementException {

LOG.tracef("Requested key id: %s", kid);

int currentTime = Time.currentTime();
long currentTime = Time.currentTime();

PublicKey res;
if (currentTime > this.lastRequestTime + this.cacheEntryTtl) {
Expand Down
8 changes: 4 additions & 4 deletions common/src/main/java/org/keycloak/common/util/Time.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public class Time {
* Returns current time in seconds adjusted by adding {@link #offset) seconds.
* @return see description
*/
public static int currentTime() {
return ((int) (System.currentTimeMillis() / 1000)) + offset;
public static long currentTime() {
return (System.currentTimeMillis() / 1000) + offset;
}

/**
Expand All @@ -47,7 +47,7 @@ public static long currentTimeMillis() {
* @param time Time in milliseconds since the epoch
* @return see description
*/
public static Date toDate(int time) {
public static Date toDate(long time) {
return new Date(time * 1000L);
}

Expand All @@ -56,7 +56,7 @@ public static Date toDate(int time) {
* @param time Time in milliseconds since the epoch
* @return see description
*/
public static Date toDate(long time) {
public static Date toDateFromMillis(long time) {
return new Date(time);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,22 +194,6 @@ public AccessToken id(String id) {
return (AccessToken) super.id(id);
}

@Override
public AccessToken expiration(int expiration) {
return (AccessToken) super.expiration(expiration);
}

@Override
public AccessToken notBefore(int notBefore) {
return (AccessToken) super.notBefore(notBefore);
}


@Override
public AccessToken issuedAt(int issuedAt) {
return (AccessToken) super.issuedAt(issuedAt);
}

@Override
public AccessToken issuer(String issuer) {
return (AccessToken) super.issuer(issuer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class AccessTokenResponse {
protected String idToken;

@JsonProperty("not-before-policy")
protected int notBeforePolicy;
protected long notBeforePolicy;

@JsonProperty("session_state")
protected String sessionState;
Expand Down Expand Up @@ -126,11 +126,11 @@ public void setIdToken(String idToken) {
this.idToken = idToken;
}

public int getNotBeforePolicy() {
public long getNotBeforePolicy() {
return notBeforePolicy;
}

public void setNotBeforePolicy(int notBeforePolicy) {
public void setNotBeforePolicy(long notBeforePolicy) {
this.notBeforePolicy = notBeforePolicy;
}

Expand Down
13 changes: 3 additions & 10 deletions core/src/main/java/org/keycloak/representations/IDToken.java
10BC8
Original file line number Diff line number Diff line change
Expand Up @@ -158,25 +158,18 @@ public Long getAuth_time() {
}

/**
* @deprecated int will overflow with values after 2038. Use {@link #getAuth_time()} instead.
* @deprecated will return nbf null-safe. Use {@link #getAuth_time()} instead.
*/
@Deprecated
@JsonIgnore
public int getAuthTime() {
return auth_time != null ? auth_time.intValue() : 0;
public long getAuthTime() {
return auth_time != null ? auth_time : 0;
}

public void setAuth_time(Long auth_time) {
this.auth_time = auth_time;
}

/**
* @deprecated int will overflow with values after 2038. Use {@link #setAuth_time(Long)} ()} instead.
*/
public void setAuthTime(int authTime) {
this.auth_time = Long.valueOf(authTime);
}

@JsonProperty(SESSION_ID)
public String getSessionId() {
return sessionState;
Expand Down
49 changes: 10 additions & 39 deletions core/src/main/java/org/keycloak/representations/JsonWebToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,19 @@ public Long getExp() {
}

/**
* @deprecated int will overflow with values after 2038. Use {@link #getExp()} instead.
* @deprecated will return expiration null-safe. Use {@link #getExp()} instead.
*/
@Deprecated
@JsonIgnore
public int getExpiration() {
return exp != null ? exp.intValue() : 0;
public long getExpiration() {
return exp != null ? exp : 0;
}

public JsonWebToken exp(Long exp) {
this.exp = exp;
return this;
}

/**
* @deprecated int will overflow with values after 2038. Use {@link #exp(Long)} instead.
*/
public JsonWebToken expiration(int expiration) {
this.exp = Long.valueOf(expiration);
return this;
}

@JsonIgnore
public boolean isExpired() {
return exp != null && exp != 0 ? Time.currentTime() > exp : false;
Expand All @@ -106,29 +98,19 @@ public Long getNbf() {
}

/**
* @deprecated int will overflow with values after 2038. Use {@link #getNbf()} instead.
* @deprecated will return nbf null-safe. Use {@link #getNbf()} instead.
*/
@Deprecated
@JsonIgnore
public int getNotBefore() {
return nbf != null ? nbf.intValue() : 0;
public long getNotBefore() {
return nbf != null ? nbf : 0;
}

public JsonWebToken nbf(Long nbf) {
this.nbf = nbf;
return this;
}

/**
* @deprecated int will overflow with values after 2038. Use {@link #nbf(Long)} instead.
*/
@Deprecated
@JsonIgnore
public JsonWebToken notBefore(int notBefore) {
this.nbf = Long.valueOf(notBefore);
return this;
}

@JsonIgnore
public boolean isNotBefore(int allowedTimeSkew) {
return nbf != null ? Time.currentTime() + allowedTimeSkew >= nbf : true;
Expand Down Expand Up @@ -163,20 +145,20 @@ public Long getIat() {
}

/**
* @deprecated int will overflow with values after 2038. Use {@link #getIat()} instead.
* @deprecated will return iat null-safe. Use {@link #getIat()} instead.
*/
@Deprecated
@JsonIgnore
public int getIssuedAt() {
return iat != null ? iat.intValue() : 0;
public long getIssuedAt() {
return iat != null ? iat : 0;
}

/**
* Set issuedAt to the current time
*/
@JsonIgnore
public JsonWebToken issuedNow() {
iat = Long.valueOf(Time.currentTime());
iat = Time.currentTime();
return this;
}

Expand All @@ -185,17 +167,6 @@ public JsonWebToken iat(Long iat) {
return this;
}

/**
* @deprecated int will overflow with values after 2038. Use {@link #iat(Long)} ()} instead.
*/
@Deprecated
@JsonIgnore
public JsonWebToken issuedAt(int issuedAt) {
this.iat = Long.valueOf(issuedAt);
return this;
}


public String getIssuer() {
return issuer;
}
Expand Down
45BA
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static DeviceRepresentation unknown() {
private String osVersion;
private String browser;
private String device;
private int lastAccess;
private long lastAccess;
private Boolean current;
private List<SessionRepresentation> sessions;
private boolean mobile;
Expand Down Expand Up @@ -108,11 +108,11 @@ public void setDevice(String device) {
this.device = device;
}

public int getLastAccess() {
public long getLastAccess() {
return lastAccess;
}

public void setLastAccess(int lastAccess) {
public void setLastAccess(long lastAccess) {
this.lastAccess = lastAccess;
}

Expand Down
Loading
0