dc6fe3908295886e319500f2761c5f91985de8ac
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / oauth / HRenewingTokenSS.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;
23
24 import java.io.IOException;
25 import java.net.HttpURLConnection;
26 import java.security.GeneralSecurityException;
27
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;
37
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;
43         
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);
46         }
47         
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;
51                 try {
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());
57                         if(nss.length>0) {
58                                 boolean hasDefault = false;
59                                 for(String ns : nss) {
60                                         if(ns.equals(defaultNS)) {
61                                                 hasDefault = true;
62                                         }
63                                 }
64                                 if(hasDefault) {
65                                         scopes=nss;             
66                                 } else {
67                                         String[] nssPlus = new String[nss.length+1];
68                                         nssPlus[0]=defaultNS;
69                                         System.arraycopy(nss, 0, nssPlus, 1, nss.length);
70                                         scopes = nssPlus;
71                                 }
72                         } else {
73                                 scopes = new String[] {defaultNS};
74                         }
75
76                 } catch (GeneralSecurityException | IOException | LocatorException | APIException e) {
77                         throw new CadiException(e);
78                 }
79         }
80
81         /* (non-Javadoc)
82          * @see org.onap.aaf.cadi.client.AbsAuthentication#headValue()
83          */
84         @Override
85         protected String headValue() throws IOException {
86                 Result<TimedToken> token;
87                 try {
88                         token = tc.getToken(Kind.OAUTH,scopes);
89                         if(token.isOK()) {
90                                 return "Bearer " + token.value.getAccessToken();
91                         } else {
92                                 throw new IOException("Token cannot be obtained: " + token.code + '-' + token.error);
93                         }
94                 } catch (IOException e) {
95                         throw e;
96                 } catch (LocatorException | CadiException | APIException e) {
97                         throw new IOException(e);
98                 }
99         }
100
101         public String tokenURL() {
102                 return tokenURL;
103         }
104 }