From f333a89fe662d9da9dae4c351961d6d37e18c1fa Mon Sep 17 00:00:00 2001 From: "ivan.zubok" Date: Thu, 24 Nov 2016 15:47:45 +0200 Subject: [PATCH] add passport-ouath2-client-password --- passport-oauth2-client-password/index.d.ts | 30 ++++++++++ .../passport-oauth2-client-password-tests.ts | 60 +++++++++++++++++++ passport-oauth2-client-password/tsconfig.json | 19 ++++++ passport-oauth2-client-password/tslint.json | 1 + 4 files changed, 110 insertions(+) create mode 100644 passport-oauth2-client-password/index.d.ts create mode 100644 passport-oauth2-client-password/passport-oauth2-client-password-tests.ts create mode 100644 passport-oauth2-client-password/tsconfig.json create mode 100644 passport-oauth2-client-password/tslint.json diff --git a/passport-oauth2-client-password/index.d.ts b/passport-oauth2-client-password/index.d.ts new file mode 100644 index 0000000000..2db225c413 --- /dev/null +++ b/passport-oauth2-client-password/index.d.ts @@ -0,0 +1,30 @@ +// Type definitions for passport-oauth2-client-password 0.1.2 +// Project: https://github.com/jaredhanson/passport-oauth2-client-password +// Definitions by: Ivan Zubok +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// +/// + +import passport = require('passport'); +import express = require('express'); + +interface StrategyOptionsWithRequestInterface { + passReqToCallback: boolean; +} + +interface VerifyFunctionWithRequest { + (req: express.Request, clientId: string, clientSecret: string, done: (error: any, client?: any, info?: any) => void): void; +} + +interface VerifyFunction { + (clientId: string, clientSecret: string, done: (error: any, client?: any, info?: any) => void): void; +} + +declare class Strategy implements passport.Strategy { + constructor(options: StrategyOptionsWithRequestInterface, verify: VerifyFunctionWithRequest); + constructor(verify: VerifyFunction); + + name: string; + authenticate: (req: express.Request, options?: Object) => void; +} diff --git a/passport-oauth2-client-password/passport-oauth2-client-password-tests.ts b/passport-oauth2-client-password/passport-oauth2-client-password-tests.ts new file mode 100644 index 0000000000..a9092ce3ed --- /dev/null +++ b/passport-oauth2-client-password/passport-oauth2-client-password-tests.ts @@ -0,0 +1,60 @@ +'use strict'; + +/** + * Created by Ivan Zubok . + */ + +import express = require('../express'); +import passport = require('passport'); +import oauth2ClientPassword = require('passport-oauth2-client-password'); + +//#region Test Models +interface IClient { + clientId: string; + clientSecret: string; +} + +class Client implements IClient { + public clientId: string; + public clientSecret: string; + + static findOne(client: IClient, callback: (err: Error, client: Client) => void): void { + callback(null, new this); + } +} +//#endregion + +passport.use(new oauth2ClientPassword.Strategy((clientId: string, clientSecret: string, done: any) => { + Client.findOne({clientId, clientSecret}, function(err, client) { + if (err) { + return done(err); + } + + if (!client) { + return done(null, false); + } + + return done(null, client); + }); +})); + +passport.use(new oauth2ClientPassword.Strategy({ + passReqToCallback: false +}, function(req: express.Request, clientId: string, clientSecret: string, done: any) { + Client.findOne({clientId, clientSecret}, function(err, client) { + if (err) { + return done(err, null, { message: 'Access Denied' }); + } + + if (!client) { + return done(null, false, 'Access Denied'); + } + + return done(null, client); + }); +})); + +let app = express(); +app.post('/login', passport.authenticate('oauth2-client-password'), function (req: express.Request, res: express.Response) { + res.redirect('/'); +}); diff --git a/passport-oauth2-client-password/tsconfig.json b/passport-oauth2-client-password/tsconfig.json new file mode 100644 index 0000000000..60557fc1eb --- /dev/null +++ b/passport-oauth2-client-password/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "passport-oauth2-client-password-tests.ts" + ] +} \ No newline at end of file diff --git a/passport-oauth2-client-password/tslint.json b/passport-oauth2-client-password/tslint.json new file mode 100644 index 0000000000..377cc837d4 --- /dev/null +++ b/passport-oauth2-client-password/tslint.json @@ -0,0 +1 @@ +{ "extends": "../tslint.json" }