ru.foodtechlab.lib.auth.service.domain.token.usecases.CreateAccessTokenUseCase Maven / Gradle / Ivy
package ru.foodtechlab.lib.auth.service.domain.token.usecases;
import com.rcore.commons.utils.DateTimeUtils;
import com.rcore.domain.commons.usecase.UseCase;
import com.rcore.domain.commons.usecase.model.SingletonEntityOutputValues;
import lombok.*;
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.AccessTokenEntity;
import ru.foodtechlab.lib.auth.service.domain.token.entity.RefreshTokenEntity;
import ru.foodtechlab.lib.auth.service.domain.token.port.AccessTokenIdGenerator;
import ru.foodtechlab.lib.auth.service.domain.token.port.AccessTokenRepository;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
@RequiredArgsConstructor
public class CreateAccessTokenUseCase extends UseCase> {
private final AccessTokenRepository accessTokenRepository;
private final AccessTokenIdGenerator> accessTokenIdGenerator;
private final TokenLifeCycleConfig tokenLifeCycleConfig;
@Override
public SingletonEntityOutputValues execute(InputValues inputValues) {
AccessTokenEntity accessTokenEntity = new AccessTokenEntity();
accessTokenEntity.setId(accessTokenIdGenerator.generate());
accessTokenEntity.setCredential(inputValues.getRefreshTokenEntity().getCredential());
accessTokenEntity.setTtl(tokenLifeCycleConfig.getAccessTokenLifetimeInSeconds());
accessTokenEntity.setExpireAt(Instant.now().plus(tokenLifeCycleConfig.getAccessTokenLifetimeInSeconds(), ChronoUnit.SECONDS));
accessTokenEntity.setCreateByRefreshToken(inputValues.getRefreshTokenEntity());
accessTokenEntity.setSign(AccessTokenEntity.sign(accessTokenEntity.getId(), accessTokenEntity.getExpireAt().toEpochMilli(), inputValues.getRefreshTokenEntity()));
accessTokenEntity.setAuthSessionId(inputValues.getAuthSessionId());
return SingletonEntityOutputValues.of(accessTokenRepository.save(accessTokenEntity));
}
@AllArgsConstructor(staticName = "of")
@Getter
public static class InputValues implements UseCase.InputValues {
private CredentialEntity credentialEntity;
private RefreshTokenEntity refreshTokenEntity;
private String authSessionId;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy