keycloak-service
静的公開メンバ関数 | 静的非公開メンバ関数 | 全メンバ一覧
org.keycloak.exportimport.util.ExportUtils クラス
org.keycloak.exportimport.util.ExportUtils 連携図
Collaboration graph

静的公開メンバ関数

static RealmRepresentation exportRealm (KeycloakSession session, RealmModel realm, boolean includeUsers, boolean internal)
 
static RealmRepresentation exportRealm (KeycloakSession session, RealmModel realm, ExportOptions options, boolean internal)
 
static MultivaluedHashMap< String, ComponentExportRepresentation > exportComponents (RealmModel realm, String parentId)
 
static ClientRepresentation exportClient (KeycloakSession session, ClientModel client)
 
static ResourceServerRepresentation exportAuthorizationSettings (KeycloakSession session, ClientModel client)
 
static List< RoleRepresentation > exportRoles (Collection< RoleModel > roles)
 
static List< String > getRoleNames (Collection< RoleModel > roles)
 
static RoleRepresentation exportRole (RoleModel role)
 
static UserRepresentation exportUser (KeycloakSession session, RealmModel realm, UserModel user, ExportOptions options)
 
static FederatedIdentityRepresentation exportSocialLink (FederatedIdentityModel socialLink)
 
static CredentialRepresentation exportCredential (CredentialModel userCred)
 
static void exportUsersToStream (KeycloakSession session, RealmModel realm, List< UserModel > usersToExport, ObjectMapper mapper, OutputStream os) throws IOException
 
static void exportUsersToStream (KeycloakSession session, RealmModel realm, List< UserModel > usersToExport, ObjectMapper mapper, OutputStream os, ExportOptions options) throws IOException
 
static void exportFederatedUsersToStream (KeycloakSession session, RealmModel realm, List< String > usersToExport, ObjectMapper mapper, OutputStream os) throws IOException
 
static void exportFederatedUsersToStream (KeycloakSession session, RealmModel realm, List< String > usersToExport, ObjectMapper mapper, OutputStream os, ExportOptions options) throws IOException
 
static UserRepresentation exportFederatedUser (KeycloakSession session, RealmModel realm, String id, ExportOptions options)
 

静的非公開メンバ関数

static PolicyRepresentation createPolicyRepresentation (AuthorizationProvider authorizationProvider, Policy policy)
 

詳解

著者
Marek Posolda

関数詳解

◆ createPolicyRepresentation()

static PolicyRepresentation org.keycloak.exportimport.util.ExportUtils.createPolicyRepresentation ( AuthorizationProvider  authorizationProvider,
Policy  policy 
)
inlinestaticprivate
369  {
370  try {
371  PolicyRepresentation rep = toRepresentation(policy, authorizationProvider, true, true);
372 
373  Map<String, String> config = new HashMap<>(rep.getConfig());
374 
375  rep.setConfig(config);
376 
377  Set<Scope> scopes = policy.getScopes();
378 
379  if (!scopes.isEmpty()) {
380  List<String> scopeNames = scopes.stream().map(Scope::getName).collect(Collectors.toList());
381  config.put("scopes", JsonSerialization.writeValueAsString(scopeNames));
382  }
383 
384  Set<Resource> policyResources = policy.getResources();
385 
386  if (!policyResources.isEmpty()) {
387  List<String> resourceNames = policyResources.stream().map(Resource::getName).collect(Collectors.toList());
388  config.put("resources", JsonSerialization.writeValueAsString(resourceNames));
389  }
390 
391  Set<Policy> associatedPolicies = policy.getAssociatedPolicies();
392 
393  if (!associatedPolicies.isEmpty()) {
394  config.put("applyPolicies", JsonSerialization.writeValueAsString(associatedPolicies.stream().map(associated -> associated.getName()).collect(Collectors.toList())));
395  }
396 
397  return rep;
398  } catch (Exception e) {
399  throw new RuntimeException("Error while exporting policy [" + policy.getName() + "].", e);
400  }
401  }

◆ exportAuthorizationSettings()

