@hewmen/passport-twitch
Version:
Twitch authentication strategy using Helix for Passport. Supports the April 2020 Twitch changes!
130 lines • 4.91 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Strategy = void 0;
/**
* Module dependencies.
*/
const passport_oauth2_1 = __importStar(require("passport-oauth2"));
class OAuthStrategyWithPEM extends passport_oauth2_1.default {
constructor(options, verify) {
super(options, verify);
}
}
/**
* `Strategy` constructor.
*
* The Twitch authentication strategy authenticates requests by delegating to
* Twitch using the OAuth 2.0 protocol.
*
* Applications must supply a `verify` callback which accepts an `accessToken`,
* `refreshToken` and service-specific `profile`, and then calls the `done`
* callback supplying a `user`, which should be set to `false` if the
* credentials are not valid. If an exception occured, `err` should be set.
*
* Options:
* - `clientID` your Twitch application"s client id
* - `clientSecret` your Twitch application"s client secret
* - `callbackURL` URL to which Twitch will redirect the user after granting authorization
* - `pem` Signing certificate used for decoding a user's OIDC token
*
* Examples:
*
* passport.use(new TwitchStrategy({
* clientID: "123-456-789",
* clientSecret: "shhh-its-a-secret"
* callbackURL: "https://www.example.net/auth/twitch/callback"
* },
* function(accessToken, refreshToken, profile, done) {
* User.findOrCreate(..., function (err, user) {
* done(err, user)
* })
* }
* ))
*
* @param {InputStrategyOptions} options
* @param {VerifyFunction} verify
* @api public
*/
class Strategy extends OAuthStrategyWithPEM {
constructor(options, verify) {
options = options || {};
options.authorizationURL = options.authorizationURL || "https://id.twitch.tv/oauth2/authorize";
options.tokenURL = options.tokenURL || "https://id.twitch.tv/oauth2/token";
options.customHeaders = options.customHeaders || {};
options.customHeaders["Client-ID"] = options.clientID;
super(options, verify);
this.name = "twitch";
this.pem = options.pem;
this._oauth2.setAuthMethod("Bearer");
this._oauth2.useAuthorizationHeaderforGET(true);
this.__userProfileURL = "https://api.twitch.tv/helix/users";
}
/**
* Retrieve user profile from Twitch.
*
* This function constructs a normalized profile, with the following properties:
*
* - `provider` always set to `twitch`
* - `id`
* - `username`
* - `displayName`
*
* @param {String} accessToken
* @param {((e: InternalOAuthError | null, payload: UserProfile | undefined) => void)} done
* @api protected
*/
userProfile(accessToken, done) {
this._oauth2.get(this.__userProfileURL, accessToken, function (err, body, res) {
if (err) {
return done(new passport_oauth2_1.InternalOAuthError("failed to fetch user profile", err));
}
if (body === undefined) {
return done(new passport_oauth2_1.InternalOAuthError("body was empty", new Error("body was empty")));
}
let output;
if (typeof body === "string") {
output = body;
}
else {
output = body.toString("utf-8");
}
try {
done(null, {
...JSON.parse(output).data[0],
provider: "twitch"
});
}
catch (e) {
done(e);
}
});
}
authorizationParams(options) {
const params = {};
if (typeof options.forceVerify !== "undefined") {
params.force_verify = !!options.forceVerify;
}
return params;
}
}
exports.Strategy = Strategy;
//# sourceMappingURL=oauth2.js.map