Skip to content

Commit

Permalink
Check whole token chain for expiry
Browse files Browse the repository at this point in the history
Also ensures the whole token chain is valid after calling refresh
  • Loading branch information
RaphiMC committed Aug 22, 2024
1 parent b3a49f8 commit a56d467
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,21 @@ public abstract static class StepResult<P extends StepResult<?>> {
protected abstract P prevResult();

/**
* Checks if this result is expired.<br>
* Checks if this or any previous result is expired.<br>
* Results that have no expire time returned by their API are never considered expired.<br>
* If you want to minimize the amount of HTTP requests, you should only call {@link AbstractStep#refresh} if this method returns true.<br>
* For certain use cases, like joining a Minecraft server, you want to make sure that the data is up-to-date, even if it is not expired yet. See {@link StepResult#isExpiredOrOutdated}.
*
* @return true if this result is expired
* @return true if this or any previous result is expired
*/
public abstract boolean isExpired();

/**
* Checks if this result is expired or potentially outdated.<br>
* Checks if this or any previous result is expired or potentially outdated.<br>
* Results that have no expire time returned by their API are always considered outdated.<br>
* If you want the data in the result to be up-to-date, you should call {@link AbstractStep#refresh} if this method returns true.<br>
*
* @return true if this result is potentially outdated or expired
* @return true if this or any previous result is potentially outdated or expired
*/
public boolean isExpiredOrOutdated() {
return this.isExpired();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ protected StepXblXstsToken.XblXsts<?> prevResult() {

@Override
public boolean isExpired() {
if (this.prevResult().isExpired()) {
return true;
}

// Cache the result for 1 second because it's expensive to check
if (System.currentTimeMillis() - this.lastExpireCheckTimeMs < 1000) {
return this.lastExpireCheckResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ protected StepXblXstsToken.XblXsts<?> prevResult() {

@Override
public boolean isExpired() {
return this.expireTimeMs <= System.currentTimeMillis();
return this.expireTimeMs <= System.currentTimeMillis() || this.prevResult().isExpired();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ protected StepXblXstsToken.XblXsts<?> prevResult() {

@Override
public boolean isExpired() {
return this.expireTimeMs <= System.currentTimeMillis();
return this.expireTimeMs <= System.currentTimeMillis() || this.prevResult().isExpired();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected StepMCToken.MCToken prevResult() {

@Override
public boolean isExpired() {
return this.expireTimeMs <= System.currentTimeMillis();
return this.expireTimeMs <= System.currentTimeMillis() || this.prevResult().isExpired();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ protected StepInitialXblSession.InitialXblSession prevResult() {

@Override
public boolean isExpired() {
return this.userToken.isExpired() || this.titleToken.isExpired() || this.xstsToken.isExpired();
return this.userToken.isExpired() || this.titleToken.isExpired() || this.xstsToken.isExpired() || this.prevResult().isExpired();
}

@Override
public boolean isExpiredOrOutdated() {
return this.userToken.isExpiredOrOutdated() || this.titleToken.isExpiredOrOutdated() || this.xstsToken.isExpiredOrOutdated();
return this.userToken.isExpiredOrOutdated() || this.titleToken.isExpiredOrOutdated() || this.xstsToken.isExpiredOrOutdated() || this.prevResult().isExpiredOrOutdated();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ protected StepInitialXblSession.InitialXblSession prevResult() {

@Override
public boolean isExpired() {
return this.expireTimeMs <= System.currentTimeMillis();
return this.expireTimeMs <= System.currentTimeMillis() || this.prevResult().isExpired();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ protected StepInitialXblSession.InitialXblSession prevResult() {

@Override
public boolean isExpired() {
return this.expireTimeMs <= System.currentTimeMillis();
return this.expireTimeMs <= System.currentTimeMillis() || this.prevResult().isExpired();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ protected StepFullXblSession.FullXblSession prevResult() {

@Override
public boolean isExpired() {
return this.expireTimeMs <= System.currentTimeMillis();
return this.expireTimeMs <= System.currentTimeMillis() || this.prevResult().isExpired();
}

}
Expand Down

0 comments on commit a56d467

Please sign in to comment.