Instrumental | bbe7154 | 2018-05-25 12:29:38 -0500 | [diff] [blame] | 1 | .. This work is licensed under a Creative Commons Attribution 4.0 International License. |
| 2 | .. http://creativecommons.org/licenses/by/4.0 |
| 3 | .. Copyright © 2017 AT&T Intellectual Property. All rights reserved. |
| 4 | |
| 5 | AAF From Source Code |
| 6 | ==================== |
| 7 | |
Sai Gandham | 4e6f7bc | 2018-06-15 08:37:30 -0500 | [diff] [blame] | 8 | Example Source Code |
| 9 | ------------------- |
| 10 | Note the FULL class is available in the authz repo, cadi_aaf/org/onap/aaf/client/sample/Sample.java |
| 11 | |
| 12 | .. code-block:: java |
| 13 | |
| 14 | |
| 15 | /** |
| 16 | * ============LICENSE_START==================================================== |
| 17 | * org.onap.aaf |
| 18 | * =========================================================================== |
| 19 | * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. |
| 20 | * =========================================================================== |
| 21 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 22 | * you may not use this file except in compliance with the License. |
| 23 | * You may obtain a copy of the License at |
| 24 | * |
| 25 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 26 | * |
| 27 | * Unless required by applicable law or agreed to in writing, software |
| 28 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 29 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 30 | * See the License for the specific language governing permissions and |
| 31 | * limitations under the License. |
| 32 | * ============LICENSE_END==================================================== |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | package org.onap.aaf.client.sample; |
| 37 | |
| 38 | import java.io.IOException; |
| 39 | import java.security.Principal; |
| 40 | import java.util.ArrayList; |
| 41 | import java.util.List; |
| 42 | |
| 43 | import org.onap.aaf.cadi.Access; |
| 44 | import org.onap.aaf.cadi.CadiException; |
| 45 | import org.onap.aaf.cadi.LocatorException; |
| 46 | import org.onap.aaf.cadi.Permission; |
| 47 | import org.onap.aaf.cadi.PropAccess; |
| 48 | import org.onap.aaf.cadi.aaf.AAFPermission; |
| 49 | import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn; |
| 50 | import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp; |
| 51 | import org.onap.aaf.cadi.aaf.v2_0.AAFLurPerm; |
| 52 | import org.onap.aaf.cadi.principal.UnAuthPrincipal; |
| 53 | import org.onap.aaf.cadi.util.Split; |
| 54 | import org.onap.aaf.misc.env.APIException; |
| 55 | |
| 56 | public class Sample { |
| 57 | private static Sample singleton; |
| 58 | final private AAFConHttp aafcon; |
| 59 | final private AAFLurPerm aafLur; |
| 60 | final private AAFAuthn<?> aafAuthn; |
| 61 | |
| 62 | /** |
| 63 | * This method is to emphasize the importance of not creating the AAFObjects over and over again. |
| 64 | * @return |
| 65 | */ |
| 66 | public static Sample singleton() { |
| 67 | return singleton; |
| 68 | } |
| 69 | |
| 70 | public Sample(Access myAccess) throws APIException, CadiException, LocatorException { |
| 71 | aafcon = new AAFConHttp(myAccess); |
| 72 | aafLur = aafcon.newLur(); |
| 73 | aafAuthn = aafcon.newAuthn(aafLur); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Checking credentials outside of HTTP/S presents fewer options initially. There is not, for instance, |
| 78 | * the option of using 2-way TLS HTTP/S. |
| 79 | * |
| 80 | * However, Password Checks are still useful, and, if the Client Certificate could be obtained in other ways, the |
| 81 | * Interface can be expanded in the future to include Certificates. |
| 82 | * @throws CadiException |
| 83 | * @throws IOException |
| 84 | */ |
| 85 | public Principal checkUserPass(String fqi, String pass) throws IOException, CadiException { |
| 86 | String ok = aafAuthn.validate(fqi, pass); |
| 87 | if(ok==null) { |
| 88 | System.out.println("Success!"); |
| 89 | /* |
| 90 | UnAuthPrincipal means that it is not coming from the official Authorization chain. |
| 91 | This is useful for Security Plugins which don't use Principal as the tie between |
| 92 | Authentication and Authorization |
| 93 | |
| 94 | You can also use this if you want to check Authorization without actually Authenticating, as may |
| 95 | be the case with certain Onboarding Tooling. |
| 96 | */ |
| 97 | return new UnAuthPrincipal(fqi); |
| 98 | } else { |
| 99 | System.out.printf("Failure: %s\n",ok); |
| 100 | return null; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * An example of looking for One Permission within all the permissions user has. CADI does cache these, |
| 108 | * so the call is not expensive. |
| 109 | * |
| 110 | * Note: If you are using "J2EE" (Servlets), CADI ties this function to the method: |
| 111 | * HttpServletRequest.isUserInRole(String user) |
| 112 | * |
| 113 | * The J2EE user can expect that his servlet will NOT be called without a Validated Principal, and that |
| 114 | * "isUserInRole()" will validate if the user has the Permission designated. |
| 115 | * |
| 116 | */ |
| 117 | public boolean oneAuthorization(Principal fqi, Permission p) { |
| 118 | return aafLur.fish(fqi, p); |
| 119 | } |
| 120 | |
| 121 | public List<Permission> allAuthorization(Principal fqi) { |
| 122 | List<Permission> pond = new ArrayList<Permission>(); |
| 123 | aafLur.fishAll(fqi, pond); |
| 124 | return pond; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | public static void main(String[] args) { |
| 129 | // Note: you can pick up Properties from Command line as well as VM Properties |
| 130 | // Code "user_fqi=... user_pass=..." (where user_pass can be encrypted) in the command line for this sample. |
| 131 | // Also code "perm=<perm type>|<instance>|<action>" to test a specific Permission |
| 132 | PropAccess myAccess = new PropAccess(args); |
| 133 | try { |
| 134 | /* |
| 135 | * NOTE: Do NOT CREATE new aafcon, aafLur and aafAuthn each transaction. They are built to be |
| 136 | * reused! |
| 137 | * |
| 138 | * This is why this code demonstrates "Sample" as a singleton. |
| 139 | */ |
| 140 | singleton = new Sample(myAccess); |
| 141 | String user = myAccess.getProperty("user_fqi"); |
| 142 | String pass= myAccess.getProperty("user_pass"); |
| 143 | |
| 144 | if(user==null || pass==null) { |
| 145 | System.err.println("This Sample class requires properties user_fqi and user_pass"); |
| 146 | } else { |
| 147 | pass = myAccess.decrypt(pass, false); // Note, with "false", decryption will only happen if starts with "enc:" |
| 148 | // See the CODE for Java Methods used |
| 149 | Principal fqi = Sample.singleton().checkUserPass(user,pass); |
| 150 | |
| 151 | if(fqi==null) { |
| 152 | System.out.println("OK, normally, you would cease processing for an " |
| 153 | + "unauthenticated user, but for the purpose of Sample, we'll keep going.\n"); |
| 154 | fqi=new UnAuthPrincipal(user); |
| 155 | } |
| 156 | |
| 157 | // AGAIN, NOTE: If your client fails Authentication, the right behavior 99.9% |
| 158 | // of the time is to drop the transaction. We continue for sample only. |
| 159 | |
| 160 | // note, default String for perm |
| 161 | String permS = myAccess.getProperty("perm","org.osaaf.aaf.access|*|read"); |
| 162 | String[] permA = Split.splitTrim('|', permS); |
| 163 | if(permA.length>2) { |
| 164 | final Permission perm = new AAFPermission(permA[0],permA[1],permA[2]); |
| 165 | // See the CODE for Java Methods used |
| 166 | if(singleton().oneAuthorization(fqi, perm)) { |
| 167 | System.out.printf("Success: %s has %s\n",fqi.getName(),permS); |
| 168 | } else { |
| 169 | System.out.printf("%s does NOT have %s\n",fqi.getName(),permS); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | |
| 174 | // Another form, you can get ALL permissions in a list |
| 175 | // See the CODE for Java Methods used |
| 176 | List<Permission> permL = singleton().allAuthorization(fqi); |
| 177 | if(permL.size()==0) { |
| 178 | System.out.printf("User %s has no Permissions THAT THE CALLER CAN SEE",fqi.getName()); |
| 179 | } else { |
| 180 | System.out.print("Success:\n"); |
| 181 | for(Permission p : permL) { |
| 182 | System.out.printf("\t%s has %s\n",fqi.getName(),p.getKey()); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | } catch (APIException | CadiException | LocatorException | IOException e) { |
| 187 | e.printStackTrace(); |
| 188 | } |
| 189 | } |
| 190 | } |