Get rid of refresh token interface.
This commit is contained in:
parent
0ad45adac1
commit
7e95c3a867
@ -37,7 +37,7 @@ public class AuthServiceImpl implements AuthService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private RefreshToken createRefreshToken(User principal) {
|
private RefreshToken createRefreshToken(User principal) {
|
||||||
final RefreshTokenEntity refreshTokenDraft = new RefreshTokenEntity();
|
final RefreshToken refreshTokenDraft = new RefreshToken();
|
||||||
refreshTokenDraft.setToken(UUID.randomUUID());
|
refreshTokenDraft.setToken(UUID.randomUUID());
|
||||||
refreshTokenDraft.setIssued(OffsetDateTime.now());
|
refreshTokenDraft.setIssued(OffsetDateTime.now());
|
||||||
refreshTokenDraft.setExpiration(OffsetDateTime.now().plusSeconds(this.refreshTokenLifetime));
|
refreshTokenDraft.setExpiration(OffsetDateTime.now().plusSeconds(this.refreshTokenLifetime));
|
||||||
@ -75,15 +75,15 @@ public class AuthServiceImpl implements AuthService {
|
|||||||
throw new LoginException(LoginExceptionReason.NO_REFRESH_TOKEN, "No refresh token provided.");
|
throw new LoginException(LoginExceptionReason.NO_REFRESH_TOKEN, "No refresh token provided.");
|
||||||
}
|
}
|
||||||
|
|
||||||
final RefreshTokenEntity old = this.refreshTokenRepository.findByToken(refreshToken)
|
final RefreshToken old = this.refreshTokenRepository.findByToken(refreshToken)
|
||||||
.orElseThrow(() -> new LoginException(
|
.orElseThrow(() -> new LoginException(
|
||||||
LoginExceptionReason.INVALID_REFRESH_TOKEN,
|
LoginExceptionReason.INVALID_REFRESH_TOKEN,
|
||||||
"No such refresh token: " + refreshToken
|
"No such refresh token: " + refreshToken
|
||||||
));
|
));
|
||||||
if (old.isRevoked() || old.isDeleted()) {
|
if (old.getRevoked() || old.getDeleted()) {
|
||||||
throw new LoginException(LoginExceptionReason.INVALID_REFRESH_TOKEN, "Invalid refresh token.");
|
throw new LoginException(LoginExceptionReason.INVALID_REFRESH_TOKEN, "Invalid refresh token.");
|
||||||
}
|
}
|
||||||
if (old.getExpires().isBefore(OffsetDateTime.now())) {
|
if (old.getExpiration().isBefore(OffsetDateTime.now())) {
|
||||||
throw new LoginException(LoginExceptionReason.EXPIRED_REFRESH_TOKEN, "Refresh token is expired.");
|
throw new LoginException(LoginExceptionReason.EXPIRED_REFRESH_TOKEN, "Refresh token is expired.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,13 +1,40 @@
|
|||||||
package app.mealsmadeeasy.api.auth;
|
package app.mealsmadeeasy.api.auth;
|
||||||
|
|
||||||
|
import app.mealsmadeeasy.api.user.User;
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public interface RefreshToken {
|
@Entity
|
||||||
UUID getToken();
|
@Table(name = "refresh_token")
|
||||||
long getLifetime();
|
@Data
|
||||||
OffsetDateTime getExpires();
|
public class RefreshToken {
|
||||||
OffsetDateTime getIssued();
|
|
||||||
boolean isRevoked();
|
@Id
|
||||||
boolean isDeleted();
|
@Column(nullable = false)
|
||||||
|
private UUID token;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
private OffsetDateTime issued;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
private OffsetDateTime expiration;
|
||||||
|
|
||||||
|
@ManyToOne(optional = false)
|
||||||
|
@JoinColumn(name = "owner_id", nullable = false)
|
||||||
|
private User owner;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
private Boolean deleted = false;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
private Boolean revoked = false;
|
||||||
|
|
||||||
|
public long getLifetime() {
|
||||||
|
return ChronoUnit.SECONDS.between(this.issued, this.expiration);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,92 +0,0 @@
|
|||||||
package app.mealsmadeeasy.api.auth;
|
|
||||||
|
|
||||||
import app.mealsmadeeasy.api.user.User;
|
|
||||||
import jakarta.persistence.*;
|
|
||||||
|
|
||||||
import java.time.OffsetDateTime;
|
|
||||||
import java.time.temporal.ChronoUnit;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
@Entity(name = "RefreshToken")
|
|
||||||
@Table(name = "refresh_token")
|
|
||||||
public class RefreshTokenEntity implements RefreshToken {
|
|
||||||
|
|
||||||
@Id
|
|
||||||
@Column(nullable = false)
|
|
||||||
private UUID token;
|
|
||||||
|
|
||||||
@Column(nullable = false)
|
|
||||||
private OffsetDateTime issued;
|
|
||||||
|
|
||||||
@Column(nullable = false)
|
|
||||||
private OffsetDateTime expiration;
|
|
||||||
|
|
||||||
@ManyToOne(optional = false)
|
|
||||||
@JoinColumn(name = "owner_id", nullable = false)
|
|
||||||
private User owner;
|
|
||||||
|
|
||||||
@Column(nullable = false)
|
|
||||||
private Boolean deleted = false;
|
|
||||||
|
|
||||||
@Column(nullable = false)
|
|
||||||
private Boolean revoked = false;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public UUID getToken() {
|
|
||||||
return this.token;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setToken(UUID token) {
|
|
||||||
this.token = token;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public OffsetDateTime getIssued() {
|
|
||||||
return this.issued;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setIssued(OffsetDateTime issued) {
|
|
||||||
this.issued = issued;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public OffsetDateTime getExpires() {
|
|
||||||
return this.expiration;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setExpiration(OffsetDateTime expiration) {
|
|
||||||
this.expiration = expiration;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isRevoked() {
|
|
||||||
return this.revoked;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setRevoked(boolean revoked) {
|
|
||||||
this.revoked = revoked;
|
|
||||||
}
|
|
||||||
|
|
||||||
public User getOwner() {
|
|
||||||
return this.owner;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setOwner(User owner) {
|
|
||||||
this.owner = owner;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isDeleted() {
|
|
||||||
return this.deleted;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDeleted(boolean deleted) {
|
|
||||||
this.deleted = deleted;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public long getLifetime() {
|
|
||||||
return ChronoUnit.SECONDS.between(this.issued, this.expiration);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -8,9 +8,9 @@ import org.springframework.data.jpa.repository.Query;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public interface RefreshTokenRepository extends JpaRepository<RefreshTokenEntity, Long> {
|
public interface RefreshTokenRepository extends JpaRepository<RefreshToken, Long> {
|
||||||
|
|
||||||
Optional<RefreshTokenEntity> findByToken(UUID token);
|
Optional<RefreshToken> findByToken(UUID token);
|
||||||
|
|
||||||
@Modifying
|
@Modifying
|
||||||
@Transactional
|
@Transactional
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user