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.nio.ByteBuffer;
24 import java.security.NoSuchAlgorithmException;
25 import java.security.SecureRandom;
27 import org.apache.shiro.authc.AuthenticationInfo;
28 import org.apache.shiro.authc.AuthenticationToken;
29 import org.apache.shiro.authc.UsernamePasswordToken;
30 import org.apache.shiro.subject.PrincipalCollection;
31 import org.onap.aaf.cadi.Access;
32 import org.onap.aaf.cadi.Hash;
33 import org.onap.aaf.cadi.Access.Level;
35 public class AAFAuthenticationInfo implements AuthenticationInfo {
36 private static final long serialVersionUID = -1502704556864321020L;
37 // We assume that Shiro is doing Memory Only, and this salt is not needed cross process
38 private final static int salt = new SecureRandom().nextInt();
40 private final AAFPrincipalCollection apc;
41 private final byte[] hash;
42 private Access access;
44 public AAFAuthenticationInfo(Access access, String username, String password) {
46 apc = new AAFPrincipalCollection(username);
47 hash = getSaltedCred(password);
50 public byte[] getCredentials() {
51 access.log(Level.DEBUG, "AAFAuthenticationInfo.getCredentials");
56 public PrincipalCollection getPrincipals() {
57 access.log(Level.DEBUG, "AAFAuthenticationInfo.getPrincipals");
61 public boolean matches(AuthenticationToken atoken) {
62 if(atoken instanceof UsernamePasswordToken) {
63 UsernamePasswordToken upt = (UsernamePasswordToken)atoken;
64 if(apc.getPrimaryPrincipal().getName().equals(upt.getPrincipal())) {
65 byte[] newhash = getSaltedCred(new String(upt.getPassword()));
66 if(newhash.length==hash.length) {
67 for(int i=0;i<hash.length;++i) {
68 if(hash[i]!=newhash[i]) {
79 private byte[] getSaltedCred(String password) {
80 byte[] pbytes = password.getBytes();
81 ByteBuffer bb = ByteBuffer.allocate(pbytes.length+Integer.SIZE/8);
82 bb.asIntBuffer().put(salt);
83 bb.put(password.getBytes());
85 return Hash.hashSHA256(bb.array());
86 } catch (NoSuchAlgorithmException e) {
87 return new byte[0]; // should never get here