This repository has been archived by the owner on Dec 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
272a3db
commit ffc5c32
Showing
6 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/auth/AdminAuthorizationAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package br.com.fiap.grupo30.fastfood.infrastructure.auth; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.util.Base64; | ||
import java.util.List; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Before; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
|
||
@Aspect | ||
@Component | ||
public class AdminAuthorizationAspect { | ||
|
||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
private static String ADMIN_ROLE = "admin-group"; | ||
private static String BEARER_TYPE = "Bearer"; | ||
private static Integer JWT_PARTS = 3; | ||
|
||
@Before("@annotation(AdminRequired)") | ||
public void checkAdminRole() throws Exception { | ||
ServletRequestAttributes attrs = | ||
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); | ||
HttpServletRequest request = attrs.getRequest(); | ||
HttpServletResponse response = attrs.getResponse(); | ||
|
||
String authorizationHeader = request.getHeader("Authorization"); | ||
|
||
if (authorizationHeader == null || !authorizationHeader.startsWith(BEARER_TYPE)) { | ||
response.sendError( | ||
HttpServletResponse.SC_UNAUTHORIZED, "Missing or invalid Authorization header"); | ||
return; | ||
} | ||
|
||
String jwtToken = authorizationHeader.substring(7); | ||
|
||
String[] tokenParts = jwtToken.split("\\."); | ||
|
||
if (tokenParts.length != JWT_PARTS) { | ||
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid JWT token structure"); | ||
return; | ||
} | ||
|
||
String payload = new String(Base64.getDecoder().decode(tokenParts[1])); | ||
JsonNode jsonNode = objectMapper.readTree(payload); | ||
|
||
JsonNode groupsNode = jsonNode.get("cognito:groups"); | ||
if (groupsNode != null && groupsNode.isArray()) { | ||
List<String> groups = objectMapper.convertValue(groupsNode, List.class); | ||
if (!groups.contains(ADMIN_ROLE)) { | ||
response.sendError( | ||
HttpServletResponse.SC_UNAUTHORIZED, "User does not have admin role"); | ||
return; | ||
} | ||
} else { | ||
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "No cognito:groups found"); | ||
return; | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/auth/AdminRequired.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package br.com.fiap.grupo30.fastfood.infrastructure.auth; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface AdminRequired {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters