fb8445181e7a4c7f1fe942aa472344a5f2ba90f8
[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                 String ci;
48                 if((ci = access.getProperty(Config.AAF_APPID,null))==null) {
49                         if((ci = access.getProperty(Config.CADI_ALIAS,null))==null) {
50                                 throw new CadiException(Config.AAF_APPID + REQUIRED_FOR_OAUTH2);
51                         }
52                 }
53                 client_id = ci;
54
55                 synchronized(access) {
56                         if(tokenClientPool==null) {
57                                 tokenClientPool = new Pool<TokenClient>(new TCCreator(access));
58                         }
59                         try {
60                                 tkMgr = TokenMgr.getInstance(access, token_url, introspect_url);
61                         } catch (APIException e) {
62                                 throw new CadiException("Unable to create TokenManager",e);
63                         }
64                 }
65         }
66
67         private class TCCreator implements Creator<TokenClient> {
68                 private TokenClientFactory tcf;
69                 private final int timeout;
70                 private final String url,enc_secret;
71                 
72                 public TCCreator(PropAccess access) throws CadiException { 
73                         try {
74                                 tcf = TokenClientFactory.instance(access);
75                         } catch (APIException | GeneralSecurityException | IOException e1) {
76                                 throw new CadiException(e1);
77                         }
78                         
79                         if((url = access.getProperty(Config.AAF_OAUTH2_TOKEN_URL,null))==null) {
80                                 throw new CadiException(Config.AAF_OAUTH2_TOKEN_URL + REQUIRED_FOR_OAUTH2);
81                         }
82                         
83                         try {
84                                 timeout = Integer.parseInt(access.getProperty(Config.AAF_CONN_TIMEOUT, Config.AAF_CONN_TIMEOUT_DEF));
85                         } catch (NumberFormatException e) {
86                                 throw new CadiException("Bad format for " + Config.AAF_CONN_TIMEOUT, e);
87                         }
88                         if((enc_secret= access.getProperty(Config.AAF_APPPASS,null))==null) {
89                                 throw new CadiException(Config.AAF_APPPASS + REQUIRED_FOR_OAUTH2);
90                         }
91                 }
92                 
93                 @Override
94                 public TokenClient create() throws APIException {
95                         try {
96                                 TokenClient tc = tcf.newClient(url, timeout);
97                                 tc.client_creds(client_id, access.decrypt(enc_secret, true));
98                                 return tc;
99                         } catch (CadiException | LocatorException | IOException e) {
100                                 throw new APIException(e);
101                         }
102                 }
103
104                 @Override
105                 public void destroy(TokenClient t) {
106                 }
107
108                 @Override
109                 public boolean isValid(TokenClient t) {
110                         return t!=null && t.client_id()!=null;
111                 }
112
113                 @Override
114                 public void reuse(TokenClient t) {
115                 }
116         };
117
118         /* (non-Javadoc)
119          * @see org.onap.aaf.cadi.Lur#destroy()
120          */
121         public void destroy() {
122                 tkMgr.close();
123         }
124         
125         /* (non-Javadoc)
126          * @see org.onap.aaf.cadi.Lur#clear(java.security.Principal, java.lang.StringBuilder)
127          */
128         public void clear(Principal p, StringBuilder report) {
129                 tkMgr.clear(p, report);
130         }
131         
132
133         
134 }