Skip to content

Commit

Permalink
Adds signing key in the metadata service provider generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Moiron authored and markstos committed Oct 3, 2018
1 parent 25ac94e commit 4b02e16
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 25 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,15 @@ app.get('/login',
);
```
### generateServiceProviderMetadata( decryptionCert )
### generateServiceProviderMetadata( decryptionCert, signingCert )
As a convenience, the strategy object exposes a `generateServiceProviderMetadata` method which will generate a service provider metadata document suitable for supplying to an identity provider. This method will only work on strategies which are configured with a `callbackUrl` (since the relative path for the callback is not sufficient information to generate a complete metadata document).
The `decryptionCert` argument should be a public certificate matching the `decryptionPvk` and is required if the strategy is configured with a `decryptionPvk`.
The `signingCert` argument should be a public certificate matching the `privateCert` and is required if the strategy is configured with a `privateCert`.
## Security and signatures
Expand Down
67 changes: 49 additions & 18 deletions lib/passport-saml/saml.js
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ function processValidlySignedPostRequest(self, doc, callback) {
}
}

SAML.prototype.generateServiceProviderMetadata = function( decryptionCert ) {
SAML.prototype.generateServiceProviderMetadata = function( decryptionCert, signingCert ) {
var metadata = {
'EntityDescriptor' : {
'@xmlns': 'urn:oasis:names:tc:SAML:2.0:metadata',
Expand All @@ -1012,26 +1012,58 @@ SAML.prototype.generateServiceProviderMetadata = function( decryptionCert ) {
throw new Error(
"Missing decryptionCert while generating metadata for decrypting service provider");
}
}

if(this.options.privateCert){
if(!signingCert){
throw new Error(
"Missing signingCert while generating metadata for signing service provider messages");
}
}

decryptionCert = decryptionCert.replace( /-+BEGIN CERTIFICATE-+\r?\n?/, '' );
decryptionCert = decryptionCert.replace( /-+END CERTIFICATE-+\r?\n?/, '' );
decryptionCert = decryptionCert.replace( /\r\n/g, '\n' );
if(this.options.decryptionPvk || this.options.privateCert){
metadata.EntityDescriptor.SPSSODescriptor.KeyDescriptor=[];
if (this.options.privateCert) {

metadata.EntityDescriptor.SPSSODescriptor.KeyDescriptor = {
'ds:KeyInfo' : {
'ds:X509Data' : {
'ds:X509Certificate': {
'#text': decryptionCert
signingCert = signingCert.replace( /-+BEGIN CERTIFICATE-+\r?\n?/, '' );
signingCert = signingCert.replace( /-+END CERTIFICATE-+\r?\n?/, '' );
signingCert = signingCert.replace( /\r\n/g, '\n' );

metadata.EntityDescriptor.SPSSODescriptor.KeyDescriptor.push({
'@use': 'signing',
'ds:KeyInfo' : {
'ds:X509Data' : {
'ds:X509Certificate': {
'#text': signingCert
}
}
}
},
'EncryptionMethod' : [
// this should be the set that the xmlenc library supports
{ '@Algorithm': 'http://www.w3.org/2001/04/xmlenc#aes256-cbc' },
{ '@Algorithm': 'http://www.w3.org/2001/04/xmlenc#aes128-cbc' },
{ '@Algorithm': 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc' }
]
};
});
}

if (this.options.decryptionPvk) {

decryptionCert = decryptionCert.replace( /-+BEGIN CERTIFICATE-+\r?\n?/, '' );
decryptionCert = decryptionCert.replace( /-+END CERTIFICATE-+\r?\n?/, '' );
decryptionCert = decryptionCert.replace( /\r\n/g, '\n' );

metadata.EntityDescriptor.SPSSODescriptor.KeyDescriptor.push({
'@use': 'encryption',
'ds:KeyInfo' : {
'ds:X509Data' : {
'ds:X509Certificate': {
'#text': decryptionCert
}
}
},
'EncryptionMethod' : [
// this should be the set that the xmlenc library supports
{ '@Algorithm': 'http://www.w3.org/2001/04/xmlenc#aes256-cbc' },
{ '@Algorithm': 'http://www.w3.org/2001/04/xmlenc#aes128-cbc' },
{ '@Algorithm': 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc' }
]
});
}
}

if (this.options.logoutCallbackUrl) {
Expand All @@ -1048,7 +1080,6 @@ SAML.prototype.generateServiceProviderMetadata = function( decryptionCert ) {
'@Binding': 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST',
'@Location': this.getCallbackUrl({})
};

return xmlbuilder.create(metadata).end({ pretty: true, indent: ' ', newline: '\n' });
};

Expand Down
4 changes: 2 additions & 2 deletions lib/passport-saml/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ Strategy.prototype.logout = function(req, callback) {
this._saml.getLogoutUrl(req, {}, callback);
};

Strategy.prototype.generateServiceProviderMetadata = function( decryptionCert ) {
return this._saml.generateServiceProviderMetadata( decryptionCert );
Strategy.prototype.generateServiceProviderMetadata = function( decryptionCert, signingCert ) {
return this._saml.generateServiceProviderMetadata( decryptionCert, signingCert );
};

module.exports = Strategy;
2 changes: 1 addition & 1 deletion test/static/expected metadata.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<EntityDescriptor xmlns="urn:oasis:names:tc:SAML:2.0:metadata" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" entityID="http://example.serviceprovider.com" ID="http___example_serviceprovider_com">
<SPSSODescriptor protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
<KeyDescriptor>
<KeyDescriptor use="encryption">
<ds:KeyInfo>
<ds:X509Data>
<ds:X509Certificate>MIIEsDCCApigAwIBAgIBADANBgkqhkiG9w0BAQUFADATMREwDwYDVQQDEwgxMC4w
Expand Down
65 changes: 65 additions & 0 deletions test/static/expectedMetadataWithBothKeys.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0"?>
<EntityDescriptor xmlns="urn:oasis:names:tc:SAML:2.0:metadata" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" entityID="http://example.serviceprovider.com" ID="http___example_serviceprovider_com">
<SPSSODescriptor protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
<KeyDescriptor use="signing">
<ds:KeyInfo>
<ds:X509Data>
<ds:X509Certificate>MIICrjCCAZYCCQDWybyUsLVkXzANBgkqhkiG9w0BAQsFADAZMRcwFQYDVQQDFA5h
Y21lX3Rvb2xzLmNvbTAeFw0xNTA4MTgwOD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gmHglL5JxhpRX2IoPY5X
ItmR8IaYKhOBOrO/qercJ6Z3rQ6fIrs8HrYgeTO/uE9ww6WRWCWDaCR4oWDVwjKz
GfP1oJtAIQ+UNjOddH5otDawXJRlQWpr72qT7+WOK/yzJhN0xmXDkmt5psMY/CDn
4kXMM0xaRpfYOn5Sc1oelQ+hIm8/rHVK/0krhCDOniqj+L/ne2SoyoXYgS6o0Nlk
NhtTHmjjosE7HV6JfAUgq2D9dq00JO967I3P0VF6F8lcdaq0tovJhKWRjrrigYd7
56/3aT2B83JWA1RA9niXmrXfnAPZrNCfWvj+X2alUN/izdIqRts0UpWToVUr0ozW
gmbmp5ZOGYOQh+lNcAp3V9lOrdbZVSITM47RYGBZaZNDA2PbDzC13MS/EjTEvSko
nwtlCg==
</ds:X509Certificate>
</ds:X509Data>
</ds:KeyInfo>
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc"/>
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/>
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc"/>
</KeyDescriptor>
<NameIDFormat>urn:oasis:names:tc:SAML:2.0:nameid-format:transient</NameIDFormat>
<AssertionConsumerService index="1" isDefault="true" Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" Location="http://example.serviceprovider.com/saml/callback"/>
</SPSSODescriptor>
</EntityDescriptor>
24 changes: 21 additions & 3 deletions test/tests.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4b02e16

Please sign in to comment.