diff --git a/passport-jwt/passport-jwt-tests.ts b/passport-jwt/passport-jwt-tests.ts new file mode 100644 index 00000000000000..ea173f3e0700e6 --- /dev/null +++ b/passport-jwt/passport-jwt-tests.ts @@ -0,0 +1,36 @@ +/// +/// +'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; diff --git a/passport-jwt/passport-jwt.d.ts b/passport-jwt/passport-jwt.d.ts new file mode 100644 index 00000000000000..9f6d91cc4144a8 --- /dev/null +++ b/passport-jwt/passport-jwt.d.ts @@ -0,0 +1,50 @@ +// Type definitions for passport-jwt 2.0 +// Project: https://github.com/themikenicholson/passport-jwt +// Definitions by: TANAKA Koichi +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +/// +/// + +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; + } +}