Skip to content

Commit

Permalink
Add checks on RAT and IDtoken tests
Browse files Browse the repository at this point in the history
Fixed default validity value for IDTokens
  • Loading branch information
enricovianello committed Dec 1, 2023
1 parent cbeedc5 commit f41bca7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@

import it.infn.mw.iam.api.common.error.NoSuchAccountError;
import it.infn.mw.iam.authn.util.Authorities;
import it.infn.mw.iam.config.client_registration.ClientRegistrationProperties;
import it.infn.mw.iam.persistence.model.IamAccount;
import it.infn.mw.iam.persistence.repository.IamAccountRepository;

Expand All @@ -83,13 +84,14 @@ public class IamOIDCTokenService implements OIDCTokenService {
private final ClientKeyCacheService encrypters;
private final SymmetricKeyJWTValidatorCacheService symmetricCacheService;
private final OAuth2TokenEntityService tokenService;
private final ClientRegistrationProperties clientProps;

public IamOIDCTokenService(Clock clock, JWTProfileResolver profileResolver,
IamAccountRepository accountRepository, JWTSigningAndValidationService jwtService,
AuthenticationHolderRepository authenticationHolderRepository,
ConfigurationPropertiesBean configBean, ClientKeyCacheService encrypters,
SymmetricKeyJWTValidatorCacheService symmetricCacheService,
OAuth2TokenEntityService tokenService) {
OAuth2TokenEntityService tokenService, ClientRegistrationProperties clientProps) {
this.clock = clock;
this.profileResolver = profileResolver;
this.accountRepository = accountRepository;
Expand All @@ -99,6 +101,7 @@ public IamOIDCTokenService(Clock clock, JWTProfileResolver profileResolver,
this.encrypters = encrypters;
this.symmetricCacheService = symmetricCacheService;
this.tokenService = tokenService;
this.clientProps = clientProps;
}


Expand Down Expand Up @@ -156,10 +159,13 @@ public JWT createIdToken(ClientDetailsEntity client, OAuth2Request request, Date
idClaims.issueTime(issueTime);
handleAuthTimestamp(client, request, idClaims);

if (client.getIdTokenValiditySeconds() != null) {
Date expiration =
new Date(System.currentTimeMillis() + (client.getIdTokenValiditySeconds() * 1000L));
idClaims.expirationTime(expiration);
if (client.getIdTokenValiditySeconds() != null && client.getIdTokenValiditySeconds() > 0) {
idClaims.expirationTime(
new Date(System.currentTimeMillis() + (client.getIdTokenValiditySeconds() * 1000L)));
} else {
idClaims.expirationTime(
new Date(System.currentTimeMillis()
+ (clientProps.getClientDefaults().getDefaultIdTokenValiditySeconds() * 1000L)));
}

String nonce = (String) request.getExtensions().get(NONCE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import static org.hamcrest.Matchers.not;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

import java.text.ParseException;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -32,6 +34,9 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

import com.nimbusds.jwt.JWT;
import com.nimbusds.jwt.JWTParser;

import io.restassured.RestAssured;
import it.infn.mw.iam.api.client.management.service.ClientManagementService;
import it.infn.mw.iam.api.common.client.RegisteredClientDTO;
Expand Down Expand Up @@ -69,7 +74,7 @@ public void setup() {
}

@Test
public void testRatWorkAsExpected() {
public void testRatWorkAsExpected() throws ParseException {

String clientJson = ClientJsonStringBuilder.builder().scopes("openid").build();

Expand All @@ -87,6 +92,8 @@ public void testRatWorkAsExpected() {
// @formatter:on

assertThat(registerResponse.getRegistrationAccessToken(), notNullValue());
JWT jwt = JWTParser.parse(registerResponse.getRegistrationAccessToken());
assertThat(jwt.getJWTClaimsSet().getExpirationTime(), nullValue());
assertThat(registerResponse.getScope(), not(empty()));

RegisteredClientDTO getResponse =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void testEnhancedProfileClaimsOk() throws Exception {
assertThat(token.getJWTClaimsSet().getClaim("preferred_username"), is(notNullValue()));
assertThat(token.getJWTClaimsSet().getClaim("organisation_name"), is(notNullValue()));
assertThat(token.getJWTClaimsSet().getClaim("groups"), is(notNullValue()));

assertThat(token.getJWTClaimsSet().getExpirationTime(), is(notNullValue()));
}

@Test
Expand Down

0 comments on commit f41bca7

Please sign in to comment.