Skip to content

Commit 5d18201

Browse files
committed
show when token expired
1 parent 48082c9 commit 5d18201

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

core/src/main/java/jenkins/security/ApiTokenProperty.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,16 @@ public static class TokenInfoAndStats {
257257
public final Date lastUseDate;
258258
public final long numDaysUse;
259259
public final String expirationDate;
260+
public final boolean expired;
260261

261262
public TokenInfoAndStats(@NonNull ApiTokenStore.HashedToken token, @NonNull ApiTokenStats.SingleTokenStats stats) {
262263
this.uuid = token.getUuid();
263264
this.name = token.getName();
264265
this.creationDate = token.getCreationDate();
265266
this.numDaysCreation = token.getNumDaysCreation();
266267
this.isLegacy = token.isLegacy();
268+
this.expired = token.isExpired();
269+
267270
LocalDate expirationDate = token.getExpirationDate();
268271
if (expirationDate == null) {
269272
this.expirationDate = "never";

core/src/main/java/jenkins/security/apitoken/ApiTokenStore.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,12 @@ public void rename(String newName) {
433433
this.name = newName;
434434
}
435435

436+
public boolean isExpired() {
437+
LocalDate now = LocalDate.now();
438+
LocalDate expiration = Objects.requireNonNullElseGet(expirationDate, LocalDate::now);
439+
return expiration.isBefore(now);
440+
}
441+
436442
public boolean match(byte[] hashedBytes) {
437443
byte[] hashFromHex;
438444
try {
@@ -442,9 +448,7 @@ public boolean match(byte[] hashedBytes) {
442448
return false;
443449
}
444450

445-
LocalDate now = LocalDate.now();
446-
LocalDate expiration = Objects.requireNonNullElseGet(expirationDate, LocalDate::now);
447-
boolean expired = expiration.isBefore(now);
451+
boolean expired = isExpired();
448452
// String.equals() is not constant-time but this method is. No link between correctness and time spent
449453
return MessageDigest.isEqual(hashFromHex, hashedBytes) && !expired;
450454
}

core/src/main/resources/jenkins/security/ApiTokenProperty/config.jelly

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ THE SOFTWARE.
164164
</div>
165165
<div>
166166
<j:choose>
167+
<j:when test="${token.expired}">
168+
<span class="error">
169+
${%This token has expired}
170+
</span>
171+
</j:when>
167172
<j:when test="${token.expirationDate == 'never'}">
168173
<span class="warning">
169174
${%This token does not expire}

0 commit comments

Comments
 (0)