d772d4935be402b22e4738890184b08777e76f49
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / taf / EpiTaf.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.CadiException;
25 import org.onap.aaf.cadi.Taf;
26
27 /**
28  * EpiTAF
29  * 
30  * Short for "Epic TAF". Be able to run through a series of TAFs to obtain the validation needed.
31  * 
32  * OK, the name could probably be better as "Tafs", like it was originally, but the pun was too
33  * irresistible for this author to pass up.
34  * 
35  * @author Jonathan
36  *
37  */
38 public class EpiTaf implements Taf {
39         private Taf[] tafs;
40         
41         /**
42          * EpiTaf constructor
43          * 
44          * Construct the EpiTaf from variable TAF parameters
45          * @param tafs
46          * @throws CadiException
47          */
48         public EpiTaf(Taf ... tafs) throws CadiException{
49                 this.tafs = tafs;
50                 if(tafs.length==0) throw new CadiException("Need at least one Taf implementation in constructor");
51         }
52
53         /**
54          * validate
55          * 
56          * Respond with the first TAF to authenticate user based on variable info and "LifeForm" (is it 
57          * a human behind an interface, or a server behind a protocol).
58          * 
59          * If there is no TAF that can authenticate, respond with the first TAF that suggests it can
60          * establish an Authentication conversation (TRY_AUTHENTICATING).
61          * 
62          * If no TAF declares either, respond with NullTafResp (which denies all questions)
63          */
64         public TafResp validate(LifeForm reading, String... info) {
65                 TafResp tresp,firstTryAuth=null;
66                 for(Taf taf : tafs) {
67                         tresp = taf.validate(reading, info);
68                         switch(tresp.isAuthenticated()) {
69                                 case TRY_ANOTHER_TAF:
70                                         break;
71                                 case TRY_AUTHENTICATING:
72                                         if(firstTryAuth==null)firstTryAuth=tresp;
73                                         break;
74                                 default:
75                                         return tresp;
76                         }
77                 }
78
79                 // No TAFs configured, at this point.  It is safer at this point to be "not validated", 
80                 // rather than "let it go"
81                 return firstTryAuth == null?NullTafResp.singleton():firstTryAuth;
82         }
83
84 }