mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-23 12:56:46 +08:00
Add definition for passport-jwt
npm: https://www.npmjs.com/package/passport-jwt project: https://github.com/themikenicholson/passport-jwt
This commit is contained in:
36
passport-jwt/passport-jwt-tests.ts
Normal file
36
passport-jwt/passport-jwt-tests.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
/// <reference path="passport-jwt.d.ts" />
|
||||
/// <reference path="../passport/passport.d.ts" />
|
||||
'use strict';
|
||||
|
||||
import {Strategy as JwtStrategy, ExtractJwt, StrategyOptions} from 'passport-jwt';
|
||||
import {Request} from 'express';
|
||||
import * as passport from 'passport';
|
||||
|
||||
let opts: StrategyOptions = {
|
||||
jwtFromRequest: ExtractJwt.fromAuthHeader(),
|
||||
secretOrKey: 'secret',
|
||||
issuer: "accounts.example.com",
|
||||
audience: "example.org"
|
||||
};
|
||||
|
||||
passport.use(new JwtStrategy(opts, function(jwt_payload, done) {
|
||||
findUser({id: jwt_payload.sub}, function(err, user) {
|
||||
if (err) {
|
||||
return done(err, false);
|
||||
}
|
||||
if (user) {
|
||||
done(null, user);
|
||||
} else {
|
||||
done(null, false, {message: 'foo'});
|
||||
// or you could create a new account
|
||||
}
|
||||
});
|
||||
}));
|
||||
|
||||
opts.jwtFromRequest = ExtractJwt.fromHeader('x-api-key');
|
||||
opts.jwtFromRequest = ExtractJwt.fromBodyField('field_name');
|
||||
opts.jwtFromRequest = ExtractJwt.fromUrlQueryParameter('param_name');
|
||||
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('param_name');
|
||||
opts.jwtFromRequest = (req: Request) => { return req.query.token; };
|
||||
|
||||
declare function findUser(condition: {id: string}, callback: (error: any, user :any) => void): void;
|
||||
50
passport-jwt/passport-jwt.d.ts
vendored
Normal file
50
passport-jwt/passport-jwt.d.ts
vendored
Normal file
@@ -0,0 +1,50 @@
|
||||
// Type definitions for passport-jwt 2.0
|
||||
// Project: https://github.com/themikenicholson/passport-jwt
|
||||
// Definitions by: TANAKA Koichi <https://github.com/mugeso/>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
/// <reference path="../passport-strategy/passport-strategy.d.ts" />
|
||||
/// <reference path="../express/express.d.ts" />
|
||||
|
||||
declare module 'passport-jwt' {
|
||||
import {Strategy as PassportStrategy} from 'passport-strategy';
|
||||
import {Request} from 'express';
|
||||
|
||||
export class Strategy extends PassportStrategy {
|
||||
constructor(opt: StrategyOptions, verify: VerifyCallback);
|
||||
constructor(opt: StrategyOptions, verify: VerifyCallbackWithRequest);
|
||||
}
|
||||
|
||||
export interface StrategyOptions {
|
||||
secretOrKey: string;
|
||||
jwtFromRequest: JwtFromRequestFunction;
|
||||
issuer?: string;
|
||||
audience?: string;
|
||||
algorithms?: string[];
|
||||
ignoreExpiration?: boolean;
|
||||
passReqToCallback?: boolean;
|
||||
}
|
||||
|
||||
export interface VerifyCallback {
|
||||
(payload: any, done: VerifiedCallback): void;
|
||||
}
|
||||
|
||||
export interface VerifyCallbackWithRequest {
|
||||
(req: Request, payload: any, done: VerifiedCallback): void;
|
||||
}
|
||||
|
||||
export interface VerifiedCallback {
|
||||
(error: any, user?: any, info?: any): void;
|
||||
}
|
||||
|
||||
export interface JwtFromRequestFunction {
|
||||
(req: Request): string;
|
||||
}
|
||||
|
||||
export namespace ExtractJwt {
|
||||
export function fromHeader(header_name: string): JwtFromRequestFunction;
|
||||
export function fromBodyField(field_name: string): JwtFromRequestFunction;
|
||||
export function fromUrlQueryParameter(param_name: string): JwtFromRequestFunction;
|
||||
export function fromAuthHeaderWithScheme(auth_scheme: string): JwtFromRequestFunction;
|
||||
export function fromAuthHeader(): JwtFromRequestFunction;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user