Merge pull request #32226 from ReggaePanda/passport-github

Improve passport-github type definitions. Add passReqToCallback StrategyOption
This commit is contained in:
Benjamin Lichtman 2019-01-23 08:42:57 -08:00 committed by GitHub
commit abaf92b5b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 7 deletions

View File

@ -1,33 +1,50 @@
// Type definitions for passport-github 1.1
// Project: https://github.com/jaredhanson/passport-github
// Definitions by: Yasunori Ohoka <https://github.com/yasupeke>
// Definitions by: Yasunori Ohoka <https://github.com/yasupeke>
// Manuel Ruck <https://github.com/reggaepanda>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
// TypeScript Version: 2.8
import passport = require('passport');
import express = require('express');
import OAuth2 = require('passport-oauth2');
import { OutgoingHttpHeaders } from 'http';
export interface Profile extends passport.Profile {
profileUrl: string;
}
export interface StrategyOption {
export interface StrategyOptionBase {
clientID: string;
clientSecret: string;
callbackURL: string;
scope?: string[];
userAgent?: string;
state?: string;
authorizationURL?: string;
tokenURL?: string;
scopeSeparator?: string;
customHeaders?: string;
customHeaders?: OutgoingHttpHeaders;
userProfileURL?: string;
}
export type OAuth2StrategyOptionsWithoutRequiredURLs = Pick<
OAuth2._StrategyOptionsBase,
Exclude<keyof OAuth2._StrategyOptionsBase, 'authorizationURL' | 'tokenURL'>
>;
export interface StrategyOption extends StrategyOptionBase {
passReqToCallback?: false;
}
export interface StrategyOptionWithRequest extends StrategyOptionBase {
passReqToCallback: true;
}
export class Strategy extends passport.Strategy {
constructor(options: StrategyOption, verify: (accessToken: string, refreshToken: string, profile: Profile, done: (error: any, user?: any) => void) => void);
constructor(options: StrategyOptionWithRequest, verify: (req: express.Request, accessToken: string, refreshToken: string, profile: Profile, done: (error: any, user?: any) => void) => void);
userProfile: (accessToken: string, done?: (error: any, profile: Profile) => void) => void;
name: string;

View File

@ -3,6 +3,7 @@
*/
import passport = require('passport');
import github = require('passport-github');
import express = require('express');
// just some test model
const User = {
@ -16,15 +17,15 @@ const clientID = process.env.PASSPORT_GITHUB_CONSUMER_KEY;
const clientSecret = process.env.PASSPORT_GITHUB_CONSUMER_SECRET;
if (typeof callbackURL === "undefined") {
throw new Error("callbackURL is undefined");
throw new Error("callbackURL is undefined");
}
if (typeof clientID === "undefined") {
throw new Error("clientID is undefined");
throw new Error("clientID is undefined");
}
if (typeof clientSecret === "undefined") {
throw new Error("clientSecret is undefined");
throw new Error("clientSecret is undefined");
}
passport.use(new github.Strategy(
@ -40,3 +41,18 @@ passport.use(new github.Strategy(
});
})
);
passport.use(new github.Strategy(
{
callbackURL,
clientID,
clientSecret,
passReqToCallback: true
},
(request: express.Request, accessToken: string, refreshToken: string, profile: github.Profile, done: (error: any, user?: any) => void) => {
User.findOrCreate(profile.id, profile.provider, (err, user) => {
if (err) { done(err); return; }
done(null, user);
});
})
);