for 3
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
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.SessionProperties;
|
||||
import ru.mtusi.messenger.dto.SessionDTO;
|
||||
import ru.mtusi.messenger.dto.UserDTO;
|
||||
import ru.mtusi.messenger.exception.UnauthorizedException;
|
||||
import ru.mtusi.messenger.model.Session;
|
||||
import ru.mtusi.messenger.repository.SessionRepository;
|
||||
import ru.mtusi.messenger.util.TokenGenerator;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.Instant;
|
||||
import java.util.UUID;
|
||||
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
public class SessionService {
|
||||
private final TokenGenerator tokenGenerator;
|
||||
private final SessionProperties sessionProperties;
|
||||
private final SessionRepository sessionRepository;
|
||||
|
||||
@Transactional
|
||||
public SessionDTO createSession(UserDTO userDTO) {
|
||||
String token = tokenGenerator.generate();
|
||||
Session session = sessionRepository.save(buildSession(userDTO,token));
|
||||
return toSessionDTO(session, token, userDTO);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public SessionRecord requireActive(String token) {
|
||||
Session s = sessionRepository.findByTokenHash(sha256(token))
|
||||
.filter(x -> x.getRevokedAt() == null)
|
||||
.filter(x -> x.getExpiresAt().isAfter(Instant.now()))
|
||||
.orElseThrow(() -> new UnauthorizedException("Invalid or expired session"));
|
||||
return new SessionRecord(s.getId(), s.getUserId());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void revoke(UUID sessionId) {
|
||||
sessionRepository.findById(sessionId).ifPresent(s -> {
|
||||
if (s.getRevokedAt() == null) {
|
||||
s.setRevokedAt(Instant.now());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static byte[] sha256(String token) {
|
||||
try {
|
||||
return MessageDigest.getInstance("SHA-256")
|
||||
.digest(token.getBytes(StandardCharsets.UTF_8));
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new IllegalStateException("SHA-256 not available", e);
|
||||
}
|
||||
}
|
||||
|
||||
private Session buildSession(UserDTO userDTO, String token) {
|
||||
Session session = new Session();
|
||||
session.setUserId(userDTO.getId());
|
||||
session.setTokenHash(sha256(token));
|
||||
session.setExpiresAt(Instant.now().plus(sessionProperties.ttl()));
|
||||
return session;
|
||||
}
|
||||
|
||||
private SessionDTO toSessionDTO(Session session, String tokenRaw, UserDTO userDTO) {
|
||||
return new SessionDTO()
|
||||
.token(tokenRaw)
|
||||
.tokenType(SessionDTO.TokenTypeEnum.BEARER)
|
||||
.expiresAt(session.getExpiresAt())
|
||||
.user(userDTO);
|
||||
}
|
||||
|
||||
public record SessionRecord(UUID sessionId, UUID userId) {}
|
||||
}
|
||||
Reference in New Issue
Block a user