83 lines
2.9 KiB
Java
83 lines
2.9 KiB
Java
package ru.mtusi.messenger.service;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import ru.mtusi.messenger.config.properties.SessionProperties;
|
|
import ru.mtusi.messenger.dto.SessionDTO;
|
|
import ru.mtusi.messenger.dto.UserDTO;
|
|
import ru.mtusi.messenger.exception.UnauthorizedException;
|
|
import ru.mtusi.messenger.mapper.SessionMapper;
|
|
import ru.mtusi.messenger.model.Session;
|
|
import ru.mtusi.messenger.repository.SessionRepository;
|
|
import ru.mtusi.messenger.util.TokenGenerator;
|
|
import ru.mtusi.messenger.util.TokenHasher;
|
|
|
|
import java.time.Instant;
|
|
import java.time.temporal.ChronoUnit;
|
|
import java.util.UUID;
|
|
|
|
@Slf4j
|
|
@RequiredArgsConstructor
|
|
@Service
|
|
public class SessionService {
|
|
private final TokenGenerator tokenGenerator;
|
|
private final TokenHasher tokenHasher;
|
|
private final SessionProperties sessionProperties;
|
|
private final SessionRepository sessionRepository;
|
|
private final SessionMapper sessionMapper;
|
|
private final SessionCacheService sessionCacheService;
|
|
|
|
@Transactional
|
|
public SessionDTO createSession(UserDTO userDTO) {
|
|
String token = tokenGenerator.generate();
|
|
Session session = sessionRepository.save(buildSession(userDTO, token));
|
|
return sessionMapper.toSessionDTO(session, token, userDTO);
|
|
}
|
|
|
|
public SessionRecord requireActive(String token) {
|
|
sessionCacheService.dropCacheIfStale();
|
|
String hex = tokenHasher.hash(token);
|
|
SessionRecord record = sessionCacheService.lookupByHash(hex);
|
|
requireNotExpired(record, hex);
|
|
return record;
|
|
}
|
|
|
|
private void requireNotExpired(SessionRecord record, String hex) {
|
|
if (record == null || isExpired(record)) {
|
|
sessionCacheService.evict(hex);
|
|
throw new UnauthorizedException("Invalid or expired session");
|
|
}
|
|
}
|
|
|
|
private boolean isExpired(SessionRecord record) {
|
|
return record.expiresAt().isBefore(Instant.now());
|
|
}
|
|
|
|
@Transactional
|
|
public void revoke(UUID sessionId) {
|
|
sessionRepository.findById(sessionId).ifPresent(this::markRevokedAndEvict);
|
|
}
|
|
|
|
private void markRevokedAndEvict(Session session) {
|
|
if (session.getRevokedAt() == null) {
|
|
session.setRevokedAt(Instant.now().truncatedTo(ChronoUnit.MILLIS));
|
|
}
|
|
sessionCacheService.evict(tokenHasher.hash(session.getTokenHash()));
|
|
}
|
|
|
|
private Session buildSession(UserDTO userDTO, String token) {
|
|
Session session = new Session();
|
|
session.setUserId(userDTO.getId());
|
|
session.setTokenHash(tokenHasher.sha256(token));
|
|
session.setExpiresAt(
|
|
Instant.now().plus(sessionProperties.ttl()).truncatedTo(ChronoUnit.MILLIS));
|
|
return session;
|
|
}
|
|
|
|
public record SessionRecord(UUID sessionId, UUID userId, Instant expiresAt) {}
|
|
}
|