AT&T 2.0.19 Code drop, stage 2
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / oauth / AbsOTafLur.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.security.GeneralSecurityException;
26 import java.security.Principal;
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.config.Config;
32 import org.onap.aaf.misc.env.APIException;
33 import org.onap.aaf.misc.env.util.Pool;
34 import org.onap.aaf.misc.env.util.Pool.Creator;
35
36 public abstract class AbsOTafLur {
37         protected static final String ERROR_GETTING_TOKEN_CLIENT = "Error getting TokenClient";
38         protected static final String REQUIRED_FOR_OAUTH2 = " is required for OAuth Access";
39
40         protected final TokenMgr tkMgr;
41         protected final PropAccess access;
42         protected final String client_id;
43         protected static Pool<TokenClient> tokenClientPool;
44         
45         protected AbsOTafLur(final PropAccess access, final String token_url, final String introspect_url) throws CadiException {
46                 this.access = access;
47                 if((client_id = access.getProperty(Config.AAF_APPID,null))==null) {
48                         throw new CadiException(Config.AAF_APPID + REQUIRED_FOR_OAUTH2);
49                 }
50
51                 synchronized(access) {
52                         if(tokenClientPool==null) {
53                                 tokenClientPool = new Pool<TokenClient>(new TCCreator(access));
54                         }
55                         try {
56                                 tkMgr = TokenMgr.getInstance(access, token_url, introspect_url);
57                         } catch (APIException e) {
58                                 throw new CadiException("Unable to create TokenManager",e);
59                         }
60                 }
61         }
62
63         private class TCCreator implements Creator<TokenClient> {
64                 private TokenClientFactory tcf;
65                 private final int timeout;
66                 private final String url,enc_secret;
67                 
68                 public TCCreator(PropAccess access) throws CadiException { 
69                         try {
70                                 tcf = TokenClientFactory.instance(access);
71                         } catch (APIException | GeneralSecurityException | IOException e1) {
72                                 throw new CadiException(e1);
73                         }
74                         
75                         if((url = access.getProperty(Config.AAF_OAUTH2_TOKEN_URL,null))==null) {
76                                 throw new CadiException(Config.AAF_OAUTH2_TOKEN_URL + REQUIRED_FOR_OAUTH2);
77                         }
78                         
79                         try {
80                                 timeout = Integer.parseInt(access.getProperty(Config.AAF_CONN_TIMEOUT, Config.AAF_CONN_TIMEOUT_DEF));
81                         } catch (NumberFormatException e) {
82                                 throw new CadiException("Bad format for " + Config.AAF_CONN_TIMEOUT, e);
83                         }
84                         if((enc_secret= access.getProperty(Config.AAF_APPPASS,null))==null) {
85                                 throw new CadiException(Config.AAF_APPPASS + REQUIRED_FOR_OAUTH2);
86                         }
87                 }
88                 
89                 @Override
90                 public TokenClient create() throws APIException {
91                         try {
92                                 TokenClient tc = tcf.newClient(url, timeout);
93                                 tc.client_creds(client_id, access.decrypt(enc_secret, true));
94                                 return tc;
95                         } catch (CadiException | LocatorException | IOException e) {
96                                 throw new APIException(e);
97                         }
98                 }
99
100                 @Override
101                 public void destroy(TokenClient t) {
102                 }
103
104                 @Override
105                 public boolean isValid(TokenClient t) {
106                         return t!=null && t.client_id()!=null;
107                 }
108
109                 @Override
110                 public void reuse(TokenClient t) {
111                 }
112         };
113
114         /* (non-Javadoc)
115          * @see org.onap.aaf.cadi.Lur#destroy()
116          */
117         public void destroy() {
118                 tkMgr.close();
119         }
120         
121         /* (non-Javadoc)
122          * @see org.onap.aaf.cadi.Lur#clear(java.security.Principal, java.lang.StringBuilder)
123          */
124         public void clear(Principal p, StringBuilder report) {
125                 tkMgr.clear(p, report);
126         }
127         
128
129         
130 }