All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkus.oidc.runtime.OidcSessionImpl Maven / Gradle / Ivy

Go to download

Secure your applications with OpenID Connect Adapter and IDP such as Keycloak

There is a newer version: 3.17.5
Show newest version
package io.quarkus.oidc.runtime;

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

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import org.eclipse.microprofile.jwt.JsonWebToken;

import io.quarkus.oidc.IdToken;
import io.quarkus.oidc.OidcSession;
import io.quarkus.oidc.OidcTenantConfig;
import io.smallrye.mutiny.Uni;
import io.vertx.ext.web.RoutingContext;

@ApplicationScoped
public class OidcSessionImpl implements OidcSession {

    @Inject
    RoutingContext routingContext;

    @Inject
    DefaultTenantConfigResolver resolver;

    @Inject
    @IdToken
    JsonWebToken idToken;

    @Override
    public String getTenantId() {
        return routingContext.get(OidcUtils.TENANT_ID_ATTRIBUTE);
    }

    @Override
    public Uni logout() {
        String sessionCookieName = routingContext.get(OidcUtils.SESSION_COOKIE_NAME);
        if (sessionCookieName != null) {
            Uni oidcConfigUni = resolver.resolveConfig(routingContext);
            return oidcConfigUni.onItem().transformToUni(new Function>() {

                @Override
                public Uni apply(OidcTenantConfig oidcConfig) {
                    return OidcUtils.removeSessionCookie(routingContext, oidcConfig, sessionCookieName,
                            resolver.getTokenStateManager());
                }

            });
        }
        return Uni.createFrom().voidItem();
    }

    @Override
    public Instant expiresIn() {
        final long nowSecs = System.currentTimeMillis() / 1000;
        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;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy