989e56ab6b8502b763a99792d0e6a42d0635aaf1
[aai/schema-service.git] /
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.aai.schemaservice.interceptors.pre;
21
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;
29
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;
40
41 @Profile(Profiles.ONE_WAY_SSL)
42 @PreMatching
43 @Priority(AAIRequestFilterPriority.AUTHORIZATION)
44 public class OneWaySslAuthorization extends AAIContainerFilter implements ContainerRequestFilter {
45
46     @Autowired
47     private AuthorizationService authorizationService;
48
49     @Override
50     public void filter(ContainerRequestContext containerRequestContext) throws IOException {
51
52         if (containerRequestContext.getUriInfo().getRequestUri().getPath().matches("^.*/util/echo$")) {
53             return;
54         }
55
56         String basicAuth = containerRequestContext.getHeaderString("Authorization");
57         List<MediaType> acceptHeaderValues = containerRequestContext.getAcceptableMediaTypes();
58
59         if (basicAuth == null || !basicAuth.startsWith("Basic ")) {
60             Optional<Response> responseOptional = errorResponse("AAI_3300", acceptHeaderValues);
61             containerRequestContext.abortWith(responseOptional.get());
62             return;
63         }
64
65         basicAuth = basicAuth.substring(6);
66
67         if (!authorizationService.checkIfUserAuthorized(basicAuth)) {
68             Optional<Response> responseOptional = errorResponse("AAI_3300", acceptHeaderValues);
69             containerRequestContext.abortWith(responseOptional.get());
70             return;
71         }
72
73     }
74
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<>()))
79             .build());
80
81     }
82 }