Remove Tabs, per Jococo
[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     // Note: Valid Resp is based on Principal being non-null
41     protected final TaggedPrincipal principal;
42     protected final String target;
43     protected final String desc;
44     private float timing;
45
46     /**
47      * AbsTafResp
48      * 
49      * Set and hold
50      * Description (for logging)
51      * Principal (as created by derived class)
52      * Access (for access to underlying container, i.e. for Logging, auditing, ClassLoaders, etc)
53      *  
54      * @param access
55      * @param tafname 
56      * @param principal
57      * @param description
58      */
59     public AbsTafResp(Access access, String tafname, TaggedPrincipal principal, String description) {
60         this.access = access;
61         this.tafName = tafname;
62         this.principal = principal;
63         this.target = principal==null?"unknown":principal.getName();
64         this.desc = description;
65     }
66     
67     /**
68      * AbsTafResp
69      * 
70      * Set and hold
71      * Description (for logging)
72      * Principal (as created by derived class)
73      * Access (for access to underlying container, i.e. for Logging, auditing, ClassLoaders, etc)
74      *  
75      * @param access
76      * @param tafname 
77      * @param principal
78      * @param description
79      */
80     public AbsTafResp(Access access, String tafname, String target, String description) {
81         this.access = access;
82         this.tafName = tafname;
83         this.principal = null;
84         this.target = target;
85         this.desc = description;
86     }
87
88     /**
89      * isValid()
90      * 
91      * Respond in the affirmative if the TAF was able to Authenticate
92      */
93     public boolean isValid() {
94         return principal != null;
95     }
96
97     /**
98      * desc()
99      * 
100      * Respond with description of response as given by the TAF  
101      */
102     public String desc() {
103         return desc;
104     }
105
106     /**
107      * isAuthenticated()
108      * 
109      * Respond with the TAF's code of whether Authenticated, or suggested next steps
110      * default is either IS_AUTHENTICATED, or TRY_ANOTHER_TAF.  The TAF can overload
111      * and suggest others, such as "NO_FURTHER_PROCESSING", if it can detect that this
112      * is some sort of security breach (i.e. Denial of Service)  
113      */
114     public RESP isAuthenticated() {
115         return principal==null?RESP.TRY_ANOTHER_TAF:RESP.IS_AUTHENTICATED;
116     }
117
118     /**
119      * getPrincipal()
120      * 
121      * Return the principal created by the TAF based on Authentication. 
122      * 
123      * Returns "null" if Authentication failed (no principal)
124      */
125     public TaggedPrincipal getPrincipal() {
126         return principal;
127     }
128
129     /* (non-Javadoc)
130      * @see org.onap.aaf.cadi.taf.TafResp#getTarget()
131      */
132     @Override
133     public String getTarget() {
134         return target;
135     }
136     
137     /**
138      * getAccess()
139      * 
140      * Get the Access object from the TAF, so that appropriate Logging, etc can be coordinated.
141      */
142     public Access getAccess() {
143         return access;
144     }
145
146     /* (non-Javadoc)
147      * @see org.onap.aaf.cadi.taf.TafResp#isFailedAttempt()
148      */
149     public boolean isFailedAttempt() {
150         return false;
151     }
152
153     @Override
154     public float timing() {
155         return timing;
156     }
157     
158     @Override
159     public void timing(final long start) {
160         timing = Timing.millis(start);
161     }
162
163     @Override
164     public String taf() {
165         return tafName;
166     }
167
168 }