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.HashSet;
27 import java.util.List;
29 import org.apache.shiro.authc.AuthenticationException;
30 import org.apache.shiro.authc.AuthenticationInfo;
31 import org.apache.shiro.authc.AuthenticationToken;
32 import org.apache.shiro.authc.UsernamePasswordToken;
33 import org.apache.shiro.realm.AuthorizingRealm;
34 import org.apache.shiro.subject.PrincipalCollection;
35 import org.onap.aaf.cadi.Access.Level;
36 import org.onap.aaf.cadi.CadiException;
37 import org.onap.aaf.cadi.LocatorException;
38 import org.onap.aaf.cadi.Permission;
39 import org.onap.aaf.cadi.PropAccess;
40 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;
41 import org.onap.aaf.cadi.aaf.v2_0.AAFCon;
42 import org.onap.aaf.cadi.aaf.v2_0.AAFLurPerm;
43 import org.onap.aaf.cadi.config.Config;
44 import org.onap.aaf.misc.env.APIException;
46 public class AAFRealm extends AuthorizingRealm {
47 public static final String AAF_REALM = "AAFRealm";
49 private PropAccess access;
50 private AAFCon<?> acon;
51 private AAFAuthn<?> authn;
52 private HashSet<Class<? extends AuthenticationToken>> supports;
53 private AAFLurPerm authz;
58 * There appears to be no configuration objects or references available for CADI to start with.
62 access = new PropAccess(); // pick up cadi_prop_files from VM_Args
63 String cadi_prop_files = access.getProperty(Config.CADI_PROP_FILES);
64 if(cadi_prop_files==null) {
65 String msg = Config.CADI_PROP_FILES + " in VM Args is required to initialize AAFRealm.";
66 access.log(Level.INIT,msg);
67 throw new RuntimeException(msg);
70 acon = AAFCon.newInstance(access);
71 authn = acon.newAuthn();
72 authz = acon.newLur(authn);
73 } catch (APIException | CadiException | LocatorException e) {
74 String msg = "Cannot initiate AAFRealm";
75 access.log(Level.INIT,msg,e.getMessage());
76 throw new RuntimeException(msg,e);
79 supports = new HashSet<Class<? extends AuthenticationToken>>();
80 supports.add(UsernamePasswordToken.class);
84 protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
85 access.log(Level.DEBUG, "AAFRealm.doGetAuthenticationInfo",token);
87 final UsernamePasswordToken upt = (UsernamePasswordToken)token;
88 String password=new String(upt.getPassword());
91 err = authn.validate(upt.getUsername(),password);
92 } catch (IOException e) {
93 err = "Credential cannot be validated";
98 access.log(Level.DEBUG, err);
99 throw new AuthenticationException(err);
102 return new AAFAuthenticationInfo(
110 protected void assertCredentialsMatch(AuthenticationToken atoken, AuthenticationInfo ai)throws AuthenticationException {
111 if(ai instanceof AAFAuthenticationInfo) {
112 if(!((AAFAuthenticationInfo)ai).matches(atoken)) {
113 throw new AuthenticationException("Credentials do not match");
116 throw new AuthenticationException("AuthenticationInfo is not an AAFAuthenticationInfo");
122 protected AAFAuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
123 access.log(Level.DEBUG, "AAFRealm.doGetAuthenthorizationInfo");
124 Principal bait = (Principal)principals.getPrimaryPrincipal();
125 List<Permission> pond = new ArrayList<>();
126 authz.fishAll(bait,pond);
128 return new AAFAuthorizationInfo(access,bait,pond);
133 public boolean supports(AuthenticationToken token) {
134 return supports.contains(token.getClass());
138 public String getName() {