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

ru.foodtechlab.lib.auth.service.domain.token.usecases.CreateTokenPairUseCase Maven / Gradle / Ivy

There is a newer version: 4.1.0
Show newest version
package ru.foodtechlab.lib.auth.service.domain.token.usecases;

import com.rcore.domain.commons.usecase.UseCase;
import com.rcore.domain.security.model.AccessTokenData;
import com.rcore.domain.security.model.RefreshTokenData;
import com.rcore.domain.security.port.TokenGenerator;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import ru.foodtechlab.lib.auth.service.domain.auth.entity.AuthSessionEntity;
import ru.foodtechlab.lib.auth.service.domain.auth.port.AuthorizationSessionRepository;
import ru.foodtechlab.lib.auth.service.domain.credential.entity.CredentialEntity;
import ru.foodtechlab.lib.auth.service.domain.token.entity.TokenPair;

import java.util.Optional;

@RequiredArgsConstructor
public class CreateTokenPairUseCase extends UseCase {

    private final CreateRefreshTokenUseCase createRefreshToken;
    private final CreateAccessTokenUseCase createAccessToken;
    private final TokenGenerator accessTokenDataTokenGenerator;
    private final TokenGenerator refreshTokenDataTokenGenerator;
    private final AuthorizationSessionRepository authorizationSessionRepository;

    @Override
    public OutputValues execute(InputValues inputValues) {
        var credential = inputValues.getCredential();
        var refreshToken = createRefreshToken.execute(CreateRefreshTokenUseCase.InputValues.of(credential, inputValues.getAuthSessionId())).getEntity();
        var accessToken = createAccessToken.execute(CreateAccessTokenUseCase.InputValues.of(credential, refreshToken, inputValues.getAuthSessionId())).getEntity();

        return OutputValues.of(TokenPair.builder()
                .accessToken(accessTokenDataTokenGenerator.generate(accessToken.toAccessTokenData()))
                .refreshToken(refreshTokenDataTokenGenerator.generate(refreshToken.toRefreshTokenData()))
                .build());
    }

    @AllArgsConstructor(staticName = "of")
    @Getter
    public static class InputValues implements UseCase.InputValues {
        String authSessionId;
        CredentialEntity credential;

        public static InputValues of(AuthSessionEntity authSession) {
            return InputValues.of(authSession.getId(), authSession.getCredential());
        }
    }

    @Value(staticConstructor = "of")
    @Getter
    public static class OutputValues implements UseCase.OutputValues {
        TokenPair tokenPair;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy