Skip to content

Instantly share code, notes, and snippets.

@passatgt
Last active February 21, 2017 20:39
Show Gist options
  • Save passatgt/2ec77210659e200c48795fe6bbbac94f to your computer and use it in GitHub Desktop.
Save passatgt/2ec77210659e200c48795fe6bbbac94f to your computer and use it in GitHub Desktop.
Exchange the stormpath token to an oauth access token with id site
//This works with the node.js stormpath sdk
const stormpath = require('stormpath');
/*
This route will redirect to the Stormpath ID Site
*/
module.exports.idSiteRedirect = function(req,res,next) {
//Create ID Site Url
var url = req.app.get('stormpathApplication').createIdSiteUrl({
callbackUri: 'ENTER YOUR REDIRECT URL HERE'
});
//Redirect to ID site
res.redirect(url);
res.end();
}
/*
Once we get back from Stormapth ID Site, verify the results and exchange the stormpath token with an oauth token pair
*/
module.exports.idSiteCallback = function(req,res,next) {
//Get the Stormpath app
var stormpathApplication = req.app.get('stormpathApplication');
//Check the response
stormpathApplication.handleIdSiteCallback(req.url, function (err, idSiteAuthenticationResult) {
if (err) {
console.error(err);
res.end(500);
} else {
//Createe a new authenticator to exchange the tokens
var authenticator = new stormpath.OAuthStormpathTokenAuthenticator(stormpathApplication);
var tokenRequest = {
stormpath_token: req.query.jwtResponse
};
//Authenticate the user and return the new access token
authenticator.authenticate(tokenRequest, function(err, oAuthStormpathTokenAuthenticationResult) {
if (err) {
console.log(err);
res.end(500);
}
//Check if account exists
oAuthStormpathTokenAuthenticationResult.getAccount(function(err, account){
if (err || !account) {
res.end(500);
} else {
//Return the access tokens
res.send(oAuthStormpathTokenAuthenticationResult.accessTokenResponse);
}
});
});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment