Remove Tabs, per Jococo
[aaf/authz.git] / cadi / oauth-enduser / src / test / java / org / onap / aaf / cadi / enduser / test / OnapClientExample.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 OnapClientExample {
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         // Note: This style will load "cadi_prop_files" from VM Args
64         // access = PropAccess();
65         try {
66             Map<String, String> aaf_urls = Agent.loadURLs(access);
67             Agent.fillMissing(access, aaf_urls);
68         
69             // Token aware Client Factory
70             tcf = TokenClientFactory.instance(access);
71         } catch (APIException | GeneralSecurityException | IOException | CadiException e1) {
72             access.log(e1, "Unable to setup OAuth Client Factory, Fail Fast");
73             System.exit(1);
74         }
75         
76         final int CALL_TIMEOUT = Integer.parseInt(access.getProperty(Config.AAF_CALL_TIMEOUT,Config.AAF_CALL_TIMEOUT_DEF));
77         
78         try {
79             //////////////////////////////////////////////////////////////////////
80             // Scenario 1:
81             // Get and use an OAuth Client, which understands Token Management
82             //////////////////////////////////////////////////////////////////////
83             // Create a Token Client, that gets its tokens from expected OAuth Server
84             //   In this example, it is AAF, but it can be the Alternate OAuth
85
86             TokenClient tc = tcf.newClient(Config.AAF_OAUTH2_TOKEN_URL); // can set your own timeout here (url, timeoutMilliseconds)
87             
88             // Here's a trick to get the namespace out of a Fully Qualified AAF Identity (your MechID)
89             String ns = FQI.reverseDomain(tc.client_id());
90             System.out.printf("\nNote: The AAF Namespace of FQI (Fully Qualified Identity) %s is %s\n\n",tc.client_id(), ns);
91
92             // Now, we can get a Token.  Note: for "scope", use AAF Namespaces to get AAF Permissions embedded in
93             // Note: getToken checks if Token is expired, if so, then refreshes before handing back.
94             Result<TimedToken> rtt = tc.getToken(ns,"org.onap.test"); // get multiple scopes
95             
96             // Note: you can clear a Token's Disk/Memory presence by
97             //  1) removing the Token from the "token/outgoing" directory on the O/S
98             //  2) programmatically by calling "clearToken" with exact params as "getToken", when it has the same credentials set
99             //       tc.clearToken("org.onap.aaf","org.onap.test");
100             
101             // Result Object can be queried for success
102             if (rtt.isOK()) {
103                 TimedToken token = rtt.value;
104                 print(token); // Take a look at what's in a Token
105                 
106                 // Use this Token in your client calls with "Tokenized Client" (TzClient)
107                 // These should NOT be used cross thread.
108                 // Get Hello Service URL... roll your own in your own world.
109                 final String endServicesURL = access.getProperty(Config.AAF_OAUTH2_HELLO_URL);
110
111
112                 TzClient helloClient = tcf.newTzClient(endServicesURL);
113                 helloClient.setToken(tc.client_id(), token);
114                 
115                 // This client call style, "best" call with "Retryable" inner class covers finding an available Service 
116                 // (when Multi-services exist) for the best service, based (currently) on distance.
117                 //
118                 // the "Generic" in Type gives a Return Value for the Code, which you can set on the "best" method
119                 // Note that variables used in the inner class from this part of the code must be "final", see "CALL_TIMEOUT"
120                 String rv = helloClient.best(new Retryable<String>() {
121                     @Override
122                     public String code(Rcli<?> client) throws CadiException, ConnectException, APIException {
123                         Future<String> future = client.read("hello","text/plain");
124                         // The "future" calling method allows you to do other processing, such as call more than one backend
125                         // client before picking up the result
126                         // If "get" matches the HTTP Code for the method (i.e. read HTTP Return value is 200), then 
127                         if (future.get(CALL_TIMEOUT)) {
128                             // Client Returned expected value
129                             return future.value;
130                         } else {
131                             throw new APIException(future.code()  + future.body());
132                         }                    
133                     }
134                 });
135                 
136                 // You want to do something with returned value.  Here, we say "hello"
137                 System.out.printf("\nPositive Response from Hello: %s\n",rv);
138                 
139                 
140                 //////////////////////////////////////////////////////////////////////
141                 // Scenario 2:
142                 // As a Service, read Introspection information as proof of Authenticated Authorization
143                 //////////////////////////////////////////////////////////////////////
144                 // CADI Framework (i.e. CadiFilter) works with the Introspection to drive the J2EE interfaces (
145                 // i.e. if (isUserInRole("ns.perm|instance|action")) {...
146                 //
147                 // Here, however, is a way to introspect via Java
148                 //
149                 // now, call Introspect (making sure right URLs are set in properties)
150                 // We need a Different Introspect TokenClient, because different Endpoint (and usually different Services)
151                 TokenClient tci = tcf.newClient(Config.AAF_OAUTH2_INTROSPECT_URL);
152                 Result<Introspect> is = tci.introspect(token.getAccessToken());
153                 if (is.isOK()) {
154                     // Note that AAF will add JSON set of Permissions as part of "Content:", legitimate extension of OAuth Structure
155                     print(is.value); // do something with Introspect Object
156                 } else {
157                     access.printf(Level.ERROR, "Unable to introspect OAuth Token %s: %d %s\n",
158                             token.getAccessToken(),rtt.code,rtt.error);
159                 }
160             } else {
161                 access.printf(Level.ERROR, "Unable to obtain OAuth Token: %d %s\n",rtt.code,rtt.error);
162             }
163             
164         } catch (CadiException | LocatorException | APIException | IOException e) {
165             e.printStackTrace();
166         }
167     }
168     
169     /////////////////////////////////////////////////////////////
170     // Examples of Object Access
171     /////////////////////////////////////////////////////////////
172     private static void print(Token t) {
173         GregorianCalendar exp_date = new GregorianCalendar();
174         exp_date.add(GregorianCalendar.SECOND, t.getExpiresIn());
175         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",
176         t.getAccessToken(),
177         t.getTokenType(),
178         t.getExpiresIn(),
179         Chrono.timeStamp(new Date(System.currentTimeMillis()+(t.getExpiresIn()*1000))),
180         t.getScope(),
181         t.getRefreshToken());
182     }
183     
184     private static void print(Introspect ti) {
185         if (ti==null || ti.getClientId()==null) {
186             System.out.println("Empty Introspect");
187             return;
188         }
189         Date exp = new Date(ti.getExp()*1000); // seconds
190         System.out.printf("Introspect\n"
191                 + "\tAccessToken:\t%s\n"
192                 + "\tClient-id:\t%s\n"
193                 + "\tClient Type:\t%s\n"
194                 + "\tActive:  \t%s\n"
195                 + "\tUserName:\t%s\n"
196                 + "\tExpires: \t%d (%s)\n"
197                 + "\tScope:\t\t%s\n"
198                 + "\tContent:\t%s\n",
199         ti.getAccessToken(),
200         ti.getClientId(),
201         ti.getClientType(),
202         ti.isActive()?Boolean.TRUE.toString():Boolean.FALSE.toString(),
203         ti.getUsername(),
204         ti.getExp(),
205         Chrono.timeStamp(exp),
206         ti.getScope(),
207         ti.getContent()==null?"":ti.getContent());
208         
209         System.out.println();
210     }
211
212 }