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.HashSet;
28 import java.util.List;
30 import java.util.Map.Entry;
32 import java.util.TreeMap;
34 import org.apache.shiro.authc.AuthenticationException;
35 import org.apache.shiro.authc.AuthenticationInfo;
36 import org.apache.shiro.authc.AuthenticationToken;
37 import org.apache.shiro.authc.UsernamePasswordToken;
38 import org.apache.shiro.realm.AuthorizingRealm;
39 import org.apache.shiro.subject.PrincipalCollection;
40 import org.onap.aaf.cadi.Access.Level;
41 import org.onap.aaf.cadi.CadiException;
42 import org.onap.aaf.cadi.LocatorException;
43 import org.onap.aaf.cadi.Permission;
44 import org.onap.aaf.cadi.PropAccess;
45 import org.onap.aaf.cadi.Symm;
46 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;
47 import org.onap.aaf.cadi.aaf.v2_0.AAFCon;
48 import org.onap.aaf.cadi.aaf.v2_0.AAFLurPerm;
49 import org.onap.aaf.cadi.config.Config;
50 import org.onap.aaf.cadi.filter.MapBathConverter;
51 import org.onap.aaf.cadi.util.CSV;
52 import org.onap.aaf.misc.env.APIException;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 public class AAFRealm extends AuthorizingRealm {
57 public static final String AAF_REALM = "AAFRealm";
58 private static final Logger logger = LoggerFactory.getLogger(AAFRealm.class);
59 private static Singleton singleton = Singleton.singleton();
61 private static class Singleton {
62 private AAFCon<?> acon;
63 private AAFAuthn<?> authn;
64 private Set<Class<? extends AuthenticationToken>> supports;
65 private AAFLurPerm authz;
66 private MapBathConverter mbc;
67 private Map<String,String> idMap;
71 String cadi_prop_files = access.getProperty(Config.CADI_PROP_FILES);
72 if(cadi_prop_files==null) {
73 String msg = Config.CADI_PROP_FILES + " in VM Args is required to initialize AAFRealm.";
74 access.log(Level.INFO,msg);
75 throw new RuntimeException(msg);
78 acon = AAFCon.newInstance(access);
79 authn = acon.newAuthn();
80 authz = acon.newLur(authn);
82 final String csv = access.getProperty(Config.CADI_BATH_CONVERT);
85 mbc = new MapBathConverter(access, new CSV(csv));
86 access.log(Level.INFO, "MapBathConversion enabled with file ",csv);
87 idMap = Collections.synchronizedMap(new TreeMap<String,String>());
89 for(Entry<String, String> es : mbc.map().entrySet()) {
90 String oldID = es.getKey();
91 if(oldID.startsWith("Basic ")) {
92 oldID = Symm.base64noSplit.decode(oldID.substring(6));
93 int idx = oldID.indexOf(':');
95 oldID = oldID.substring(0, idx);
98 String newID = es.getValue();
99 if(newID.startsWith("Basic ")) {
100 newID = Symm.base64noSplit.decode(newID.substring(6));
101 int idx = newID.indexOf(':');
103 newID = newID.substring(0, idx);
106 idMap.put(oldID,newID);
109 } catch (IOException e) {
113 } catch (APIException | CadiException | LocatorException e) {
114 String msg = "Cannot initiate AAFRealm";
115 access.log(Level.ERROR,e,msg);
116 throw new RuntimeException(msg,e);
119 supports = Collections.synchronizedSet(new HashSet<>());
120 supports.add(UsernamePasswordToken.class);
123 public static synchronized Singleton singleton() {
124 if(singleton==null) {
125 singleton = new Singleton();
130 // pick up cadi_prop_files from VM_Args
131 private final PropAccess access = new PropAccess() {
133 public void log(Exception e, Object... elements) {
134 logger.error(buildMsg(Level.ERROR, elements).toString(),e);
138 public void log(Level level, Object... elements) {
140 String str = buildMsg(level, elements).toString();
166 public void printf(Level level, String fmt, Object... elements) {
168 String str = String.format(fmt, elements);
194 public boolean willLog(Level level) {
195 if(super.willLog(level)) {
199 return logger.isWarnEnabled();
201 return logger.isDebugEnabled();
203 return logger.isErrorEnabled();
206 return logger.isInfoEnabled();
210 return logger.isTraceEnabled();
220 * There appears to be no configuration objects or references available for CADI to start with.
225 protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
226 final UsernamePasswordToken upt = (UsernamePasswordToken)token;
227 final String user = upt.getUsername();
228 String authUser = user;
229 final String password=new String(upt.getPassword());
230 String authPassword = password;
231 if(singleton.mbc!=null) {
233 final String oldBath = "Basic " + Symm.base64noSplit.encode(user+':'+password);
234 String bath = singleton.mbc.convert(singleton.access, oldBath);
236 bath = Symm.base64noSplit.decode(bath.substring(6));
237 int colon = bath.indexOf(':');
239 authUser = bath.substring(0, colon);
240 authPassword = bath.substring(colon+1);
243 } catch (IOException e) {
244 singleton.access.log(e);
249 err = singleton.authn.validate(authUser,authPassword);
251 singleton.access.log(Level.INFO, err);
252 throw new AuthenticationException(err);
255 } catch (IOException e) {
256 singleton.access.log(e,"Credential cannot be validated");
259 return new AAFAuthenticationInfo(
267 protected void assertCredentialsMatch(AuthenticationToken atoken, AuthenticationInfo ai)throws AuthenticationException {
269 if(ai instanceof AAFAuthenticationInfo) {
270 if(!((AAFAuthenticationInfo)ai).matches(atoken)) {
271 throw new AuthenticationException("Credentials do not match");
275 throw new AuthenticationException("AuthenticationInfo is not an AAFAuthenticationInfo");
284 protected AAFAuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
285 Principal bait = (Principal)principals.getPrimaryPrincipal();
286 Principal newBait = bait;
287 if(singleton.idMap!=null) {
288 final String newID = singleton.idMap.get(bait.getName());
290 singleton.access.printf(Level.INFO,"Successful authentication Translation %s to %s",bait.getName(), newID);
291 newBait = new Principal() {
293 public String getName() {
299 List<Permission> pond = new ArrayList<>();
300 singleton.authz.fishAll(newBait,pond);
301 return new AAFAuthorizationInfo(singleton.access,bait,pond);
305 public boolean supports(AuthenticationToken token) {
306 return singleton.supports.contains(token.getClass());
310 public String getName() {