From d57c207a00690f43bcca1bb1393bb7f8f82db02b Mon Sep 17 00:00:00 2001 From: Maarten Mulders Date: Tue, 15 Aug 2017 15:49:16 +0200 Subject: [PATCH] Type definitions for Passport-GitHub (2) --- types/passport-github2/index.d.ts | 36 ++++++++++++++++ .../passport-github2-tests.ts | 42 +++++++++++++++++++ types/passport-github2/tsconfig.json | 22 ++++++++++ types/passport-github2/tslint.json | 1 + 4 files changed, 101 insertions(+) create mode 100644 types/passport-github2/index.d.ts create mode 100644 types/passport-github2/passport-github2-tests.ts create mode 100644 types/passport-github2/tsconfig.json create mode 100644 types/passport-github2/tslint.json diff --git a/types/passport-github2/index.d.ts b/types/passport-github2/index.d.ts new file mode 100644 index 0000000000..cd7f6a2566 --- /dev/null +++ b/types/passport-github2/index.d.ts @@ -0,0 +1,36 @@ +// Type definitions for passport-github 1.1 +// Project: https://github.com/jaredhanson/passport-github +// Definitions by: Yasunori Ohoka +// Maarten Mulders +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +import passport = require('passport'); +import express = require('express'); + +export interface Profile extends passport.Profile { + profileUrl: string; +} + +export interface StrategyOption { + clientID: string; + clientSecret: string; + callbackURL: string; + + scope?: string[]; + userAgent?: string; + + authorizationURL?: string; + tokenURL?: string; + scopeSeparator?: string; + customHeaders?: string; + userProfileURL?: string; +} + +export class Strategy implements passport.Strategy { + constructor(options: StrategyOption, verify: (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; + authenticate: (req: express.Request, options?: passport.AuthenticateOptions) => void; +} diff --git a/types/passport-github2/passport-github2-tests.ts b/types/passport-github2/passport-github2-tests.ts new file mode 100644 index 0000000000..ee101b618b --- /dev/null +++ b/types/passport-github2/passport-github2-tests.ts @@ -0,0 +1,42 @@ +/** + * Created by jcabresos on 4/19/2014. + */ +import passport = require('passport'); +import github = require('passport-github2'); + +// just some test model +const User = { + findOrCreate(id: string, provider: string, callback: (err: any, user: any) => void): void { + callback(null, { username: 'james' }); + } +}; + +const callbackURL = process.env.PASSPORT_GITHUB_CALLBACK_URL; +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"); +} + +if (typeof clientID === "undefined") { + throw new Error("clientID is undefined"); +} + +if (typeof clientSecret === "undefined") { + throw new Error("clientSecret is undefined"); +} + +passport.use(new github.Strategy( + { + callbackURL, + clientID, + clientSecret + }, + (accessToken: string, refreshToken: string, profile: github.Profile, done: (error: any, user?: any) => void) => { + User.findOrCreate(profile.id, profile.provider, (err, user) => { + if (err) { return done(err); } + done(null, user); + }); + }) +); diff --git a/types/passport-github2/tsconfig.json b/types/passport-github2/tsconfig.json new file mode 100644 index 0000000000..23822974be --- /dev/null +++ b/types/passport-github2/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "passport-github2-tests.ts" + ] +} diff --git a/types/passport-github2/tslint.json b/types/passport-github2/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/passport-github2/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }