12
12
import io .jans .ca .plugin .adminui .model .auth .DCRResponse ;
13
13
import io .jans .ca .plugin .adminui .model .config .AUIConfiguration ;
14
14
import io .jans .ca .plugin .adminui .model .config .LicenseConfiguration ;
15
- import io .jans .ca .plugin .adminui .model .config .LicenseSpringCredentials ;
16
15
import io .jans .ca .plugin .adminui .model .exception .ApplicationException ;
17
16
import io .jans .ca .plugin .adminui .rest .license .LicenseResource ;
18
17
import io .jans .ca .plugin .adminui .service .BaseService ;
19
18
import io .jans .ca .plugin .adminui .utils .AppConstants ;
20
- import io .jans .ca .plugin .adminui .utils .ClientFactory ;
21
- import io .jans .ca .plugin .adminui .utils .CommonUtils ;
22
19
import io .jans .ca .plugin .adminui .utils .ErrorResponse ;
23
20
import io .jans .configapi .service .auth .ConfigurationService ;
24
21
import io .jans .orm .PersistenceEntryManager ;
25
22
import jakarta .inject .Inject ;
26
23
import jakarta .inject .Singleton ;
27
- import jakarta .json .JsonObject ;
28
- import jakarta .ws .rs .client .Entity ;
29
- import jakarta .ws .rs .client .Invocation ;
30
- import jakarta .ws .rs .core .MediaType ;
31
- import jakarta .ws .rs .core .MultivaluedHashMap ;
32
- import jakarta .ws .rs .core .MultivaluedMap ;
33
24
import jakarta .ws .rs .core .Response ;
34
25
import org .apache .commons .lang3 .StringUtils ;
35
26
import org .slf4j .Logger ;
36
27
37
- import java .util .Base64 ;
38
- import java .util .HashMap ;
39
28
import java .util .Map ;
40
29
41
30
@ Singleton
@@ -90,18 +79,6 @@ public AUIConfiguration getAUIConfiguration(String appType) throws Exception {
90
79
appConfigurationMap .put (appType , auiConfiguration );
91
80
}
92
81
}
93
- //check if LicenseConfiguration contains valid values in every request
94
- logger .info ("Checking if LicenseConfiguration present." );
95
- if (!appType .equals (AppConstants .APPLICATION_KEY_ADS )) {
96
- LicenseConfiguration lc = appConfigurationMap .get (appType ).getLicenseConfiguration ();
97
- if (lc == null || Strings .isNullOrEmpty (lc .getApiKey ())) {
98
- logger .info ("Trying to add properties to LicenseConfiguration." );
99
- AdminConf appConf = entryManager .find (AdminConf .class , AppConstants .ADMIN_UI_CONFIG_DN );
100
- auiConfiguration = appConfigurationMap .get (appType );
101
- auiConfiguration .setLicenseConfiguration (addPropertiesToLicenseConfiguration (appConf ));
102
- appConfigurationMap .put (appType , auiConfiguration );
103
- }
104
- }
105
82
return appConfigurationMap .get (appType );
106
83
} catch (Exception e ) {
107
84
logger .error (ErrorResponse .ERROR_READING_CONFIG .getDescription ());
@@ -141,44 +118,39 @@ private AUIConfiguration addPropertiesToAUIConfiguration(String appType, AdminCo
141
118
return auiConfig ;
142
119
}
143
120
144
- private LicenseConfiguration addPropertiesToLicenseConfiguration (AdminConf appConf ) throws Exception {
121
+ private LicenseConfiguration addPropertiesToLicenseConfiguration (AdminConf appConf ) {
145
122
LicenseConfiguration licenseConfiguration = new LicenseConfiguration ();
146
123
try {
147
124
LicenseConfig licenseConfig = appConf .getMainSettings ().getLicenseConfig ();
148
125
149
126
if (licenseConfig != null ) {
150
127
151
- LicenseSpringCredentials licenseSpringCredentials = requestLicenseCredentialsFromScan (licenseConfig );
152
- licenseConfiguration .setApiKey (licenseSpringCredentials .getApiKey ());
153
- licenseConfiguration .setProductCode (licenseSpringCredentials .getProductCode ());
154
- licenseConfiguration .setSharedKey (licenseSpringCredentials .getSharedKey ());
128
+ validateLicenseClientOnAuthServer (licenseConfig );
155
129
licenseConfiguration .setHardwareId (licenseConfig .getLicenseHardwareKey ());
156
130
licenseConfiguration .setLicenseKey (licenseConfig .getLicenseKey ());
131
+ licenseConfiguration .setScanApiHostname (licenseConfig .getScanLicenseApiHostname ());
132
+ licenseConfiguration .setScanAuthServerHostname (licenseConfig .getOidcClient ().getOpHost ());
133
+ licenseConfiguration .setScanApiClientId (licenseConfig .getOidcClient ().getClientId ());
134
+ licenseConfiguration .setScanApiClientSecret (licenseConfig .getOidcClient ().getClientSecret ());
157
135
}
158
136
return licenseConfiguration ;
159
137
} catch (Exception e ) {
160
- logger .error (ErrorResponse .LICENSE_SPRING_CREDENTIALS_ERROR .getDescription ());
138
+ logger .error (ErrorResponse .ERROR_IN_LICENSE_CONFIGURATION_VALIDATION .getDescription ());
161
139
}
162
140
return null ;
163
141
}
164
142
165
- /**
166
- * It's a function that makes a call to a REST API endpoint to get a token, then uses that token to make another call
167
- * to a different REST API endpoint to get some license credentials
168
- *
169
- * @param licenseConfig This is the object that contains the configuration parameters for the license.
170
- */
171
- private LicenseSpringCredentials requestLicenseCredentialsFromScan (LicenseConfig licenseConfig ) throws Exception {
143
+ private void validateLicenseClientOnAuthServer (LicenseConfig licenseConfig ) throws ApplicationException {
172
144
try {
173
145
logger .info ("Inside method to request license credentials from SCAN api." );
174
- io .jans .as .client .TokenResponse tokenResponse = generateToken (licenseConfig );
146
+ io .jans .as .client .TokenResponse tokenResponse = generateToken (licenseConfig . getOidcClient (). getOpHost (), licenseConfig . getOidcClient (). getClientId (), licenseConfig . getOidcClient (). getClientSecret () );
175
147
if (tokenResponse == null ) {
176
148
//try to re-generate clients using old SSA
177
149
DCRResponse dcrResponse = executeDCR (licenseConfig .getSsa ());
178
150
if (dcrResponse == null ) {
179
151
throw new ApplicationException (Response .Status .INTERNAL_SERVER_ERROR .getStatusCode (), ErrorResponse .ERROR_IN_DCR .getDescription ());
180
152
}
181
- tokenResponse = generateToken (licenseConfig );
153
+ tokenResponse = generateToken (licenseConfig . getOidcClient (). getOpHost (), licenseConfig . getOidcClient (). getClientId (), licenseConfig . getOidcClient (). getClientSecret () );
182
154
183
155
if (tokenResponse == null ) {
184
156
throw new ApplicationException (Response .Status .INTERNAL_SERVER_ERROR .getStatusCode (), ErrorResponse .TOKEN_GENERATION_ERROR .getDescription ());
@@ -190,63 +162,23 @@ private LicenseSpringCredentials requestLicenseCredentialsFromScan(LicenseConfig
190
162
lc .setOidcClient (oidcClient );
191
163
appConf .getMainSettings ().setLicenseConfig (lc );
192
164
entryManager .merge (appConf );
193
- licenseConfig = lc ;
194
- }
195
- // create request header
196
- MultivaluedMap <String , Object > headers = new MultivaluedHashMap <>();
197
- headers .putSingle ("Content-Type" , "application/json" );
198
- headers .putSingle ("Authorization" , "Bearer " + tokenResponse .getAccessToken ());
199
-
200
- logger .info ("Trying to get license credentials from SCAN api." );
201
- String licenseCredentailsUrl = (new StringBuffer ()).append (licenseConfig .getScanLicenseApiHostname ())
202
- .append ("/scan/license/credentials" ).toString ();
203
-
204
- Invocation .Builder request = ClientFactory .instance ().getClientBuilder (licenseCredentailsUrl );
205
- request .headers (headers );
206
-
207
- Map <String , String > body = new HashMap <>();
208
- body .put ("pubKey" , licenseConfig .getCredentialsEncryptionKey ().getPublicKey ());
209
-
210
- Response response = request .post (Entity .entity (body , MediaType .APPLICATION_JSON ));
211
- logger .info (" license credentials from scan request status code: {}" , response .getStatus ());
212
- if (response .getStatus () == 200 ) {
213
- JsonObject entity = response .readEntity (JsonObject .class );
214
- if (!Strings .isNullOrEmpty (entity .getString ("apiKey" ))) {
215
- //get license spring credentials
216
- LicenseSpringCredentials licenseSpringCredentials = new LicenseSpringCredentials ();
217
- licenseSpringCredentials .setHardwareId (licenseConfig .getLicenseHardwareKey ());
218
-
219
- String privateKey = (new String (Base64 .getDecoder ().decode (licenseConfig .getCredentialsEncryptionKey ().getPrivateKey ())))
220
- .replace ("-----BEGIN PRIVATE KEY-----" , "" )
221
- .replaceAll (System .lineSeparator (), "" )
222
- .replace ("-----END PRIVATE KEY-----" , "" );
223
- licenseSpringCredentials .setApiKey (CommonUtils .decode (entity .getString ("apiKey" ), privateKey ));
224
- licenseSpringCredentials .setProductCode (CommonUtils .decode (entity .getString ("productCode" ), privateKey ));
225
- licenseSpringCredentials .setSharedKey (CommonUtils .decode (entity .getString ("sharedKey" ), privateKey ));
226
-
227
- return licenseSpringCredentials ;
228
- }
229
165
}
230
- String errorResponse = response .readEntity (String .class );
231
- logger .error ("license Activation error response: {}, code: {}" , errorResponse , response .getStatus ());
232
- throw new ApplicationException (Response .Status .INTERNAL_SERVER_ERROR .getStatusCode (), ErrorResponse .LICENSE_SPRING_CREDENTIALS_ERROR .getDescription ());
233
166
} catch (Exception e ) {
234
- logger .error (ErrorResponse .LICENSE_SPRING_CREDENTIALS_ERROR .getDescription ());
235
- throw new ApplicationException (Response .Status .INTERNAL_SERVER_ERROR .getStatusCode (), ErrorResponse .LICENSE_SPRING_CREDENTIALS_ERROR .getDescription ());
167
+ logger .error (ErrorResponse .ERROR_IN_LICENSE_CONFIGURATION_VALIDATION .getDescription ());
168
+ throw new ApplicationException (Response .Status .INTERNAL_SERVER_ERROR .getStatusCode (), ErrorResponse .ERROR_IN_LICENSE_CONFIGURATION_VALIDATION .getDescription ());
236
169
}
237
170
}
238
171
239
- private io .jans .as .client .TokenResponse generateToken (LicenseConfig licenseConfig ) {
172
+ private io .jans .as .client .TokenResponse generateToken (String opHost , String clientId , String clientSecret ) {
240
173
try {
241
174
TokenRequest tokenRequest = new TokenRequest (GrantType .CLIENT_CREDENTIALS );
242
- tokenRequest .setAuthUsername (licenseConfig . getOidcClient (). getClientId () );
243
- tokenRequest .setAuthPassword (licenseConfig . getOidcClient (). getClientSecret () );
175
+ tokenRequest .setAuthUsername (clientId );
176
+ tokenRequest .setAuthPassword (clientSecret );
244
177
tokenRequest .setGrantType (GrantType .CLIENT_CREDENTIALS );
245
178
tokenRequest .setScope (LicenseResource .SCOPE_LICENSE_READ );
246
179
247
- logger .info ("licenseConfig.toString(): " + licenseConfig .toString ());
248
- logger .info ("Trying to get access token from auth server." );
249
- String scanLicenseApiHostname = (new StringBuffer ()).append (StringUtils .removeEnd (licenseConfig .getOidcClient ().getOpHost (), "/" ))
180
+ logger .info ("Trying to get access token from auth server: {}" , opHost );
181
+ String scanLicenseApiHostname = (new StringBuffer ()).append (StringUtils .removeEnd (opHost , "/" ))
250
182
.append ("/jans-auth/restv1/token" ).toString ();
251
183
io .jans .as .client .TokenResponse tokenResponse = null ;
252
184
tokenResponse = getToken (tokenRequest , scanLicenseApiHostname );
0 commit comments