-
Notifications
You must be signed in to change notification settings - Fork 534
/
ApplicationDAOImpl.java
2960 lines (2558 loc) · 131 KB
/
ApplicationDAOImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.identity.application.mgt.dao.impl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException;
import org.wso2.carbon.identity.application.common.model.ApplicationBasicInfo;
import org.wso2.carbon.identity.application.common.model.ApplicationPermission;
import org.wso2.carbon.identity.application.common.model.AuthenticationStep;
import org.wso2.carbon.identity.application.common.model.Claim;
import org.wso2.carbon.identity.application.common.model.ClaimConfig;
import org.wso2.carbon.identity.application.common.model.ClaimMapping;
import org.wso2.carbon.identity.application.common.model.FederatedAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.model.IdentityProvider;
import org.wso2.carbon.identity.application.common.model.InboundAuthenticationConfig;
import org.wso2.carbon.identity.application.common.model.InboundAuthenticationRequestConfig;
import org.wso2.carbon.identity.application.common.model.InboundProvisioningConfig;
import org.wso2.carbon.identity.application.common.model.JustInTimeProvisioningConfig;
import org.wso2.carbon.identity.application.common.model.LocalAndOutboundAuthenticationConfig;
import org.wso2.carbon.identity.application.common.model.LocalAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.model.LocalRole;
import org.wso2.carbon.identity.application.common.model.OutboundProvisioningConfig;
import org.wso2.carbon.identity.application.common.model.PermissionsAndRoleConfig;
import org.wso2.carbon.identity.application.common.model.Property;
import org.wso2.carbon.identity.application.common.model.ProvisioningConnectorConfig;
import org.wso2.carbon.identity.application.common.model.RequestPathAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.model.RoleMapping;
import org.wso2.carbon.identity.application.common.model.ServiceProvider;
import org.wso2.carbon.identity.application.common.model.ServiceProviderProperty;
import org.wso2.carbon.identity.application.common.model.User;
import org.wso2.carbon.identity.application.common.util.IdentityApplicationManagementUtil;
import org.wso2.carbon.identity.application.mgt.AbstractInboundAuthenticatorConfig;
import org.wso2.carbon.identity.application.mgt.ApplicationConstants;
import org.wso2.carbon.identity.application.mgt.ApplicationMgtDBQueries;
import org.wso2.carbon.identity.application.mgt.ApplicationMgtSystemConfig;
import org.wso2.carbon.identity.application.mgt.ApplicationMgtUtil;
import org.wso2.carbon.identity.application.mgt.dao.ApplicationDAO;
import org.wso2.carbon.identity.application.mgt.dao.IdentityProviderDAO;
import org.wso2.carbon.identity.application.mgt.internal.ApplicationManagementServiceComponent;
import org.wso2.carbon.identity.application.mgt.internal.ApplicationManagementServiceComponentHolder;
import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.core.util.IdentityUtil;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.core.util.UserCoreUtil;
import org.wso2.carbon.utils.DBUtils;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* This class access the IDN_APPMGT database to store/update and delete application configurations.
* The IDN_APPMGT database contains few tables
* <ul>
* <li>IDN_APPMGT_APP</li>
* <li>IDN_APPMGT_CLIENT</li>
* <li>IDN_APPMGT_STEP</li>
* <li>IDN_APPMGT_STEP_IDP</li>
* <li>IDN_APPMGT_CLAIM_MAPPING</li>
* <li>IDN_APPMGT_ROLE_MAPPING</li>
* </ul>
*/
public class ApplicationDAOImpl implements ApplicationDAO {
private Log log = LogFactory.getLog(ApplicationDAOImpl.class);
private List<String> standardInboundAuthTypes;
public ApplicationDAOImpl() {
standardInboundAuthTypes = new ArrayList<String>();
standardInboundAuthTypes.add("oauth2");
standardInboundAuthTypes.add("wstrust");
standardInboundAuthTypes.add("samlsso");
standardInboundAuthTypes.add("openid");
standardInboundAuthTypes.add("passivests");
}
private boolean isCustomInboundAuthType(String authType) {
return !standardInboundAuthTypes.contains(authType);
}
/**
* Get Service provider properties
* @param dbConnection database connection
* @param SpId SP Id
* @return service provider properties
*/
private List<ServiceProviderProperty> getServicePropertiesBySpId(Connection dbConnection, int SpId)
throws SQLException {
String sqlStmt = ApplicationMgtDBQueries.GET_SP_METADATA_BY_SP_ID;
PreparedStatement prepStmt = null;
ResultSet rs = null;
List<ServiceProviderProperty> idpProperties = new ArrayList<ServiceProviderProperty>();
try {
prepStmt = dbConnection.prepareStatement(sqlStmt);
prepStmt.setInt(1, SpId);
rs = prepStmt.executeQuery();
while (rs.next()) {
ServiceProviderProperty property = new ServiceProviderProperty();
property.setName(rs.getString("NAME"));
property.setValue(rs.getString("VALUE"));
property.setDisplayName(rs.getString("DISPLAY_NAME"));
idpProperties.add(property);
}
} finally {
IdentityApplicationManagementUtil.closeStatement(prepStmt);
IdentityApplicationManagementUtil.closeResultSet(rs);
}
return idpProperties;
}
/**
* Add Service provider properties
*
* @param dbConnection
* @param spId
* @param properties
* @throws SQLException
*/
private void addServiceProviderProperties(Connection dbConnection, int spId,
List<ServiceProviderProperty> properties, int tenantId)
throws SQLException {
String sqlStmt = ApplicationMgtDBQueries.ADD_SP_METADATA;
PreparedStatement prepStmt = null;
try {
prepStmt = dbConnection.prepareStatement(sqlStmt);
for (ServiceProviderProperty property : properties) {
prepStmt.setInt(1, spId);
prepStmt.setString(2, property.getName());
prepStmt.setString(3, property.getValue());
prepStmt.setString(4, property.getDisplayName());
prepStmt.setInt(5, tenantId);
prepStmt.addBatch();
}
prepStmt.executeBatch();
} finally {
IdentityApplicationManagementUtil.closeStatement(prepStmt);
}
}
/**
* Update Service provider properties
*
* @param dbConnection
* @param spId
* @param properties
* @throws SQLException
*/
private void updateServiceProviderProperties(Connection dbConnection, int spId,
List<ServiceProviderProperty> properties, int tenantId)
throws SQLException {
PreparedStatement prepStmt = null;
try {
prepStmt = dbConnection.prepareStatement(ApplicationMgtDBQueries.DELETE_SP_METADATA);
prepStmt.setInt(1, spId);
prepStmt.executeUpdate();
prepStmt = dbConnection.prepareStatement(ApplicationMgtDBQueries.ADD_SP_METADATA);
for (ServiceProviderProperty property : properties) {
prepStmt.setInt(1, spId);
prepStmt.setString(2, property.getName());
prepStmt.setString(3, property.getValue());
prepStmt.setString(4, property.getDisplayName());
prepStmt.setInt(5, tenantId);
prepStmt.addBatch();
}
prepStmt.executeBatch();
} finally {
IdentityApplicationManagementUtil.closeStatement(prepStmt);
}
}
/**
* Stores basic application information and meta-data such as the application name, creator and
* tenant.
*
* @param serviceProvider
* @throws IdentityApplicationManagementException
*/
@Override
public int createApplication(ServiceProvider serviceProvider, String tenantDomain)
throws IdentityApplicationManagementException {
// get logged-in users tenant identifier.
int tenantID = MultitenantConstants.INVALID_TENANT_ID;
if (tenantDomain != null) {
tenantID = IdentityTenantUtil.getTenantId(tenantDomain);
}
String qualifiedUsername = CarbonContext.getThreadLocalCarbonContext().getUsername();
if (ApplicationConstants.LOCAL_SP.equals(serviceProvider.getApplicationName())) {
qualifiedUsername = CarbonConstants.REGISTRY_SYSTEM_USERNAME;
}
String username = UserCoreUtil.removeDomainFromName(qualifiedUsername);
String userStoreDomain = IdentityUtil.extractDomainFromName(qualifiedUsername);
String applicationName = serviceProvider.getApplicationName();
String description = serviceProvider.getDescription();
if (log.isDebugEnabled()) {
log.debug("Creating Application " + applicationName + " for user " + qualifiedUsername);
}
Connection connection = IdentityDatabaseUtil.getDBConnection();
PreparedStatement storeAppPrepStmt = null;
ResultSet results = null;
try {
String dbProductName = connection.getMetaData().getDatabaseProductName();
storeAppPrepStmt = connection.prepareStatement(
ApplicationMgtDBQueries.STORE_BASIC_APPINFO, new String[]{
DBUtils.getConvertedAutoGeneratedColumnName(dbProductName, "ID")});
// TENANT_ID, APP_NAME, USER_STORE, USERNAME, DESCRIPTION, AUTH_TYPE
storeAppPrepStmt.setInt(1, tenantID);
storeAppPrepStmt.setString(2, applicationName);
storeAppPrepStmt.setString(3, userStoreDomain);
storeAppPrepStmt.setString(4, username);
storeAppPrepStmt.setString(5, description);
// by default authentication type would be default.
// default authenticator is defined system-wide - in the configuration file.
storeAppPrepStmt.setString(6, ApplicationConstants.AUTH_TYPE_DEFAULT);
storeAppPrepStmt.setString(7, "0");
storeAppPrepStmt.setString(8, "0");
storeAppPrepStmt.execute();
results = storeAppPrepStmt.getGeneratedKeys();
int applicationId = 0;
if (results.next()) {
applicationId = results.getInt(1);
}
// some JDBC Drivers returns this in the result, some don't
if (applicationId == 0) {
if (log.isDebugEnabled()) {
log.debug("JDBC Driver did not return the application id, executing Select operation");
}
applicationId = getApplicationIDByName(applicationName, tenantID, connection);
}
if (serviceProvider.getSpProperties() != null) {
addServiceProviderProperties(connection, applicationId,
Arrays.asList(serviceProvider.getSpProperties()), tenantID);
}
if (!connection.getAutoCommit()) {
connection.commit();
}
if (log.isDebugEnabled()) {
log.debug("Application Stored successfully with application id " + applicationId);
}
return applicationId;
} catch (SQLException e) {
try {
if (connection != null) {
connection.rollback();
}
} catch (SQLException sql) {
throw new IdentityApplicationManagementException(
"Error while Creating Application", sql);
}
throw new IdentityApplicationManagementException("Error while Creating Application", e);
} finally {
IdentityApplicationManagementUtil.closeResultSet(results);
IdentityApplicationManagementUtil.closeStatement(storeAppPrepStmt);
IdentityApplicationManagementUtil.closeConnection(connection);
}
}
@Override
public void updateApplication(ServiceProvider serviceProvider, String tenantDomain)
throws IdentityApplicationManagementException {
Connection connection = IdentityDatabaseUtil.getDBConnection();
int applicationId = serviceProvider.getApplicationID();
int tenantID = MultitenantConstants.INVALID_TENANT_ID;
if (tenantDomain != null) {
tenantID = IdentityTenantUtil.getTenantId(tenantDomain);
}
try {
if (ApplicationManagementServiceComponent.getFileBasedSPs().containsKey(
serviceProvider.getApplicationName())) {
throw new IdentityApplicationManagementException(
"Application with the same name laoded from the file system.");
}
// update basic information of the application.
// you can change application name, description, isSasApp...
updateBasicApplicationData(serviceProvider, connection);
updateInboundProvisioningConfiguration(applicationId,
serviceProvider.getInboundProvisioningConfig(), connection);
if (serviceProvider.getSpProperties() != null) {
updateServiceProviderProperties(connection, applicationId,
Arrays.asList(serviceProvider.getSpProperties()), tenantID);
}
// delete all in-bound authentication requests.
deleteInboundAuthRequestConfiguration(serviceProvider.getApplicationID(), connection);
// update all in-bound authentication requests.
updateInboundAuthRequestConfiguration(serviceProvider.getApplicationID(),
serviceProvider.getInboundAuthenticationConfig(), connection);
// delete local and out-bound authentication configuration.
deleteLocalAndOutboundAuthenticationConfiguration(applicationId, connection);
// update local and out-bound authentication configuration.
updateLocalAndOutboundAuthenticationConfiguration(serviceProvider.getApplicationID(),
serviceProvider.getLocalAndOutBoundAuthenticationConfig(), connection);
deleteRequestPathAuthenticators(applicationId, connection);
updateRequestPathAuthenticators(applicationId,
serviceProvider.getRequestPathAuthenticatorConfigs(), connection);
deteClaimConfiguration(applicationId, connection);
updateClaimConfiguration(serviceProvider.getApplicationID(),
serviceProvider.getClaimConfig(), applicationId, connection);
deleteOutboundProvisioningConfiguration(applicationId, connection);
updateOutboundProvisioningConfiguration(applicationId,
serviceProvider.getOutboundProvisioningConfig(), connection);
deletePermissionAndRoleConfiguration(applicationId, connection);
updatePermissionAndRoleConfiguration(serviceProvider.getApplicationID(),
serviceProvider.getPermissionAndRoleConfig(), connection);
deleteAssignedPermissions(connection, serviceProvider.getApplicationName(),
serviceProvider.getPermissionAndRoleConfig().getPermissions());
if (!connection.getAutoCommit()) {
connection.commit();
}
} catch (SQLException | UserStoreException e) {
try {
if (connection != null) {
connection.rollback();
}
} catch (SQLException e1) {
throw new IdentityApplicationManagementException(
"Failed to update service provider " + applicationId, e);
}
throw new IdentityApplicationManagementException("Failed to update service provider "
+ applicationId, e);
} finally {
IdentityApplicationManagementUtil.closeConnection(connection);
}
}
/**
* @param serviceProvider
* @param connection
* @throws SQLException
* @throws UserStoreException
* @throws IdentityApplicationManagementException
*/
private void updateBasicApplicationData(ServiceProvider serviceProvider, Connection connection) throws SQLException, UserStoreException,
IdentityApplicationManagementException {
int applicationId = serviceProvider.getApplicationID();
String applicationName = serviceProvider.getApplicationName();
String description = serviceProvider.getDescription();
boolean isSaasApp = serviceProvider.isSaasApp();
int tenantID = CarbonContext.getThreadLocalCarbonContext().getTenantId();
String storedAppName = null;
if (applicationName == null) {
// check for required attributes.
throw new IdentityApplicationManagementException("Application Name is required.");
}
if (log.isDebugEnabled()) {
log.debug("Updating Application with ID: " + applicationId);
}
// reads back the Application Name. This is to check if the Application
// has been renamed
storedAppName = getApplicationName(applicationId, connection);
if (log.isDebugEnabled()) {
log.debug("Stored Application Name " + storedAppName);
}
// only if the application has been renamed
if (!StringUtils.equals(applicationName, storedAppName)) {
String applicationNameforRole = IdentityUtil.addDomainToName(applicationName, ApplicationConstants.
APPLICATION_DOMAIN);
String storedAppNameforRole = IdentityUtil.addDomainToName(storedAppName, ApplicationConstants.
APPLICATION_DOMAIN);
// rename the role
ApplicationMgtUtil.renameRole(storedAppNameforRole, applicationNameforRole);
if (log.isDebugEnabled()) {
log.debug("Renaming application role from " + storedAppName + " to "
+ applicationName);
}
Map<String, String> applicationPermissions = readApplicationPermissions(storedAppName);
for (Map.Entry<String, String> entry : applicationPermissions.entrySet()) {
updatePermissionPath(entry.getKey(), entry.getValue().replace(storedAppName.toLowerCase(),
applicationName.toLowerCase()));
}
}
// update the application data
PreparedStatement storeAppPrepStmt = null;
try {
storeAppPrepStmt = connection
.prepareStatement(ApplicationMgtDBQueries.UPDATE_BASIC_APPINFO);
// SET APP_NAME=?, DESCRIPTION=? IS_SAAS_APP=? WHERE TENANT_ID= ? AND ID = ?
storeAppPrepStmt.setString(1, applicationName);
storeAppPrepStmt.setString(2, description);
storeAppPrepStmt.setString(3, isSaasApp ? "1" : "0");
storeAppPrepStmt.setInt(4, tenantID);
storeAppPrepStmt.setInt(5, applicationId);
storeAppPrepStmt.executeUpdate();
} finally {
IdentityApplicationManagementUtil.closeStatement(storeAppPrepStmt);
}
if (log.isDebugEnabled()) {
log.debug("Updated Application successfully");
}
}
private List<Property> filterEmptyProperties(Property[] propertiesArray){
List<Property> propertyArrayList = new ArrayList<>();
if(ArrayUtils.isNotEmpty(propertiesArray)) {
for (Property property : propertiesArray) {
if (property != null && StringUtils.isNotBlank(property.getValue())) {
propertyArrayList.add(property);
}
}
}
return propertyArrayList ;
}
/**
* @param applicationId
* @param inBoundAuthenticationConfig
* @param connection
* @throws SQLException
*/
private void updateInboundAuthRequestConfiguration(int applicationId,
InboundAuthenticationConfig inBoundAuthenticationConfig, Connection connection)
throws IdentityApplicationManagementException {
int tenantID = CarbonContext.getThreadLocalCarbonContext().getTenantId();
PreparedStatement inboundAuthReqConfigPrepStmt = null;
try {
if (inBoundAuthenticationConfig == null
|| inBoundAuthenticationConfig.getInboundAuthenticationRequestConfigs() == null
|| inBoundAuthenticationConfig.getInboundAuthenticationRequestConfigs().length == 0) {
// no in-bound authentication requests defined.
return;
}
inboundAuthReqConfigPrepStmt = connection
.prepareStatement(ApplicationMgtDBQueries.STORE_CLIENT_INFO);
InboundAuthenticationRequestConfig[] authRequests = inBoundAuthenticationConfig
.getInboundAuthenticationRequestConfigs();
for (InboundAuthenticationRequestConfig authRequest : authRequests) {
if (authRequest == null || authRequest.getInboundAuthType() == null) {
log.warn("Invalid in-bound authentication request");
// not a valid authentication request. Must have client and a type.
continue;
}
Property[] propertiesArray = authRequest.getProperties();
List<Property> propertyArrayList = new ArrayList<>();
String authKey = null;
if (standardInboundAuthTypes.contains(authRequest.getInboundAuthType())) {
authKey = authRequest.getInboundAuthKey();
propertyArrayList = filterEmptyProperties(propertiesArray);
} else {
String configType = getConfigTypeFromSPProperties(getServicePropertiesBySpId(connection,
applicationId),authRequest.getInboundAuthType());
AbstractInboundAuthenticatorConfig inboundAuthenticatorConfig =
ApplicationManagementServiceComponentHolder.getInboundAuthenticatorConfig(configType);
if (inboundAuthenticatorConfig != null &&
StringUtils.isNotBlank(inboundAuthenticatorConfig.getAuthKey())) {
authKey = inboundAuthenticatorConfig.getAuthKey();
propertyArrayList = filterEmptyProperties(propertiesArray);
} else if (inboundAuthenticatorConfig != null &&
StringUtils.isNotBlank(inboundAuthenticatorConfig.getRelyingPartyKey())) {
if (propertiesArray != null && propertiesArray.length > 0) {
for (Property prop : propertiesArray) {
if (inboundAuthenticatorConfig.getRelyingPartyKey().equals(prop.getName())) {
if (StringUtils.isNotBlank(prop.getValue())) {
authKey = prop.getValue();
}
} else {
if (StringUtils.isNotBlank(prop.getValue())) {
propertyArrayList.add(prop);
}
}
}
}
} else {
propertyArrayList = filterEmptyProperties(propertiesArray);
}
}
if (StringUtils.isBlank(authKey)) {
continue;
}
if (!propertyArrayList.isEmpty()) {
for (Property prop : propertyArrayList) {
inboundAuthReqConfigPrepStmt.setInt(1, tenantID);
inboundAuthReqConfigPrepStmt.setString(2,authKey);
inboundAuthReqConfigPrepStmt.setString(3,authRequest.getInboundAuthType());
inboundAuthReqConfigPrepStmt.setString(4,prop.getName());
inboundAuthReqConfigPrepStmt.setString(5,prop.getValue());
inboundAuthReqConfigPrepStmt.setInt(6, applicationId);
inboundAuthReqConfigPrepStmt.addBatch();
}
} else {
inboundAuthReqConfigPrepStmt.setInt(1, tenantID);
inboundAuthReqConfigPrepStmt.setString(2,authKey);
inboundAuthReqConfigPrepStmt.setString(3,authRequest.getInboundAuthType());
inboundAuthReqConfigPrepStmt.setString(4, null);
inboundAuthReqConfigPrepStmt.setString(5, null);
inboundAuthReqConfigPrepStmt.setInt(6, applicationId);
inboundAuthReqConfigPrepStmt.addBatch();
}
if (log.isDebugEnabled()) {
log.debug("Updating inbound authentication request configuration of the application "
+ applicationId
+ "inbound auth key: "
+ authRequest.getInboundAuthKey()
+ " inbound auth type: "
+ authRequest.getInboundAuthType());
}
}
inboundAuthReqConfigPrepStmt.executeBatch();
} catch (SQLException e) {
log.error("Error occurred while updating the Inbound Authentication Request Configuration.", e);
} finally {
IdentityApplicationManagementUtil.closeStatement(inboundAuthReqConfigPrepStmt);
}
}
/**
* @param applicationId
* @param inBoundProvisioningConfig
* @param connection
* @throws SQLException
*/
private void updateInboundProvisioningConfiguration(int applicationId,
InboundProvisioningConfig inBoundProvisioningConfig, Connection connection)
throws SQLException {
int tenantID = CarbonContext.getThreadLocalCarbonContext().getTenantId();
PreparedStatement inboundProConfigPrepStmt = null;
try {
inboundProConfigPrepStmt = connection
.prepareStatement(ApplicationMgtDBQueries.UPDATE_BASIC_APPINFO_WITH_PRO_PROPERTIES);
// PROVISIONING_USERSTORE_DOMAIN=?
inboundProConfigPrepStmt.setString(1, inBoundProvisioningConfig.getProvisioningUserStore());
inboundProConfigPrepStmt.setString(2, inBoundProvisioningConfig.isDumbMode() ? "1" : "0");
inboundProConfigPrepStmt.setInt(3, tenantID);
inboundProConfigPrepStmt.setInt(4, applicationId);
inboundProConfigPrepStmt.execute();
} finally {
IdentityApplicationManagementUtil.closeStatement(inboundProConfigPrepStmt);
}
}
/**
* @param applicationId
* @param outBoundProvisioningConfig
* @param connection
* @throws SQLException
*/
private void updateOutboundProvisioningConfiguration(int applicationId,
OutboundProvisioningConfig outBoundProvisioningConfig, Connection connection)
throws SQLException {
int tenantID = CarbonContext.getThreadLocalCarbonContext().getTenantId();
PreparedStatement outboundProConfigPrepStmt = null;
IdentityProvider[] proProviders = outBoundProvisioningConfig
.getProvisioningIdentityProviders();
try {
if (outBoundProvisioningConfig == null || proProviders == null
|| proProviders.length == 0) {
// no in-bound authentication requests defined.
return;
}
outboundProConfigPrepStmt = connection
.prepareStatement(ApplicationMgtDBQueries.STORE_PRO_CONNECTORS);
// TENANT_ID, IDP_NAME, CONNECTOR_NAME, APP_ID
for (IdentityProvider proProvider : proProviders) {
if (proProvider != null) {
ProvisioningConnectorConfig proConnector = proProvider
.getDefaultProvisioningConnectorConfig();
if (proConnector == null) {
continue;
}
String jitEnabled = "0";
if (proProvider.getJustInTimeProvisioningConfig() != null
&& proProvider.getJustInTimeProvisioningConfig()
.isProvisioningEnabled()) {
jitEnabled = "1";
}
String blocking = "0";
if (proProvider.getDefaultProvisioningConnectorConfig() != null
&& proProvider.getDefaultProvisioningConnectorConfig().isBlocking()) {
blocking = "1";
}
outboundProConfigPrepStmt.setInt(1, tenantID);
outboundProConfigPrepStmt.setString(2, proProvider.getIdentityProviderName());
outboundProConfigPrepStmt.setString(3, proConnector.getName());
outboundProConfigPrepStmt.setInt(4, applicationId);
outboundProConfigPrepStmt.setString(5, jitEnabled);
outboundProConfigPrepStmt.setString(6, blocking);
outboundProConfigPrepStmt.addBatch();
}
}
outboundProConfigPrepStmt.executeBatch();
} finally {
IdentityApplicationManagementUtil.closeStatement(outboundProConfigPrepStmt);
}
}
/**
* @param applicationId
* @param connection
* @return
* @throws SQLException
*/
private InboundProvisioningConfig getInboundProvisioningConfiguration(int applicationId,
Connection connection, int tenantID) throws SQLException {
PreparedStatement inboundProConfigPrepStmt = null;
InboundProvisioningConfig inBoundProvisioningConfig = new InboundProvisioningConfig();
ResultSet resultSet = null;
try {
inboundProConfigPrepStmt = connection
.prepareStatement(ApplicationMgtDBQueries.LOAD_PRO_PROPERTIES_BY_APP_ID);
// PROVISIONING_USERSTORE_DOMAIN
inboundProConfigPrepStmt.setInt(1, tenantID);
inboundProConfigPrepStmt.setInt(2, applicationId);
resultSet = inboundProConfigPrepStmt.executeQuery();
while (resultSet.next()) {
inBoundProvisioningConfig.setProvisioningUserStore(resultSet.getString(1));
inBoundProvisioningConfig.setDumbMode("1".equals(resultSet.getString(2)));
}
} finally {
IdentityApplicationManagementUtil.closeStatement(inboundProConfigPrepStmt);
}
return inBoundProvisioningConfig;
}
/**
* @param applicationId
* @param connection
* @return
* @throws SQLException
*/
private OutboundProvisioningConfig getOutboundProvisioningConfiguration(int applicationId,
Connection connection, int tenantID) throws SQLException {
PreparedStatement outboundProConfigPrepStmt = null;
OutboundProvisioningConfig outBoundProvisioningConfig = new OutboundProvisioningConfig();
ResultSet resultSet = null;
List<IdentityProvider> idpProConnectors = new ArrayList<IdentityProvider>();
try {
outboundProConfigPrepStmt = connection
.prepareStatement(ApplicationMgtDBQueries.LOAD_PRO_CONNECTORS_BY_APP_ID);
// IDP_NAME, CONNECTOR_NAM
outboundProConfigPrepStmt.setInt(1, applicationId);
outboundProConfigPrepStmt.setInt(2, tenantID);
resultSet = outboundProConfigPrepStmt.executeQuery();
while (resultSet.next()) {
ProvisioningConnectorConfig proConnector = null;
IdentityProvider fedIdp = null;
fedIdp = new IdentityProvider();
fedIdp.setIdentityProviderName(resultSet.getString(1));
proConnector = new ProvisioningConnectorConfig();
proConnector.setName(resultSet.getString(2));
if ("1".equals(resultSet.getString(3))) {
JustInTimeProvisioningConfig jitConfig = new JustInTimeProvisioningConfig();
jitConfig.setProvisioningEnabled(true);
fedIdp.setJustInTimeProvisioningConfig(jitConfig);
}
if ("1".equals(resultSet.getString(4))) {
proConnector.setBlocking(true);
} else {
proConnector.setBlocking(false);
}
fedIdp.setDefaultProvisioningConnectorConfig(proConnector);
idpProConnectors.add(fedIdp);
}
outBoundProvisioningConfig.setProvisioningIdentityProviders(idpProConnectors
.toArray(new IdentityProvider[idpProConnectors
.size()]));
} finally {
IdentityApplicationManagementUtil.closeStatement(outboundProConfigPrepStmt);
}
return outBoundProvisioningConfig;
}
/**
* @param applicationId
* @param localAndOutboundAuthConfig
* @param connection
* @throws SQLException
* @throws IdentityApplicationManagementException
*/
private void updateLocalAndOutboundAuthenticationConfiguration(int applicationId,
LocalAndOutboundAuthenticationConfig localAndOutboundAuthConfig, Connection connection)
throws SQLException, IdentityApplicationManagementException {
int tenantID = CarbonContext.getThreadLocalCarbonContext().getTenantId();
PreparedStatement updateAuthTypePrepStmt = null;
if (localAndOutboundAuthConfig == null) {
// no local or out-bound configuration for this service provider.
return;
}
PreparedStatement storeSendAuthListOfIdPsPrepStmt = null;
try {
storeSendAuthListOfIdPsPrepStmt = connection
.prepareStatement(ApplicationMgtDBQueries.UPDATE_BASIC_APPINFO_WITH_SEND_AUTH_LIST_OF_IDPS);
// IS_SEND_LOCAL_SUBJECT_ID=? WHERE TENANT_ID= ? AND ID = ?
storeSendAuthListOfIdPsPrepStmt.setString(1, localAndOutboundAuthConfig
.isAlwaysSendBackAuthenticatedListOfIdPs() ? "1" : "0");
storeSendAuthListOfIdPsPrepStmt.setInt(2, tenantID);
storeSendAuthListOfIdPsPrepStmt.setInt(3, applicationId);
storeSendAuthListOfIdPsPrepStmt.executeUpdate();
} finally {
IdentityApplicationManagementUtil.closeStatement(storeSendAuthListOfIdPsPrepStmt);
}
PreparedStatement storeUseTenantDomainInLocalSubjectIdStmt = null;
try {
storeUseTenantDomainInLocalSubjectIdStmt = connection
.prepareStatement(ApplicationMgtDBQueries
.UPDATE_BASIC_APPINFO_WITH_USE_TENANT_DOMAIN_LOCAL_SUBJECT_ID);
// IS_USE_TENANT_DIMAIN_LOCAL_SUBJECT_ID=? WHERE TENANT_ID= ? AND ID = ?
storeUseTenantDomainInLocalSubjectIdStmt.setString(1, localAndOutboundAuthConfig
.isUseTenantDomainInLocalSubjectIdentifier() ? "1" : "0");
storeUseTenantDomainInLocalSubjectIdStmt.setInt(2, tenantID);
storeUseTenantDomainInLocalSubjectIdStmt.setInt(3, applicationId);
storeUseTenantDomainInLocalSubjectIdStmt.executeUpdate();
} finally {
IdentityApplicationManagementUtil.closeStatement(storeUseTenantDomainInLocalSubjectIdStmt);
}
PreparedStatement storeUseUserstoreDomainInLocalSubjectIdStmt = null;
try {
storeUseUserstoreDomainInLocalSubjectIdStmt = connection
.prepareStatement(ApplicationMgtDBQueries
.UPDATE_BASIC_APPINFO_WITH_USE_USERSTORE_DOMAIN_LOCAL_SUBJECT_ID);
// IS_USE_USERSTORE_DIMAIN_LOCAL_SUBJECT_ID=? WHERE TENANT_ID= ? AND ID = ?
storeUseUserstoreDomainInLocalSubjectIdStmt.setString(1, localAndOutboundAuthConfig
.isUseUserstoreDomainInLocalSubjectIdentifier() ? "1" : "0");
storeUseUserstoreDomainInLocalSubjectIdStmt.setInt(2, tenantID);
storeUseUserstoreDomainInLocalSubjectIdStmt.setInt(3, applicationId);
storeUseUserstoreDomainInLocalSubjectIdStmt.executeUpdate();
} finally {
IdentityApplicationManagementUtil.closeStatement(storeUseUserstoreDomainInLocalSubjectIdStmt);
}
PreparedStatement storeSubjectClaimUri = null;
try {
storeSubjectClaimUri = connection
.prepareStatement(ApplicationMgtDBQueries.UPDATE_BASIC_APPINFO_WITH_SUBJECT_CLAIM_URI);
// SUBJECT_CLAIM_URI=? WHERE TENANT_ID= ? AND ID = ?
storeSubjectClaimUri.setString(1, localAndOutboundAuthConfig.getSubjectClaimUri());
storeSubjectClaimUri.setInt(2, tenantID);
storeSubjectClaimUri.setInt(3, applicationId);
storeSubjectClaimUri.executeUpdate();
} finally {
IdentityApplicationManagementUtil.closeStatement(storeSubjectClaimUri);
}
AuthenticationStep[] authSteps = localAndOutboundAuthConfig.getAuthenticationSteps();
if (authSteps == null || authSteps.length == 0) {
// if no authentication steps defined - it should be the default behavior.
localAndOutboundAuthConfig
.setAuthenticationType(ApplicationConstants.AUTH_TYPE_DEFAULT);
}
try {
if (localAndOutboundAuthConfig.getAuthenticationType() == null) {
// no authentication type defined - set to default.
localAndOutboundAuthConfig
.setAuthenticationType(ApplicationConstants.AUTH_TYPE_DEFAULT);
}
updateAuthTypePrepStmt = connection
.prepareStatement(ApplicationMgtDBQueries.UPDATE_BASIC_APPINFO_WITH_AUTH_TYPE);
// AUTH_TYPE=? WHERE TENANT_ID= ? AND ID = ?
updateAuthTypePrepStmt.setString(1, localAndOutboundAuthConfig.getAuthenticationType());
updateAuthTypePrepStmt.setInt(2, tenantID);
updateAuthTypePrepStmt.setInt(3, applicationId);
updateAuthTypePrepStmt.execute();
} finally {
IdentityApplicationManagementUtil.closeStatement(updateAuthTypePrepStmt);
}
if (authSteps != null && authSteps.length > 0) {
// we have authentications steps defined.
PreparedStatement storeStepIDPAuthnPrepStmt = null;
storeStepIDPAuthnPrepStmt = connection
.prepareStatement(ApplicationMgtDBQueries.STORE_STEP_IDP_AUTH);
try {
if (ApplicationConstants.AUTH_TYPE_LOCAL
.equalsIgnoreCase(localAndOutboundAuthConfig.getAuthenticationType())) {
// for local authentication there can only be only one authentication step and
// only one local authenticator.
if (authSteps.length != 1
|| authSteps[0] == null
|| authSteps[0].getLocalAuthenticatorConfigs() == null
|| authSteps[0].getLocalAuthenticatorConfigs().length != 1
|| (authSteps[0].getFederatedIdentityProviders() != null && authSteps[0]
.getFederatedIdentityProviders().length >= 1)) {
String errorMessage = "Invalid local authentication configuration."
+ " For local authentication there can only be only one authentication step and only one local authenticator";
throw new IdentityApplicationManagementException(errorMessage);
}
} else if (ApplicationConstants.AUTH_TYPE_FEDERATED
.equalsIgnoreCase(localAndOutboundAuthConfig.getAuthenticationType())) {
// for federated authentication there can only be only one authentication step
// and only one federated authenticator - which is the default authenticator of
// the corresponding authenticator.
if (authSteps.length != 1 || authSteps[0] == null
|| authSteps[0].getFederatedIdentityProviders() == null
|| authSteps[0].getFederatedIdentityProviders().length != 1
|| authSteps[0].getLocalAuthenticatorConfigs().length > 0) {
String errorMessage = "Invalid federated authentication configuration."
+ " For federated authentication there can only be only one authentication step and only one federated authenticator";
throw new IdentityApplicationManagementException(errorMessage);
}
IdentityProvider fedIdp = authSteps[0].getFederatedIdentityProviders()[0];
if (fedIdp.getDefaultAuthenticatorConfig() == null || fedIdp.getFederatedAuthenticatorConfigs() == null) {
IdentityProviderDAO idpDAO = ApplicationMgtSystemConfig.getInstance().getIdentityProviderDAO();
String defualtAuthName = idpDAO.getDefaultAuthenticator(fedIdp
.getIdentityProviderName());
// set the default authenticator.
FederatedAuthenticatorConfig defaultAuth = new FederatedAuthenticatorConfig();
defaultAuth.setName(defualtAuthName);
fedIdp.setDefaultAuthenticatorConfig(defaultAuth);
fedIdp.setFederatedAuthenticatorConfigs(new FederatedAuthenticatorConfig[]{defaultAuth});
}
}
// iterating through each step.
for (AuthenticationStep authStep : authSteps) {
int stepId = 0;
IdentityProvider[] federatedIdps = authStep.getFederatedIdentityProviders();
// an authentication step should have at least one federated identity
// provider or a local authenticator.
if ((federatedIdps == null || federatedIdps.length == 0)
&& (authStep.getLocalAuthenticatorConfigs() == null || authStep
.getLocalAuthenticatorConfigs().length == 0)) {
String errorMesssage = "Invalid authentication configuration."
+ "An authentication step should have at least one federated identity "
+ "provider or a local authenticator";
throw new IdentityApplicationManagementException(errorMesssage);
}
// we have valid federated identity providers.
PreparedStatement storeStepPrepStmtz = null;
ResultSet result = null;
try {
String dbProductName = connection.getMetaData().getDatabaseProductName();
storeStepPrepStmtz = connection.prepareStatement(
ApplicationMgtDBQueries.STORE_STEP_INFO, new String[]{
DBUtils.getConvertedAutoGeneratedColumnName(dbProductName, "ID")});
// TENANT_ID, STEP_ORDER, APP_ID
storeStepPrepStmtz.setInt(1, tenantID);
storeStepPrepStmtz.setInt(2, authStep.getStepOrder());
storeStepPrepStmtz.setInt(3, applicationId);
storeStepPrepStmtz.setString(4, authStep.isSubjectStep() ? "1" : "0");
storeStepPrepStmtz.setString(5, authStep.isAttributeStep() ? "1" : "0");
storeStepPrepStmtz.execute();
result = storeStepPrepStmtz.getGeneratedKeys();
if (result.next()) {
stepId = result.getInt(1);
}
} finally {
IdentityApplicationManagementUtil.closeResultSet(result);
IdentityApplicationManagementUtil.closeStatement(storeStepPrepStmtz);
}
if (authStep.getLocalAuthenticatorConfigs() != null
&& authStep.getLocalAuthenticatorConfigs().length > 0) {
for (LocalAuthenticatorConfig lclAuthenticator : authStep
.getLocalAuthenticatorConfigs()) {
// set the identity provider name to LOCAL.
int authenticatorId = getAuthentictorID(connection, tenantID,
ApplicationConstants.LOCAL_IDP_NAME, lclAuthenticator.getName());
if (authenticatorId < 0) {
authenticatorId = addAuthenticator(connection, tenantID,
ApplicationConstants.LOCAL_IDP_NAME,
lclAuthenticator.getName(),
lclAuthenticator.getDisplayName());
}
if (authenticatorId > 0) {
// ID, TENANT_ID, AUTHENTICATOR_ID
storeStepIDPAuthnPrepStmt.setInt(1, stepId);
storeStepIDPAuthnPrepStmt.setInt(2, tenantID);
storeStepIDPAuthnPrepStmt.setInt(3, authenticatorId);
storeStepIDPAuthnPrepStmt.addBatch();
}
if (log.isDebugEnabled()) {
log.debug("Updating Local IdP of Application " + applicationId
+ " Step Order: " + authStep.getStepOrder() + " IdP: "
+ ApplicationConstants.LOCAL_IDP + " Authenticator: "