Created
September 5, 2017 11:59
-
-
Save dagingaa/68081c9a979983bda99c8c6168d41999 to your computer and use it in GitHub Desktop.
A small wrapper around openid-client passport for integrating with Grean EasyID
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
"use strict"; | |
const { Issuer, Strategy } = require("openid-client"); | |
Issuer.defaultHttpOptions = { timeout: 5000 }; | |
function createUser(userinfo) { | |
const name = userinfo.name.split(","); | |
return { | |
id: userinfo.uniqueuserid, | |
provider: "grean", | |
socialno: userinfo.socialno, | |
name: { | |
familyName: name[0], | |
givenName: name[1].substr(1), | |
}, | |
displayName: userinfo.name, | |
}; | |
} | |
class PassportGrean { | |
constructor({ issuerUrl, clientId, clientSecret, redirectUri }) { | |
this.issuerUrl = issuerUrl; | |
this.clientId = clientId; | |
this.clientSecret = clientSecret; | |
this.redirectUri = redirectUri; | |
this.init(); | |
} | |
init() { | |
this.issuer = new Issuer({ | |
issuer: this.issuerUrl, | |
authorization_endpoint: `${this.issuerUrl}/oauth2/authorize`, | |
token_endpoint: `${this.issuerUrl}/oauth2/token`, | |
userinfo_endpoint: `${this.issuerUrl}/oauth2/userinfo`, | |
jwks_uri: `${this.issuerUrl}/.well-known/jwks`, | |
}); | |
this.client = new this.issuer.Client({ | |
client_id: this.clientId, | |
client_secret: this.clientSecret, | |
}); | |
} | |
createStrategy() { | |
return new Strategy( | |
{ | |
client: this.client, | |
params: { redirect_uri: this.redirectUri }, | |
}, | |
(tokenset, userinfo, done) => { | |
done(null, createUser(userinfo)); | |
}, | |
); | |
} | |
} | |
module.exports = PassportGrean; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment