This article will demonstrate how to create an Apereo CAS Custom Metadata Resolver for individual services in your json service registry. In the event that you need to troubleshoot the resolution of a service providers metadata, you can quickly swap the default CAS resolver with a groovy resolver and catch exceptions, improve logging, etc.
Metadata Resolver with Groovy
import java.nio.charset.*
import java.text.*
import java.util.*
import org.ldaptive.auth.*
import org.apereo.cas.*
import org.apereo.cas.authentication.*
import org.apereo.cas.authentication.support.*
import org.apereo.cas.authentication.support.password.*
import org.apereo.cas.support.saml.*
def run(Object[] args) {
def registeredService = args[0]
def samlConfigBean = args[1]
def samlIdPProperties = args[2]
def logger = args[4]
try {
logger.debug("Downloading metadata...")
def metadata = new URL("https://example.com/metadata.xml").getText([connectTimeout: 10000, readTimeout: 10000])
logger.info("Downloaded metadata ${metadata}")
def inputStream = new ByteArrayInputStream(metadata.getBytes(StandardCharsets.UTF_8))
def resolver = new InMemoryResourceMetadataResolver(inputStream, samlConfigBean)
resolver.setId("GroovyMetadataResolver")
resolver.initialize()
return resolver
} catch (Exception e) {
throw e;
}
}Dynamic metadata url
Add this section to your service config to create a location property that you can retrieve in the custom groovy script.
"properties" : {
"@class" : "java.util.HashMap",
"metadataLocation" : { "@class" : "org.apereo.cas.services.DefaultRegisteredServiceProperty",
"values" : [ "java.util.HashSet", [ "https://example.com/metadata.xml" ] ]
}
}Then in the script do this:
def location = registeredService.getProperties().get("metadataLocation").getValues().iterator().next()
def metadata = new URL(location)JSON Service Config
"metadataLocation": "file:/etc/cas/config/metadata/metadata.groovy"Conclusion – Apereo CAS Custom Metadata Resolver
This article has demonstrated how to configure a custom metadata resolver in the CAS IDP (Identity Provider). Let us know in the comments if you have any questions. If you found this article helpful, check out our other articles on Apereo CAS.
Leave a Reply