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;
24 import java.io.IOException;
25 import java.net.HttpURLConnection;
27 import java.net.URISyntaxException;
28 import java.nio.file.Path;
29 import java.security.GeneralSecurityException;
30 import java.security.NoSuchAlgorithmException;
32 import java.util.concurrent.ConcurrentHashMap;
33 import java.util.regex.Pattern;
35 import org.onap.aaf.cadi.Access;
36 import org.onap.aaf.cadi.CadiException;
37 import org.onap.aaf.cadi.Hash;
38 import org.onap.aaf.cadi.Locator;
39 import org.onap.aaf.cadi.LocatorException;
40 import org.onap.aaf.cadi.Symm;
41 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
42 import org.onap.aaf.cadi.aaf.v2_0.AAFLocator;
43 import org.onap.aaf.cadi.config.Config;
44 import org.onap.aaf.cadi.config.SecurityInfoC;
45 import org.onap.aaf.cadi.locator.PropertyLocator;
46 import org.onap.aaf.cadi.oauth.TokenClient.AUTHN_METHOD;
47 import org.onap.aaf.cadi.persist.Persist;
48 import org.onap.aaf.cadi.principal.Kind;
49 import org.onap.aaf.misc.env.APIException;
50 import org.onap.aaf.misc.rosetta.env.RosettaEnv;
52 import aafoauth.v2_0.Token;
54 public class TokenClientFactory extends Persist<Token,TimedToken> {
55 private static TokenClientFactory instance;
56 private Map<String,AAFConHttp> aafcons = new ConcurrentHashMap<String, AAFConHttp>();
57 private SecurityInfoC<HttpURLConnection> hsi;
61 private TokenClientFactory(Access pa) throws APIException, GeneralSecurityException, IOException, CadiException {
62 super(pa, new RosettaEnv(pa.getProperties()),Token.class,"outgoing");
64 if(access.getProperty(Config.AAF_OAUTH2_TOKEN_URL,null)==null) {
65 access.getProperties().put(Config.AAF_OAUTH2_TOKEN_URL, "https://AAF_LOCATE_URL/AAF_NS.token/2.0"); // Default to AAF
67 if(access.getProperty(Config.AAF_OAUTH2_INTROSPECT_URL,null)==null) {
68 access.getProperties().put(Config.AAF_OAUTH2_INTROSPECT_URL, "https://AAF_LOCATE_URL/AAF_NS.introspect/2.0"); // Default to AAF);
71 symm = Symm.encrypt.obtain();
72 hsi = SecurityInfoC.instance(access, HttpURLConnection.class);
75 public synchronized static final TokenClientFactory instance(Access access) throws APIException, GeneralSecurityException, IOException, CadiException {
77 instance = new TokenClientFactory(access);
83 * Pickup Timeout from Properties
87 * @throws CadiException
88 * @throws LocatorException
89 * @throws APIException
91 public<INTR> TokenClient newClient(final String tagOrURL) throws CadiException, LocatorException, APIException {
92 return newClient(tagOrURL,Integer.parseInt(access.getProperty(Config.AAF_CONN_TIMEOUT, Config.AAF_CONN_TIMEOUT_DEF)));
95 public<INTR> TokenClient newClient(final String tagOrURL, final int timeout) throws CadiException, LocatorException, APIException {
98 throw new CadiException("parameter tagOrURL cannot be null.");
100 ach = aafcons.get(tagOrURL);
102 aafcons.put(tagOrURL, ach=new AAFConHttp(access,tagOrURL));
106 if( Config.AAF_OAUTH2_TOKEN_URL.equals(tagOrURL) ||
107 Config.AAF_OAUTH2_INTROSPECT_URL.equals(tagOrURL) ||
108 tagOrURL.equals(access.getProperty(Config.AAF_OAUTH2_TOKEN_URL, null)) ||
109 tagOrURL.equals(access.getProperty(Config.AAF_OAUTH2_INTROSPECT_URL, null))
111 okind = Kind.AAF_OAUTH;
115 TokenClient tci = new TokenClient(
121 tci.client_creds(access);
125 public TzClient newTzClient(final String locatorURL) throws CadiException, LocatorException {
127 return new TzHClient(access,hsi,bestLocator(locatorURL));
128 } catch (URISyntaxException e) {
129 throw new LocatorException(e);
133 static String getKey(char tokenSource,String client_id, String username, byte[] hash, String scope) throws CadiException {
135 StringBuilder sb = new StringBuilder(client_id);
141 sb.append(tokenSource);
142 byte[] tohash=scope.getBytes();
143 if(hash!=null && hash.length>0) {
144 byte temp[] = new byte[hash.length+tohash.length];
145 System.arraycopy(tohash, 0, temp, 0, tohash.length);
146 System.arraycopy(hash, 0, temp, tohash.length, hash.length);
149 if(scope!=null && scope.length()>0) {
150 sb.append(Hash.toHexNo0x(Hash.hashSHA256(tohash)));
152 return sb.toString();
153 } catch (NoSuchAlgorithmException e) {
154 throw new CadiException(e);
159 protected TimedToken newCacheable(Token t, long expires, byte[] hash, Path path) throws IOException {
160 return new TimedToken(this,t,expires,hash,path);
163 public TimedToken putTimedToken(String key, Token token, byte[] hash) throws IOException, CadiException {
164 TimedToken tt = new TimedToken(this,token,token.getExpiresIn()+(System.currentTimeMillis()/1000),hash,getPath(key));
169 private static final Pattern locatePattern = Pattern.compile("https://.*/locate/.*");
170 public Locator<URI> bestLocator(final String locatorURL ) throws LocatorException, URISyntaxException {
171 if(locatorURL==null) {
172 throw new LocatorException("Cannot have a null locatorURL in bestLocator");
174 if(locatorURL.startsWith("https://AAF_LOCATE_URL/") || locatePattern.matcher(locatorURL).matches()) {
175 return new AAFLocator(hsi,new URI(locatorURL));
177 return new PropertyLocator(locatorURL);
179 // Note: Removed DME2Locator... If DME2 client is needed, use DME2Clients