This repository has been archived by the owner on Dec 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 106
Saml integration #557
Open
the-overengineer
wants to merge
13
commits into
master
Choose a base branch
from
saml-integration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Saml integration #557
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
025e43f
Prototype SAML integration
cedea97
Update SAML, start work on SAML response verification
9f452f8
Update SAML, add and test logout
bbcd636
Cleanup
052e6c7
Further cleanup
2bec5ae
Disable saml by default
e96e787
Update settings and basic docs, start work on tests
e62cd04
Remove dead code, change docs to match SAML
4d983ac
Documentation tweaks
cba7cd4
Fixes and cleanup
0484074
Update SAML tests
35dc063
Remove unused variable
6f56d76
Remove unused prop
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict'; | ||
|
||
var stormpath = require('stormpath'); | ||
|
||
/** | ||
* This controller initiates a SAML login process, allowing the user to register | ||
* via a registered SAML provider. | ||
* | ||
* When the user logs in through the SAML provider, they will be redirected back | ||
* (to the SAML verification URL). | ||
* | ||
* @method | ||
* | ||
* @param {Object} req - The http request. | ||
* @param {Object} res - The http response. | ||
*/ | ||
module.exports = function (req, res) { | ||
var application = req.app.get('stormpathApplication'); | ||
var builder = new stormpath.SamlIdpUrlBuilder(application); | ||
var config = req.app.get('stormpathConfig'); | ||
var cbUri = req.protocol + '://' + req.get('host') + config.web.saml.verifyUri; | ||
|
||
var samlOptions = { | ||
cb_uri: cbUri | ||
}; | ||
|
||
builder.build(samlOptions, function (err, url) { | ||
if (err) { | ||
throw err; | ||
} | ||
|
||
res.writeHead(302, { | ||
'Cache-Control': 'no-store', | ||
'Location': url, | ||
'Pragma': 'no-cache' | ||
}); | ||
|
||
res.end(); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
'use strict'; | ||
|
||
var url = require('url'); | ||
var stormpath = require('stormpath'); | ||
|
||
var helpers = require('../helpers'); | ||
|
||
/** | ||
* This controller handles a Stormpath SAML authentication. Once a user is | ||
* authenticated, they'll be returned to the site. | ||
* | ||
* The returned JWT is verified and an attempt is made to exchange it for an | ||
* access token and refresh token, using the `stormpath_token` grant type, and | ||
* recording the user in the session. | ||
* | ||
* @method | ||
* | ||
* @param {Object} req - The http request. | ||
* @param {Object} res - The http response. | ||
*/ | ||
module.exports = function (req, res) { | ||
var application = req.app.get('stormpathApplication'); | ||
var config = req.app.get('stormpathConfig'); | ||
var logger = req.app.get('stormpathLogger'); | ||
|
||
var params = req.query || {}; | ||
var stormpathToken = params.jwtResponse || ''; | ||
var assertionAuthenticator = new stormpath.StormpathAssertionAuthenticator(application); | ||
|
||
assertionAuthenticator.authenticate(stormpathToken, function (err) { | ||
if (err) { | ||
logger.info('During a SAML login attempt, we were unable to verify the JWT response.'); | ||
return helpers.writeJsonError(res, err); | ||
} | ||
|
||
function redirectNext() { | ||
var nextUri = config.web.saml.nextUri; | ||
var nextQueryPath = url.parse(params.next || '').path; | ||
res.redirect(302, nextQueryPath || nextUri); | ||
} | ||
|
||
function authenticateToken(callback) { | ||
var stormpathTokenAuthenticator = new stormpath.OAuthStormpathTokenAuthenticator(application); | ||
|
||
stormpathTokenAuthenticator.authenticate({ stormpath_token: stormpathToken }, function (err, authenticationResult) { | ||
if (err) { | ||
logger.info('During a SAML login attempt, we were unable to create a Stormpath session.'); | ||
return helpers.writeJsonError(res, err); | ||
} | ||
|
||
authenticationResult.getAccount(function (err, account) { | ||
if (err) { | ||
logger.info('During a SAML login attempt, we were unable to retrieve an account from the authentication result.'); | ||
return helpers.writeJsonError(res, err); | ||
} | ||
|
||
helpers.expandAccount(account, config.expand, logger, function (err, expandedAccount) { | ||
if (err) { | ||
logger.info('During a SAML login attempt, we were unable to expand the Stormpath account.'); | ||
return helpers.writeJsonError(res, err); | ||
} | ||
|
||
helpers.createSession(authenticationResult, expandedAccount, req, res); | ||
|
||
callback(null, expandedAccount); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
function handleAuthRequest(callback) { | ||
var handler = config.postLoginHandler; | ||
|
||
if (handler) { | ||
authenticateToken(function (err, expandedAccount) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
handler(expandedAccount, req, res, callback); | ||
}); | ||
} else { | ||
authenticateToken(callback); | ||
} | ||
} | ||
|
||
handleAuthRequest(redirectNext); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
button.btn.btn-saml(onclick='samlLogin()') SAML | ||
script(type='text/javascript'). | ||
function samlLogin() { | ||
window.location = '#{stormpathConfig.web.saml.uri}'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
'use strict'; | ||
|
||
var assert = require('assert'); | ||
var cheerio = require('cheerio'); | ||
var request = require('supertest'); | ||
var uuid = require('uuid'); | ||
|
||
var helpers = require('../helpers'); | ||
|
||
function isSamlRedirect(res) { | ||
var location = res && res.headers && res.headers.location; | ||
var error = new Error('Expected Location header with redirect to saml/sso, but got ' + location); | ||
|
||
if (location) { | ||
var match = location.match(/\/saml\/sso\/idpRedirect/); | ||
return match ? null : error; | ||
} | ||
|
||
return error; | ||
} | ||
|
||
function prepareSaml(app, callbackUri, cb) { | ||
if (app.authorizedCallbackUris.indexOf(callbackUri) === -1) { | ||
app.authorizedCallbackUris.push(callbackUri); | ||
} | ||
|
||
app.save(cb); | ||
} | ||
|
||
function revertSaml(app, callbackUri, cb) { | ||
var index = app.authorizedCallbackUris.indexOf(callbackUri); | ||
|
||
if (index !== -1) { | ||
app.authorizedCallbackUris.splice(index, 1); | ||
} | ||
|
||
app.save(cb); | ||
} | ||
|
||
function initSamlApp(application, options, cb) { | ||
var webOpts = { | ||
login: { | ||
enabled: true | ||
}, | ||
register: { | ||
enabled: true | ||
}, | ||
saml: { | ||
enabled: true | ||
} | ||
}; | ||
|
||
Object.keys(options).forEach(function (key) { | ||
webOpts[key] = options[key]; | ||
}); | ||
|
||
var app = helpers.createStormpathExpressApp({ | ||
application: { | ||
href: application.href | ||
}, | ||
web: webOpts | ||
}); | ||
|
||
app.on('stormpath.ready', function () { | ||
var config = app.get('stormpathConfig'); | ||
var server = app.listen(function () { | ||
var address = server.address().address === '::' ? 'http://localhost' : server.address().address; | ||
address = address === '0.0.0.0' ? 'http://localhost' : address; | ||
var host = address + ':' + server.address().port; | ||
var callbackUri = host + config.web.saml.verifyUri; | ||
prepareSaml(app.get('stormpathApplication'), callbackUri, function (err) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
|
||
cb(null, { | ||
application: app, | ||
config: config, | ||
host: host | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
describe('saml', function () { | ||
var stormpathApplication, app, host, config, callbackUri; | ||
|
||
var accountData = { | ||
givenName: uuid.v4(), | ||
surname: uuid.v4(), | ||
email: uuid.v4() + '@test.com', | ||
password: uuid.v4() + uuid.v4().toUpperCase() + '!' | ||
}; | ||
|
||
before(function (done) { | ||
var client = helpers.createClient().on('ready', function () { | ||
helpers.createApplication(client, function (err, _app) { | ||
if (err) { | ||
return done(err); | ||
} | ||
|
||
stormpathApplication = _app; | ||
|
||
stormpathApplication.createAccount(accountData, function (err) { | ||
if (err) { | ||
return done(err); | ||
} | ||
|
||
initSamlApp(stormpathApplication, {}, function (err, data) { | ||
if (err) { | ||
return done(err); | ||
} | ||
|
||
app = data.application; | ||
config = data.config; | ||
host = data.host; | ||
|
||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
after(function (done) { | ||
revertSaml(app.get('stormpathApplication'), callbackUri, function () { | ||
helpers.destroyApplication(stormpathApplication, done); | ||
}); | ||
}); | ||
|
||
it('should contain the saml link in the login form, if saml is enabled', function (done) { | ||
request(host) | ||
.get(config.web.login.uri) | ||
.expect(200) | ||
.end(function (err, res) { | ||
var $ = cheerio.load(res.text); | ||
assert.equal($('.social-area').length, 1); | ||
assert.equal($('.btn-saml').length, 1); | ||
|
||
done(err); | ||
}); | ||
}); | ||
|
||
it('should perform a redirect in the SAML verification flow', function (done) { | ||
request(host) | ||
.get(config.web.saml.uri) | ||
.expect(302) | ||
.expect(isSamlRedirect) | ||
.end(done); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm using a similar approach to support SAML login. I'd recommend supporting a
host
config option here instead of assuming thereq.host
. Running this in a Docker container behind an nginx proxy winds up with thehost
being reported as the Docker service name instead of the public URI.