Add expiry to SecurityContext
@@ -16,15 +16,21 @@ public class SecurityContext {
private final Principal principal;
private final Set<Role> roles;
private final Instant issuedAt;
+ private final Instant expiresAt;
public SecurityContext(Principal principal, Set<Role> roles, Instant issuedAt) {
+ this(principal, roles, issuedAt, Instant.MAX);
+ }
+
+ public SecurityContext(Principal principal, Set<Role> roles, Instant issuedAt, Instant expiresAt) {
this.principal = Objects.requireNonNull(principal);
this.roles = Set.copyOf(roles);
this.issuedAt = Objects.requireNonNull(issuedAt);
+ this.expiresAt = Objects.requireNonNull(expiresAt);
}
public SecurityContext(Principal principal, Set<Role> roles) {
- this(principal, roles, Instant.EPOCH);
+ this(principal, roles, Instant.EPOCH, Instant.MAX);
}
public Principal principal() {
@@ -39,19 +45,21 @@ public class SecurityContext {
return issuedAt;
}
+ /** @return credential expiration or {@link Instant#MAX} is not available */
+ public Instant expiresAt() { return expiresAt; }
+
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SecurityContext that = (SecurityContext) o;
- return Objects.equals(principal, that.principal) &&
- Objects.equals(roles, that.roles) &&
- Objects.equals(issuedAt, that.issuedAt);
+ return Objects.equals(principal, that.principal) && Objects.equals(roles, that.roles)
+ && Objects.equals(issuedAt, that.issuedAt) && Objects.equals(expiresAt, that.expiresAt);
}
@Override
public int hashCode() {
- return Objects.hash(principal, roles, issuedAt);
+ return Objects.hash(principal, roles, issuedAt, expiresAt);
}
@Override
@@ -60,6 +68,7 @@ public class SecurityContext {
"principal=" + principal +
", roles=" + roles +
", issuedAt=" + issuedAt +
+ ", expiresAt=" + expiresAt +
'}';
}
