add passport-ouath2-client-password

This commit is contained in:
ivan.zubok
2016-11-24 15:47:45 +02:00
parent ea7cd856c1
commit f333a89fe6
4 changed files with 110 additions and 0 deletions
+30
View File
@@ -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 <https://github.com/akaNightmare>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="passport"/>
/// <reference types="express"/>
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;
}
@@ -0,0 +1,60 @@
'use strict';
/**
* Created by Ivan Zubok <https://github.com/akaNightmare>.
*/
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('/');
});
@@ -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"
]
}
@@ -0,0 +1 @@
{ "extends": "../tslint.json" }