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=========================================================
21 package org.onap.aai.schemaservice.interceptors.pre;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Optional;
28 import javax.annotation.Priority;
29 import javax.ws.rs.container.ContainerRequestContext;
30 import javax.ws.rs.container.ContainerRequestFilter;
31 import javax.ws.rs.container.PreMatching;
32 import javax.ws.rs.core.MediaType;
33 import javax.ws.rs.core.Response;
35 import org.onap.aai.exceptions.AAIException;
36 import org.onap.aai.logging.ErrorLogHelper;
37 import org.onap.aai.schemaservice.Profiles;
38 import org.onap.aai.schemaservice.interceptors.AAIContainerFilter;
39 import org.onap.aai.schemaservice.service.AuthorizationService;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.context.annotation.Profile;
43 @Profile(Profiles.ONE_WAY_SSL)
45 @Priority(AAIRequestFilterPriority.AUTHORIZATION)
46 public class OneWaySslAuthorization extends AAIContainerFilter implements ContainerRequestFilter {
49 private AuthorizationService authorizationService;
52 public void filter(ContainerRequestContext containerRequestContext) throws IOException {
54 if (containerRequestContext.getUriInfo().getRequestUri().getPath()
55 .matches("^.*/util/echo$")) {
59 String basicAuth = containerRequestContext.getHeaderString("Authorization");
60 List<MediaType> acceptHeaderValues = containerRequestContext.getAcceptableMediaTypes();
62 if (basicAuth == null || !basicAuth.startsWith("Basic ")) {
63 Optional<Response> responseOptional = errorResponse("AAI_3300", acceptHeaderValues);
64 containerRequestContext.abortWith(responseOptional.get());
68 basicAuth = basicAuth.substring(6);
70 if (!authorizationService.checkIfUserAuthorized(basicAuth)) {
71 Optional<Response> responseOptional = errorResponse("AAI_3300", acceptHeaderValues);
72 containerRequestContext.abortWith(responseOptional.get());
78 private Optional<Response> errorResponse(String errorCode, List<MediaType> acceptHeaderValues) {
79 AAIException aaie = new AAIException(errorCode);
80 return Optional.of(Response.status(aaie.getErrorObject().getHTTPResponseCode())
82 ErrorLogHelper.getRESTAPIErrorResponse(acceptHeaderValues, aaie, new ArrayList<>()))