static ResourceServerRepresentation org.keycloak.exportimport.util.ExportUtils.exportAuthorizationSettings ( KeycloakSession  session,
ClientModel  client 
)
inlinestatic
308  {
309  AuthorizationProviderFactory providerFactory = (AuthorizationProviderFactory) session.getKeycloakSessionFactory().getProviderFactory(AuthorizationProvider.class);
310  AuthorizationProvider authorization = providerFactory.create(session, client.getRealm());
311  StoreFactory storeFactory = authorization.getStoreFactory();
312  ResourceServer settingsModel = authorization.getStoreFactory().getResourceServerStore().findById(client.getId());
313 
314  if (settingsModel == null) {
315  return null;
316  }
317 
318  ResourceServerRepresentation representation = toRepresentation(settingsModel, client);
319 
320  representation.setId(null);
321  representation.setName(null);
322  representation.setClientId(null);
323 
324  List<ResourceRepresentation> resources = storeFactory.getResourceStore().findByResourceServer(settingsModel.getId())
325  .stream().map(resource -> {
326  ResourceRepresentation rep = toRepresentation(resource, settingsModel, authorization);
327 
328  if (rep.getOwner().getId().equals(settingsModel.getId())) {
329  rep.setOwner((ResourceOwnerRepresentation) null);
330  } else {
331  rep.getOwner().setId(null);
332  }
333  rep.getScopes().forEach(scopeRepresentation -> {
334  scopeRepresentation.setId(null);
335  scopeRepresentation.setIconUri(null);
336  });
337 
338  return rep;
339  }).collect(Collectors.toList());
340 
341  representation.setResources(resources);
342 
343  List<PolicyRepresentation> policies = new ArrayList<>();
344  PolicyStore policyStore = storeFactory.getPolicyStore();
345 
346  policies.addAll(policyStore.findByResourceServer(settingsModel.getId())
347  .stream().filter(policy -> !policy.getType().equals("resource") && !policy.getType().equals("scope") && policy.getOwner() == null)
348  .map(policy -> createPolicyRepresentation(authorization, policy)).collect(Collectors.toList()));
349  policies.addAll(policyStore.findByResourceServer(settingsModel.getId())
350  .stream().filter(policy -> (policy.getType().equals("resource") || policy.getType().equals("scope") && policy.getOwner() == null))
351  .map(policy -> createPolicyRepresentation(authorization, policy)).collect(Collectors.toList()));
352 
353  representation.setPolicies(policies);
354 
355  List<ScopeRepresentation> scopes = storeFactory.getScopeStore().findByResourceServer(settingsModel.getId()).stream().map(scope -> {
356  ScopeRepresentation rep = toRepresentation(scope);
357 
358  rep.setPolicies(null);
359  rep.setResources(null);
360 
361  return rep;
362  }).collect(Collectors.toList());
363 
364  representation.setScopes(scopes);
365 
366  return representation;
367  }
static PolicyRepresentation createPolicyRepresentation(AuthorizationProvider authorizationProvider, Policy policy)
Definition: ExportUtils.java:369

◆ exportClient()

static ClientRepresentation org.keycloak.exportimport.util.ExportUtils.exportClient ( KeycloakSession  session,
ClientModel  client 
)
inlinestatic

Full export of application including claims and secret

引数
client
戻り値
full ApplicationRepresentation
301  {
302  ClientRepresentation clientRep = ModelToRepresentation.toRepresentation(client, session);
303  clientRep.setSecret(client.getSecret());
304  clientRep.setAuthorizationSettings(exportAuthorizationSettings(session,client));
305  return clientRep;
306  }
static ResourceServerRepresentation exportAuthorizationSettings(KeycloakSession session, ClientModel client)
Definition: ExportUtils.java:308

◆ exportComponents()

static MultivaluedHashMap<String, ComponentExportRepresentation> org.keycloak.exportimport.util.ExportUtils.exportComponents ( RealmModel  realm,
String  parentId 
)
inlinestatic
280  {
281  List<ComponentModel> componentList = realm.getComponents(parentId);
282  MultivaluedHashMap<String, ComponentExportRepresentation> components = new MultivaluedHashMap<>();
283  for (ComponentModel component : componentList) {
284  ComponentExportRepresentation compRep = new ComponentExportRepresentation();
285  compRep.setId(component.getId());
286  compRep.setProviderId(component.getProviderId());
287  compRep.setConfig(component.getConfig());
288  compRep.setName(component.getName());
289  compRep.setSubType(component.getSubType());
290  compRep.setSubComponents(exportComponents(realm, component.getId()));
291  components.add(component.getProviderType(), compRep);
292  }
293  return components;
294  }
static MultivaluedHashMap< String, ComponentExportRepresentation > exportComponents(RealmModel realm, String parentId)
Definition: ExportUtils.java:280

◆ exportCredential()

static CredentialRepresentation org.keycloak.exportimport.util.ExportUtils.exportCredential ( CredentialModel  userCred)
inlinestatic
571  {
572  CredentialRepresentation credRep = new CredentialRepresentation();
573  credRep.setType(userCred.getType());
574  credRep.setDevice(userCred.getDevice());
575  credRep.setHashedSaltedValue(userCred.getValue());
576  if (userCred.getSalt() != null) credRep.setSalt(Base64.encodeBytes(userCred.getSalt()));
577  credRep.setHashIterations(userCred.getHashIterations());
578  credRep.setCounter(userCred.getCounter());
579  credRep.setAlgorithm(userCred.getAlgorithm());
580  credRep.setDigits(userCred.getDigits());
581  credRep.setCreatedDate(userCred.getCreatedDate());
582  credRep.setConfig(userCred.getConfig());
583  credRep.setPeriod(userCred.getPeriod());
584  return credRep;
585  }

◆ exportFederatedUser()

static UserRepresentation org.keycloak.exportimport.util.ExportUtils.exportFederatedUser ( KeycloakSession  session,
RealmModel  realm,
String  id,
ExportOptions  options 
)
inlinestatic

Full export of user data stored in federated storage (including role mappings and credentials)

引数
id
戻り値
fully exported user representation
653  {
654  UserRepresentation userRep = new UserRepresentation();
655  userRep.setId(id);
656  MultivaluedHashMap<String, String> attributes = session.userFederatedStorage().getAttributes(realm, id);
657  if (attributes.size() > 0) {
658  Map<String, List<String>> attrs = new HashMap<>();
659  attrs.putAll(attributes);
660  userRep.setAttributes(attrs);
661  }
662 
663  Set<String> requiredActions = session.userFederatedStorage().getRequiredActions(realm, id);
664  if (requiredActions.size() > 0) {
665  List<String> actions = new LinkedList<>();
666  actions.addAll(requiredActions);
667  userRep.setRequiredActions(actions);
668  }
669 
670 
671  // Social links
672  Set<FederatedIdentityModel> socialLinks = session.userFederatedStorage().getFederatedIdentities(id, realm);
673  List<FederatedIdentityRepresentation> socialLinkReps = new ArrayList<FederatedIdentityRepresentation>();
674  for (FederatedIdentityModel socialLink : socialLinks) {
675  FederatedIdentityRepresentation socialLinkRep = exportSocialLink(socialLink);
676  socialLinkReps.add(socialLinkRep);
677  }
678  if (socialLinkReps.size() > 0) {
679  userRep.setFederatedIdentities(socialLinkReps);
680  }
681 
682  // Role mappings
683  if (options.isGroupsAndRolesIncluded()) {
684  Set<RoleModel> roles = session.userFederatedStorage().getRoleMappings(realm, id);
685  List<String> realmRoleNames = new ArrayList<>();
686  Map<String, List<String>> clientRoleNames = new HashMap<>();
687  for (RoleModel role : roles) {
688  if (role.getContainer() instanceof RealmModel) {
689  realmRoleNames.add(role.getName());
690  } else {
691  ClientModel client = (ClientModel) role.getContainer();
692  String clientId = client.getClientId();
693  List<String> currentClientRoles = clientRoleNames.get(clientId);
694  if (currentClientRoles == null) {
695  currentClientRoles = new ArrayList<>();
696  clientRoleNames.put(clientId, currentClientRoles);
697  }
698 
699  currentClientRoles.add(role.getName());
700  }
701  }
702 
703  if (realmRoleNames.size() > 0) {
704  userRep.setRealmRoles(realmRoleNames);
705  }
706  if (clientRoleNames.size() > 0) {
707  userRep.setClientRoles(clientRoleNames);
708  }
709  }
710 
711  // Credentials
712  List<CredentialModel> creds = session.userFederatedStorage().getStoredCredentials(realm, id);
713  List<CredentialRepresentation> credReps = new ArrayList<CredentialRepresentation>();
714  for (CredentialModel cred : creds) {
715  CredentialRepresentation credRep = exportCredential(cred);
716  credReps.add(credRep);
717  }
718  userRep.setCredentials(credReps);
719 
720  // Grants
721  List<UserConsentModel> consents = session.users().getConsents(realm, id);
722  LinkedList<UserConsentRepresentation> consentReps = new LinkedList<UserConsentRepresentation>();
723  for (UserConsentModel consent : consents) {
724  UserConsentRepresentation consentRep = ModelToRepresentation.toRepresentation(consent);
725  consentReps.add(consentRep);
726  }
727  if (consentReps.size() > 0) {
728  userRep.setClientConsents(consentReps);
729  }
730 
731  // Not Before
732  int notBefore = session.userFederatedStorage().getNotBeforeOfUser(realm, userRep.getId());
733  userRep.setNotBefore(notBefore);
734 
735  if (options.isGroupsAndRolesIncluded()) {
736  List<String> groups = new LinkedList<>();
737  for (GroupModel group : session.userFederatedStorage().getGroups(realm, id)) {
738  groups.add(ModelToRepresentation.buildGroupPath(group));
739  }
740  userRep.setGroups(groups);
741  }
742  return userRep;
743  }
static CredentialRepresentation exportCredential(CredentialModel userCred)
Definition: ExportUtils.java:571
static FederatedIdentityRepresentation exportSocialLink(FederatedIdentityModel socialLink)
Definition: ExportUtils.java:563

