Skip to content

Commit

Permalink
Fix OidcSession#expiresIn and add a new method
Browse files Browse the repository at this point in the history
  • Loading branch information
sberyozkin committed Aug 19, 2022
1 parent 4d511de commit b52ece4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
@@ -1,5 +1,6 @@
package io.quarkus.oidc;

import java.time.Duration;
import java.time.Instant;

import org.eclipse.microprofile.jwt.JsonWebToken;
Expand All @@ -18,10 +19,29 @@ public interface OidcSession {
/**
* Return an {@linkplain:Instant} indicating how long will it take for the current session to expire.
*
* @return
* @deprecated Use {@link #expiresAt()}.
*
* @return Instant
*/
@Deprecated
Instant expiresIn();

/**
* Return an {@linkplain Instant} representing the current session's expiration time
* which is a number of seconds from the epoch of 1970-01-01T0:0:0Z.
*
* @return Instant
*/
Instant expiresAt();

/**
* Return a {@linkplain Duration} indicating how long the current session will remain valid for
* starting from this method's invocation time.
*
* @return Duration
*/
Duration validFor();

/**
* Perform a local logout without a redirect to the OpenId Connect provider.
*
Expand Down
@@ -1,5 +1,6 @@
package io.quarkus.oidc.runtime;

import java.time.Duration;
import java.time.Instant;
import java.util.function.Function;

Expand Down Expand Up @@ -56,9 +57,19 @@ public Instant expiresIn() {
return Instant.ofEpochSecond(idToken.getExpirationTime() - nowSecs);
}

@Override
public Instant expiresAt() {
return Instant.ofEpochSecond(idToken.getExpirationTime());
}

@Override
public Duration validFor() {
final long nowSecs = System.currentTimeMillis() / 1000;
return Duration.ofSeconds(idToken.getExpirationTime() - nowSecs);
}

@Override
public JsonWebToken getIdToken() {
return idToken;
}

}
Expand Up @@ -27,7 +27,8 @@ public String getTenant() {
@Path("query")
@Authenticated
public String getTenantWithQuery(@QueryParam("code") String value) {
return getTenant() + "?code=" + value;
return getTenant() + "?code=" + value + "&expiresIn=" + session.expiresAt().getEpochSecond()
+ "&expiresInDuration=" + session.validFor().getSeconds();
}

@GET
Expand Down
Expand Up @@ -33,6 +33,7 @@
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import io.smallrye.jwt.util.KeyUtils;
import io.vertx.core.json.JsonObject;

/**
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
Expand Down Expand Up @@ -256,10 +257,18 @@ public void testCodeFlowForceHttpsRedirectUriWithQueryAndPkce() throws Exception
URI endpointLocationWithoutQueryUri = URI.create(endpointLocationWithoutQuery);
assertEquals("code=b", endpointLocationWithoutQueryUri.getRawQuery());

page = webClient.getPage(endpointLocationWithoutQueryUri.toURL());
assertEquals("tenant-https:reauthenticated?code=b", page.getBody().asText());
Cookie sessionCookie = getSessionCookie(webClient, "tenant-https_test");
assertNotNull(sessionCookie);
JsonObject idToken = OidcUtils.decodeJwtContent(sessionCookie.getValue().split("\\|")[0]);
String expiresIn = idToken.getInteger("exp").toString();
page = webClient.getPage(endpointLocationWithoutQueryUri.toURL());
String response = page.getBody().asText();
assertTrue(
response.startsWith("tenant-https:reauthenticated?code=b&expiresIn=" + expiresIn + "&expiresInDuration="));
Integer duration = Integer.valueOf(response.substring(response.length() - 1));
assertTrue(duration > 1 && duration < 5);
sessionCookie = getSessionCookie(webClient, "tenant-https_test");
assertNotNull(sessionCookie);
webClient.getCookieManager().clearCookies();
}
}
Expand Down

0 comments on commit b52ece4

Please sign in to comment.