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;
26 import java.security.GeneralSecurityException;
28 import org.onap.aaf.cadi.CadiException;
29 import org.onap.aaf.cadi.LocatorException;
30 import org.onap.aaf.cadi.PropAccess;
31 import org.onap.aaf.cadi.client.Result;
32 import org.onap.aaf.cadi.config.SecurityInfoC;
33 import org.onap.aaf.cadi.http.HAuthorizationHeader;
34 import org.onap.aaf.cadi.principal.Kind;
35 import org.onap.aaf.cadi.util.FQI;
36 import org.onap.aaf.misc.env.APIException;
38 public class HRenewingTokenSS extends HAuthorizationHeader {
39 private TokenClientFactory tcf;
40 private final TokenClient tc;
41 private final String[] scopes;
42 private final String tokenURL;
44 public HRenewingTokenSS(final PropAccess access, final String tokenURL, final String ... nss) throws CadiException, IOException, GeneralSecurityException {
45 this(access,SecurityInfoC.instance(access, HttpURLConnection.class),tokenURL,nss);
48 public HRenewingTokenSS(final PropAccess access, final SecurityInfoC<HttpURLConnection> si, final String tokenURL, final String ... nss) throws CadiException, IOException, GeneralSecurityException {
49 super(si,null,null/*Note: HeadValue overloaded */);
50 this.tokenURL = tokenURL;
52 tcf = TokenClientFactory.instance(access);
53 tc = tcf.newClient(tokenURL);
54 tc.client_creds(access);
55 setUser(tc.client_id());
56 String defaultNS = FQI.reverseDomain(tc.client_id());
58 boolean hasDefault = false;
59 for (String ns : nss) {
60 if (ns.equals(defaultNS)) {
67 String[] nssPlus = new String[nss.length+1];
69 System.arraycopy(nss, 0, nssPlus, 1, nss.length);
73 scopes = new String[] {defaultNS};
76 } catch (GeneralSecurityException | IOException | LocatorException | APIException e) {
77 throw new CadiException(e);
82 * @see org.onap.aaf.cadi.client.AbsAuthentication#headValue()
85 protected String headValue() throws IOException {
86 Result<TimedToken> token;
88 token = tc.getToken(Kind.OAUTH,scopes);
90 return "Bearer " + token.value.getAccessToken();
92 throw new IOException("Token cannot be obtained: " + token.code + '-' + token.error);
94 } catch (IOException e) {
96 } catch (LocatorException | CadiException | APIException e) {
97 throw new IOException(e);
101 public String tokenURL() {