b3ac094576e5eb89352d24bcafe25cb062fa95ef
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / taf / AbsTafResp.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.cadi.taf;
23
24 import org.onap.aaf.cadi.Access;
25 import org.onap.aaf.cadi.principal.TaggedPrincipal;
26 import org.onap.aaf.cadi.util.Timing;
27
28 /**
29  * AbsTafResp
30  * 
31  * Base class for TafResp (TAF Response Objects)
32  * 
33  * @author Jonathan
34  *
35  */
36 public abstract class AbsTafResp implements TafResp {
37
38     protected final Access access;
39     protected final String tafName;
40     protected final TaggedPrincipal principal;
41     protected final String desc;
42     private float timing;
43
44     /**
45      * AbsTafResp
46      * 
47      * Set and hold
48      * Description (for logging)
49      * Principal (as created by derived class)
50      * Access (for access to underlying container, i.e. for Logging, auditing, ClassLoaders, etc)
51      *  
52      * @param access
53      * @param tafname 
54      * @param principal
55      * @param description
56      */
57     public AbsTafResp(Access access, String tafname, TaggedPrincipal principal, String description) {
58         this.access = access;
59         this.tafName = tafname;
60         this.principal = principal;
61         this.desc = description;
62     }
63
64     /**
65      * isValid()
66      * 
67      * Respond in the affirmative if the TAF was able to Authenticate
68      */
69     public boolean isValid() {
70         return principal != null;
71     }
72
73     /**
74      * desc()
75      * 
76      * Respond with description of response as given by the TAF  
77      */
78     public String desc() {
79         return desc;
80     }
81
82     /**
83      * isAuthenticated()
84      * 
85      * Respond with the TAF's code of whether Authenticated, or suggested next steps
86      * default is either IS_AUTHENTICATED, or TRY_ANOTHER_TAF.  The TAF can overload
87      * and suggest others, such as "NO_FURTHER_PROCESSING", if it can detect that this
88      * is some sort of security breach (i.e. Denial of Service)  
89      */
90     public RESP isAuthenticated() {
91         return principal==null?RESP.TRY_ANOTHER_TAF:RESP.IS_AUTHENTICATED;
92     }
93
94     /**
95      * getPrincipal()
96      * 
97      * Return the principal created by the TAF based on Authentication. 
98      * 
99      * Returns "null" if Authentication failed (no principal)
100      */
101     public TaggedPrincipal getPrincipal() {
102         return principal;
103     }
104
105     /**
106      * getAccess()
107      * 
108      * Get the Access object from the TAF, so that appropriate Logging, etc can be coordinated.
109      */
110     public Access getAccess() {
111         return access;
112     }
113
114     /* (non-Javadoc)
115      * @see org.onap.aaf.cadi.taf.TafResp#isFailedAttempt()
116      */
117     public boolean isFailedAttempt() {
118         return false;
119     }
120
121     @Override
122     public float timing() {
123         return timing;
124     }
125     
126     @Override
127     public void timing(final long start) {
128         timing = Timing.millis(start);
129     }
130
131     @Override
132     public String taf() {
133         return tafName;
134     }
135
136 }