20
20
21
21
import org .apache .commons .lang .ArrayUtils ;
22
22
import org .apache .commons .lang .StringUtils ;
23
+ import org .wso2 .carbon .identity .api .server .common .ContextLoader ;
23
24
import org .wso2 .carbon .identity .api .server .idp .common .Constants ;
24
25
import org .wso2 .carbon .identity .api .server .idp .common .IdentityProviderServiceHolder ;
25
26
import org .wso2 .carbon .identity .api .server .idp .v1 .model .AuthenticationType ;
26
27
import org .wso2 .carbon .identity .api .server .idp .v1 .model .Endpoint ;
27
28
import org .wso2 .carbon .identity .api .server .idp .v1 .model .FederatedAuthenticator ;
29
+ import org .wso2 .carbon .identity .api .server .idp .v1 .model .FederatedAuthenticatorListItem ;
28
30
import org .wso2 .carbon .identity .api .server .idp .v1 .model .FederatedAuthenticatorPUTRequest ;
29
31
import org .wso2 .carbon .identity .application .common .ApplicationAuthenticatorService ;
30
32
import org .wso2 .carbon .identity .application .common .model .FederatedAuthenticatorConfig ;
38
40
import org .wso2 .carbon .idp .mgt .IdentityProviderManagementServerException ;
39
41
40
42
import java .nio .charset .StandardCharsets ;
43
+ import java .util .ArrayList ;
41
44
import java .util .Arrays ;
42
45
import java .util .Base64 ;
43
46
import java .util .List ;
47
50
import java .util .function .Function ;
48
51
import java .util .stream .Collectors ;
49
52
53
+ import static org .wso2 .carbon .identity .api .server .common .Constants .V1_API_PATH_COMPONENT ;
54
+ import static org .wso2 .carbon .identity .api .server .common .Util .base64URLEncode ;
50
55
import static org .wso2 .carbon .identity .api .server .idp .common .Constants .GOOGLE_PRIVATE_KEY ;
56
+ import static org .wso2 .carbon .identity .api .server .idp .common .Constants .IDP_PATH_COMPONENT ;
51
57
52
58
/**
53
59
* The factory class for building federated authenticator configuration related models.
@@ -118,15 +124,9 @@ public static FederatedAuthenticator build(FederatedAuthenticatorConfig config)
118
124
119
125
federatedAuthenticator .setName (config .getName ());
120
126
federatedAuthenticator .setIsEnabled (config .isEnabled ());
121
-
122
- FederatedAuthenticatorConfig federatedAuthenticatorConfig =
123
- ApplicationAuthenticatorService .getInstance ().getFederatedAuthenticatorByName (
124
- config .getName ());
125
- if (federatedAuthenticatorConfig != null ) {
126
- String [] tags = federatedAuthenticatorConfig .getTags ();
127
- if (ArrayUtils .isNotEmpty (tags )) {
128
- federatedAuthenticator .setTags (Arrays .asList (tags ));
129
- }
127
+ String [] tags = resolveAuthenticatorTags (config );
128
+ if (ArrayUtils .isNotEmpty (tags )) {
129
+ federatedAuthenticator .setTags (Arrays .asList (tags ));
130
130
}
131
131
132
132
if (DefinedByType .SYSTEM == config .getDefinedByType ()) {
@@ -141,6 +141,37 @@ public static FederatedAuthenticator build(FederatedAuthenticatorConfig config)
141
141
142
142
return federatedAuthenticator ;
143
143
}
144
+
145
+ /**
146
+ * Builds a list of FederatedAuthenticatorListItem instances based on the given array of
147
+ * FederatedAuthenticatorConfig.
148
+ *
149
+ * @param fedAuthConfigs Array of FederatedAuthenticatorConfig instances.
150
+ * @return List of FederatedAuthenticatorListItem instances.
151
+ */
152
+ public static List <FederatedAuthenticatorListItem > build (FederatedAuthenticatorConfig [] fedAuthConfigs ,
153
+ String idpResourceId ) {
154
+
155
+ List <FederatedAuthenticatorListItem > authenticators = new ArrayList <>();
156
+ for (FederatedAuthenticatorConfig config : fedAuthConfigs ) {
157
+ FederatedAuthenticatorListItem authenticatorListItem = new FederatedAuthenticatorListItem ();
158
+ authenticatorListItem .setAuthenticatorId (base64URLEncode (config .getName ()));
159
+ authenticatorListItem .setName (config .getName ());
160
+ authenticatorListItem .setIsEnabled (config .isEnabled ());
161
+ authenticatorListItem .definedBy (FederatedAuthenticatorListItem .DefinedByEnum .valueOf (
162
+ config .getDefinedByType ().toString ()));
163
+ String [] tags = resolveAuthenticatorTags (config );
164
+ if (ArrayUtils .isNotEmpty (tags )) {
165
+ authenticatorListItem .setTags (Arrays .asList (tags ));
166
+ }
167
+ authenticatorListItem .setSelf (ContextLoader .buildURIForBody (String .format (V1_API_PATH_COMPONENT +
168
+ IDP_PATH_COMPONENT + "/%s/federated-authenticators/%s" , idpResourceId ,
169
+ base64URLEncode (config .getName ()))).toString ());
170
+ authenticators .add (authenticatorListItem );
171
+ }
172
+
173
+ return authenticators ;
174
+ }
144
175
145
176
private static FederatedAuthenticatorConfig createFederatedAuthenticatorConfig (Config config )
146
177
throws IdentityProviderManagementClientException {
@@ -435,6 +466,23 @@ private static void resolveEndpointConfiguration(FederatedAuthenticator authenti
435
466
}
436
467
}
437
468
469
+ private static String [] resolveAuthenticatorTags (FederatedAuthenticatorConfig config ) {
470
+
471
+ /* If the authenticator is defined by the user, return the tags of the authenticator config. Otherwise, return
472
+ the tags of the system registered federated authenticator template.
473
+ */
474
+ if (DefinedByType .USER == config .getDefinedByType ()) {
475
+ return config .getTags ();
476
+
477
+ }
478
+ FederatedAuthenticatorConfig federatedAuthenticatorConfig =
479
+ ApplicationAuthenticatorService .getInstance ().getFederatedAuthenticatorByName (config .getName ());
480
+ if (federatedAuthenticatorConfig != null ) {
481
+ return federatedAuthenticatorConfig .getTags ();
482
+ }
483
+ return new String [0 ];
484
+ }
485
+
438
486
/**
439
487
* Config class to build FederatedAuthenticatorConfig.
440
488
*/
0 commit comments