summaryrefslogtreecommitdiffstats
path: root/core/src/main/java/com/att/cadi/CadiWrap.java
blob: 64c565f02838c6fc7a8b28636a3ed6374f6b723f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*******************************************************************************
 * ============LICENSE_START====================================================
 * * org.onap.aaf
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * Licensed under the Apache License, Version 2.0 (the "License");
 * * you may not use this file except in compliance with the License.
 * * You may obtain a copy of the License at
 * * 
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * * 
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/
package com.att.cadi;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import com.att.cadi.Access.Level;
import com.att.cadi.filter.NullPermConverter;
import com.att.cadi.filter.PermConverter;
import com.att.cadi.lur.EpiLur;
import com.att.cadi.taf.TafResp;



/**
 * Inherit the HttpServletRequestWrapper, which calls methods of delegate it's created with, but
 * overload the key security mechanisms with CADI mechanisms
 * 
 * This works with mechanisms working strictly with HttpServletRequest (i.e. Servlet Filters)
 * 
 * Specialty cases, i.e. Tomcat, which for their containers utilize their own mechanisms and Wrappers, you may
 * need something similar.  See AppServer specific code (i.e. tomcat) for these.
 * 
 *
 */
public class CadiWrap extends HttpServletRequestWrapper implements HttpServletRequest, BasicCred {
	private Principal principal;
	private Lur lur;
	private String user; // used to set user/pass from brain-dead protocols like WSSE 
	private byte[] password;
	private PermConverter pconv;
	private Access access; 
	
	/**
	 * Standard Wrapper constructor for Delegate pattern
	 * @param request
	 */
	public CadiWrap(HttpServletRequest request, TafResp tafResp, Lur lur) {
		super(request);
		principal = tafResp.getPrincipal();
		access = tafResp.getAccess();
		this.lur = lur;
		pconv = NullPermConverter.singleton();
	}

	/**
	 * Standard Wrapper constructor for Delegate pattern, with PermConverter
	 * @param request
	 */
	public CadiWrap(HttpServletRequest request, TafResp tafResp, Lur lur, PermConverter pc) {
		super(request);
		principal = tafResp.getPrincipal();
		access = tafResp.getAccess();
		this.lur = lur;
		pconv = pc;
	}


	/**
	 * Part of the HTTP Security API.  Declare the User associated with this HTTP Transaction.
	 * CADI does this by reporting the name associated with the Principal obtained, if any.
	 */
// @Override
	public String getRemoteUser() {
		return principal==null?null:principal.getName();
	}

	/**
	 * Part of the HTTP Security API.  Return the User Principal associated with this HTTP 
	 * Transaction.
	 */
// @Override
	public Principal getUserPrincipal() {
		return principal;
	}
	
	/**
	 * This is the key API call for AUTHZ in J2EE.  Given a Role (String passed in), is the user
	 * associated with this HTTP Transaction allowed to function in this Role?
	 * 
	 * For CADI, we pass the responsibility for determining this to the "LUR", which may be
	 * determined by the Enterprise.
	 * 
	 * Note: Role check is also done in "CadiRealm" in certain cases...
	 * 
	 *
	 */
// @Override
	public boolean isUserInRole(String perm) {
		return perm==null?false:checkPerm(access,"(HttpRequest)",principal,pconv,lur,perm);
	}
	
	public static boolean checkPerm(Access access, String caller, Principal principal, PermConverter pconv, Lur lur, String perm) {
		if(principal== null) {
			access.log(Level.AUDIT,caller, "No Principal in Transaction");
			return false;
		} else { 
			perm = pconv.convert(perm);
			if(lur.fish(principal,lur.createPerm(perm))) {
				access.log(Level.DEBUG,caller, principal.getName(), "has", perm);
				return true;
			} else {
				access.log(Level.DEBUG,caller, principal.getName(), "does not have", perm);
				return false;
			}
		}

	}

	/** 
	 * CADI Function (Non J2EE standard). GetPermissions will read the Permissions from AAF (if configured) and Roles from Local Lur, etc
	 *  as implemented with lur.fishAll
	 *  
	 *  To utilize, the Request must be a "CadiWrap" object, then call.
	 */
	public List<Permission> getPermissions(Principal p) {
		List<Permission> perms = new ArrayList<Permission>();
		lur.fishAll(p, perms);
		return perms;
	}
	/**
	 * Allow setting of tafResp and lur after construction
	 * 
	 * This can happen if the CadiWrap is constructed in a Valve other than CadiValve
	 */
	public void set(TafResp tafResp, Lur lur) {
		principal = tafResp.getPrincipal();
		access = tafResp.getAccess();
		this.lur = lur;
	}

	public String getUser() {
		if(user==null && principal!=null) {
			user = principal.getName();
		}
		return user;
	}

	public byte[] getCred() {
		return password;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public void setCred(byte[] passwd) {
		password = passwd;
	}
	
	public CadiWrap setPermConverter(PermConverter pc) {
		pconv = pc;
		return this;
	}
	
	// Add a feature
	public void invalidate(String id) {
		if(lur instanceof EpiLur) {
			((EpiLur)lur).remove(id);
		} else if(lur instanceof CachingLur) {
			((CachingLur<?>)lur).remove(id);
		}
	}
	
	public Lur getLur() {
		return lur;
	}
}