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

ru.foodtechlab.lib.auth.service.domain.token.usecases.CreateRefreshTokenUseCase 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.commons.usecase.model.SingletonEntityOutputValues;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import ru.foodtechlab.lib.auth.service.domain.credential.entity.CredentialEntity;
import ru.foodtechlab.lib.auth.service.domain.token.config.TokenLifeCycleConfig;
import ru.foodtechlab.lib.auth.service.domain.token.entity.RefreshTokenEntity;
import ru.foodtechlab.lib.auth.service.domain.token.port.RefreshTokenIdGenerator;
import ru.foodtechlab.lib.auth.service.domain.token.port.RefreshTokenRepository;
import ru.foodtechlab.lib.auth.service.domain.token.port.TokenSaltGenerator;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

@RequiredArgsConstructor
public class CreateRefreshTokenUseCase extends UseCase> {

    private final RefreshTokenRepository refreshTokenRepository;
    private final RefreshTokenIdGenerator refreshTokenIdGenerator;
    private final TokenSaltGenerator tokenSaltGenerator;
    private final TokenLifeCycleConfig tokenLifeCycleConfig;

    @Override
    public SingletonEntityOutputValues execute(InputValues inputValues) {
        var refreshToken = create(inputValues.getCredentialEntity(), RefreshTokenEntity.CreateFrom.LOGIN, null, inputValues.getAuthSessionId());
        refreshTokenRepository.save(refreshToken);
        return SingletonEntityOutputValues.of(refreshToken);
    }

    @Value(staticConstructor = "of")
    public static class InputValues implements UseCase.InputValues {
        CredentialEntity credentialEntity;
        String authSessionId;
    }

    private RefreshTokenEntity create(CredentialEntity credential, RefreshTokenEntity.CreateFrom createFrom, RefreshTokenEntity refreshToken, String authSessionId){
        RefreshTokenEntity refreshTokenEntity = new RefreshTokenEntity();

        refreshTokenEntity.setId(this.refreshTokenIdGenerator.generate());
        refreshTokenEntity.setCredential(credential);
        refreshTokenEntity.setTtl(tokenLifeCycleConfig.getRefreshTokenLifetimeInSeconds());
        refreshTokenEntity.setExpireAt(Instant.now().plus(tokenLifeCycleConfig.getRefreshTokenLifetimeInSeconds(), ChronoUnit.SECONDS));
        refreshTokenEntity.setStatus(RefreshTokenEntity.Status.ACTIVE);

        refreshTokenEntity.setCreateFromToken(refreshToken);
        refreshTokenEntity.setCreateFromType(createFrom);

        refreshTokenEntity.setSalt(tokenSaltGenerator.generate());
        refreshTokenEntity.setAuthSessionId(authSessionId);
        return refreshTokenEntity;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy