use of io.micronaut.security.authentication.Authentication in project micronaut-security by micronaut-projects.
the class InterceptUrlMapRule method check.
/**
* If no configured pattern matches the request, return {@link SecurityRuleResult#UNKNOWN}.
* Reads the rules in order. The first matched rule will be used for determining authorization.
*
* @param request The current request
* @param routeMatch The matched route
* @param authentication The user authentication. Null if not authenticated
* @return The result
*/
@Override
public Publisher<SecurityRuleResult> check(HttpRequest<?> request, @Nullable RouteMatch<?> routeMatch, @Nullable Authentication authentication) {
final String path = request.getUri().getPath();
final HttpMethod httpMethod = request.getMethod();
Predicate<InterceptUrlMapPattern> exactMatch = p -> pathMatcher.matches(p.getPattern(), path) && p.getHttpMethod().isPresent() && httpMethod.equals(p.getHttpMethod().get());
Predicate<InterceptUrlMapPattern> uriPatternMatchOnly = p -> pathMatcher.matches(p.getPattern(), path) && !p.getHttpMethod().isPresent();
Optional<InterceptUrlMapPattern> matchedPattern = getPatternList().stream().filter(exactMatch).findFirst();
// if we don't get an exact match try to find a match by the uri pattern
if (!matchedPattern.isPresent()) {
if (LOG.isDebugEnabled()) {
LOG.debug("No url map pattern exact match found for path [{}] and method [{}]. Searching in patterns with no defined method.", path, httpMethod);
}
matchedPattern = getPatternList().stream().filter(uriPatternMatchOnly).findFirst();
if (LOG.isDebugEnabled()) {
if (matchedPattern.isPresent()) {
LOG.debug("Url map pattern found for path [{}]. Comparing roles.", path);
} else {
LOG.debug("No url map pattern match found for path [{}]. Returning unknown.", path);
}
}
}
return Mono.from(matchedPattern.map(pattern -> compareRoles(pattern.getAccess(), getRoles(authentication))).orElse(Mono.just(SecurityRuleResult.UNKNOWN)));
}
use of io.micronaut.security.authentication.Authentication in project micronaut-security by micronaut-projects.
the class SessionAuthenticationFetcher method fetchAuthentication.
@Override
public Publisher<Authentication> fetchAuthentication(HttpRequest<?> request) {
return Mono.<Authentication>create(emitter -> {
Optional<Session> opt = request.getAttributes().get(HttpSessionFilter.SESSION_ATTRIBUTE, Session.class);
if (opt.isPresent()) {
Session session = opt.get();
Optional<Authentication> authentication = session.get(SecurityFilter.AUTHENTICATION, Authentication.class);
authentication.ifPresent(emitter::success);
}
emitter.success();
});
}
use of io.micronaut.security.authentication.Authentication in project micronaut-security by micronaut-projects.
the class X509AuthenticationFetcher method fetchAuthentication.
@Override
public Publisher<Authentication> fetchAuthentication(HttpRequest<?> request) {
return Mono.<Authentication>create(emitter -> {
Optional<Certificate> optionalCertificate = request.getCertificate();
if (optionalCertificate.isPresent()) {
Certificate certificate = optionalCertificate.get();
if (certificate instanceof X509Certificate) {
emitter.success(new Authentication() {
X509Certificate x509Certificate = ((X509Certificate) certificate);
@Override
public String getName() {
return x509Certificate.getIssuerX500Principal().getName();
}
@NonNull
@Override
public Map<String, Object> getAttributes() {
return Collections.emptyMap();
}
});
return;
}
}
emitter.success();
});
}
Aggregations