904f3cdc464c7f38d4efef3c00c33f0c240eaa44
[aaf/authz.git] / cadi / servlet-sample / src / test / java / org / onap / aaf / sample / cadi / jetty / MiniJASPIWrap.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 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
22 package org.onap.aaf.sample.cadi.jetty;
23
24 import java.io.IOException;
25
26 import javax.servlet.Servlet;
27 import javax.servlet.ServletException;
28 import javax.servlet.ServletRequest;
29 import javax.servlet.ServletResponse;
30 import javax.servlet.UnavailableException;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
34 import org.eclipse.jetty.server.Request;
35 import org.eclipse.jetty.servlet.ServletHolder;
36 import org.onap.aaf.cadi.filter.RolesAllowed;
37
38
39
40 /**
41  * MiniJASPIWrap
42  * 
43  * Support the ability to check JASPI Annotation Style Authorizations.
44  * 
45  * This can be a clean way to enforce API Authorization without mistakes in code.
46  * 
47  * @author JonathanGathman
48  *
49  */
50 public class MiniJASPIWrap extends ServletHolder {
51         private RolesAllowed rolesAllowed;
52         //private String roles;
53         public MiniJASPIWrap(Class<? extends Servlet> servlet) {
54                 super(servlet);
55                 this.rolesAllowed = servlet.getAnnotation(RolesAllowed.class);
56                 StringBuilder sb = new StringBuilder();
57                 boolean first = true;
58                 if(rolesAllowed!=null) {
59                         for(String str : rolesAllowed.value()) {
60                                 if(first)first=false;
61                                 else sb.append(',');
62                                 sb.append(str);
63                         }
64                 }
65                 //roles = sb.toString();
66         }
67
68         /**
69          * handle
70          * 
71          * When utilized, this class authorizes the transaction by first calling the standard J2EE API call
72          * "isUserInRole" with the role(s) found in the class Annotations (JASPI Style) 
73          */
74         @Override
75         public void handle(Request baseRequest, ServletRequest request, ServletResponse response) throws ServletException,      UnavailableException, IOException {
76                 if(rolesAllowed==null) {
77                         super.handle(baseRequest, request, response);
78                 } else { // Validate
79                         try {
80                                 
81                                 HttpServletRequest hreq = (HttpServletRequest)request;
82                                 boolean proceed = false;
83                                 for(String role : rolesAllowed.value()) {
84                                         if(hreq.isUserInRole(role)) {
85                                                 proceed = true;
86                                                 break;
87                                         }
88                                 }
89                                 if(proceed) {
90                                         super.handle(baseRequest, request, response);
91                                 } else {
92                                         //baseRequest.getServletContext().log(hreq.getUserPrincipal().getName()+" Refused " + roles);
93                                         ((HttpServletResponse)response).sendError(403); // forbidden
94                                 }
95                         } catch(ClassCastException e) {
96                                 throw new ServletException("JASPIWrap only supports HTTPServletRequest/HttpServletResponse");
97                         }
98                 }               
99         }
100
101 }