Skip to content

Commit

Permalink
Add SecurityRule
Browse files Browse the repository at this point in the history
  • Loading branch information
sdelamo committed Feb 16, 2024
1 parent 414bd3b commit 2d19b95
Show file tree
Hide file tree
Showing 19 changed files with 383 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.micronaut;

import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.async.publisher.Publishers;
import io.micronaut.http.HttpRequest;
import io.micronaut.security.authentication.Authentication;
import io.micronaut.security.token.validator.TokenValidator;
import jakarta.inject.Singleton;
import org.reactivestreams.Publisher;
import java.util.Collections;

@Singleton
class ApiKeyTokenValidator implements TokenValidator<HttpRequest<?>> {
@Override
public Publisher<Authentication> validateToken(String token, @Nullable HttpRequest<?> request) {
if (token != null && token.equals("XXX")) {
return Publishers.just(Authentication.build("sherlock", Collections.singleton("ROLE_DETECTIVE")));
} else if (token != null && token.equals("YYY")) {
return Publishers.just(Authentication.build("watson", Collections.singleton("ROLE_USER")));
}
return Publishers.empty();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.micronaut;

import io.micronaut.core.annotation.Nullable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.micronaut;

import io.micronaut.core.annotation.NonNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.micronaut;

import io.micronaut.context.annotation.Requires;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.MutableHttpRequest;
Expand All @@ -9,10 +25,13 @@
import io.micronaut.http.client.ProxyHttpClient;
import io.micronaut.http.filter.HttpServerFilter;
import io.micronaut.http.filter.ServerFilterChain;
import io.micronaut.http.filter.ServerFilterPhase;
import io.micronaut.http.uri.UriBuilder;
import io.micronaut.security.filters.SecurityFilter;
import org.reactivestreams.Publisher;
import java.net.URI;

@Requires(missingProperty = "framework")
@Filter(ServerFilter.MATCH_ALL_PATTERN)
class GatewayFilter implements HttpServerFilter {
private final RouteMatcher routeMatcher;
Expand All @@ -38,4 +57,9 @@ private MutableHttpRequest<?> mutate(@NonNull Route route, @NonNull HttpRequest<
mutableHttpRequest.uri(uri);
return mutableHttpRequest;
}

@Override
public int getOrder() {
return ServerFilterPhase.SECURITY.order() + 100;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.micronaut;

import io.micronaut.core.annotation.NonNull;
Expand All @@ -11,6 +26,9 @@ public interface Route extends Named, Ordered {
@NonNull
String getUri();

@NonNull
List<String> getRolesAllowed();

@NonNull
List<? extends RoutePredicate> getPredicates();
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.micronaut;

import io.micronaut.context.annotation.EachProperty;
import io.micronaut.context.annotation.Parameter;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.convert.format.MapFormat;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand All @@ -17,6 +34,8 @@ public class RouteConfiguration implements Route {

private Map<String, Object> predicates;

private List<String> rolesAllowed = new ArrayList<>();

public RouteConfiguration(@Parameter String name) {
this.name = name;
}
Expand All @@ -40,6 +59,16 @@ public String getUri() {
return uri;
}

@Override
@NonNull
public List<String> getRolesAllowed() {
return rolesAllowed;
}

public void setRolesAllowed(@NonNull List<String> rolesAllowed) {
this.rolesAllowed = rolesAllowed;
}

public void setUri(String uri) {
this.uri = uri;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.micronaut;

import io.micronaut.core.annotation.NonNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.micronaut;

import io.micronaut.http.HttpRequest;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.micronaut;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.async.publisher.Publishers;
import io.micronaut.http.HttpRequest;
import io.micronaut.security.authentication.Authentication;
import io.micronaut.security.rules.SecurityRule;
import io.micronaut.security.rules.SecurityRuleResult;
import jakarta.inject.Singleton;
import org.reactivestreams.Publisher;

import java.util.Optional;

@Singleton
class RouteSecurityRule<B> implements SecurityRule<HttpRequest<B>> {

private final RouteMatcher routeMatcher;

RouteSecurityRule(RouteMatcher routeMatcher) {
this.routeMatcher = routeMatcher;
}

@Override
public Publisher<SecurityRuleResult> check(@Nullable HttpRequest<B> request, @Nullable Authentication authentication) {
return Publishers.just(securityRuleResult(request, authentication));
}

@NonNull
private SecurityRuleResult securityRuleResult(@Nullable HttpRequest<?> request, @Nullable Authentication authentication) {
Optional<Route> routeOptional = routeMatcher.matches(request);
if (routeOptional.isPresent()) {
Route route = routeOptional.get();
if (authentication == null && route.getRolesAllowed().contains(SecurityRule.IS_ANONYMOUS)) {
return SecurityRuleResult.ALLOWED;
}
if (authentication != null && authentication.getRoles()
.stream()
.anyMatch(role -> route.getRolesAllowed().contains(role))) {
return SecurityRuleResult.ALLOWED;
}
}
return SecurityRuleResult.UNKNOWN;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.micronaut;

import io.micronaut.serde.annotation.Serdeable;
Expand Down
Loading

0 comments on commit 2d19b95

Please sign in to comment.