|
| 1 | +/** |
| 2 | + * Copyright (C) 2022 Red Hat, Inc. (https://github.com/Commonjava/indy-security) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.commonjava.indy.service.security.jaxrs; |
| 17 | + |
| 18 | +import io.quarkus.oidc.client.filter.runtime.AbstractOidcClientRequestFilter; |
| 19 | +import jakarta.annotation.Priority; |
| 20 | +import jakarta.ws.rs.Priorities; |
| 21 | +import jakarta.ws.rs.client.ClientRequestContext; |
| 22 | +import jakarta.ws.rs.ext.Provider; |
| 23 | +import org.eclipse.microprofile.config.inject.ConfigProperty; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +@Provider |
| 30 | +@Priority(Priorities.AUTHENTICATION) |
| 31 | +public class CustomClientRequestFilter |
| 32 | + extends AbstractOidcClientRequestFilter |
| 33 | +{ |
| 34 | + @ConfigProperty(name = "indy_security.enabled") |
| 35 | + boolean securityEnabled; |
| 36 | + |
| 37 | + private static final List<String> nonAuthMethods = Arrays.asList("GET", "HEAD"); // skip auth for GET/HEAD requests |
| 38 | + |
| 39 | + @Override |
| 40 | + public void filter(ClientRequestContext requestContext) throws IOException |
| 41 | + { |
| 42 | + if ( securityEnabled ) |
| 43 | + { |
| 44 | + String method = requestContext.getMethod().toUpperCase(); |
| 45 | + if ( nonAuthMethods.contains(method) ) |
| 46 | + { |
| 47 | + return; |
| 48 | + } |
| 49 | + super.filter( requestContext ); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments