From 41c87ae5654b7105b8c83778ddd144cc5e265fc4 Mon Sep 17 00:00:00 2001 From: Arne Schubert Date: Sun, 12 Nov 2017 14:50:18 +0100 Subject: [PATCH 1/4] Add type-definition and with tests for npm module "express-oauth-server" --- .../express-oauth-server-tests.ts | 83 +++++++++++++++++++ types/express-oauth-server/index.d.ts | 34 ++++++++ types/express-oauth-server/tsconfig.json | 23 +++++ types/express-oauth-server/tslint.json | 1 + 4 files changed, 141 insertions(+) create mode 100644 types/express-oauth-server/express-oauth-server-tests.ts create mode 100644 types/express-oauth-server/index.d.ts create mode 100644 types/express-oauth-server/tsconfig.json create mode 100644 types/express-oauth-server/tslint.json diff --git a/types/express-oauth-server/express-oauth-server-tests.ts b/types/express-oauth-server/express-oauth-server-tests.ts new file mode 100644 index 0000000000..436763b9b8 --- /dev/null +++ b/types/express-oauth-server/express-oauth-server-tests.ts @@ -0,0 +1,83 @@ +import ExpressOAuthServer = require("express-oauth-server"); +import * as OAuth2Server from "oauth2-server"; +import * as express from "express"; + +const oauth2Model: OAuth2Server.AuthorizationCodeModel = { + getClient: async (clientId: string, clientSecret: string): Promise => { + return undefined; + }, + saveToken: async (token: OAuth2Server.Token, client: OAuth2Server.Client, user: OAuth2Server.User): Promise => { + return token; + }, + getAccessToken: async (accessToken: string): Promise => { + return { + accessToken, + client: {id: "testClient", grants: ["access_token"]}, + user: {id: "testUser"} + }; + }, + verifyScope: async (token: OAuth2Server.Token, scope: string): Promise => { + return true; + }, + getAuthorizationCode: async (authorizationCode: string): Promise => { + return { + authorizationCode, + expiresAt: new Date(), + redirectUri: "www.test.com", + client: {id: "testClient", grants: ["access_token"]}, + user: {id: "testUser"} + }; + }, + saveAuthorizationCode: async (code: OAuth2Server.AuthorizationCode, client: OAuth2Server.Client, user: OAuth2Server.User): Promise => { + return code; + }, + revokeAuthorizationCode: async (code: OAuth2Server.AuthorizationCode): Promise => { + return true; + } +}; + +const serverOptions: OAuth2Server.ServerOptions = { + model: oauth2Model, +}; +const expressOAuthServer: ExpressOAuthServer = new ExpressOAuthServer(serverOptions); + +let oAuthServer: OAuth2Server; +let resultingTokenMiddleware: ( + request: express.Request, + response: express.Response, + next: express.NextFunction, +) => Promise; +let resultingAuthorizationCodeMiddleware: ( + request: express.Request, + response: express.Response, + next: express.NextFunction, +) => Promise; + +oAuthServer = expressOAuthServer.server; +resultingTokenMiddleware = expressOAuthServer.authenticate(); +resultingTokenMiddleware = expressOAuthServer.token(); +resultingAuthorizationCodeMiddleware = expressOAuthServer.authorize(); + +// Real-life example + +const expressApp = express(); + +expressApp.all( + "/path", + expressOAuthServer.authenticate(), + (req: express.Request, res: express.Response, next: express.NextFunction) => { + res.json({message: "Secure data"}); + }, +); + +expressApp.get( + "/profile", + expressOAuthServer.authenticate({scope: "profile"}), + (req: express.Request & {user?: OAuth2Server.Token}, res: express.Response, next: express.NextFunction) => { + res.json({ + profile: req.user + }); + }, +); + +expressApp.listen(1234); diff --git a/types/express-oauth-server/index.d.ts b/types/express-oauth-server/index.d.ts new file mode 100644 index 0000000000..79d45c0db2 --- /dev/null +++ b/types/express-oauth-server/index.d.ts @@ -0,0 +1,34 @@ +// Type definitions for express-oauth-server 2.0 +// Project: https://github.com/oauthjs/express-oauth-server#readme +// Definitions by: Arne Schubert +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 + +import * as express from 'express'; +import * as OAuth2Server from 'oauth2-server'; + +declare class ExpressOAuthServer { + server: OAuth2Server; + + constructor(options: OAuth2Server.ServerOptions); + + authenticate(options?: OAuth2Server.AuthenticateOptions): ( + request: express.Request, + response: express.Response, + next: express.NextFunction, + ) => Promise; + + authorize(options?: OAuth2Server.AuthorizeOptions): ( + request: express.Request, + response: express.Response, + next: express.NextFunction, + ) => Promise; + + token(options?: OAuth2Server.TokenOptions): ( + request: express.Request, + response: express.Response, + next: express.NextFunction, + ) => Promise; +} + +export = ExpressOAuthServer; diff --git a/types/express-oauth-server/tsconfig.json b/types/express-oauth-server/tsconfig.json new file mode 100644 index 0000000000..197ec65f00 --- /dev/null +++ b/types/express-oauth-server/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "express-oauth-server-tests.ts" + ] +} diff --git a/types/express-oauth-server/tslint.json b/types/express-oauth-server/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/express-oauth-server/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From 97959138da840539f8a3f52b28c8d3488232ecdb Mon Sep 17 00:00:00 2001 From: Arne Schubert Date: Sun, 12 Nov 2017 15:00:27 +0100 Subject: [PATCH 2/4] Fix TypeScript version according to the express required version --- types/express-serve-static-core/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/express-serve-static-core/index.d.ts b/types/express-serve-static-core/index.d.ts index 9119c8497f..58c8ec1bf8 100644 --- a/types/express-serve-static-core/index.d.ts +++ b/types/express-serve-static-core/index.d.ts @@ -2,7 +2,7 @@ // Project: http://expressjs.com // Definitions by: Boris Yankov , Michał Lytek , Kacper Polak // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.1 +// TypeScript Version: 2.2 // This extracts the core definitions from express to prevent a circular dependency between express and serve-static /// From a2e64cd60f574992ee8011705f31e19d0a2403ba Mon Sep 17 00:00:00 2001 From: Arne Schubert Date: Sun, 12 Nov 2017 15:26:00 +0100 Subject: [PATCH 3/4] Revert "Fix TypeScript version according to the express required version" This reverts commit 97959138da840539f8a3f52b28c8d3488232ecdb. --- types/express-serve-static-core/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/express-serve-static-core/index.d.ts b/types/express-serve-static-core/index.d.ts index 58c8ec1bf8..9119c8497f 100644 --- a/types/express-serve-static-core/index.d.ts +++ b/types/express-serve-static-core/index.d.ts @@ -2,7 +2,7 @@ // Project: http://expressjs.com // Definitions by: Boris Yankov , Michał Lytek , Kacper Polak // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.2 +// TypeScript Version: 2.1 // This extracts the core definitions from express to prevent a circular dependency between express and serve-static /// From 906b7c329a0a60ab55c20c6659621df6b145f97b Mon Sep 17 00:00:00 2001 From: Arne Schubert Date: Sun, 12 Nov 2017 15:26:39 +0100 Subject: [PATCH 4/4] Fix TypeScript version according to the express required version --- types/express-oauth-server/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/express-oauth-server/index.d.ts b/types/express-oauth-server/index.d.ts index 79d45c0db2..0a5a1162ea 100644 --- a/types/express-oauth-server/index.d.ts +++ b/types/express-oauth-server/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/oauthjs/express-oauth-server#readme // Definitions by: Arne Schubert // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.1 +// TypeScript Version: 2.2 import * as express from 'express'; import * as OAuth2Server from 'oauth2-server';