Add instructions to invoke the linter and code formatter plugins to the README and...
[aai/schema-service.git] / aai-schema-service / src / main / java / org / onap / aai / schemaservice / interceptors / pre / OneWaySslAuthorization.java
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
21 package org.onap.aai.schemaservice.interceptors.pre;
22
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Optional;
27
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;
34
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;
42
43 @Profile(Profiles.ONE_WAY_SSL)
44 @PreMatching
45 @Priority(AAIRequestFilterPriority.AUTHORIZATION)
46 public class OneWaySslAuthorization extends AAIContainerFilter implements ContainerRequestFilter {
47
48     @Autowired
49     private AuthorizationService authorizationService;
50
51     @Override
52     public void filter(ContainerRequestContext containerRequestContext) throws IOException {
53
54         if (containerRequestContext.getUriInfo().getRequestUri().getPath()
55             .matches("^.*/util/echo$")) {
56             return;
57         }
58
59         String basicAuth = containerRequestContext.getHeaderString("Authorization");
60         List<MediaType> acceptHeaderValues = containerRequestContext.getAcceptableMediaTypes();
61
62         if (basicAuth == null || !basicAuth.startsWith("Basic ")) {
63             Optional<Response> responseOptional = errorResponse("AAI_3300", acceptHeaderValues);
64             containerRequestContext.abortWith(responseOptional.get());
65             return;
66         }
67
68         basicAuth = basicAuth.substring(6);
69
70         if (!authorizationService.checkIfUserAuthorized(basicAuth)) {
71             Optional<Response> responseOptional = errorResponse("AAI_3300", acceptHeaderValues);
72             containerRequestContext.abortWith(responseOptional.get());
73             return;
74         }
75
76     }
77
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())
81             .entity(
82                 ErrorLogHelper.getRESTAPIErrorResponse(acceptHeaderValues, aaie, new ArrayList<>()))
83             .build());
84
85     }
86 }