Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow id token lifetime to be configurable #24

Merged
merged 5 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -181,11 +181,12 @@ RSAPublicKey publicKey() {
)));

protected final @NonNull String token() {
IdTokenConfiguration cfg = IdTokenConfiguration.get();
JwtBuilder builder = Jwts.builder().
setHeaderParam("kid", getId()).
setIssuer(issuer != null ? issuer : findIssuer().url()).
setAudience(audience).
setExpiration(Date.from(new Date().toInstant().plus(1, ChronoUnit.HOURS))).
setExpiration(Date.from(new Date().toInstant().plus(cfg.getTokenLifetime(), ChronoUnit.SECONDS))).
iwarapter marked this conversation as resolved.
Show resolved Hide resolved
iwarapter marked this conversation as resolved.
Show resolved Hide resolved
setIssuedAt(new Date());
Map<String, String> env;
if (build != null) {
Expand All @@ -198,7 +199,6 @@ RSAPublicKey publicKey() {
// EnvVars.masterEnvVars might not be safe to expose
env = Collections.singletonMap("JENKINS_URL", Jenkins.get().getRootUrl());
}
IdTokenConfiguration cfg = IdTokenConfiguration.get();
AtomicBoolean definedSub = new AtomicBoolean();
Consumer<List<ClaimTemplate>> addClaims = claimTemplates -> {
for (ClaimTemplate t : claimTemplates) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
return ExtensionList.lookupSingleton(IdTokenConfiguration.class);
}

private Integer tokenLifetime = 3600;
iwarapter marked this conversation as resolved.
Show resolved Hide resolved

private @CheckForNull List<ClaimTemplate> claimTemplates;
private @CheckForNull List<ClaimTemplate> buildClaimTemplates;
private @CheckForNull List<ClaimTemplate> globalClaimTemplates;
Expand All @@ -80,6 +82,14 @@ public IdTokenConfiguration() {
}
}

public Integer getTokenLifetime() {
iwarapter marked this conversation as resolved.
Show resolved Hide resolved
return tokenLifetime;
}

@DataBoundSetter public void setTokenLifetime(final Integer lifetime) {
iwarapter marked this conversation as resolved.
Show resolved Hide resolved
this.tokenLifetime = lifetime;
}

public @NonNull List<ClaimTemplate> getClaimTemplates() {
return claimTemplates != null ? claimTemplates : DEFAULT_CLAIM_TEMPLATES;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ THE SOFTWARE.
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:section title="OpenID Connect">
<f:entry field="tokenLifetime" title="${%Token Lifetime}">
<f:number title="Token Lifetime"/>
iwarapter marked this conversation as resolved.
Show resolved Hide resolved
</f:entry>

<f:advanced title="${%Claim templates}" align="left">
<f:entry field="claimTemplates" title="${%General claim templates}">
<f:repeatableProperty field="claimTemplates">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
The time in seconds the issued id_token is valid for.
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class ConfigurationAsCodeTest {
@ConfiguredWithCode("global.yaml")
@Test public void globalConfiguration() throws Exception {
IdTokenConfiguration cfg = IdTokenConfiguration.get();
assertEquals(Integer.valueOf(60), cfg.getTokenLifetime());
iwarapter marked this conversation as resolved.
Show resolved Hide resolved
assertEquals(ClaimTemplate.xmlForm(Collections.singletonList(new ClaimTemplate("ok", "true", new BooleanClaimType()))),
ClaimTemplate.xmlForm(cfg.getClaimTemplates()));
assertEquals(ClaimTemplate.xmlForm(Collections.singletonList(new ClaimTemplate("sub", "jenkins", new StringClaimType()))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import java.math.BigInteger;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
iwarapter marked this conversation as resolved.
Show resolved Hide resolved
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import jenkins.model.Jenkins;
Expand Down Expand Up @@ -132,6 +135,27 @@ public class IdTokenCredentialsTest {
});
}

@Test public void tokenLifetime() throws Throwable {
rr.then(r -> {
IdTokenStringCredentials c = new IdTokenStringCredentials(CredentialsScope.GLOBAL, "test", null);
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), c);
IdTokenConfiguration cfg = IdTokenConfiguration.get();
cfg.setTokenLifetime(60);
cfg.setClaimTemplates(Collections.singletonList(new ClaimTemplate("ok", "true", new BooleanClaimType())));
cfg.setGlobalClaimTemplates(Collections.singletonList(new ClaimTemplate("sub", "jenkins", new StringClaimType())));
cfg.setBuildClaimTemplates(Arrays.asList(new ClaimTemplate("sub", "${JOB_NAME}", new StringClaimType()), new ClaimTemplate("num", "${BUILD_NUMBER}", new IntegerClaimType())));
iwarapter marked this conversation as resolved.
Show resolved Hide resolved
String idToken = c.getSecret().getPlainText();
System.out.println(idToken);
Claims claims = Jwts.parserBuilder().
setSigningKey(c.publicKey()).
build().
parseClaimsJws(idToken).
getBody();

assertTrue(new Date().toInstant().plus(61, ChronoUnit.SECONDS).isAfter(claims.getExpiration().toInstant()));
iwarapter marked this conversation as resolved.
Show resolved Hide resolved
});
}

@Test public void customClaims() throws Throwable {
rr.then(r -> {
IdTokenStringCredentials c = new IdTokenStringCredentials(CredentialsScope.GLOBAL, "test", null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
security:
idToken:
tokenLifetime: 60
claimTemplates:
- name: ok
format: "true"
Expand Down