Remove Tabs, per Jococo
[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             if(user==null || type==null || pass==null) {
56                 return false;
57             }
58         
59             try {
60                 AuthzTrans trans;
61                 boolean transfer = false;
62                 if (state !=null) {
63                     if (state instanceof AuthzTrans) {
64                         trans = (AuthzTrans)state;
65                     } else {
66                         trans = env.newTransNoAvg();
67                         if (state instanceof HttpServletRequest) {
68                             trans.set((HttpServletRequest)state,null);
69                             transfer=true;
70                         }
71                     }
72                 } else {
73                     trans = env.newTransNoAvg();
74                 }
75                 Result<Date> result = question.doesUserCredMatch(trans, user, pass);
76                 if(transfer) {
77                     ((HttpServletRequest)state).setAttribute("CRED_TAG", trans.getTag());
78                 }
79                 trans.logAuditTrail(env.debug());
80                 switch(result.status) {
81                     case OK:
82                         return true;
83                     default:
84                         String ip = trans.ip()==null?"":trans.ip();
85                         env.audit().printf("user=%s,tag=%s,ip=%s,msg=\"failed password validation: %s\"",user,trans.getTag(),ip,result.errorString());
86                 }
87             } catch (DAOException e) {
88                 env.error().log(e,"Cannot validate user/pass from cassandra");
89             }
90         return false;
91     }
92 }