-
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.
add method to check authenticated user against request
- Loading branch information
1 parent
3db7bbc
commit fd98434
Showing
4 changed files
with
78 additions
and
12 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
48 changes: 48 additions & 0 deletions
48
src/main/java/it/gov/pagopa/rtp/activator/utils/Authorizations.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,48 @@ | ||
package it.gov.pagopa.rtp.activator.utils; | ||
|
||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.ReactiveSecurityContextHolder; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.function.BiPredicate; | ||
import java.util.function.Function; | ||
|
||
public final class Authorizations { | ||
|
||
private Authorizations(){} | ||
|
||
/** | ||
* Verifies that the subject in the request matches the authenticated user's subject. | ||
* It uses the provided {@code extractSubject} function to extract the subject from the request object, | ||
* and compares it with the authenticated user's name. | ||
* | ||
* @param <T> The type of the request body. | ||
* @param requestBody A {@link Mono} containing the request body that needs to be verified. | ||
* @param extractSubject A function that extracts the subject (e.g., user identifier) from the request body. | ||
* @return A {@link Mono} containing the request body if the subjects match, or an error if they don't. | ||
*/ | ||
public static <T> Mono<T> verifySubjectRequest(Mono<T> requestBody, Function<T, String> extractSubject) { | ||
return verifyRequestBody(requestBody, (request, auth) -> extractSubject.apply(request).equals(auth.getName())); | ||
} | ||
|
||
/** | ||
* Verifies that the request body passes a custom verification function that involves the authenticated user. | ||
* This method takes a {@link Mono} of the request body and checks the provided {@code verify} predicate to ensure | ||
* the request meets the security requirements. If the predicate fails, an {@link AccessDeniedException} is thrown. | ||
* | ||
* @param <T> The type of the request body. | ||
* @param requestBody A {@link Mono} containing the request body that needs to be verified. | ||
* @param verify A {@link BiPredicate} that performs a custom verification on the request body and the authenticated user. | ||
* @return A {@link Mono} containing the request body if the verification succeeds. | ||
*/ | ||
public static <T> Mono<T> verifyRequestBody(Mono<T> requestBody, BiPredicate<T, Authentication> verify) { | ||
return ReactiveSecurityContextHolder.getContext().flatMap(securityContext -> | ||
requestBody.flatMap(request -> verify.test(request, securityContext.getAuthentication()) ? | ||
Mono.just(request) : | ||
Mono.error(new AccessDeniedException("Authenticated user doesn't have permission to perform this action.")) | ||
) | ||
); | ||
} | ||
|
||
} |
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