3ef532b4bcdc16ba71e30be52bd239e178352376
[aaf/authz.git] / auth / auth-cass / src / main / java / org / onap / aaf / auth / direct / DirectAAFUserPass.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
22 package org.onap.aaf.auth.direct;
23
24 import static org.onap.aaf.auth.layer.Result.OK;
25
26 import java.util.Date;
27
28 import javax.servlet.http.HttpServletRequest;
29
30 import org.onap.aaf.auth.dao.DAOException;
31 import org.onap.aaf.auth.dao.hl.Question;
32 import org.onap.aaf.auth.env.AuthzEnv;
33 import org.onap.aaf.auth.env.AuthzTrans;
34 import org.onap.aaf.auth.layer.Result;
35 import org.onap.aaf.cadi.CredVal;
36
37 /**
38  * DirectAAFUserPass is intended to provide password Validation directly from Cassandra Database, and is only
39  * intended for use in AAF itself.  The normal "AAF Taf" objects are, of course, clients.
40  * 
41  * @author Jonathan
42  *
43  */
44 public class DirectAAFUserPass implements CredVal {
45     private final AuthzEnv env;
46     private final Question question;
47     
48     public DirectAAFUserPass(AuthzEnv env, Question question) {
49         this.env = env;
50         this.question = question;
51     }
52
53     @Override
54     public boolean validate(String user, Type type, byte[] pass, Object state) {
55             try {
56                 AuthzTrans trans;
57                 if (state !=null) {
58                     if (state instanceof AuthzTrans) {
59                         trans = (AuthzTrans)state;
60                     } else {
61                         trans = env.newTransNoAvg();
62                         if (state instanceof HttpServletRequest) {
63                             trans.set((HttpServletRequest)state);
64                         }
65                     }
66                 } else {
67                     trans = env.newTransNoAvg();
68                 }
69                 Result<Date> result = question.doesUserCredMatch(trans, user, pass);
70                 trans.logAuditTrail(env.info());
71                 switch(result.status) {
72                     case OK:
73                         return true;
74                     default:
75                         String ip = trans.ip()==null?"":(", ip="+trans.ip());
76                         env.warn().log(user, "failed password validation" + ip + ':',result.errorString());
77                 }
78             } catch (DAOException e) {
79                 env.error().log(e,"Cannot validate user/pass from cassandra");
80             }
81         return false;
82     }
83 }