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

io.convergence_platform.services.security.JwtHelper Maven / Gradle / Ivy

Go to download

Holds the common functionality needed by all Convergence Platform-based services written in Java.

The newest version!
package io.convergence_platform.services.security;

import io.convergence_platform.common.Constants;
import io.convergence_platform.services.SecurityHelper;
import io.convergence_platform.services.constants.IServiceInfo;
import com.auth0.jwt.JWT;

import java.time.Instant;
import java.util.List;

public class JwtHelper {
    private final static long INTER_SERVICE_ACCESS_SESSION_DURATION = 60; // 1 minute
    public static String createInterServiceJWT(IServiceInfo serviceInfo, String authority, String secret) {
        return JWT.create()
                .withSubject(serviceInfo.getServiceName())
                .withExpiresAt(Instant.now().plusSeconds(INTER_SERVICE_ACCESS_SESSION_DURATION))
                .withIssuer(serviceInfo.getServiceName())
                .withClaim(Constants.JWT_AUTHORITY_CLAIM_FIELD, List.of(authority))
                .withClaim("is_inter_service_call", true)
                .sign(SecurityHelper.getJwtAlgorithm(secret));
    }

    public static String createInterServiceJWT(IServiceInfo serviceInfo, String secret) {
        return JWT.create()
                .withSubject(serviceInfo.getServiceName())
                .withExpiresAt(Instant.now().plusSeconds(INTER_SERVICE_ACCESS_SESSION_DURATION))
                .withIssuer(serviceInfo.getServiceName())
                .withClaim(Constants.JWT_AUTHORITY_CLAIM_FIELD, List.of())
                .withClaim("is_inter_service_call", true)
                .sign(SecurityHelper.getJwtAlgorithm(secret));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy