2 * ============LICENSE_START====================================================
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
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.aaf.cadi.shiro;
23 import java.io.IOException;
24 import java.security.Principal;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
29 import java.util.Map.Entry;
30 import java.util.TreeMap;
32 import org.apache.shiro.authc.AuthenticationException;
33 import org.apache.shiro.authc.AuthenticationInfo;
34 import org.apache.shiro.authc.AuthenticationToken;
35 import org.apache.shiro.authc.UsernamePasswordToken;
36 import org.apache.shiro.realm.AuthorizingRealm;
37 import org.apache.shiro.subject.PrincipalCollection;
38 import org.onap.aaf.cadi.Access.Level;
39 import org.onap.aaf.cadi.CadiException;
40 import org.onap.aaf.cadi.LocatorException;
41 import org.onap.aaf.cadi.Permission;
42 import org.onap.aaf.cadi.PropAccess;
43 import org.onap.aaf.cadi.Symm;
44 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;
45 import org.onap.aaf.cadi.aaf.v2_0.AAFCon;
46 import org.onap.aaf.cadi.aaf.v2_0.AAFLurPerm;
47 import org.onap.aaf.cadi.config.Config;
48 import org.onap.aaf.cadi.filter.MapBathConverter;
49 import org.onap.aaf.cadi.util.CSV;
50 import org.onap.aaf.misc.env.APIException;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 public class AAFRealm extends AuthorizingRealm {
55 public static final String AAF_REALM = "AAFRealm";
56 private static final Logger logger = LoggerFactory.getLogger(AAFRealm.class);
57 private static Singleton singleton = Singleton.singleton();
59 private static class Singleton {
60 private AAFCon<?> acon;
61 private AAFAuthn<?> authn;
62 // private Set<Class<? extends AuthenticationToken>> supports;
63 private AAFLurPerm authz;
64 private MapBathConverter mbc;
65 private Map<String,String> idMap;
69 String cadi_prop_files = access.getProperty(Config.CADI_PROP_FILES);
70 if(cadi_prop_files==null) {
71 String msg = Config.CADI_PROP_FILES + " in VM Args is required to initialize AAFRealm.";
72 access.log(Level.INFO,msg);
73 throw new RuntimeException(msg);
76 acon = AAFCon.newInstance(access);
77 authn = acon.newAuthn();
78 authz = acon.newLur(authn);
80 final String csv = access.getProperty(Config.CADI_BATH_CONVERT);
83 mbc = new MapBathConverter(access, new CSV(access,csv));
84 access.log(Level.INFO, "MapBathConversion enabled with file ",csv);
85 idMap = Collections.synchronizedMap(new TreeMap<String,String>());
87 for(Entry<String, String> es : mbc.map().entrySet()) {
88 String oldID = es.getKey();
89 if(oldID.startsWith("Basic ")) {
90 oldID = Symm.base64noSplit.decode(oldID.substring(6));
91 int idx = oldID.indexOf(':');
93 oldID = oldID.substring(0, idx);
96 String newID = es.getValue();
97 if(newID.startsWith("Basic ")) {
98 newID = Symm.base64noSplit.decode(newID.substring(6));
99 int idx = newID.indexOf(':');
101 newID = newID.substring(0, idx);
104 idMap.put(oldID,newID);
106 } catch (IOException e) {
110 } catch (APIException | CadiException | LocatorException e) {
111 String msg = "Cannot initiate AAFRealm";
112 access.log(Level.ERROR,e,msg);
113 throw new RuntimeException(msg,e);
117 // There is only one of these. If there are more, put back
118 // supports = Collections.synchronizedSet(new HashSet<>());
119 // supports.add(UsernamePasswordToken.class);
122 public static synchronized Singleton singleton() {
123 if(singleton==null) {
124 singleton = new Singleton();
129 // pick up cadi_prop_files from VM_Args
130 private final PropAccess access = new PropAccess() {
132 public void log(Exception e, Object... elements) {
133 logger.error(buildMsg(Level.ERROR, elements).toString(),e);
137 public void log(Level level, Object... elements) {
139 String str = buildMsg(level, elements).toString();
165 public void printf(Level level, String fmt, Object... elements) {
167 String str = String.format(fmt, elements);
193 public boolean willLog(Level level) {
194 if(super.willLog(level)) {
198 return logger.isWarnEnabled();
200 return logger.isDebugEnabled();
202 return logger.isErrorEnabled();
205 return logger.isInfoEnabled();
209 return logger.isTraceEnabled();
219 * There appears to be no configuration objects or references available for CADI to start with.
224 protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
225 final UsernamePasswordToken upt = (UsernamePasswordToken)token;
226 final String user = upt.getUsername();
227 String authUser = user;
228 final String password=new String(upt.getPassword());
229 String authPassword = password;
230 if(singleton.mbc!=null) {
232 final String oldBath = "Basic " + Symm.base64noSplit.encode(user+':'+password);
233 String bath = singleton.mbc.convert(singleton.access, oldBath);
235 bath = Symm.base64noSplit.decode(bath.substring(6));
236 int colon = bath.indexOf(':');
238 authUser = bath.substring(0, colon);
239 authPassword = bath.substring(colon+1);
242 } catch (IOException e) {
243 singleton.access.log(e);
248 err = singleton.authn.validate(authUser,authPassword);
250 singleton.access.log(Level.INFO, err);
251 throw new AuthenticationException(err);
254 } catch (IOException e) {
255 singleton.access.log(e,"Credential cannot be validated");
258 return new AAFAuthenticationInfo(
266 protected void assertCredentialsMatch(AuthenticationToken atoken, AuthenticationInfo ai)throws AuthenticationException {
267 if(ai instanceof AAFAuthenticationInfo) {
268 if(!((AAFAuthenticationInfo)ai).matches(atoken)) {
269 throw new AuthenticationException("Credentials do not match");
272 throw new AuthenticationException("AuthenticationInfo is not an AAFAuthenticationInfo");
277 protected AAFAuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
278 Principal bait = (Principal)principals.getPrimaryPrincipal();
279 Principal newBait = bait;
280 if(singleton.idMap!=null) {
281 final String newID = singleton.idMap.get(bait.getName());
283 singleton.access.printf(Level.INFO,"Successful authentication Translation %s to %s",bait.getName(), newID);
284 newBait = new Principal() {
286 public String getName() {
292 List<Permission> pond = new ArrayList<>();
293 singleton.authz.fishAll(newBait,pond);
294 return new AAFAuthorizationInfo(singleton.access,bait,pond);
298 public boolean supports(AuthenticationToken token) {
299 // Only one was being loaded. If more are needed uncomment the multi-class mode
300 return token instanceof UsernamePasswordToken;
301 // return singleton.supports.contains(token.getClass());
305 public String getName() {