mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-08-14 05:50:20 +00:00
Merge pull request #7848 from isman-usoh/passport-http-bearer
add type definitions for passport-http-bearer 1.0.1
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
/// <reference path="./passport-http-bearer.d.ts"/>
|
||||
|
||||
/**
|
||||
* Created by Isman Usoh <https://github.com/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("/");
|
||||
});
|
||||
+40
@@ -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 <https://github.com/isman-usoh>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
/// <reference path="../passport/passport.d.ts"/>
|
||||
/// <reference path="../express/express.d.ts"/>
|
||||
|
||||
declare module "passport-http-bearer" {
|
||||
|
||||
import passport = require("passport");
|
||||
import express = require("express");
|
||||
|
||||
interface IStrategyOptions {
|
||||
scope: string | Array<string>;
|
||||
realm: string;
|
||||
passReqToCallback: boolean;
|
||||
}
|
||||
interface IVerifyOptions {
|
||||
message: string;
|
||||
scope: string | Array<string>;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user