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.oauth.test;
24 import java.net.ConnectException;
25 import java.util.Date;
26 import java.util.GregorianCalendar;
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;
48 import aafoauth.v2_0.Introspect;
49 import aafoauth.v2_0.Token;
50 import junit.framework.Assert;
52 public class JU_OAuthTest {
54 private static PropAccess access;
55 private static TokenClientFactory tcf;
58 public static void setUpBeforeClass() {
59 access = new PropAccess();
61 tcf = TokenClientFactory.instance(access);
62 } catch (Exception e) {
69 public static void tearDownAfterClass() throws Exception {
73 public void setUp() throws Exception {
77 public void tearDown() throws Exception {
81 public void testROPCFlowHappy() {
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");
95 Result<TimedToken> rtt;
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");
103 rtt = tc.refreshToken(rtt.value);
106 TokenClient ic = tcf.newClient(tokenIntrospectURL,3000);
107 ic.client_creds(client_id,client_secret);
109 Result<Introspect> ri = ic.introspect(rtt.value.getAccessToken());
113 System.out.println(ri.code + ' ' + ri.error);
114 Assert.fail(ri.code + ' ' + ri.error);
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);
122 System.out.println(rtt.code + ' ' + rtt.error);
123 Assert.fail(rtt.code + ' ' + rtt.error);
126 System.out.println(rtt.code + ' ' + rtt.error);
127 Assert.fail(rtt.code + ' ' + rtt.error);
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('@');
142 tc.password(at>=0?username.substring(0, at):username,access.getProperty("cadi_password"));
143 rtt = tc.getToken("org.osaaf.aaf","org.osaaf.test");
146 rtt = tc.refreshToken(rtt.value);
150 tc = tcf.newClient(tokenAltIntrospectURL, 3000);
151 tc.client_creds(client_id, client_secret);
152 Result<Introspect> rti = tc.introspect(rtt.value.getAccessToken());
154 System.out.print("Normal ISAM ");
157 System.out.println(rti.code + ' ' + rti.error);
158 Assert.fail(rtt.code + ' ' + rtt.error);
161 tc = tcf.newClient(tokenIntrospectURL, 3000);
162 tc.client_creds(client_id, client_secret);
163 rti = tc.introspect(rtt.value.getAccessToken());
165 System.out.print("AAF with ISAM Token ");
168 System.out.println(rti.code + ' ' + rti.error);
170 Assert.fail(rti.code + ' ' + rti.error);
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);
181 System.out.println(rtt.code + ' ' + rtt.error);
182 Assert.fail(rtt.code + ' ' + rtt.error);
185 System.out.println(rtt.code + ' ' + rtt.error);
186 Assert.fail(rtt.code + ' ' + rtt.error);
189 Assert.fail(Config.AAF_ALT_OAUTH2_TOKEN_URL + " is required");
192 } catch (Exception e) {
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);
205 Result<TimedToken> rt = tclient.getToken(scope);
208 Result<Introspect> rti = tclient.introspect(rt.value.getAccessToken());
220 private String serviceCall(TzClient tzClient) throws Exception {
221 return tzClient.best(new Retryable<String>() {
223 public String code(Rcli<?> client) throws CadiException, ConnectException, APIException {
224 Future<String> future = client.read(null,"text/plain");
225 if(future.get(3000)) {
228 throw new APIException(future.code() + future.body());
233 private void printAndFail(Result<?> rt) {
234 System.out.printf("HTTP Code %d: %s\n", rt.code, rt.error);
235 Assert.fail(rt.toString());
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",
245 Chrono.timeStamp(new Date(System.currentTimeMillis()+(t.getExpiresIn()*1000))),
247 t.getRefreshToken());
250 private void print(Introspect ti) {
251 if(ti==null || ti.getClientId()==null) {
252 System.out.println("Empty Introspect");
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"
261 + "\tUserName:\t%s\n"
262 + "\tExpires: \t%d (%s)\n"
264 + "\tContent:\t\t%s\n",
268 ti.isActive()?Boolean.TRUE.toString():Boolean.FALSE.toString(),
271 Chrono.timeStamp(exp),
273 ti.getContent()==null?"":ti.getContent());
275 System.out.println();