45a7d341572bfe73987d89c06fdc1863e03ce0b1
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / client / sample / Sample.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.client.sample;
23
24 import java.io.IOException;
25 import java.security.Principal;
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import org.onap.aaf.cadi.Access;
30 import org.onap.aaf.cadi.CadiException;
31 import org.onap.aaf.cadi.LocatorException;
32 import org.onap.aaf.cadi.Permission;
33 import org.onap.aaf.cadi.PropAccess;
34 import org.onap.aaf.cadi.aaf.AAFPermission;
35 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;
36 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
37 import org.onap.aaf.cadi.aaf.v2_0.AAFLurPerm;
38 import org.onap.aaf.cadi.principal.UnAuthPrincipal;
39 import org.onap.aaf.cadi.util.Split;
40 import org.onap.aaf.misc.env.APIException;
41
42 public class Sample {
43         private static Sample singleton;
44         final private AAFConHttp aafcon;
45         final private AAFLurPerm aafLur;
46         final private AAFAuthn<?> aafAuthn;
47         
48         /**
49          * This method is to emphasize the importance of not creating the AAFObjects over and over again.
50          * @return
51          */
52         public static Sample singleton() {
53                 return singleton;
54         }
55
56         public Sample(Access myAccess) throws APIException, CadiException, LocatorException {
57                 aafcon = new AAFConHttp(myAccess);
58                 aafLur = aafcon.newLur();
59                 aafAuthn = aafcon.newAuthn(aafLur);
60         }
61         
62         /**
63          * Checking credentials outside of HTTP/S presents fewer options initially. There is not, for instance,
64          * the option of using 2-way TLS HTTP/S. 
65          *  
66          *  However, Password Checks are still useful, and, if the Client Certificate could be obtained in other ways, the 
67          *  Interface can be expanded in the future to include Certificates.
68          * @throws CadiException 
69          * @throws IOException 
70          */
71         public Principal checkUserPass(String fqi, String pass) throws IOException, CadiException {
72                 String ok = aafAuthn.validate(fqi, pass);
73                 if(ok==null) {
74                         System.out.println("Success!");
75                         /*
76                          UnAuthPrincipal means that it is not coming from the official Authorization chain.
77                          This is useful for Security Plugins which don't use Principal as the tie between
78                          Authentication and Authorization
79                         
80                          You can also use this if you want to check Authorization without actually Authenticating, as may
81                          be the case with certain Onboarding Tooling.
82                         */
83                         return new UnAuthPrincipal(fqi);
84                 } else {
85                         System.out.printf("Failure: %s\n",ok);
86                         return null;
87                 }
88                 
89
90         }
91
92         /**
93          * An example of looking for One Permission within all the permissions user has.  CADI does cache these,
94          * so the call is not expensive.
95          * 
96          * Note: If you are using "J2EE" (Servlets), CADI ties this function to the method: 
97          *    HttpServletRequest.isUserInRole(String user)
98          *    
99          *  The J2EE user can expect that his servlet will NOT be called without a Validated Principal, and that
100          *  "isUserInRole()" will validate if the user has the Permission designated.
101          *  
102          */
103         public boolean oneAuthorization(Principal fqi, Permission p) {
104                 return aafLur.fish(fqi, p);
105         }
106         
107         public List<Permission> allAuthorization(Principal fqi) {
108                 List<Permission> pond = new ArrayList<>();
109                 aafLur.fishAll(fqi, pond);
110                 return pond;
111         }
112         
113         
114         public static void main(String[] args) {
115                 // Note: you can pick up Properties from Command line as well as VM Properties
116                 // Code "user_fqi=... user_pass=..." (where user_pass can be encrypted) in the command line for this sample.
117                 // Also code "perm=<perm type>|<instance>|<action>" to test a specific Permission
118                 PropAccess myAccess = new PropAccess(args); 
119                 try {
120                         /*
121                          * NOTE:  Do NOT CREATE new aafcon, aafLur and aafAuthn each transaction.  They are built to be
122                          * reused!
123                          * 
124                          * This is why this code demonstrates "Sample" as a singleton.
125                          */
126                         singleton = new Sample(myAccess);
127                         String user = myAccess.getProperty("user_fqi");
128                         String pass= myAccess.getProperty("user_pass");
129                         
130                         if(user==null || pass==null) {
131                                 System.err.println("This Sample class requires properties user_fqi and user_pass");
132                         } else {
133                                 pass =  myAccess.decrypt(pass, false); // Note, with "false", decryption will only happen if starts with "enc:"
134                                 // See the CODE for Java Methods used
135                                 Principal fqi = Sample.singleton().checkUserPass(user,pass);
136                                 
137                                 if(fqi==null) {
138                                         System.out.println("OK, normally, you would cease processing for an "
139                                                         + "unauthenticated user, but for the purpose of Sample, we'll keep going.\n");
140                                         fqi=new UnAuthPrincipal(user);
141                                 }
142                                 
143                                 // AGAIN, NOTE: If your client fails Authentication, the right behavior 99.9%
144                                 // of the time is to drop the transaction.  We continue for sample only.
145                                 
146                                 // note, default String for perm
147                                 String permS = myAccess.getProperty("perm","org.osaaf.aaf.access|*|read");
148                                 String[] permA = Split.splitTrim('|', permS);
149                                 if(permA.length>2) {
150                                         final Permission perm = new AAFPermission(permA[0],permA[1],permA[2]);
151                                         // See the CODE for Java Methods used
152                                         if(singleton().oneAuthorization(fqi, perm)) {
153                                                 System.out.printf("Success: %s has %s\n",fqi.getName(),permS);
154                                         } else {
155                                                 System.out.printf("%s does NOT have %s\n",fqi.getName(),permS);
156                                         }
157                                 }
158                                 
159                                 
160                                 // Another form, you can get ALL permissions in a list
161                                 // See the CODE for Java Methods used
162                                 List<Permission> permL = singleton().allAuthorization(fqi);
163                                 if(permL.size()==0) {
164                                         System.out.printf("User %s has no Permissions THAT THE CALLER CAN SEE\n",fqi.getName());
165                                 } else {
166                                         System.out.print("Success:\n");
167                                         for(Permission p : permL) {
168                                                 System.out.printf("\t%s has %s\n",fqi.getName(),p.getKey());
169                                         }
170                                 }
171                         }
172                 } catch (APIException | CadiException | LocatorException | IOException e) {
173                         e.printStackTrace();
174                 }
175         }
176 }