2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.aai.schemaservice.interceptors.pre;
22 import org.onap.aai.exceptions.AAIException;
23 import org.onap.aai.logging.ErrorLogHelper;
24 import org.onap.aai.schemaservice.Profiles;
25 import org.onap.aai.schemaservice.interceptors.AAIContainerFilter;
26 import org.onap.aai.schemaservice.service.AuthorizationService;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.context.annotation.Profile;
30 import javax.annotation.Priority;
31 import javax.ws.rs.container.ContainerRequestContext;
32 import javax.ws.rs.container.ContainerRequestFilter;
33 import javax.ws.rs.container.PreMatching;
34 import javax.ws.rs.core.MediaType;
35 import javax.ws.rs.core.Response;
36 import java.io.IOException;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.Optional;
41 @Profile(Profiles.ONE_WAY_SSL)
43 @Priority(AAIRequestFilterPriority.AUTHORIZATION)
44 public class OneWaySslAuthorization extends AAIContainerFilter implements ContainerRequestFilter {
47 private AuthorizationService authorizationService;
50 public void filter(ContainerRequestContext containerRequestContext) throws IOException {
52 if (containerRequestContext.getUriInfo().getRequestUri().getPath().matches("^.*/util/echo$")) {
56 String basicAuth = containerRequestContext.getHeaderString("Authorization");
57 List<MediaType> acceptHeaderValues = containerRequestContext.getAcceptableMediaTypes();
59 if (basicAuth == null || !basicAuth.startsWith("Basic ")) {
60 Optional<Response> responseOptional = errorResponse("AAI_3300", acceptHeaderValues);
61 containerRequestContext.abortWith(responseOptional.get());
65 basicAuth = basicAuth.substring(6);
67 if (!authorizationService.checkIfUserAuthorized(basicAuth)) {
68 Optional<Response> responseOptional = errorResponse("AAI_3300", acceptHeaderValues);
69 containerRequestContext.abortWith(responseOptional.get());
75 private Optional<Response> errorResponse(String errorCode, List<MediaType> acceptHeaderValues) {
76 AAIException aaie = new AAIException(errorCode);
77 return Optional.of(Response.status(aaie.getErrorObject().getHTTPResponseCode())
78 .entity(ErrorLogHelper.getRESTAPIErrorResponse(acceptHeaderValues, aaie, new ArrayList<>()))