◆ exportFederatedUsersToStream() [1/2]

static void org.keycloak.exportimport.util.ExportUtils.exportFederatedUsersToStream ( KeycloakSession  session,
RealmModel  realm,
List< String >  usersToExport,
ObjectMapper  mapper,
OutputStream  os 
) throws IOException
inlinestatic
618  {
619  exportFederatedUsersToStream(session, realm, usersToExport, mapper, os, new ExportOptions());
620  }
static void exportFederatedUsersToStream(KeycloakSession session, RealmModel realm, List< String > usersToExport, ObjectMapper mapper, OutputStream os)
Definition: ExportUtils.java:618

◆ exportFederatedUsersToStream() [2/2]

static void org.keycloak.exportimport.util.ExportUtils.exportFederatedUsersToStream ( KeycloakSession  session,
RealmModel  realm,
List< String >  usersToExport,
ObjectMapper  mapper,
OutputStream  os,
ExportOptions  options 
) throws IOException
inlinestatic
622  {
623  JsonFactory factory = mapper.getFactory();
624  JsonGenerator generator = factory.createGenerator(os, JsonEncoding.UTF8);
625  try {
626  if (mapper.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
627  generator.useDefaultPrettyPrinter();
628  }
629  generator.writeStartObject();
630  generator.writeStringField("realm", realm.getName());
631  // generator.writeStringField("strategy", strategy.toString());
632  generator.writeFieldName("federatedUsers");
633  generator.writeStartArray();
634 
635  for (String userId : usersToExport) {
636  UserRepresentation userRep = ExportUtils.exportFederatedUser(session, realm, userId, options);
637  generator.writeObject(userRep);
638  }
639 
640  generator.writeEndArray();
641  generator.writeEndObject();
642  } finally {
643  generator.close();
644  }
645  }

◆ exportRealm() [1/2]

static RealmRepresentation org.keycloak.exportimport.util.ExportUtils.exportRealm ( KeycloakSession  session,
RealmModel  realm,
boolean  includeUsers,
boolean  internal 
)
inlinestatic
88  {
89  ExportOptions opts = new ExportOptions(false, true, true);
90  if (includeUsers) {
91  opts.setUsersIncluded(true);
92  }
93  return exportRealm(session, realm, opts, internal);
94  }
static RealmRepresentation exportRealm(KeycloakSession session, RealmModel realm, boolean includeUsers, boolean internal)
Definition: ExportUtils.java:88

◆ exportRealm() [2/2]

static RealmRepresentation org.keycloak.exportimport.util.ExportUtils.exportRealm ( KeycloakSession  session,
RealmModel  realm,
ExportOptions  options,
boolean  internal 
)
inlinestatic
96  {
97  RealmRepresentation rep = ModelToRepresentation.toRepresentation(realm, internal);
98  ModelToRepresentation.exportAuthenticationFlows(realm, rep);
99  ModelToRepresentation.exportRequiredActions(realm, rep);
100 
101  // Project/product version
102  rep.setKeycloakVersion(Version.VERSION);
103 
104  // Client Scopes
105  List<ClientScopeModel> clientScopeModels = realm.getClientScopes();
106  List<ClientScopeRepresentation> clientScopesReps = new ArrayList<>();
107  for (ClientScopeModel app : clientScopeModels) {
108  ClientScopeRepresentation clientRep = ModelToRepresentation.toRepresentation(app);
109  clientScopesReps.add(clientRep);
110  }
111  rep.setClientScopes(clientScopesReps);
112 
113  List<String> defaultClientScopeNames = realm.getDefaultClientScopes(true).stream().map((ClientScopeModel clientScope) -> {
114  return clientScope.getName();
115  }).collect(Collectors.toList());
116  rep.setDefaultDefaultClientScopes(defaultClientScopeNames);
117 
118  List<String> optionalClientScopeNames = realm.getDefaultClientScopes(false).stream().map((ClientScopeModel clientScope) -> {
119  return clientScope.getName();
120  }).collect(Collectors.toList());
121  rep.setDefaultOptionalClientScopes(optionalClientScopeNames);
122 
123  // Clients
124  List<ClientModel> clients = Collections.emptyList();
125 
126  if (options.isClientsIncluded()) {
127  clients = realm.getClients();
128  List<ClientRepresentation> clientReps = new ArrayList<>();
129  for (ClientModel app : clients) {
130  ClientRepresentation clientRep = exportClient(session, app);
131  clientReps.add(clientRep);
132  }
133  rep.setClients(clientReps);
134  }
135 
136  // Groups and Roles
137  if (options.isGroupsAndRolesIncluded()) {
138  ModelToRepresentation.exportGroups(realm, rep);
139 
140  List<RoleRepresentation> realmRoleReps = null;
141  Map<String, List<RoleRepresentation>> clientRolesReps = new HashMap<>();
142 
143  Set<RoleModel> realmRoles = realm.getRoles();
144  if (realmRoles != null && realmRoles.size() > 0) {
145  realmRoleReps = exportRoles(realmRoles);
146  }
147 
148  RolesRepresentation rolesRep = new RolesRepresentation();
149  if (realmRoleReps != null) {
150  rolesRep.setRealm(realmRoleReps);
151  }
152 
153  if (options.isClientsIncluded()) {
154  for (ClientModel client : clients) {
155  Set<RoleModel> currentAppRoles = client.getRoles();
156  List<RoleRepresentation> currentAppRoleReps = exportRoles(currentAppRoles);
157  clientRolesReps.put(client.getClientId(), currentAppRoleReps);
158  }
159  if (clientRolesReps.size() > 0) {
160  rolesRep.setClient(clientRolesReps);
161  }
162  }
163  rep.setRoles(rolesRep);
164  }
165 
166  // Scopes
167  Map<String, List<ScopeMappingRepresentation>> clientScopeReps = new HashMap<>();
168 
169  if (options.isClientsIncluded()) {
170  List<ClientModel> allClients = new ArrayList<>(clients);
171 
172  // Scopes of clients
173  for (ClientModel client : allClients) {
174  Set<RoleModel> clientScopes = client.getScopeMappings();
175  ScopeMappingRepresentation scopeMappingRep = null;
176  for (RoleModel scope : clientScopes) {
177  if (scope.getContainer() instanceof RealmModel) {
178  if (scopeMappingRep == null) {
179  scopeMappingRep = rep.clientScopeMapping(client.getClientId());
180  }
181  scopeMappingRep.role(scope.getName());
182  } else {
183  ClientModel app = (ClientModel) scope.getContainer();
184  String appName = app.getClientId();
185  List<ScopeMappingRepresentation> currentAppScopes = clientScopeReps.get(appName);
186  if (currentAppScopes == null) {
187  currentAppScopes = new ArrayList<>();
188  clientScopeReps.put(appName, currentAppScopes);
189  }
190 
191  ScopeMappingRepresentation currentClientScope = null;
192  for (ScopeMappingRepresentation scopeMapping : currentAppScopes) {
193  if (client.getClientId().equals(scopeMapping.getClient())) {
194  currentClientScope = scopeMapping;
195  break;
196  }
197  }
198  if (currentClientScope == null) {
199  currentClientScope = new ScopeMappingRepresentation();
200  currentClientScope.setClient(client.getClientId());
201  currentAppScopes.add(currentClientScope);
202  }
203  currentClientScope.role(scope.getName());
204  }
205  }
206  }
207  }
208 
209  // Scopes of client scopes
210  for (ClientScopeModel clientScope : realm.getClientScopes()) {
211  Set<RoleModel> clientScopes = clientScope.getScopeMappings();
212  ScopeMappingRepresentation scopeMappingRep = null;
213  for (RoleModel scope : clientScopes) {
214  if (scope.getContainer() instanceof RealmModel) {
215  if (scopeMappingRep == null) {
216  scopeMappingRep = rep.clientScopeScopeMapping(clientScope.getName());
217  }
218  scopeMappingRep.role(scope.getName());
219  } else {
220  ClientModel app = (ClientModel)scope.getContainer();
221  String appName = app.getClientId();
222  List<ScopeMappingRepresentation> currentAppScopes = clientScopeReps.get(appName);
223  if (currentAppScopes == null) {
224  currentAppScopes = new ArrayList<>();
225  clientScopeReps.put(appName, currentAppScopes);
226  }
227 
228  ScopeMappingRepresentation currentClientTemplateScope = null;
229  for (ScopeMappingRepresentation scopeMapping : currentAppScopes) {
230  if (clientScope.getName().equals(scopeMapping.getClientScope())) {
231  currentClientTemplateScope = scopeMapping;
232  break;
233  }
234  }
235  if (currentClientTemplateScope == null) {
236  currentClientTemplateScope = new ScopeMappingRepresentation();
237  currentClientTemplateScope.setClientScope(clientScope.getName());
238  currentAppScopes.add(currentClientTemplateScope);
239  }
240  currentClientTemplateScope.role(scope.getName());
241  }
242  }
243  }
244 
245  if (clientScopeReps.size() > 0) {
246  rep.setClientScopeMappings(clientScopeReps);
247  }
248 
249  // Finally users if needed
250  if (options.isUsersIncluded()) {
251  List<UserModel> allUsers = session.users().getUsers(realm, true);
252  List<UserRepresentation> users = new LinkedList<>();
253  for (UserModel user : allUsers) {
254  UserRepresentation userRep = exportUser(session, realm, user, options);
255  users.add(userRep);
256  }
257 
258  if (users.size() > 0) {
259  rep.setUsers(users);
260  }
261 
262  List<UserRepresentation> federatedUsers = new LinkedList<>();
263  for (String userId : session.userFederatedStorage().getStoredUsers(realm, 0, -1)) {
264  UserRepresentation userRep = exportFederatedUser(session, realm, userId, options);
265  federatedUsers.add(userRep);
266  }
267  if (federatedUsers.size() > 0) {
268  rep.setFederatedUsers(federatedUsers);
269  }
270 
271  }
272 
273  // components
274  MultivaluedHashMap<String, ComponentExportRepresentation> components = exportComponents(realm, realm.getId());
275  rep.setComponents(components);
276 
277  return rep;
278  }
static UserRepresentation exportFederatedUser(KeycloakSession session, RealmModel realm, String id, ExportOptions options)
Definition: ExportUtils.java:653
static UserRepresentation exportUser(KeycloakSession session, RealmModel realm, UserModel user, ExportOptions options)
Definition: ExportUtils.java:478
static ClientRepresentation exportClient(KeycloakSession session, ClientModel client)
Definition: ExportUtils.java:301
static List< RoleRepresentation > exportRoles(Collection< RoleModel > roles)
Definition: ExportUtils.java:403
static MultivaluedHashMap< String, ComponentExportRepresentation > exportComponents(RealmModel realm, String parentId)
Definition: ExportUtils.java:280

◆ exportRole()

static RoleRepresentation org.keycloak.exportimport.util.ExportUtils.exportRole ( RoleModel  role)
inlinestatic

Full export of role including composite roles

引数
role
戻り値
RoleRepresentation with all stuff filled (including composite roles)
426  {
427  RoleRepresentation roleRep = ModelToRepresentation.toRepresentation(role);
428 
429  Set<RoleModel> composites = role.getComposites();
430  if (composites != null && composites.size() > 0) {
431  Set<String> compositeRealmRoles = null;
432  Map<String, List<String>> compositeClientRoles = null;
433 
434  for (RoleModel composite : composites) {
435  RoleContainerModel crContainer = composite.getContainer();
436  if (crContainer instanceof RealmModel) {
437 
438  if (compositeRealmRoles == null) {
439  compositeRealmRoles = new HashSet<>();
440  }
441  compositeRealmRoles.add(composite.getName());
442  } else {
443  if (compositeClientRoles == null) {
444  compositeClientRoles = new HashMap<>();
445  }
446 
447  ClientModel app = (ClientModel)crContainer;
448  String appName = app.getClientId();
449  List<String> currentAppComposites = compositeClientRoles.get(appName);
450  if (currentAppComposites == null) {
451  currentAppComposites = new ArrayList<>();
452  compositeClientRoles.put(appName, currentAppComposites);
453  }
454  currentAppComposites.add(composite.getName());
455  }
456  }
457 
458  RoleRepresentation.Composites compRep = new RoleRepresentation.Composites();
459  if (compositeRealmRoles != null) {
460  compRep.setRealm(compositeRealmRoles);
461  }
462  if (compositeClientRoles != null) {
463  compRep.setClient(compositeClientRoles);
464  }
465 
466  roleRep.setComposites(compRep);
467  }
468 
469  return roleRep;
470  }

◆ exportRoles()

static List<RoleRepresentation> org.keycloak.exportimport.util.ExportUtils.exportRoles ( Collection< RoleModel >  roles)
inlinestatic
403  {
404  List<RoleRepresentation> roleReps = new ArrayList<RoleRepresentation>();
405 
406  for (RoleModel role : roles) {
407  RoleRepresentation roleRep = exportRole(role);
408  roleReps.add(roleRep);
409  }
410  return roleReps;
411  }
static RoleRepresentation exportRole(RoleModel role)
Definition: ExportUtils.java:426

◆ exportSocialLink()

static FederatedIdentityRepresentation org.keycloak.exportimport.util.ExportUtils.exportSocialLink ( FederatedIdentityModel  socialLink)
inlinestatic
563  {
564  FederatedIdentityRepresentation socialLinkRep = new FederatedIdentityRepresentation();
565  socialLinkRep.setIdentityProvider(socialLink.getIdentityProvider());
566  socialLinkRep.setUserId(socialLink.getUserId());
567  socialLinkRep.setUserName(socialLink.getUserName());
568  return socialLinkRep;
569  }

◆ exportUser()

static UserRepresentation org.keycloak.exportimport.util.ExportUtils.exportUser ( KeycloakSession  session,
RealmModel  realm,
UserModel  user,
ExportOptions  options 
)
inlinestatic

Full export of user (including role mappings and credentials)

