Created
September 19, 2016 03:46
-
-
Save mdeggies/21a01ef648256664436a31ae522bbf20 to your computer and use it in GitHub Desktop.
Using ID Site with Stormpath's Node SDK
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
/*** Sources: https://github.com/stormpath/stormpath-sdk-node/blob/master/lib/resource/Application.js#L122 | |
https://docs.stormpath.com/rest/product-guide/latest/idsite.html ***/ | |
'use strict'; | |
var stormpath = require('stormpath'); | |
var http = require('http'); | |
var Router = require('router'); | |
var finalhandler = require('finalhandler'); | |
var PORT = 3000; | |
var router = Router(); | |
var server = http.createServer(function onRequest(req, res) { | |
router(req, res, finalhandler(req, res)); | |
}); | |
var apiKey = new stormpath.ApiKey( | |
process.env.STORMPATH_CLIENT_APIKEY_ID, | |
process.env.STORMPATH_CLIENT_APIKEY_SECRET | |
); | |
var client = new stormpath.Client({ apiKey: apiKey, baseUrl: 'https://api.stormpath.com/v1' }); | |
var applicationHref = 'https://api.stormpath.com/v1/applications/$APP_ID'; | |
//Redirect user to ID Site when he lands on http://localhost:3000/login | |
router.get('/login', function(req, res) { | |
client.getApplication(applicationHref, function(err, application) { | |
if (err) { | |
console.error(err); | |
res.end(500); | |
} else { | |
var url = application.createIdSiteUrl({ | |
callbackUri: 'http://localhost:3000/idSiteResult' | |
}); | |
res.writeHead(302, {"Location": url}); | |
res.end(); | |
} | |
}); | |
}); | |
//Handle the JWT response, and display the account (if it has successfully authenticated) | |
router.get('/idSiteResult', function (req, res) { | |
client.getApplication(applicationHref, function(err, application) { | |
application.handleIdSiteCallback(req.url, function (err, idSiteAuthenticationResult) { | |
if (err) { | |
console.error(err); | |
res.end(500); | |
} else { | |
res.writeHead(200, {'Content-type': 'application/json'}); | |
res.write(JSON.stringify(idSiteAuthenticationResult.account)); | |
res.end(); | |
} | |
}); | |
}); | |
}); | |
server.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment