pac4j

Looking for resolution to error "SAMLException: Unable to generate metadata for service provider"


I am trying to generate SP Metadata for a SAML based application with pac4j. I am working for a POC for my current project. Very beginner in knowledge towards SAML and pac4j. Any help would be really appreciable for guiding to identify the current runtime error as mentioned below.

I tried different way to use the APIs related to serviceProviderMetadataResolver which is related to the error. But could able to come out of the problem. If anybody can guide for the problem statement would be really very much helpful

My Sample application

SAML2Configuration cfg = new SAML2Configuration(
                "sampletestKeystore.jks",  // Keystore path
                "Password1",          // Keystore password
                "Password1",          // Private key password
                "idp-meta-downloaded.xml");    // Identity provider metadata

        SAML2Client saml2Client = new SAML2Client(cfg);
        
        Clients clients = new Clients("http://localhost", saml2Client);
        clients.init();

        cfg.setSamlAttributeConverter(new ComplexTypeSAML2AttributeConverter());
        cfg.setForceAuth(true);
        cfg.setPassive(true);
        cfg.setAuthnRequestBindingType(SAMLConstants.SAML2_REDIRECT_BINDING_URI);
        cfg.setResponseBindingType(SAMLConstants.SAML2_POST_BINDING_URI);
        cfg.setCallbackUrl("http://localhost");
        cfg.setServiceProviderEntityId("http://localhost");
        cfg.setUseNameQualifier(true);
        cfg.setResponseDestinationAttributeMandatory(false);
        cfg.setAttributeConsumingServiceIndex(1);
        cfg.setAssertionConsumerServiceIndex(1);
        cfg.setWantsAssertionsSigned(true);
        cfg.setAuthnRequestSigned(true);

        saml2Client.init();

        String spMetadata = saml2Client.getServiceProviderMetadataResolver().getMetadata();
        
        System.out.println("Metadata : "+spMetadata);

But I am getting the below exception stack at runtime.

Exception in thread "main" org.pac4j.saml.exceptions.SAMLException: Unable to generate metadata for service provider
    at org.pac4j.saml.metadata.SAML2ServiceProviderMetadataResolver.prepareServiceProviderMetadata(SAML2ServiceProviderMetadataResolver.java:61)
    at org.pac4j.saml.metadata.SAML2ServiceProviderMetadataResolver.<init>(SAML2ServiceProviderMetadataResolver.java:31)
    at org.pac4j.saml.client.SAML2Client.initServiceProviderMetadataResolver(SAML2Client.java:212)
    at org.pac4j.saml.client.SAML2Client.internalInit(SAML2Client.java:116)
    at org.pac4j.core.util.InitializableObject.init(InitializableObject.java:61)
    at org.pac4j.core.util.InitializableObject.init(InitializableObject.java:38)
    at pac4j.saml.connector.SamlConnectorMaven.generateMetaData1(SamlConnectorMaven.java:181)
    at pac4j.saml.connector.SamlConnectorMaven.main(SamlConnectorMaven.java:34)
**Caused by: java.lang.NullPointerException: Cannot invoke "org.springframework.core.io.Resource.getFile()" because "this.metadataResource" is null
    at **org.pac4j.saml.metadata.SAML2FileSystemMetadataGenerator.createMetadataResolver(SAML2FileSystemMetadataGenerator.java:34)
    at org.pac4j.saml.metadata.BaseSAML2MetadataGenerator.buildMetadataResolver(BaseSAML2MetadataGenerator.java:103)
    at org.pac4j.saml.metadata.SAML2ServiceProviderMetadataResolver.prepareServiceProviderMetadata(SAML2ServiceProviderMetadataResolver.java:59)
    ... 7 more

Solution

  • You need to define the resource associated with your SP metadata, for example: cfg.setServiceProviderMetadataPath(new File("target", "sp-metadata.xml").getAbsolutePath());