Expected Behavior
The spring-security-saml extension provides a HTTPMetadataProvider which is able to automatically refresh saml metadata in configurable intervals. It would be nice if this feature would also be supported by spring security.
Current Behavior
The current API, as far as I can see, does not support automatic metadata reloading.
I tried to implement a custom InMemoryRelyingPartyRegistrationRepository, but it seems that there are edge cases which I don't know how to handle correctly. E.g it should be possible to start the application while the IDP is not available and it should recover after IDP is available again.
Comment From: jzheaux
I think that there are a couple of ways that you can achieve this with existing Spring components:
-
First, you can create an implementation of
RelyingPartyRegistrationRepositorywherefindByRegistrationIdcomputes theRelyingPartyRegistrationand then caches the results for a period of time, like with the@Cacheableannotation. -
Second, you can create an implementation of
RelyingPartyRegistrationRepositorywhere an additional@Scheduledmethod computes theRelyingPartyRegistrationperiodically.
Spring's caching and scheduling support is very likely to exceed anything that Spring Security could provide. This approach is also nice because it allows you to periodically update the relying party side of the configuration.
Does that clear things up, or is there still something you'd like to see Spring Security provide?
Comment From: dawi
Yes, it's fairly easy to create a simple RelyingPartyRegistrationRepository implementation.
I found one pitfall though: If you implement a custom RelyingPartyRegistrationRepository by implementing RelyingPartyRegistrationRepository and don't implement Iterable<RelyingPartyRegistration> too, then singleProvider in Saml2LoginConfigurer will be false and the "auto-redirect to provider login page" is not done. Therefore it may make sense to implement Iterable<RelyingPartyRegistration> also if only one RelyingPartyRegistration is configured. See: https://github.com/spring-projects/spring-security/blob/6714112961335adad004979f7e38f580dc5bd004/config/src/main/java/org/springframework/security/config/annotation/web/configurers/saml2/Saml2LoginConfigurer.java#L214
I created two examples of how metadata-reloading could be implemented:
Nice thing is that not only IDP metadata is reloaded, but SP certificates as well.
Those are only examples and may very well be improved, but I think they show that it's not as simple as it should/could be.
From a user perspective it would be nice if all this could be part of spring security and spring boot.
If this is really out of scope of spring security, maybe I can open a ticket in spring-boot.
Comment From: jzheaux
@dawi, I think one thing that might help the community is a sample. Would you be able to contribute a Boot-based sample to spring-security-samples that shows what a custom RelyingPartyRegistrationRepository looks like?
This would help with the gotcha you mentioned as well as give a starting point to folks who are migrating spring-security-saml apps.
Comment From: jzheaux
Closing in favor of https://github.com/spring-projects/spring-security-samples/issues/2
Comment From: OrangeDog
SAML specifies both cacheDuration and validUntil attributes. An implementation should follow these for each entity and refresh accordingly, rather than refresh everything at a fixed interval.