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