Created
August 4, 2016 10:20
-
-
Save JanKoppe/1491e37d1022c77a286087e6c81d6092 to your computer and use it in GitHub Desktop.
Passport.js and ORCiD.org
This file contains hidden or 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
/* | |
* Example for authenticationg against the ORCiD.org Public API with | |
* passport.js and retrieving the authenticated orcid. | |
* | |
* The main difficulty is that ORCiD.org will send the authenticated orcid | |
* in the accessToken request, which is normally not passed on to the | |
* passport.js-callback. Without the orcid, properly authenticating with | |
* ORCiD.org is impossible though. | |
* | |
* The trick is to pass on the `passReqToCallback`-option to the | |
* OAuth2Strategy, so that the callback will receive the complete content of | |
* the accessToken request. We can then use a callback function with the | |
* signature `(req, accessToken, refreshToken, params, profile, cb)`, where | |
* the parameter `params` will contain the JSON-parsed values of the | |
* accessToken answer. The orcid can then be accessed via `params.orcid`. | |
*/ | |
const passport = require('passport'); | |
const OAuth2Strategy = require('passport-oauth2').Strategy; | |
const orcid = new OAuth2Strategy( | |
{ // Configure your OAuth2Stragey as usual, but set `passReqToCallback` | |
…, | |
passReqToCallback : true | |
}, | |
(req, accessToken, refreshToken, params, profile, cb) => { | |
// You can now access the orcid and the name of the authenticated user | |
// in your passport callback and e.g. save it to your database for | |
// further use. | |
console.log(params.orcid); | |
console.log(params.name); | |
} | |
); | |
/* | |
* The remaining code would just be your usual passport.js configuration. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The complete application this configuration was originally developed for is https://github.com/o2r-project/o2r-bouncer
Further comments pointing to other implementations using this approach are very welcome!