forked from keycloak/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SAML/ OIDC Identity Provider AutoUpdate
Closes keycloak#11692
- Loading branch information
cgeorgilakis-grnet
committed
Oct 14, 2024
1 parent
dee2001
commit 9349c93
Showing
36 changed files
with
611 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...rage-private/src/main/java/org/keycloak/services/scheduled/UpdateAutoUpdatedIdPsTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.keycloak.services.scheduled; | ||
|
||
|
||
import org.jboss.logging.Logger; | ||
import org.keycloak.broker.provider.IdentityProvider; | ||
import org.keycloak.broker.provider.IdentityProviderFactory; | ||
import org.keycloak.broker.social.SocialIdentityProvider; | ||
import org.keycloak.connections.httpclient.HttpClientProvider; | ||
import org.keycloak.models.IdentityProviderModel; | ||
import org.keycloak.models.KeycloakSession; | ||
import org.keycloak.models.RealmModel; | ||
import org.keycloak.timer.ScheduledTask; | ||
|
||
import java.io.IOException; | ||
import java.time.Instant; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.stream.Stream; | ||
|
||
public class UpdateAutoUpdatedIdPsTask implements ScheduledTask { | ||
|
||
protected static final Logger logger = Logger.getLogger(UpdateAutoUpdatedIdPsTask.class); | ||
protected final String realmId; | ||
|
||
public UpdateAutoUpdatedIdPsTask(String realmId) { | ||
this.realmId = realmId; | ||
} | ||
|
||
@Override | ||
public void run(KeycloakSession session) { | ||
logger.info(" Updating autoupdated identity providers in realm= " + realmId); | ||
RealmModel realm = session.realms().getRealm(realmId); | ||
session.getContext().setRealm(realm); | ||
session.identityProviders().getAllStream(Map.of(IdentityProviderModel.AUTO_UPDATE, "true"), null, null).forEach(idp -> autoUpdateIdP(idp, session)); | ||
realm.setAutoUpdatedIdPsLastRefreshTime(Instant.now().toEpochMilli()); | ||
|
||
} | ||
|
||
private void autoUpdateIdP(IdentityProviderModel idp, KeycloakSession session) { | ||
try { | ||
String file = session.getProvider(HttpClientProvider.class).getString(idp.getConfig().get(IdentityProviderModel.METADATA_DESCRIPTOR_URL)); | ||
idp = getProviderFactoryById(idp.getProviderId(), session).parseConfig(session, file, idp); | ||
session.identityProviders().update(idp); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private IdentityProviderFactory<?> getProviderFactoryById(String providerId, KeycloakSession session) { | ||
return Stream.concat(session.getKeycloakSessionFactory().getProviderFactoriesStream(IdentityProvider.class), | ||
session.getKeycloakSessionFactory().getProviderFactoriesStream(SocialIdentityProvider.class)) | ||
.filter(providerFactory -> Objects.equals(providerId, providerFactory.getId())) | ||
.map(IdentityProviderFactory.class::cast) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
|
||
} |
Oops, something went wrong.