From 39a41a4404992fbee86e8096d2c4052680b5cdc2 Mon Sep 17 00:00:00 2001 From: Isman Usoh Date: Thu, 28 Jan 2016 04:11:34 +0700 Subject: [PATCH] add definitions for passport-http-bearer 1.0.1 --- .../passport-http-bearer-tests.ts | 60 +++++++++++++++++++ .../passport-http-bearer.d.ts | 40 +++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 passport-http-bearer/passport-http-bearer-tests.ts create mode 100644 passport-http-bearer/passport-http-bearer.d.ts diff --git a/passport-http-bearer/passport-http-bearer-tests.ts b/passport-http-bearer/passport-http-bearer-tests.ts new file mode 100644 index 0000000000..b776bbbbd7 --- /dev/null +++ b/passport-http-bearer/passport-http-bearer-tests.ts @@ -0,0 +1,60 @@ +/// + +/** + * Created by Isman Usoh . + */ + +import express = require("express"); +import passport = require("passport"); +import httpBearer = require("passport-http-bearer"); + +//#region Test Models +interface IUser { + token: string; +} + +class User implements IUser { + public token: string; + + static findOne(user: IUser, callback: (err: Error, user: User) => void): void { + callback(null, new User()); + } +} +//#endregion + +passport.use(new httpBearer.Strategy((token: string, done: any) => { + User.findOne({ token: token }, function(err, user) { + if (err) { + return done(err); + } + + if (!user) { + return done(null, false); + } + + return done(null, user); + }); +})); + +passport.use(new httpBearer.Strategy({ + scope: ["read", "write"], + realm: "User", + passReqToCallback: true +}, function(req: express.Request, token: string, done: any) { + User.findOne({ token: token }, function(err, user) { + if (err) { + return done(err, null, { message: "Access Denied" }); + } + + if (!user) { + return done(null, false, "Access Denied"); + } + + return done(null, user); + }); +})); + +let app = express(); +app.post("/login", passport.authenticate("bearer", { failureRedirect: "/login" }), function(req, res) { + res.redirect("/"); +}); diff --git a/passport-http-bearer/passport-http-bearer.d.ts b/passport-http-bearer/passport-http-bearer.d.ts new file mode 100644 index 0000000000..6573b4afa4 --- /dev/null +++ b/passport-http-bearer/passport-http-bearer.d.ts @@ -0,0 +1,40 @@ +// Type definitions for passport-http-bearer 1.0.1 +// Project: https://github.com/jaredhanson/passport-http-bearer +// Definitions by: Isman Usoh +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// +/// + +declare module "passport-http-bearer" { + + import passport = require("passport"); + import express = require("express"); + + interface IStrategyOptions { + scope: string | Array; + realm: string; + passReqToCallback: boolean; + } + interface IVerifyOptions { + message: string; + scope: string | Array; + } + + interface VerifyFunction { + (token: string, done: (error: any, user?: any, options?: IVerifyOptions | string) => void): void; + } + + interface VerifyFunctionWithRequest { + (req: express.Request, token: string, done: (error: any, user?: any, options?: IVerifyOptions | string) => void): void; + } + + class Strategy implements passport.Strategy { + constructor(verify: VerifyFunction); + constructor(options: IStrategyOptions, verify: VerifyFunction); + constructor(options: IStrategyOptions, verify: VerifyFunctionWithRequest); + + name: string; + authenticate: (req: express.Request, options?: Object) => void; + } +}