samlify icon indicating copy to clipboard operation
samlify copied to clipboard

Integrate samlify with passport as a passport strategy?

Open RebelSyntax opened this issue 9 years ago • 3 comments

I am looking for a maintainable SAML SP engine for nodejs. So far i see auth0 has created a saml passport strategy passport-wsfed-saml2 based on passport-saml.

Has there been any discussion on the differences between samlify and something like passport-saml?

In particular, I am interested in knowing how much customization to the SAML authn request I can make outbound, and how much customization I can handle in the inbound SAML assertion.

I would lean towards integrating samlify into a passport strategy so that I may have my saml solution run next to my OIDC solution and select the strategy based on configuration of the call to my overall SSO engine.

Any insights are appreciated, thanks in advance.

RebelSyntax avatar Aug 09 '17 18:08 RebelSyntax

@RebelSyntax

Has there been any discussion on the differences between samlify and something like passport-saml?

I am not sure if passport-saml is still actively maintained. In fact, passport-saml and samlify allow you to integrate sso in your service (sp) and federation service (idp). Last but not least, samlify also allows you to configure your own federation service (idp) as well.

In particular, I am interested in knowing how much customization to the SAML authn request I can make outbound, and how much customization I can handle in the inbound SAML assertion.

For request and response, you can make your own custom template and replace those defined tags during runtime, that means most likely you can customize whatever you want.

Or using our default template which has a minimal setup to get it work.

I would lean towards integrating samlify into a passport strategy so that I may have my saml solution run next to my OIDC solution and select the strategy based on configuration of the call to my overall SSO engine.

I am happy to help, feel free to send an email to [email protected] if you need to.

tngan avatar Aug 10 '17 01:08 tngan

Hope this helps anyone trying to quickly get going with Samlify with a passport strategy. If confused PM me.

First npm install passport-strategy, it will be used as the base.

///samlifySamlStrategy.js
import util from 'util'
import passport from 'passport-strategy'
import * as saml from 'samlify'

function Strategy(samlConfig, verify) {
	passport.Strategy.call(this)
	this.name = 'saml'
	this._verify = verify
	this._sp = saml.ServiceProvider(samlConfig.sp)
	this._idp = saml.IdentityProvider(samlConfig.idp)
}

util.inherits(Strategy, passport.Strategy)

Strategy.prototype.authenticate = function(req) {
	const self = this

	const verified = function (err, user, info) {
		if (err) { return self.error(err); }
		if (!user) { return self.fail(info); }
		return self.success(user, info);
	}

	if (req.body && req.body.SAMLResponse) {
		this._sp.parseLoginResponse(this._idp, 'post', req)
			.then( async (parseResult) => {
				self._verify(req, parseResult, verified)
			})
			.catch( (e) => {
				console.log(e)
				return self.error(e)
			})
	} else {
		try {
			const url = this._sp.createLoginRequest(this._idp, 'redirect').context
			return self.redirect(url)
		} catch (e) {
			console.log(e)
			return self.error(e)
		}
	}
}

export default Strategy

Stuff your idp and sp configs from samlify into a wrapper object as shown here. This function generates a saml strategy and you can use. Write your own response handler, example shown below. It will be called by the strategy like so: self._verify(req, parseResult, verified).

///mySamlHelper.js
export const mySamlStrategyCreator = (configsIfNeeded) => new SamlStrategy(
	{
		idp: {
			isAssertionEncrypted: false,
			requestSignatureAlgorithm: 'http://www.w3.org/2000/09/xmldsig#rsa-sha1',
			metadata: fs.readFileSync('my/path/to/metadata_idp.xml')
		},
		sp: {
			privateKey: 'my-private-key',
			privateKeyPass: 'my-private-key-pass',
			requestSignatureAlgorithm: 'http://www.w3.org/2000/09/xmldsig#rsa-sha1',
			metadata: fs.readFileSync('my/path/to/metadata_sp.xml')
		}
	}, samlResponseHandler
)

const samlResponseHandler = async (req, parseResult, done) => {
	const statusCode = parseResult.extract.statuscode.value

	if (statusCode !== 'urn:oasis:names:tc:SAML:2.0:status:Success') {
          // LOOKS BAD TO ME
		return done(null, false, {message: "Authentication failed."})
	} else {
         // LOOKS GOOD TO ME
         const userId = parseResult.extract.attribute['something.user.id']
         const user = await req.db.findUser(id=userId).dataValues
         return (null, user) 
    }
}
/// app.js
passport.use("saml", mySamlStrategyCreator(configsIfNeeded))

How to use it on your router:

     // This will generate the redirect url (saml entry point is HERE)
    router.get('/saml/sso-redirect', passport.authenticate('saml'))
     
     // This will process the resulting post redirect and forward to your webapp (saml auth ends HERE)
	router.post('/saml/assert', passport.authenticate('saml'), (req, res) => {
		res.redirect('https://mywebapp.example.com')
	})

hammadzz avatar Sep 26 '17 17:09 hammadzz

@hammadzz I really appreciated your effort. Personally, I suggest to use and contribute passport-saml back if you really need to use passport. I will close this issue because we are not intended to start a new clone for bridging samlify and passport at the moment.

tngan avatar Oct 13 '17 16:10 tngan