AT&T 2.0.19 Code drop, stage 2
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / example / JU_X509Test.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
4  * * ===========================================================================
5  * * Copyright © 2017 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.example;
23
24 import java.security.Principal;
25
26 import org.onap.aaf.cadi.PropAccess;
27 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
28 import org.onap.aaf.cadi.aaf.v2_0.AAFLurPerm;
29 import org.onap.aaf.cadi.client.Future;
30 import org.onap.aaf.cadi.locator.DNSLocator;
31 import org.onap.aaf.cadi.lur.LocalPermission;
32
33 public class JU_X509Test {
34         public static void main(String args[]) {
35                 // Link or reuse to your Logging mechanism
36                 
37                 PropAccess myAccess = new PropAccess();
38                 
39                 // 
40                 try {
41                         AAFConHttp con = new AAFConHttp(myAccess, 
42                                         new DNSLocator(myAccess,"https","mithrilcsp.sbc.com","8100"));
43                         
44                         // AAFLur has pool of DME clients as needed, and Caches Client lookups
45                         AAFLurPerm aafLur = con.newLur();
46                         
47                         // Note: If you need both Authn and Authz construct the following:
48 //                      AAFAuthn<?> aafAuthn = con.newAuthn(aafLur);
49                         
50                         // con.x509Alias("aaf.att"); // alias in keystore
51
52                         try {
53                                 
54                                 // Normally, you obtain Principal from Authentication System.
55 //                              // For J2EE, you can ask the HttpServletRequest for getUserPrincipal()
56 //                              // If you use CADI as Authenticator, it will get you these Principals from
57 //                              // CSP or BasicAuth mechanisms.
58 //                              String id = "cluster_admin@gridcore.att.com";
59 //
60 //                              // If Validate succeeds, you will get a Null, otherwise, you will a String for the reason.
61                                 Future<String> fs = 
62                                                 con.client("2.0").read("/authz/perms/com.att.aaf.ca","application/Perms+json");
63                                 if(fs.get(3000)) {
64                                         System.out.println(fs.value);
65                                 } else {
66                                         System.out.println("Error: "  + fs.code() + ':' + fs.body());
67                                 }
68                                 
69                                 // Check on Perms with LUR
70                                 if(aafLur.fish(new Principal() {
71                                         @Override
72                                         public String getName() {
73                                                 return "m12345@aaf.att.com";
74                                         }
75                                 }, new LocalPermission("org.osaaf.aaf.ca|aaf|request"))) {
76                                         System.out.println("Has Perm");
77                                 } else {
78                                         System.out.println("Does NOT Have Perm");
79                                 }
80                         } finally {
81                                 aafLur.destroy();
82                         }
83                 } catch (Exception e) {
84                         e.printStackTrace();
85                 }
86
87         }
88 }