Merge pull request #21452 from atd-schubert/add-express-oauth-server

Add type-definition with tests for npm module "express-oauth-server"
This commit is contained in:
Nathan Shively-Sanders
2017-11-13 16:05:21 -08:00
committed by GitHub
4 changed files with 141 additions and 0 deletions

View File

@@ -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<OAuth2Server.Client | OAuth2Server.Falsey> => {
return undefined;
},
saveToken: async (token: OAuth2Server.Token, client: OAuth2Server.Client, user: OAuth2Server.User): Promise<OAuth2Server.Token> => {
return token;
},
getAccessToken: async (accessToken: string): Promise<OAuth2Server.Token> => {
return {
accessToken,
client: {id: "testClient", grants: ["access_token"]},
user: {id: "testUser"}
};
},
verifyScope: async (token: OAuth2Server.Token, scope: string): Promise<boolean> => {
return true;
},
getAuthorizationCode: async (authorizationCode: string): Promise<OAuth2Server.AuthorizationCode> => {
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<OAuth2Server.AuthorizationCode> => {
return code;
},
revokeAuthorizationCode: async (code: OAuth2Server.AuthorizationCode): Promise<boolean> => {
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<OAuth2Server.Token>;
let resultingAuthorizationCodeMiddleware: (
request: express.Request,
response: express.Response,
next: express.NextFunction,
) => Promise<OAuth2Server.AuthorizationCode>;
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);

34
types/express-oauth-server/index.d.ts vendored Normal file
View File

@@ -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 <https://github.com/atd-schubert>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
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<OAuth2Server.Token>;
authorize(options?: OAuth2Server.AuthorizeOptions): (
request: express.Request,
response: express.Response,
next: express.NextFunction,
) => Promise<OAuth2Server.AuthorizationCode>;
token(options?: OAuth2Server.TokenOptions): (
request: express.Request,
response: express.Response,
next: express.NextFunction,
) => Promise<OAuth2Server.Token>;
}
export = ExpressOAuthServer;

View File

@@ -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"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }