From 6200c9a89605f61e32bafbb8fbfe0f14515c4966 Mon Sep 17 00:00:00 2001 From: Steve Hipwell Date: Wed, 29 Mar 2017 11:35:56 +0100 Subject: [PATCH] Updated oauth2orize typings --- types/oauth2orize/index.d.ts | 182 +++++++++++++++++++------ types/oauth2orize/oauth2orize-tests.ts | 32 ++--- 2 files changed, 156 insertions(+), 58 deletions(-) diff --git a/types/oauth2orize/index.d.ts b/types/oauth2orize/index.d.ts index 2c4b0a7601..a8fbf9c41b 100644 --- a/types/oauth2orize/index.d.ts +++ b/types/oauth2orize/index.d.ts @@ -1,46 +1,143 @@ -// Type definitions for oauth2orize v1.5.1 +// Type definitions for oauth2orize 1.8 // Project: https://github.com/jaredhanson/oauth2orize/ -// Definitions by: Wonshik Kim , Kei Son -// Definitions: https://github.com/borisyankov/DefinitelyTyped +// Definitions by: Wonshik Kim , Kei Son , Steve Hipwell +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// +/// -import * as http from "http"; +import { ServerRequest, ServerResponse } from "http"; -interface ServerOptions { +interface OAuth2 { + client: any; + + user: any; + + transactionID: string; + + redirectURI: string; + + req: OAuth2Req; + + info: OAuth2Info; +} + +interface OAuth2Req { + clientID: string; + + redirectURI: string; + + scope: string; + + state: string; + + type: string; + + transactionID: string; +} + +interface OAuth2Info { + scope: string; +} + +export interface MiddlewareRequest extends ServerRequest { + oauth2?: OAuth2; + + user?: any; +} + +export interface ServerOptions { store: any; loadTransaction: boolean; } -export function createServer(options?: ServerOptions): OAuth2Server; + +export const createServer: (options?: ServerOptions) => OAuth2Server; export interface AuthorizeOptions { idLength?: number; sessionKey?: string; } +export interface DecisionOptions { + cancelField: string; + userProperty: string; + sessionKey: string; +} + export interface ErrorHandlerOptions { mode?: string; } -type MiddlewareFunction = (req: http.IncomingMessage, res: http.ServerResponse, next: Function) => void; -type ValidatedFunction = (err: Error | null, client?: any, redirectURI?: string) => void; -type IssuedFunction = (err: Error | null, accessToken?: string | boolean, refreshToken?: string, params?: any) => void; +type MiddlewareFunction = (req: MiddlewareRequest, res: ServerResponse, next: MiddlewareNextFunction) => void; + +type MiddlewareErrorFunction = (err: Error, req: MiddlewareRequest, res: ServerResponse, next: MiddlewareNextFunction) => void; + +type MiddlewareNextFunction = (err?: Error) => void; + +type ValidateFunction = (clientId: string, redirectURI: string, validated: (err: Error | null, client?: any, redirectURI?: string) => void) => void; + +type ImmediateFunction = (client: any, user: any, scope: string[], type: string, areq: any, done: (err: Error | null, allow: boolean, info: any, locals: any) => void) => void; + +type DecisionParseFunction = (req: MiddlewareRequest, done: (err: Error | null, params: any) => void) => void; + +type SerializeClientFunction = (client: any, done: SerializeClientDoneFunction) => void; +type SerializeClientDoneFunction = (err: Error | null, id: string) => void; + +type DeserializeClientFunction = (id: string, done: DeserializeClientDoneFunction) => void; +type DeserializeClientDoneFunction = (err: Error | null, client?: any | boolean) => void; + +type IssueGrantCodeFunction = (client: any, redirectUri: string, user: any, res: any, issued: (err: Error | null, code?: string) => void) => void; + +type IssueGrantTokenFunction = (client: any, user: any, ares: any, issued: (err: Error | null, code?: string, params?: any) => void) => void; + + +type IssueExchangeCodeFunction = (client: any, code: string, redirectURI: string, issued: ExchangeDoneFunction) => void; + +type ExchangeDoneFunction = (err: Error | null, accessToken?: string | boolean, refreshToken?: string, params?: any) => void; + export class OAuth2Server { - exchange(fn: MiddlewareFunction): OAuth2Server; + grant(type: string, fn: MiddlewareFunction): OAuth2Server; + grant(fn: MiddlewareFunction): OAuth2Server; + exchange(type: string, fn: MiddlewareFunction): OAuth2Server; - // Parses requests to obtain authorization - authorize (options: AuthorizeOptions, validate: (clientId: string, redirectURI: string, validated: ValidatedFunction) => void): MiddlewareFunction; - authorization(options: AuthorizeOptions, validate: (clientId: string, redirectURI: string, validated: ValidatedFunction) => void): MiddlewareFunction; - authorize (validate: (clientId: string, redirectURI: string, validated: ValidatedFunction) => void): MiddlewareFunction; - authorization(validate: (clientId: string, redirectURI: string, validated: ValidatedFunction) => void): MiddlewareFunction; + exchange(fn: MiddlewareFunction): OAuth2Server; + + authorize(options: AuthorizeOptions, validate: ValidateFunction): MiddlewareFunction; + authorize(validate: ValidateFunction): MiddlewareFunction; + + authorization(options: AuthorizeOptions, validate: ValidateFunction, immediate?: ImmediateFunction): MiddlewareFunction; + authorization(validate: ValidateFunction, immediate?: ImmediateFunction): MiddlewareFunction; + + decision(options: DecisionOptions, parse: DecisionParseFunction): MiddlewareFunction; + decision(parse: DecisionParseFunction): MiddlewareFunction; token(options?: any): MiddlewareFunction; - errorHandler(options?: any): (err: Error, req: http.IncomingMessage, res: http.ServerResponse, next: any) => void; - serializeClient(fn: (client: any, done: (err: Error | null, id: string) => void) => void): void; - serializeClient(client: any, done: (err: Error | null, id: string) => void): void; - deserializeClient(fn: (id: string, done: (err: Error | null, client?: any | boolean) => void) => void): void; - deserializeClient(obj: any, done: (err: Error | null, client?: any | boolean) => void): void; + + errorHandler(options?: any): MiddlewareErrorFunction; + + serializeClient(fn: SerializeClientFunction): void; + serializeClient(client: any, done: SerializeClientDoneFunction): void; + + deserializeClient(fn: DeserializeClientFunction): void; + deserializeClient(obj: any, done: DeserializeClientDoneFunction): void; +} + +export namespace grant { + interface Options { + // For maximum flexibility, multiple scope spearators can optionally be + // allowed. This allows the server to accept clients that separate scope + // with either space or comma (' ', ','). This violates the specification, + // but achieves compatibility with existing client libraries that are already + // deployed. + scopeSeparator?: string; + } + + function code(options: Options, issue: IssueGrantCodeFunction): MiddlewareFunction; + function code(issue: IssueGrantCodeFunction): MiddlewareFunction; + + function token(options: Options, issue: IssueGrantTokenFunction): MiddlewareFunction; + function token(issue: IssueGrantTokenFunction): MiddlewareFunction; } export namespace exchange { @@ -57,41 +154,42 @@ export namespace exchange { scopeSeparator?: string; } - function authorizationCode(options: Options, issue: (client: any, code: string, redirectURI: string, issued: IssuedFunction) => void): MiddlewareFunction; - function authorizationCode(issue: (client: any, code: string, redirectURI: string, issued: IssuedFunction) => void): MiddlewareFunction; - function code(options: Options, issue: (client: any, code: string, redirectURI: string, issued: IssuedFunction) => void): MiddlewareFunction; - function code(issue: (client: any, code: string, redirectURI: string, issued: IssuedFunction) => void): MiddlewareFunction; + function authorizationCode(options: Options, issue: IssueExchangeCodeFunction): MiddlewareFunction; + function authorizationCode(issue: IssueExchangeCodeFunction): MiddlewareFunction; + + function code(options: Options, issue: IssueExchangeCodeFunction): MiddlewareFunction; + function code(issue: IssueExchangeCodeFunction): MiddlewareFunction; // arity == 5; issue(client, scope, req.body, req.authInfo, issued); - function clientCredentials(options: Options, issue: (client: any, scope: string[], body: any, authInfo: any, issued: IssuedFunction) => void): MiddlewareFunction; + function clientCredentials(options: Options, issue: (client: any, scope: string[], body: any, authInfo: any, issued: ExchangeDoneFunction) => void): MiddlewareFunction; // arity == 4; issue(client, scope, req.body, issued); - function clientCredentials(options: Options, issue: (client: any, scope: string[], body: any, issued: IssuedFunction) => void): MiddlewareFunction; + function clientCredentials(options: Options, issue: (client: any, scope: string[], body: any, issued: ExchangeDoneFunction) => void): MiddlewareFunction; // arity == 3; issue(client, scope, issued); - function clientCredentials(options: Options, issue: (client: any, scope: string[], issued: IssuedFunction) => void): MiddlewareFunction; + function clientCredentials(options: Options, issue: (client: any, scope: string[], issued: ExchangeDoneFunction) => void): MiddlewareFunction; // arity == 2; issue(client, issued); - function clientCredentials(options: Options, issue: (client: any, issued: IssuedFunction) => void): MiddlewareFunction; - function clientCredentials(issue: (client: any, scope: string[], issued: IssuedFunction) => void): MiddlewareFunction; - function clientCredentials(issue: (client: any, issued: IssuedFunction) => void): MiddlewareFunction; + function clientCredentials(options: Options, issue: (client: any, issued: ExchangeDoneFunction) => void): MiddlewareFunction; + function clientCredentials(issue: (client: any, scope: string[], issued: ExchangeDoneFunction) => void): MiddlewareFunction; + function clientCredentials(issue: (client: any, issued: ExchangeDoneFunction) => void): MiddlewareFunction; // arity == 7; issue(client, username, passwd, scope, req.body, req.authInfo, issued); - function password(options: Options, issue: (client: any, username: string, password: string, scope: string[], body: any, authInfo: any, issued: IssuedFunction) => void): MiddlewareFunction; + function password(options: Options, issue: (client: any, username: string, password: string, scope: string[], body: any, authInfo: any, issued: ExchangeDoneFunction) => void): MiddlewareFunction; // arity == 6; issue(client, username, passwd, scope, req.body, issued); - function password(options: Options, issue: (client: any, username: string, password: string, scope: string[], body: any, issued: IssuedFunction) => void): MiddlewareFunction; + function password(options: Options, issue: (client: any, username: string, password: string, scope: string[], body: any, issued: ExchangeDoneFunction) => void): MiddlewareFunction; // arity == 5; issue(client, username, passwd, scope, issued); - function password(options: Options, issue: (client: any, username: string, password: string, scope: string[], issued: IssuedFunction) => void): MiddlewareFunction; + function password(options: Options, issue: (client: any, username: string, password: string, scope: string[], issued: ExchangeDoneFunction) => void): MiddlewareFunction; // arity == 4; issue(client, username, passwd, issued); - function password(options: Options, issue: (client: any, username: string, password: string, issued: IssuedFunction) => void): MiddlewareFunction; - function password(issue: (client: any, username: string, password: string, scope: string[], issued: IssuedFunction) => void): MiddlewareFunction; - function password(issue: (client: any, username: string, password: string, issued: IssuedFunction) => void): MiddlewareFunction; + function password(options: Options, issue: (client: any, username: string, password: string, issued: ExchangeDoneFunction) => void): MiddlewareFunction; + function password(issue: (client: any, username: string, password: string, scope: string[], issued: ExchangeDoneFunction) => void): MiddlewareFunction; + function password(issue: (client: any, username: string, password: string, issued: ExchangeDoneFunction) => void): MiddlewareFunction; // arity == 6; issue(client, refreshToken, scope, req.body, req.authInfo, issued); - function refreshToken(options: Options, issue: (client: any, refreshToken: string, scope: string[], body: any, authInfo: any, issued: IssuedFunction) => void): MiddlewareFunction; + function refreshToken(options: Options, issue: (client: any, refreshToken: string, scope: string[], body: any, authInfo: any, issued: ExchangeDoneFunction) => void): MiddlewareFunction; // arity == 5; issue(client, refreshToken, scope, req.body, issued); - function refreshToken(options: Options, issue: (client: any, refreshToken: string, scope: string[], body: any, issued: IssuedFunction) => void): MiddlewareFunction; + function refreshToken(options: Options, issue: (client: any, refreshToken: string, scope: string[], body: any, issued: ExchangeDoneFunction) => void): MiddlewareFunction; // arity == 4; issue(client, refreshToken, scope, issued); - function refreshToken(options: Options, issue: (client: any, refreshToken: string, scope: string[], issued: IssuedFunction) => void): MiddlewareFunction; + function refreshToken(options: Options, issue: (client: any, refreshToken: string, scope: string[], issued: ExchangeDoneFunction) => void): MiddlewareFunction; // arity == 3; issue(client, refreshToken, issued); - function refreshToken(options: Options, issue: (client: any, refreshToken: string, issued: IssuedFunction) => void): MiddlewareFunction; - function refreshToken(issue: (client: any, refreshToken: string, scope: string[], issued: IssuedFunction) => void): MiddlewareFunction; - function refreshToken(issue: (client: any, refreshToken: string, issued: IssuedFunction) => void): MiddlewareFunction; + function refreshToken(options: Options, issue: (client: any, refreshToken: string, issued: ExchangeDoneFunction) => void): MiddlewareFunction; + function refreshToken(issue: (client: any, refreshToken: string, scope: string[], issued: ExchangeDoneFunction) => void): MiddlewareFunction; + function refreshToken(issue: (client: any, refreshToken: string, issued: ExchangeDoneFunction) => void): MiddlewareFunction; } diff --git a/types/oauth2orize/oauth2orize-tests.ts b/types/oauth2orize/oauth2orize-tests.ts index 9795b3e89e..49d7c94794 100644 --- a/types/oauth2orize/oauth2orize-tests.ts +++ b/types/oauth2orize/oauth2orize-tests.ts @@ -7,15 +7,15 @@ import * as http from 'http'; const server = oauth2orize.createServer(); // Register Grants -// server.grant(oauth2orize.grant.code(function(client, redirectURI, user, ares, done) { -// var code = utils.uid(16); +server.grant(oauth2orize.grant.code((client, redirectURI, user, ares, done) => { + // var code = utils.uid(16); -// var ac = new AuthorizationCode(code, client.id, redirectURI, user.id, ares.scope); -// ac.save(function(err) { -// if (err) { return done(err); } -// return done(null, code); -// }); -// })); + // var ac = new AuthorizationCode(code, client.id, redirectURI, user.id, ares.scope); + // ac.save(function(err) { + // if (err) { return done(err); } + // return done(null, code); + // }); +})); // Register Exchanges @@ -25,8 +25,8 @@ class AuthorizationCode { }) => void): void {} } -server.exchange(oauth2orize.exchange.code(function(client, code, redirectURI, done) { - AuthorizationCode.findOne(code, function(err, code) { +server.exchange(oauth2orize.exchange.code((client, code, redirectURI, done) => { + AuthorizationCode.findOne(code, (err, code) => { if (err) { return done(err); } if (client.id !== code.clientId) { return done(null, false); } if (redirectURI !== code.redirectURI) { return done(null, false); } @@ -50,27 +50,27 @@ class Clients { // app.get('/dialog/authorize', // login.ensureLoggedIn(), - server.authorize(function(clientID, redirectURI, done) { - Clients.findOne(clientID, function(err, client) { + server.authorize((clientID, redirectURI, done) => { + Clients.findOne(clientID, (err, client) => { if (err) { return done(err); } if (!client) { return done(null, false); } if (client.redirectURI != redirectURI) { return done(null, false); } return done(null, client, client.redirectURI); }); }), - function(req: http.IncomingMessage, res: http.ServerResponse) { + (req: http.IncomingMessage, res: http.ServerResponse) => { // res.render('dialog', { transactionID: req.oauth2.transactionID, // user: req.user, client: req.oauth2.client }); } // ); // Session Serialization -server.serializeClient(function(client, done) { +server.serializeClient((client, done) => { return done(null, client.id); }); -server.deserializeClient(function(id, done) { - Clients.findOne(id, function(err, client) { +server.deserializeClient((id, done) => { + Clients.findOne(id, (err, client) => { if (err) { return done(err); } return done(null, client); });