Skip to content

Instantly share code, notes, and snippets.

@dagingaa
Created September 5, 2017 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dagingaa/68081c9a979983bda99c8c6168d41999 to your computer and use it in GitHub Desktop.
Save dagingaa/68081c9a979983bda99c8c6168d41999 to your computer and use it in GitHub Desktop.
A small wrapper around openid-client passport for integrating with Grean EasyID
"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