Working on refresh auth flow.
This commit is contained in:
parent
6a104f7f1a
commit
e2765c4a96
@ -14,7 +14,9 @@ public class MvcConfiguration {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addCorsMappings(CorsRegistry registry) {
|
public void addCorsMappings(CorsRegistry registry) {
|
||||||
registry.addMapping("/**").allowedOrigins("http://localhost:5173");
|
registry.addMapping("/**")
|
||||||
|
.allowedOrigins("http://localhost:5173")
|
||||||
|
.allowCredentials(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -15,6 +15,7 @@ public final class AuthController {
|
|||||||
final ResponseCookie.ResponseCookieBuilder b = ResponseCookie.from("refresh-token")
|
final ResponseCookie.ResponseCookieBuilder b = ResponseCookie.from("refresh-token")
|
||||||
.httpOnly(true)
|
.httpOnly(true)
|
||||||
.secure(true)
|
.secure(true)
|
||||||
|
.sameSite("Lax")
|
||||||
.maxAge(maxAge);
|
.maxAge(maxAge);
|
||||||
if (token != null) {
|
if (token != null) {
|
||||||
b.value(token);
|
b.value(token);
|
||||||
@ -35,7 +36,9 @@ public final class AuthController {
|
|||||||
refreshToken.getLifetime()
|
refreshToken.getLifetime()
|
||||||
);
|
);
|
||||||
final var loginView = new LoginView(
|
final var loginView = new LoginView(
|
||||||
loginDetails.getUsername(), loginDetails.getAccessToken().getToken()
|
loginDetails.getUsername(),
|
||||||
|
loginDetails.getAccessToken().getToken(),
|
||||||
|
loginDetails.getAccessToken().getExpires()
|
||||||
);
|
);
|
||||||
return ResponseEntity.ok()
|
return ResponseEntity.ok()
|
||||||
.header(HttpHeaders.SET_COOKIE, refreshCookie.toString())
|
.header(HttpHeaders.SET_COOKIE, refreshCookie.toString())
|
||||||
|
@ -73,7 +73,7 @@ public final class AuthServiceImpl implements AuthService {
|
|||||||
if (old.isRevoked()) {
|
if (old.isRevoked()) {
|
||||||
throw new LoginException("RefreshToken is revoked.");
|
throw new LoginException("RefreshToken is revoked.");
|
||||||
}
|
}
|
||||||
if (old.getExpiration().isBefore(LocalDateTime.now())) {
|
if (old.getExpires().isBefore(LocalDateTime.now())) {
|
||||||
throw new LoginException("RefreshToken is expired.");
|
throw new LoginException("RefreshToken is expired.");
|
||||||
}
|
}
|
||||||
final UserEntity principal = old.getOwner();
|
final UserEntity principal = old.getOwner();
|
||||||
|
@ -1,13 +1,17 @@
|
|||||||
package app.mealsmadeeasy.api.auth;
|
package app.mealsmadeeasy.api.auth;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
public final class LoginView {
|
public final class LoginView {
|
||||||
|
|
||||||
private final String username;
|
private final String username;
|
||||||
private final String accessToken;
|
private final String accessToken;
|
||||||
|
private final LocalDateTime expires;
|
||||||
|
|
||||||
public LoginView(String username, String accessToken) {
|
public LoginView(String username, String accessToken, LocalDateTime expires) {
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.accessToken = accessToken;
|
this.accessToken = accessToken;
|
||||||
|
this.expires = expires;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUsername() {
|
public String getUsername() {
|
||||||
@ -18,4 +22,8 @@ public final class LoginView {
|
|||||||
return this.accessToken;
|
return this.accessToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public LocalDateTime getExpires() {
|
||||||
|
return this.expires;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,5 @@ import java.time.LocalDateTime;
|
|||||||
|
|
||||||
public interface RefreshToken extends AuthToken {
|
public interface RefreshToken extends AuthToken {
|
||||||
LocalDateTime getIssued();
|
LocalDateTime getIssued();
|
||||||
LocalDateTime getExpiration();
|
|
||||||
boolean isRevoked();
|
boolean isRevoked();
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ public class RefreshTokenEntity implements RefreshToken {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LocalDateTime getExpiration() {
|
public LocalDateTime getExpires() {
|
||||||
return this.expiration;
|
return this.expiration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,8 @@ import org.springframework.stereotype.Service;
|
|||||||
|
|
||||||
import javax.crypto.SecretKey;
|
import javax.crypto.SecretKey;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.ZoneId;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -33,14 +35,19 @@ public final class JwtServiceImpl implements JwtService {
|
|||||||
@Override
|
@Override
|
||||||
public AuthToken generateAccessToken(String username) {
|
public AuthToken generateAccessToken(String username) {
|
||||||
final Instant now = Instant.now();
|
final Instant now = Instant.now();
|
||||||
|
final Instant expires = Instant.from(now.plusSeconds(accessTokenLifetime));
|
||||||
final String token = Jwts.builder()
|
final String token = Jwts.builder()
|
||||||
.subject(username)
|
.subject(username)
|
||||||
.issuedAt(Date.from(now))
|
.issuedAt(Date.from(now))
|
||||||
.expiration(Date.from(now.plusSeconds(this.accessTokenLifetime)))
|
.expiration(Date.from(expires))
|
||||||
.signWith(this.secretKey)
|
.signWith(this.secretKey)
|
||||||
.json(this.serializer)
|
.json(this.serializer)
|
||||||
.compact();
|
.compact();
|
||||||
return new SimpleAuthToken(token, this.accessTokenLifetime);
|
return new SimpleAuthToken(
|
||||||
|
token,
|
||||||
|
this.accessTokenLifetime,
|
||||||
|
LocalDateTime.ofInstant(expires, ZoneId.systemDefault())
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package app.mealsmadeeasy.api.security;
|
package app.mealsmadeeasy.api.security;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
public interface AuthToken {
|
public interface AuthToken {
|
||||||
String getToken();
|
String getToken();
|
||||||
long getLifetime();
|
long getLifetime();
|
||||||
|
LocalDateTime getExpires();
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,17 @@
|
|||||||
package app.mealsmadeeasy.api.security;
|
package app.mealsmadeeasy.api.security;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
public final class SimpleAuthToken implements AuthToken {
|
public final class SimpleAuthToken implements AuthToken {
|
||||||
|
|
||||||
private final String token;
|
private final String token;
|
||||||
private final long lifetime;
|
private final long lifetime;
|
||||||
|
private LocalDateTime expires;
|
||||||
|
|
||||||
public SimpleAuthToken(String token, long lifetime) {
|
public SimpleAuthToken(String token, long lifetime, LocalDateTime expires) {
|
||||||
this.token = token;
|
this.token = token;
|
||||||
this.lifetime = lifetime;
|
this.lifetime = lifetime;
|
||||||
|
this.expires = expires;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -20,4 +24,9 @@ public final class SimpleAuthToken implements AuthToken {
|
|||||||
return this.lifetime;
|
return this.lifetime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public LocalDateTime getExpires() {
|
||||||
|
return this.expires;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ spring.datasource.username=meals-made-easy-api-user
|
|||||||
spring.datasource.password=devpass
|
spring.datasource.password=devpass
|
||||||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||||
app.mealsmadeeasy.api.baseUrl=http://localhost:8080
|
app.mealsmadeeasy.api.baseUrl=http://localhost:8080
|
||||||
app.mealsmadeeasy.api.security.access-token-lifetime=60
|
app.mealsmadeeasy.api.security.access-token-lifetime=10
|
||||||
app.mealsmadeeasy.api.security.refresh-token-lifetime=120
|
app.mealsmadeeasy.api.security.refresh-token-lifetime=120
|
||||||
app.mealsmadeeasy.api.minio.endpoint=http://localhost:9000
|
app.mealsmadeeasy.api.minio.endpoint=http://localhost:9000
|
||||||
app.mealsmadeeasy.api.minio.accessKey=minio-root
|
app.mealsmadeeasy.api.minio.accessKey=minio-root
|
||||||
|
Loading…
Reference in New Issue
Block a user