Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/main/java/com/somemore/auth/jwt/domain/EncodedToken.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
package com.somemore.auth.jwt.domain;

public record EncodedToken(String value) {

public boolean isUninitialized() {
return value == null
|| value.isEmpty()
|| value.equals("UNINITIALIZED");
}

public EncodedToken removePrefix(String prefix) {
if (this.value.startsWith(prefix)) {
return new EncodedToken(this.value.substring(prefix.length()));
}
return this;
}
}
47 changes: 38 additions & 9 deletions src/main/java/com/somemore/auth/jwt/filter/JwtAuthFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import com.somemore.auth.authentication.JwtAuthenticationToken;
import com.somemore.auth.jwt.domain.EncodedToken;
import com.somemore.auth.jwt.domain.TokenType;
import com.somemore.auth.jwt.exception.JwtErrorType;
import com.somemore.auth.jwt.exception.JwtException;
import com.somemore.auth.jwt.usecase.JwtUseCase;
import io.jsonwebtoken.Claims;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
Expand All @@ -19,7 +21,9 @@
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@RequiredArgsConstructor
@Slf4j
Expand All @@ -30,11 +34,11 @@ public class JwtAuthFilter extends OncePerRequestFilter {

@Override
protected boolean shouldNotFilter(HttpServletRequest request) {
String token = request.getHeader("Authorization");
EncodedToken accessToken = getAccessToken(request);
String path = request.getRequestURI();

return token == null
|| token.isEmpty()
return accessToken == null
|| accessToken.isUninitialized()
|| path.equals("/api/center/sign-in");
}

Expand All @@ -54,17 +58,42 @@ protected void doFilterInternal(HttpServletRequest request,
}

private EncodedToken getAccessToken(HttpServletRequest request) {
String accessToken = request.getHeader("Authorization");
if (accessToken == null || accessToken.isEmpty()) {
EncodedToken accessToken = findAccessTokenFromCookie(request);

if (accessToken.isUninitialized()) {
accessToken = findAccessTokenFromHeader(request);
}

if (accessToken.isUninitialized()) {
throw new JwtException(JwtErrorType.MISSING_TOKEN);
}

String tokenPrefix = "Bearer ";
if (accessToken.startsWith(tokenPrefix)) {
return new EncodedToken(accessToken.substring(tokenPrefix.length()));
String prefix = "Bearer ";
return accessToken.removePrefix(prefix);
}

private static EncodedToken findAccessTokenFromHeader(HttpServletRequest request) {
String authorizationHeader = request.getHeader("Authorization");
if (authorizationHeader == null || authorizationHeader.isEmpty()) {
return new EncodedToken("UNINITIALIZED");
}

return new EncodedToken(authorizationHeader);
}

private EncodedToken findAccessTokenFromCookie(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
if (cookies == null) {
return new EncodedToken("UNINITIALIZED");
}

return new EncodedToken(accessToken);
return Arrays.stream(cookies)
.filter(Objects::nonNull)
.filter(cookie -> cookie.getName().equals(TokenType.ACCESS.name()))
.map(Cookie::getValue)
.findFirst()
.map(EncodedToken::new)
.orElse(new EncodedToken("UNINITIALIZED"));
}

private JwtAuthenticationToken createAuthenticationToken(Claims claims,
Expand Down
Loading