引数
user
戻り値
fully exported user representation
478  {
479  UserRepresentation userRep = ModelToRepresentation.toRepresentation(session, realm, user);
480 
481  // Social links
482  Set<FederatedIdentityModel> socialLinks = session.users().getFederatedIdentities(user, realm);
483  List<FederatedIdentityRepresentation> socialLinkReps = new ArrayList<FederatedIdentityRepresentation>();
484  for (FederatedIdentityModel socialLink : socialLinks) {
485  FederatedIdentityRepresentation socialLinkRep = exportSocialLink(socialLink);
486  socialLinkReps.add(socialLinkRep);
487  }
488  if (socialLinkReps.size() > 0) {
489  userRep.setFederatedIdentities(socialLinkReps);
490  }
491 
492  // Role mappings
493  Set<RoleModel> roles = user.getRoleMappings();
494  List<String> realmRoleNames = new ArrayList<>();
495  Map<String, List<String>> clientRoleNames = new HashMap<>();
496  for (RoleModel role : roles) {
497  if (role.getContainer() instanceof RealmModel) {
498  realmRoleNames.add(role.getName());
499  } else {
500  ClientModel client = (ClientModel)role.getContainer();
501  String clientId = client.getClientId();
502  List<String> currentClientRoles = clientRoleNames.get(clientId);
503  if (currentClientRoles == null) {
504  currentClientRoles = new ArrayList<>();
505  clientRoleNames.put(clientId, currentClientRoles);
506  }
507 
508  currentClientRoles.add(role.getName());
509  }
510  }
511 
512  if (realmRoleNames.size() > 0) {
513  userRep.setRealmRoles(realmRoleNames);
514  }
515  if (clientRoleNames.size() > 0) {
516  userRep.setClientRoles(clientRoleNames);
517  }
518 
519  // Credentials
520  List<CredentialModel> creds = session.userCredentialManager().getStoredCredentials(realm, user);
521  List<CredentialRepresentation> credReps = new ArrayList<CredentialRepresentation>();
522  for (CredentialModel cred : creds) {
523  CredentialRepresentation credRep = exportCredential(cred);
524  credReps.add(credRep);
525  }
526  userRep.setCredentials(credReps);
527  userRep.setFederationLink(user.getFederationLink());
528 
529  // Grants
530  List<UserConsentModel> consents = session.users().getConsents(realm, user.getId());
531  LinkedList<UserConsentRepresentation> consentReps = new LinkedList<UserConsentRepresentation>();
532  for (UserConsentModel consent : consents) {
533  UserConsentRepresentation consentRep = ModelToRepresentation.toRepresentation(consent);
534  consentReps.add(consentRep);
535  }
536  if (consentReps.size() > 0) {
537  userRep.setClientConsents(consentReps);
538  }
539 
540  // Not Before
541  int notBefore = session.users().getNotBeforeOfUser(realm, user);
542  userRep.setNotBefore(notBefore);
543 
544  // Service account
545  if (user.getServiceAccountClientLink() != null) {
546  String clientInternalId = user.getServiceAccountClientLink();
547  ClientModel client = realm.getClientById(clientInternalId);
548  if (client != null) {
549  userRep.setServiceAccountClientId(client.getClientId());
550  }
551  }
552 
553  if (options.isGroupsAndRolesIncluded()) {
554  List<String> groups = new LinkedList<>();
555  for (GroupModel group : user.getGroups()) {
556  groups.add(ModelToRepresentation.buildGroupPath(group));
557  }
558  userRep.setGroups(groups);
559  }
560  return userRep;
561  }
static CredentialRepresentation exportCredential(CredentialModel userCred)
Definition: ExportUtils.java:571
static FederatedIdentityRepresentation exportSocialLink(FederatedIdentityModel socialLink)
Definition: ExportUtils.java:563

◆ exportUsersToStream() [1/2]

static void org.keycloak.exportimport.util.ExportUtils.exportUsersToStream ( KeycloakSession  session,
RealmModel  realm,
List< UserModel >  usersToExport,
ObjectMapper  mapper,
OutputStream  os 
) throws IOException
inlinestatic
589  {
590  exportUsersToStream(session, realm, usersToExport, mapper, os, new ExportOptions());
591  }
static void exportUsersToStream(KeycloakSession session, RealmModel realm, List< UserModel > usersToExport, ObjectMapper mapper, OutputStream os)
Definition: ExportUtils.java:589

◆ exportUsersToStream() [2/2]

static void org.keycloak.exportimport.util.ExportUtils.exportUsersToStream ( KeycloakSession  session,
RealmModel  realm,
List< UserModel >  usersToExport,
ObjectMapper  mapper,
OutputStream  os,
ExportOptions  options 
) throws IOException
inlinestatic
593  {
594  JsonFactory factory = mapper.getFactory();
595  JsonGenerator generator = factory.createGenerator(os, JsonEncoding.UTF8);
596  try {
597  if (mapper.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
598  generator.useDefaultPrettyPrinter();
599  }
600  generator.writeStartObject();
601  generator.writeStringField("realm", realm.getName());
602  // generator.writeStringField("strategy", strategy.toString());
603  generator.writeFieldName("users");
604  generator.writeStartArray();
605 
606  for (UserModel user : usersToExport) {
607  UserRepresentation userRep = ExportUtils.exportUser(session, realm, user, options);
608  generator.writeObject(userRep);
609  }
610 
611  generator.writeEndArray();
612  generator.writeEndObject();
613  } finally {
614  generator.close();
615  }
616  }

◆ getRoleNames()

static List<String> org.keycloak.exportimport.util.ExportUtils.getRoleNames ( Collection< RoleModel >  roles)
inlinestatic
413  {
414  List<String> roleNames = new ArrayList<String>();
415  for (RoleModel role : roles) {
416  roleNames.add(role.getName());
417  }
418  return roleNames;
419  }

このクラス詳解は次のファイルから抽出されました: