From 25db1d4746951c960276b85740fe375f83c5629b Mon Sep 17 00:00:00 2001 From: Ivan Fernandes Date: Sun, 2 Dec 2018 19:13:47 -0200 Subject: [PATCH 1/5] adding missing options from option type --- types/passport-bnet/index.d.ts | 21 ++++++++++----------- types/passport-oauth2/index.d.ts | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/types/passport-bnet/index.d.ts b/types/passport-bnet/index.d.ts index 86255c782d..c8e6dd923c 100644 --- a/types/passport-bnet/index.d.ts +++ b/types/passport-bnet/index.d.ts @@ -4,7 +4,7 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 -import { Strategy as OAuth2Strategy, VerifyFunction, VerifyFunctionWithRequest } from 'passport-oauth2'; +import { Strategy as OAuth2Strategy, VerifyFunction, VerifyFunctionWithRequest, _StrategyOptionsBase } from 'passport-oauth2'; declare class BnetStrategy extends OAuth2Strategy { constructor(options: BnetStrategy.StrategyOptions, verify: VerifyFunction); @@ -12,17 +12,16 @@ declare class BnetStrategy extends OAuth2Strategy { } declare namespace BnetStrategy { - interface _BaseBnetOptions { - region?: string; - scopeSeparator?: string; - customHeaders?: object; - authorizationURL?: string; - tokenURL?: string; - scope?: string; - clientID: string; - clientSecret: string; - callbackURL?: string; + //Utility types for excluding properties from interfaces + type Diff = ({ [P in T]: P } & { [P in U]: never } & { [x: string]: never })[T]; + type Omit = Pick>; + + // passport--bnet accepts any options that passport-oauth2 accepts, but add the option region and makes authorizationURL and tokenURL optional + interface _BaseBnetOptions + extends Partial>, Omit<_StrategyOptionsBase, 'authorizationURL' | 'tokenURL'> { + + region?: string; } interface StrategyOptions extends _BaseBnetOptions { diff --git a/types/passport-oauth2/index.d.ts b/types/passport-oauth2/index.d.ts index d985852bd6..c3723b50b7 100644 --- a/types/passport-oauth2/index.d.ts +++ b/types/passport-oauth2/index.d.ts @@ -9,6 +9,7 @@ import { Request } from 'express'; import { Strategy } from 'passport'; import { OAuth2 } from 'oauth'; +import { OutgoingHttpHeaders } from 'http'; declare class OAuth2Strategy extends Strategy { name: string; @@ -32,6 +33,23 @@ declare class OAuth2Strategy extends Strategy { } declare namespace OAuth2Strategy { + interface Metadata { + authorizationURL: string; + tokenURL: string; + clientID: string; + } + + type StateStoreStoreCallback = (err: Error | null, state: any) => void; + type StateStoreVerifyCallback = (err: Error, ok: boolean, state: any) => void + + interface StateStore { + store(req: Request, callback: StateStoreStoreCallback): void; + store(req: Request, meta: Metadata, callback: StateStoreStoreCallback): void; + + verify(req: Request, state: string, callback: StateStoreVerifyCallback): void; + verify(req: Request, state: string, meta: Metadata, callback: StateStoreVerifyCallback): void; + } + type VerifyCallback = (err?: Error | null, user?: object, info?: object) => void; type VerifyFunction = @@ -47,6 +65,11 @@ declare namespace OAuth2Strategy { clientID: string; clientSecret: string; callbackURL?: string; + customHeaders?: OutgoingHttpHeaders; + scope?: string | string[]; + sessionKey?: string; + store?: StateStore; + state?: any; } interface StrategyOptions extends _StrategyOptionsBase { passReqToCallback?: false; From e7f4d0148e3d4e7e3e60a68f83ab5c02b3f114b4 Mon Sep 17 00:00:00 2001 From: Ivan Fernandes Date: Tue, 4 Dec 2018 16:16:29 -0200 Subject: [PATCH 2/5] add missing scopeSeparator option and tests --- types/passport-oauth2/index.d.ts | 1 + .../passport-oauth2/passport-oauth2-tests.ts | 33 ++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/types/passport-oauth2/index.d.ts b/types/passport-oauth2/index.d.ts index c3723b50b7..a6d519bf6a 100644 --- a/types/passport-oauth2/index.d.ts +++ b/types/passport-oauth2/index.d.ts @@ -67,6 +67,7 @@ declare namespace OAuth2Strategy { callbackURL?: string; customHeaders?: OutgoingHttpHeaders; scope?: string | string[]; + scopeSeparator?: string; sessionKey?: string; store?: StateStore; state?: any; diff --git a/types/passport-oauth2/passport-oauth2-tests.ts b/types/passport-oauth2/passport-oauth2-tests.ts index 864b2cb9d6..57dcfa56d9 100644 --- a/types/passport-oauth2/passport-oauth2-tests.ts +++ b/types/passport-oauth2/passport-oauth2-tests.ts @@ -1,5 +1,5 @@ import OAuth2Strategy = require('passport-oauth2'); -import { Strategy, StrategyOptions, StrategyOptionsWithRequest, VerifyCallback, AuthorizationError, TokenError, InternalOAuthError } from 'passport-oauth2'; +import { Strategy, StrategyOptions, StrategyOptionsWithRequest, VerifyCallback, AuthorizationError, TokenError, InternalOAuthError, Metadata, StateStore, StateStoreStoreCallback, StateStoreVerifyCallback } from 'passport-oauth2'; import { Strategy as PassportStrategy } from 'passport'; import { Request } from 'express'; @@ -56,3 +56,34 @@ class MyStrategy extends OAuth2Strategy { this._oauth2.get('http://www.example.com/profile', 'token', (err, result, response) => response); } } + +const metadata: Metadata = { + authorizationURL: 'http://www.example.com/auth', + clientID: 'dummy', + tokenURL: 'http://www.example.com/token' +} + +class MyStore implements StateStore { + store(req: Request, meta: StateStoreStoreCallback | Metadata, callback?: StateStoreStoreCallback): void {} + verify(req: Request, state: string, meta: StateStoreVerifyCallback | Metadata, callback?: StateStoreVerifyCallback): void {} +} + +const myStore = new MyStore; + +const strategyOptions3: StrategyOptions = { + authorizationURL: 'http://www.example.com/auth', + clientID: 'dummy', + clientSecret: 'secret', + tokenURL: 'http://www.example.com/token', + callbackURL: 'http://www.example.com/callback', + customHeaders: { + 'content-type': 'text/html' + }, + scope: ['scope1', 'scope2'], + scopeSeparator: ' ', + sessionKey: 'oauth', + state: {id: 1}, + store: myStore +}; + +const strategy5: Strategy = new Strategy(strategyOptions3, verifyFunction2); From ee619a0597282ea6de017ff65ec13523dc5c9d38 Mon Sep 17 00:00:00 2001 From: Ivan Fernandes Date: Tue, 4 Dec 2018 16:19:40 -0200 Subject: [PATCH 3/5] correcting code style --- types/passport-oauth2/index.d.ts | 2 +- types/passport-oauth2/passport-oauth2-tests.ts | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/types/passport-oauth2/index.d.ts b/types/passport-oauth2/index.d.ts index a6d519bf6a..cd78f377c2 100644 --- a/types/passport-oauth2/index.d.ts +++ b/types/passport-oauth2/index.d.ts @@ -40,7 +40,7 @@ declare namespace OAuth2Strategy { } type StateStoreStoreCallback = (err: Error | null, state: any) => void; - type StateStoreVerifyCallback = (err: Error, ok: boolean, state: any) => void + type StateStoreVerifyCallback = (err: Error, ok: boolean, state: any) => void; interface StateStore { store(req: Request, callback: StateStoreStoreCallback): void; diff --git a/types/passport-oauth2/passport-oauth2-tests.ts b/types/passport-oauth2/passport-oauth2-tests.ts index 57dcfa56d9..30faeebda0 100644 --- a/types/passport-oauth2/passport-oauth2-tests.ts +++ b/types/passport-oauth2/passport-oauth2-tests.ts @@ -1,5 +1,6 @@ import OAuth2Strategy = require('passport-oauth2'); -import { Strategy, StrategyOptions, StrategyOptionsWithRequest, VerifyCallback, AuthorizationError, TokenError, InternalOAuthError, Metadata, StateStore, StateStoreStoreCallback, StateStoreVerifyCallback } from 'passport-oauth2'; +import { Strategy, StrategyOptions, StrategyOptionsWithRequest, VerifyCallback, AuthorizationError, TokenError, + InternalOAuthError, Metadata, StateStore, StateStoreStoreCallback, StateStoreVerifyCallback } from 'passport-oauth2'; import { Strategy as PassportStrategy } from 'passport'; import { Request } from 'express'; @@ -61,14 +62,14 @@ const metadata: Metadata = { authorizationURL: 'http://www.example.com/auth', clientID: 'dummy', tokenURL: 'http://www.example.com/token' -} +}; class MyStore implements StateStore { store(req: Request, meta: StateStoreStoreCallback | Metadata, callback?: StateStoreStoreCallback): void {} verify(req: Request, state: string, meta: StateStoreVerifyCallback | Metadata, callback?: StateStoreVerifyCallback): void {} } -const myStore = new MyStore; +const myStore = new MyStore(); const strategyOptions3: StrategyOptions = { authorizationURL: 'http://www.example.com/auth', From f304573e356f9f89125ba2ef8397216e7fe1a953 Mon Sep 17 00:00:00 2001 From: Ivan Fernandes Date: Tue, 4 Dec 2018 17:06:54 -0200 Subject: [PATCH 4/5] removing type utilities in favor of a simpler solution --- types/passport-bnet/index.d.ts | 12 ++++-------- types/passport-bnet/passport-bnet-tests.ts | 2 -- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/types/passport-bnet/index.d.ts b/types/passport-bnet/index.d.ts index c8e6dd923c..e7ffaaedce 100644 --- a/types/passport-bnet/index.d.ts +++ b/types/passport-bnet/index.d.ts @@ -12,15 +12,11 @@ declare class BnetStrategy extends OAuth2Strategy { } declare namespace BnetStrategy { - - //Utility types for excluding properties from interfaces - type Diff = ({ [P in T]: P } & { [P in U]: never } & { [x: string]: never })[T]; - type Omit = Pick>; - // passport--bnet accepts any options that passport-oauth2 accepts, but add the option region and makes authorizationURL and tokenURL optional - interface _BaseBnetOptions - extends Partial>, Omit<_StrategyOptionsBase, 'authorizationURL' | 'tokenURL'> { - + interface _BaseBnetOptions extends Partial<_StrategyOptionsBase> { + clientID: string; + clientSecret: string; + region?: string; } diff --git a/types/passport-bnet/passport-bnet-tests.ts b/types/passport-bnet/passport-bnet-tests.ts index 06c0ffccec..b02cf40519 100644 --- a/types/passport-bnet/passport-bnet-tests.ts +++ b/types/passport-bnet/passport-bnet-tests.ts @@ -6,11 +6,9 @@ import { Strategy as OAuth2Strategy, VerifyCallback } from "passport-oauth2"; import { Request } from "express"; const strategyOptions1: StrategyOptions = { - authorizationURL: 'http://www.example.com/auth', callbackURL: 'http://www.example.com/callback', clientID: 'dummy', clientSecret: 'secret', - tokenURL: 'http://www.example.com/token', region: 'us', scope: "email", scopeSeparator: ' ', From f24094176065b5757b817f420b86f86729ae9fd9 Mon Sep 17 00:00:00 2001 From: Ivan Fernandes Date: Tue, 4 Dec 2018 20:10:08 -0200 Subject: [PATCH 5/5] changing github2 customHeaders option to correct type --- types/passport-github2/index.d.ts | 6 ++++-- types/passport-oauth2/index.d.ts | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/types/passport-github2/index.d.ts b/types/passport-github2/index.d.ts index 0b2c1f6a5d..4ca8e93124 100644 --- a/types/passport-github2/index.d.ts +++ b/types/passport-github2/index.d.ts @@ -3,12 +3,14 @@ // Definitions by: Yasunori Ohoka // Maarten Mulders // Christoph Werner +// Ivan Fernandes // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.8 import passport = require('passport'); import oauth2 = require('passport-oauth2'); import express = require('express'); +import { OutgoingHttpHeaders } from 'http'; export interface Profile extends passport.Profile { profileUrl: string; @@ -25,7 +27,7 @@ export interface StrategyOption extends passport.AuthenticateOptions { authorizationURL?: string; tokenURL?: string; scopeSeparator?: string; - customHeaders?: string; + customHeaders?: OutgoingHttpHeaders; userProfileURL?: string; } @@ -46,7 +48,7 @@ export interface _StrategyOptionsBase extends OAuth2StrategyOptionsWithoutRequir authorizationURL?: string; tokenURL?: string; scopeSeparator?: string; - customHeaders?: string; + customHeaders?: OutgoingHttpHeaders; userProfileURL?: string; } diff --git a/types/passport-oauth2/index.d.ts b/types/passport-oauth2/index.d.ts index cd78f377c2..28a1a06348 100644 --- a/types/passport-oauth2/index.d.ts +++ b/types/passport-oauth2/index.d.ts @@ -3,6 +3,7 @@ // Definitions by: Pasi Eronen // Wang Zishi // Eduardo AC +// Ivan Fernandes // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3