cc458f288ed5be1363731bfd2d7d0299cdc1a498
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / oauth / test / JU_OAuthTest.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.oauth.test;
23
24 import java.net.ConnectException;
25 import java.util.Date;
26 import java.util.GregorianCalendar;
27
28 import org.junit.After;
29 import org.junit.AfterClass;
30 import org.junit.Before;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.onap.aaf.cadi.CadiException;
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.oauth.TimedToken;
41 import org.onap.aaf.cadi.oauth.TokenClient;
42 import org.onap.aaf.cadi.oauth.TokenClientFactory;
43 import org.onap.aaf.cadi.oauth.TzClient;
44 import org.onap.aaf.cadi.principal.Kind;
45 import org.onap.aaf.misc.env.APIException;
46 import org.onap.aaf.misc.env.util.Chrono;
47
48 import aafoauth.v2_0.Introspect;
49 import aafoauth.v2_0.Token;
50 import junit.framework.Assert;
51
52 public class JU_OAuthTest {
53
54         private static PropAccess access;
55         private static TokenClientFactory tcf;
56
57         @BeforeClass
58         public static void setUpBeforeClass()  {
59                 access = new PropAccess();
60                 try {
61                         tcf = TokenClientFactory.instance(access);
62                 } catch (Exception e) {
63                         e.printStackTrace();
64                         Assert.fail();
65                 }
66         }
67
68         @AfterClass
69         public static void tearDownAfterClass() throws Exception {
70         }
71
72         @Before
73         public void setUp() throws Exception {
74         }
75
76         @After
77         public void tearDown() throws Exception {
78         }
79
80         @Test
81         public void testROPCFlowHappy() {
82                 try {
83                         // AAF OAuth
84                         String client_id = access.getProperty(Config.AAF_APPID);
85                         String client_secret = access.getProperty(Config.AAF_APPPASS);
86                         String tokenServiceURL = access.getProperty(Config.AAF_OAUTH2_TOKEN_URL);
87                         Assert.assertNotNull(tokenServiceURL);
88                         String tokenIntrospectURL = access.getProperty(Config.AAF_OAUTH2_INTROSPECT_URL);
89                         String tokenAltIntrospectURL = access.getProperty(Config.AAF_ALT_OAUTH2_INTROSPECT_URL);
90                         Assert.assertNotNull(tokenIntrospectURL);
91                         final String endServicesURL = access.getProperty(Config.AAF_OAUTH2_HELLO_URL);
92                         String username = access.getProperty("cadi_username");
93
94                         TokenClient tc;
95                         Result<TimedToken> rtt;
96                         if(true) {
97                                 tc = tcf.newClient(tokenServiceURL, 3000);
98                                 tc.client_creds(client_id,client_secret);
99                                 tc.password(access.getProperty("cadi_username"),access.getProperty("cadi_password"));
100                                 rtt = tc.getToken(Kind.BASIC_AUTH,"org.osaaf.aaf","org.osaaf.test");
101                                 if(rtt.isOK()) {
102                                         print(rtt.value);
103                                         rtt = tc.refreshToken(rtt.value);
104                                         if(rtt.isOK()) {
105                                                 print(rtt.value);
106                                                 TokenClient ic = tcf.newClient(tokenIntrospectURL,3000);
107                                                 ic.client_creds(client_id,client_secret);
108
109                                                 Result<Introspect> ri = ic.introspect(rtt.value.getAccessToken());
110                                                 if(ri.isOK()) {
111                                                         print(ri.value);
112                                                 } else {
113                                                         System.out.println(ri.code + ' ' + ri.error);
114                                                         Assert.fail(ri.code + ' ' + ri.error);
115                                                 }
116                                                 TzClient helloClient = tcf.newTzClient(endServicesURL);
117                                                 helloClient.setToken(client_id, rtt.value);
118 //                                              String rv = serviceCall(helloClient);
119 //                                              System.out.println(rv);
120                 //                              Assert.assertEquals("Hello AAF OAuth2\n",rv);
121                                         } else {
122                                                 System.out.println(rtt.code + ' ' + rtt.error);
123                                                 Assert.fail(rtt.code + ' ' + rtt.error);
124                                         }
125                                 } else {
126                                         System.out.println(rtt.code + ' ' + rtt.error);
127                                         Assert.fail(rtt.code + ' ' + rtt.error);
128                                 }
129                         }
130                 
131                         // ISAM Test
132                         if(true) {
133                                 System.out.println("**** ISAM TEST ****");
134                                 tokenServiceURL=access.getProperty(Config.AAF_ALT_OAUTH2_TOKEN_URL);
135                                 client_id=access.getProperty(Config.AAF_ALT_CLIENT_ID);
136                                 client_secret=access.getProperty(Config.AAF_ALT_CLIENT_SECRET);
137                                 if(tokenServiceURL!=null) {
138                                         tc = tcf.newClient(tokenServiceURL, 3000);
139                                         tc.client_creds(client_id, client_secret);
140                                         int at = username.indexOf('@');
141                                         
142                                         tc.password(at>=0?username.substring(0, at):username,access.getProperty("cadi_password"));
143                                         rtt = tc.getToken("org.osaaf.aaf","org.osaaf.test");
144                                         if(rtt.isOK()) {
145                                                 print(rtt.value);
146                                                 rtt = tc.refreshToken(rtt.value);
147                                                 if(rtt.isOK()) {
148                                                         print(rtt.value);
149                                                         
150                                                         tc = tcf.newClient(tokenAltIntrospectURL, 3000);
151                                                         tc.client_creds(client_id, client_secret);
152                                                         Result<Introspect> rti = tc.introspect(rtt.value.getAccessToken());
153                                                         if(rti.isOK()) {
154                                                                 System.out.print("Normal ISAM ");
155                                                                 print(rti.value);
156                                                         } else {
157                                                                 System.out.println(rti.code + ' ' + rti.error);
158                                                                 Assert.fail(rtt.code + ' ' + rtt.error);
159                                                         }
160
161                                                         tc = tcf.newClient(tokenIntrospectURL, 3000);
162                                                         tc.client_creds(client_id, client_secret);
163                                                         rti = tc.introspect(rtt.value.getAccessToken());
164                                                         if(rti.isOK()) {
165                                                                 System.out.print("AAF with ISAM Token ");
166                                                                 print(rti.value);
167                                                         } else {
168                                                                 System.out.println(rti.code + ' ' + rti.error);
169                                                                 if(rti.code!=404) {
170                                                                         Assert.fail(rti.code + ' ' + rti.error);
171                                                                 }
172                                                         }
173
174                                                         TzClient tzClient = tcf.newTzClient(endServicesURL);
175                                                         tzClient.setToken(client_id, rtt.value);
176                                                         // Note: this is AAF's "Hello" server
177                                                         String rv = serviceCall(tzClient);
178                                                         System.out.println(rv);
179                         //                              Assert.assertEquals("Hello AAF OAuth2\n",rv);
180                                                 } else {
181                                                         System.out.println(rtt.code + ' ' + rtt.error);
182                                                         Assert.fail(rtt.code + ' ' + rtt.error);
183                                                 }
184                                         } else {
185                                                 System.out.println(rtt.code + ' ' + rtt.error);
186                                                 Assert.fail(rtt.code + ' ' + rtt.error);
187                                         }
188                                 } else {
189                                         Assert.fail(Config.AAF_ALT_OAUTH2_TOKEN_URL + " is required");
190                                 }
191                         }
192                 } catch (Exception e) {
193                         e.printStackTrace();
194                         Assert.fail();
195                 }
196         }
197         
198         
199         private TokenClient testROPCFlow(final String url, final String client_id, final String client_secret, String user, String password, final String ... scope) throws Exception {
200                 TokenClient tclient = tcf.newClient(url,3000);
201                 tclient.client_creds(client_id, client_secret);
202                 if(user!=null && password!=null) {
203                         tclient.password(user,password);
204                 }
205                 Result<TimedToken> rt = tclient.getToken(scope);
206                 if(rt.isOK()) {
207                         print(rt.value);
208                         Result<Introspect> rti = tclient.introspect(rt.value.getAccessToken());
209                         if(rti.isOK()) {
210                                 print(rti.value);
211                         } else {
212                                 printAndFail(rti);
213                         }
214                 } else {
215                         printAndFail(rt);
216                 }
217                 return tclient;
218         }
219         
220         private String serviceCall(TzClient tzClient) throws Exception {
221                 return tzClient.best(new Retryable<String>() {
222                         @Override
223                         public String code(Rcli<?> client) throws CadiException, ConnectException, APIException {
224                                 Future<String> future = client.read(null,"text/plain");
225                                 if(future.get(3000)) {
226                                         return future.value;
227                                 } else {
228                                         throw new APIException(future.code()  + future.body());
229                                 }
230                         }
231                 });
232         }
233         private void printAndFail(Result<?> rt) {
234                 System.out.printf("HTTP Code %d: %s\n", rt.code, rt.error);
235                 Assert.fail(rt.toString());
236         }
237
238         private void print(Token t) {
239                 GregorianCalendar exp_date = new GregorianCalendar();
240                 exp_date.add(GregorianCalendar.SECOND, t.getExpiresIn());
241                 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",
242                 t.getAccessToken(),
243                 t.getTokenType(),
244                 t.getExpiresIn(),
245                 Chrono.timeStamp(new Date(System.currentTimeMillis()+(t.getExpiresIn()*1000))),
246                 t.getScope(),
247                 t.getRefreshToken());
248         }
249         
250         private void print(Introspect ti) {
251                 if(ti==null || ti.getClientId()==null) {
252                         System.out.println("Empty Introspect");
253                         return;
254                 }
255                 Date exp = new Date(ti.getExp()*1000); // seconds
256                 System.out.printf("Introspect\n"
257                                 + "\tAccessToken:\t%s\n"
258                                 + "\tClient-id:\t%s\n"
259                                 + "\tClient Type:\t%s\n"
260                                 + "\tActive:  \t%s\n"
261                                 + "\tUserName:\t%s\n"
262                                 + "\tExpires: \t%d (%s)\n"
263                                 + "\tScope:\t\t%s\n"
264                                 + "\tContent:\t\t%s\n",
265                 ti.getAccessToken(),
266                 ti.getClientId(),
267                 ti.getClientType(),
268                 ti.isActive()?Boolean.TRUE.toString():Boolean.FALSE.toString(),
269                 ti.getUsername(),
270                 ti.getExp(),
271                 Chrono.timeStamp(exp),
272                 ti.getScope(),
273                 ti.getContent()==null?"":ti.getContent());
274                 
275                 System.out.println();
276         }
277 }