Remove Tabs, per Jococo
[aaf/authz.git] / cadi / oauth-enduser / src / test / java / org / onap / aaf / cadi / enduser / test / OAuthExample.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.enduser.test;
23
24 import java.io.IOException;
25 import java.net.ConnectException;
26 import java.security.GeneralSecurityException;
27 import java.util.Date;
28 import java.util.GregorianCalendar;
29 import java.util.Map;
30
31 import org.onap.aaf.cadi.Access.Level;
32 import org.onap.aaf.cadi.CadiException;
33 import org.onap.aaf.cadi.LocatorException;
34 import org.onap.aaf.cadi.PropAccess;
35 import org.onap.aaf.cadi.client.Future;
36 import org.onap.aaf.cadi.client.Rcli;
37 import org.onap.aaf.cadi.client.Result;
38 import org.onap.aaf.cadi.client.Retryable;
39 import org.onap.aaf.cadi.config.Config;
40 import org.onap.aaf.cadi.configure.Agent;
41 import org.onap.aaf.cadi.oauth.TimedToken;
42 import org.onap.aaf.cadi.oauth.TokenClient;
43 import org.onap.aaf.cadi.oauth.TokenClientFactory;
44 import org.onap.aaf.cadi.oauth.TzClient;
45 import org.onap.aaf.cadi.util.FQI;
46 import org.onap.aaf.misc.env.APIException;
47 import org.onap.aaf.misc.env.util.Chrono;
48
49 import aafoauth.v2_0.Introspect;
50 import aafoauth.v2_0.Token;
51
52
53 public class OAuthExample {
54     private static TokenClientFactory tcf;
55     private static PropAccess access;
56
57     public final static void main(final String args[]) {
58         // These Objects are expected to be Long-Lived... Construct once
59         
60         // Property Access
61             // This method will allow you to set "cadi_prop_files" (or any other property) on Command line 
62         access = new PropAccess(args);
63         
64             // access = PropAccess();
65             // Note: This style will load "cadi_prop_files" from VM Args
66         
67         // Token aware Client Factory
68         try {
69             tcf = TokenClientFactory.instance(access);
70         } catch (APIException | GeneralSecurityException | IOException | CadiException e1) {
71             access.log(e1, "Unable to setup OAuth Client Factory, Fail Fast");
72             System.exit(1);
73         }
74         
75         
76         // Obtain Endpoints for OAuth2 from Properties.  Expected is "cadi.properties" file, pointed to by "cadi_prop_files"
77         try {
78             Map<String, String> aaf_urls = Agent.loadURLs(access);
79             Agent.fillMissing(access, aaf_urls);
80             String tokenServiceURL = access.getProperty(Config.AAF_OAUTH2_TOKEN_URL); // Default to AAF
81             String tokenIntrospectURL = access.getProperty(Config.AAF_OAUTH2_INTROSPECT_URL); // Default to AAF);
82             // Get Hello Service
83             final String endServicesURL = access.getProperty(Config.AAF_OAUTH2_HELLO_URL);
84     
85             final int CALL_TIMEOUT = Integer.parseInt(access.getProperty(Config.AAF_CALL_TIMEOUT,Config.AAF_CALL_TIMEOUT_DEF));
86         
87             //////////////////////////////////////////////////////////////////////
88             // Scenario 1:
89             // Get and use an OAuth Client, which understands Token Management
90             //////////////////////////////////////////////////////////////////////
91             // Create a Token Client, that gets its tokens from expected OAuth Server
92             //   In this example, it is AAF, but it can be the Alternate OAuth
93
94             TokenClient tc = tcf.newClient(tokenServiceURL); // can set your own timeout here (url, timeoutMilliseconds)
95             // Set your Application (MicroService, whatever) Credentials here
96             //   These are how your Application is known, particularly to the OAuth Server. 
97             //   If AAF Token server, then its just the same as your other AAF MechID creds
98             //   If it is the Alternate OAUTH, you'll need THOSE credentials.  See that tool's Onboarding procedures.
99             String client_id = access.getProperty(Config.AAF_APPID);
100             if (client_id==null) {
101                 // For AAF, client_id CAN be Certificate.  This is not necessarily true elsewhere
102                 client_id = access.getProperty(Config.CADI_ALIAS);
103             }
104             String client_secret = access.getProperty(Config.AAF_APPPASS);
105             tc.client_creds(client_id, client_secret);
106             
107             // If you are working with Credentials the End User, set username/password as appropriate to the OAuth Server
108             // tc.password(end_user_id, end_user_password);
109             // IMPORTANT:
110             //   if you are setting client Credentials, you MAY NOT reuse this Client mid-transaction.  You CAN reuse after setting
111             //  tc.clearEndUser();
112             // You may want to see "Pooled Client" example, using special CADI utility
113
114             // With AAF, the Scopes you put in are the AAF Namespaces you want access to.  Your Token will contain the
115             // AAF Permissions of the Namespaces (you can put in more than one), the user name (or client_id if no user_name),
116             // is allowed to see.
117             
118             // Here's a trick to get the namespace out of a Fully Qualified AAF Identity (your MechID)
119             String ns = FQI.reverseDomain(client_id);
120             System.out.printf("\nNote: The AAF Namespace of FQI (Fully Qualified Identity) %s is %s\n\n",client_id, ns);
121
122             // Now, we can get a Token.  Note: for "scope", use AAF Namespaces to get AAF Permissions embedded in
123             // Note: getToken checks if Token is expired, if so, then refreshes before handing back.
124             Result<TimedToken> rtt = tc.getToken(ns,"org.onap.test");
125             
126             // Note: you can clear a Token's Disk/Memory presence by
127             //  1) removing the Token from the "token/outgoing" directory on the O/S
128             //  2) programmatically by calling "clearToken" with exact params as "getToken", when it has the same credentials set
129             //       tc.clearToken("org.onap.aaf","org.onap.test");
130             
131             // Result Object can be queried for success
132             if (rtt.isOK()) {
133                 TimedToken token = rtt.value;
134                 print(token); // Take a look at what's in a Token
135                 
136                 // Use this Token in your client calls with "Tokenized Client" (TzClient)
137                 // These should NOT be used cross thread.
138                 TzClient helloClient = tcf.newTzClient(endServicesURL);
139                 helloClient.setToken(client_id, token);
140                 
141                 // This client call style, "best" call with "Retryable" inner class covers finding an available Service 
142                 // (when Multi-services exist) for the best service, based (currently) on distance.
143                 //
144                 // the "Generic" in Type gives a Return Value for the Code, which you can set on the "best" method
145                 // Note that variables used in the inner class from this part of the code must be "final", see "CALL_TIMEOUT"
146                 String rv = helloClient.best(new Retryable<String>() {
147                     @Override
148                     public String code(Rcli<?> client) throws CadiException, ConnectException, APIException {
149                         Future<String> future = client.read("hello","text/plain");
150                         // The "future" calling method allows you to do other processing, such as call more than one backend
151                         // client before picking up the result
152                         // If "get" matches the HTTP Code for the method (i.e. read HTTP Return value is 200), then 
153                         if (future.get(CALL_TIMEOUT)) {
154                             // Client Returned expected value
155                             return future.value;
156                         } else {
157                             throw new APIException(future.code()  + future.body());
158                         }                    
159                     }
160                 });
161                 
162                 // You want to do something with returned value.  Here, we say "hello"
163                 System.out.printf("\nPositive Response from Hello: %s\n",rv);
164                 
165                 
166                 //////////////////////////////////////////////////////////////////////
167                 // Scenario 2:
168                 // As a Service, read Introspection information as proof of Authenticated Authorization
169                 //////////////////////////////////////////////////////////////////////
170                 // CADI Framework (i.e. CadiFilter) works with the Introspection to drive the J2EE interfaces (
171                 // i.e. if (isUserInRole("ns.perm|instance|action")) {...
172                 //
173                 // Here, however, is a way to introspect via Java
174                 //
175                 // now, call Introspect (making sure right URLs are set in properties)
176                 // We need a Different Introspect TokenClient, because different Endpoint (and usually different Services)
177                 TokenClient tci = tcf.newClient(tokenIntrospectURL);
178                 tci.client_creds(client_id, client_secret);
179                 Result<Introspect> is = tci.introspect(token.getAccessToken());
180                 if (is.isOK()) {
181                     // Note that AAF will add JSON set of Permissions as part of "Content:", legitimate extension of OAuth Structure
182                     print(is.value); // do something with Introspect Object
183                 } else {
184                     access.printf(Level.ERROR, "Unable to introspect OAuth Token %s: %d %s\n",
185                             token.getAccessToken(),rtt.code,rtt.error);
186                 }
187             } else {
188                 access.printf(Level.ERROR, "Unable to obtain OAuth Token: %d %s\n",rtt.code,rtt.error);
189             }
190             
191         } catch (CadiException | LocatorException | APIException | IOException e) {
192             e.printStackTrace();
193         }
194     }
195     
196     /////////////////////////////////////////////////////////////
197     // Examples of Object Access
198     /////////////////////////////////////////////////////////////
199     private static void print(Token t) {
200         GregorianCalendar exp_date = new GregorianCalendar();
201         exp_date.add(GregorianCalendar.SECOND, t.getExpiresIn());
202         System.out.printf("Access Token\n\tToken:\t\t%s\n\tToken Type:\t%s\n\tExpires In:\t%d (%s)\n\tScope:\t\t%s\n\tRefresh Token:\t%s\n",
203         t.getAccessToken(),
204         t.getTokenType(),
205         t.getExpiresIn(),
206         Chrono.timeStamp(new Date(System.currentTimeMillis()+(t.getExpiresIn()*1000))),
207         t.getScope(),
208         t.getRefreshToken());
209     }
210     
211     private static void print(Introspect ti) {
212         if (ti==null || ti.getClientId()==null) {
213             System.out.println("Empty Introspect");
214             return;
215         }
216         Date exp = new Date(ti.getExp()*1000); // seconds
217         System.out.printf("Introspect\n"
218                 + "\tAccessToken:\t%s\n"
219                 + "\tClient-id:\t%s\n"
220                 + "\tClient Type:\t%s\n"
221                 + "\tActive:  \t%s\n"
222                 + "\tUserName:\t%s\n"
223                 + "\tExpires: \t%d (%s)\n"
224                 + "\tScope:\t\t%s\n"
225                 + "\tContent:\t%s\n",
226         ti.getAccessToken(),
227         ti.getClientId(),
228         ti.getClientType(),
229         ti.isActive()?Boolean.TRUE.toString():Boolean.FALSE.toString(),
230         ti.getUsername(),
231         ti.getExp(),
232         Chrono.timeStamp(exp),
233         ti.getScope(),
234         ti.getContent()==null?"":ti.getContent());
235         
236         System.out.println();
237     }
238
239 }