diff --git a/types/passport-github/index.d.ts b/types/passport-github/index.d.ts index 99c84bd391..4eaa13e359 100644 --- a/types/passport-github/index.d.ts +++ b/types/passport-github/index.d.ts @@ -1,33 +1,50 @@ // Type definitions for passport-github 1.1 // Project: https://github.com/jaredhanson/passport-github -// Definitions by: Yasunori Ohoka +// Definitions by: Yasunori Ohoka +// Manuel Ruck // 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 +>; + +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; diff --git a/types/passport-github/passport-github-tests.ts b/types/passport-github/passport-github-tests.ts index fb8dc73d76..11a7a8f328 100644 --- a/types/passport-github/passport-github-tests.ts +++ b/types/passport-github/passport-github-tests.ts @@ -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); + }); + }) +);