2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.cadi.lur.aaf.test1;
24 import java.security.Principal;
25 import java.util.ArrayList;
26 import java.util.List;
28 import org.onap.aaf.cadi.Permission;
29 import org.onap.aaf.cadi.PropAccess;
30 import org.onap.aaf.cadi.aaf.AAFPermission;
31 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;
32 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
33 import org.onap.aaf.cadi.aaf.v2_0.AAFLurPerm;
34 import org.onap.aaf.cadi.config.Config;
35 import org.onap.aaf.cadi.locator.PropertyLocator;
36 import org.onap.aaf.stillNeed.TestPrincipal;
38 public class MultiThreadPermHit {
39 public static void main(String args[]) {
40 // Link or reuse to your Logging mechanism
41 PropAccess myAccess = new PropAccess(); //
45 AAFConHttp con = new AAFConHttp(myAccess,new PropertyLocator("https://mithrilcsp.sbc.com:8100"));
47 // AAFLur has pool of DME clients as needed, and Caches Client lookups
48 final AAFLurPerm aafLur = con.newLur();
49 aafLur.setDebug("m12345@aaf.att.com");
51 // Note: If you need both Authn and Authz construct the following:
52 AAFAuthn<?> aafAuthn = con.newAuthn(aafLur);
54 // Do not set Mech ID until after you construct AAFAuthn,
55 // because we initiate "401" info to determine the Realm of
56 // of the service we're after.
57 final String id = myAccess.getProperty(Config.AAF_APPID,null);
58 final String pass = myAccess.decrypt(myAccess.getProperty(Config.AAF_APPPASS,null),false);
59 if(id!=null && pass!=null) {
62 // Normally, you obtain Principal from Authentication System.
63 // // For J2EE, you can ask the HttpServletRequest for getUserPrincipal()
64 // // If you use CADI as Authenticator, it will get you these Principals from
65 // // CSP or BasicAuth mechanisms.
66 // String id = "cluster_admin@gridcore.att.com";
68 // // If Validate succeeds, you will get a Null, otherwise, you will a String for the reason.
70 ok = aafAuthn.validate(id, pass,null /* use AuthzTrans or HttpServlet, if you have it */);
72 System.out.println(ok);
75 List<Permission> pond = new ArrayList<Permission>();
76 for(int i=0;i<20;++i) {
78 aafLur.fishAll(new TestPrincipal(i+id), pond);
79 if(ok!=null && i%1000==0) {
80 System.out.println(i + " " + ok);
84 for(int i=0;i<1000000;++i) {
85 ok = aafAuthn.validate( i+ id, "wrongPass",null /* use AuthzTrans or HttpServlet, if you have it */);
86 if(ok!=null && i%1000==0) {
87 System.out.println(i + " " + ok);
91 final AAFPermission perm = new AAFPermission("org.osaaf.aaf.access","*","*");
93 // Now you can ask the LUR (Local Representative of the User Repository about Authorization
94 // With CADI, in J2EE, you can call isUserInRole("org.osaaf.mygroup|mytype|write") on the Request Object
95 // instead of creating your own LUR
96 final Principal p = new TestPrincipal(id);
97 for(int i=0;i<4;++i) {
98 if(aafLur.fish(p, perm)) {
99 System.out.println("Yes, " + id + " has permission for " + perm.getKey());
101 System.out.println("No, " + id + " does not have permission for " + perm.getKey());
106 // Or you can all for all the Permissions available
107 List<Permission> perms = new ArrayList<Permission>();
110 aafLur.fishAll(p,perms);
111 System.out.println("Perms for " + id);
112 for(Permission prm : perms) {
113 System.out.println(prm.getKey());
116 System.out.println("Press any key to continue");
119 for(int j=0;j<5;++j) {
120 new Thread(new Runnable() {
123 for(int i=0;i<20;++i) {
124 if(aafLur.fish(p, perm)) {
125 System.out.println("Yes, " + id + " has permission for " + perm.getKey());
127 System.out.println("No, " + id + " does not have permission for " + perm.getKey());
138 } else { // checked on IDs
139 System.err.println(Config.AAF_APPID + " and/or " + Config.AAF_APPPASS + " are not set.");
141 } catch (Exception e) {