fix shiro logs looping issue
[aaf/cadi.git] / shiro / src / main / java / org / onap / aaf / cadi / shiro / AAFAuthenticationInfo.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 package org.onap.aaf.cadi.shiro;
22
23 import java.nio.ByteBuffer;
24 import java.security.NoSuchAlgorithmException;
25 import java.security.SecureRandom;
26
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.apache.shiro.authc.AuthenticationInfo;
30 import org.apache.shiro.authc.AuthenticationToken;
31 import org.apache.shiro.authc.UsernamePasswordToken;
32 import org.apache.shiro.subject.PrincipalCollection;
33 import org.onap.aaf.cadi.Access;
34 import org.onap.aaf.cadi.Hash;
35
36 public class AAFAuthenticationInfo implements AuthenticationInfo {
37         private static final long serialVersionUID = -1502704556864321020L;
38         
39         final static  Logger logger =  LoggerFactory.getLogger(AAFAuthenticationInfo.class);
40         
41         // We assume that Shiro is doing Memory Only, and this salt is not needed cross process
42         private final static int salt = new SecureRandom().nextInt(); 
43
44         private final AAFPrincipalCollection apc;
45         private final byte[] hash;
46         private Access access;
47
48         public AAFAuthenticationInfo(Access access, String username, String password) {
49                 this.access = access;
50                 apc = new AAFPrincipalCollection(username);
51                 hash = getSaltedCred(password);
52         }
53         @Override
54         public byte[] getCredentials() {        
55 //              logger.info("AAFAuthenticationInfo.getCredentials");
56                 return hash;
57         }
58
59         @Override
60         public PrincipalCollection getPrincipals() {
61 //              logger.info( "AAFAuthenticationInfo.getPrincipals");
62                 return apc;
63         }
64
65         public boolean matches(AuthenticationToken atoken) {
66                 if(atoken instanceof UsernamePasswordToken) {
67                         UsernamePasswordToken upt = (UsernamePasswordToken)atoken;
68                         if(apc.getPrimaryPrincipal().getName().equals(upt.getPrincipal())) {
69                                 byte[] newhash = getSaltedCred(new String(upt.getPassword()));
70                                 logger.info("Successful authentication attempt by " +upt.getPrincipal());
71                                 if(newhash.length==hash.length) {
72                                         for(int i=0;i<hash.length;++i) {
73                                                 if(hash[i]!=newhash[i]) {
74                                                         return false;
75                                                 }
76                                         }
77                                         return true;
78                                 }
79                         }
80                 }
81                 return false;
82         }
83         
84         private byte[] getSaltedCred(String password) {
85                 byte[] pbytes = password.getBytes();
86                 ByteBuffer bb = ByteBuffer.allocate(pbytes.length+Integer.SIZE/8);
87                 bb.asIntBuffer().put(salt);
88                 bb.put(password.getBytes());
89                 try {
90                         return Hash.hashSHA256(bb.array());
91                 } catch (NoSuchAlgorithmException e) {
92                         return new byte[0]; // should never get here
93                 }
94         }
95 }