diff --git a/passport-http/index.d.ts b/passport-http/index.d.ts new file mode 100644 index 0000000000..8efefc0c95 --- /dev/null +++ b/passport-http/index.d.ts @@ -0,0 +1,62 @@ +// Type definitions for passport-http 0.3.0 +// Project: https://github.com/jaredhanson/passport-http +// Definitions by: Christophe Vidal +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// +/// + +import passport = require("passport"); +import express = require("express"); + +interface BasicStrategyOptions { + realm?: string; + passReqToCallback?: boolean; +} + +interface DigestStrategyOptions { + realm?: string; + domain?: string | Array; + opaque?: string; + algorithm?: string; + qop?: string | Array; +} + +interface DigestValidateOptions { + nonce: string; + cnonce: string; + nc: number; + opaque: string; +} + +interface BasicVerifyFunction { + (username: string, password: string, done: (error: any, user?: any) => void): void; +} + +interface BasicVerifyFunctionWithRequest { + (req: express.Request, username: string, password: string, done: (error: any, user?: any) => void): void; +} + +interface DigestSecretFunction { + (username: string, done: (error: any, user?: any, password?: any) => void): void; +} + +interface DigestValidateFunction { + (params: DigestValidateOptions, done: (error: any, valid: boolean) => void): void; +} + +declare class BasicStrategy implements passport.Strategy { + constructor(verify: BasicVerifyFunction); + constructor(options: BasicStrategyOptions, verify: BasicVerifyFunction); + + name: string; + authenticate: (req: express.Request, options?: Object) => void; +} + +declare class DigestStrategy implements passport.Strategy { + constructor(secret: DigestSecretFunction, validate?: DigestValidateFunction); + constructor(options: DigestStrategyOptions, secret: DigestSecretFunction, validate?: DigestValidateFunction); + + name: string; + authenticate: (req: express.Request, options?: Object) => void; +} diff --git a/passport-http/passport-http-tests.ts b/passport-http/passport-http-tests.ts new file mode 100644 index 0000000000..22a486e6ec --- /dev/null +++ b/passport-http/passport-http-tests.ts @@ -0,0 +1,138 @@ +/// + +/** + * Created by Christophe Vidal + */ + +import passport = require("passport"); +import http = require("passport-http"); + +interface IUser { + username: string; + password?: string; +} + +class User { + public password: string; + + static findOne(user: IUser, callback: (error: Error, user: User) => void): void { + callback(null, new User()); + } +} + +function validateNonce(nonce: string) { +} + +function validateParams(nonce: string, cnonce: string, nc: number, opaque: string) { +} + +passport.use(new http.BasicStrategy((username: string, password: string, done: any) => { + User.findOne({ + username: username, + password: password, + }, (error, user) => { + if (error) { + return done(error); + } + + if (!user) { + return done(null, false); + } + + done(null, user); + }); +})); + +passport.use(new http.BasicStrategy({ + realm: "User", + passReqToCallback: true, +}, (username: string, password: string, done: any) => { + User.findOne({ + username: username, + password: password, + }, (error, user) => { + if (error) { + return done(error); + } + + if (!user) { + return done(null, false); + } + + done(null, user); + }); +})); + +passport.use(new http.DigestStrategy((username: string, done: any) => { + User.findOne({username: username}, (error, user) => { + if (error) { + return done(error); + } + + if (!user) { + return done(null, false); + } + + done(null, user, user.password); + }); +})); + +passport.use(new http.DigestStrategy((username: string, done: any) => { + User.findOne({username: username}, (error, user) => { + if (error) { + return done(error); + } + + if (!user) { + return done(null, false); + } + + done(null, user, user.password); + }); +}, (params: http.DigestValidateOptions, done: any) => { + validateParams(params.nonce, params.cnonce, params.nc, params.opaque); + done(null, true); +})); + +passport.use(new http.DigestStrategy({ + realm: "User", + domain: "example.com", + opaque: "OpAqUeStRiNg", + algorithm: "MD5", + qop: "auth", +}, (username: string, done: any) => { + User.findOne({username: username}, (error, user) => { + if (error) { + return done(error); + } + + if (!user) { + return done(null, false); + } + + done(null, user, user.password); + }); +})); + +passport.use(new http.DigestStrategy({ + realm: "User", + domain: "example.com", + opaque: "OpAqUeStRiNg", + algorithm: "MD5", + qop: "auth", +}, (username: string, done: any) => { + User.findOne({username: username}, (error, user) => { + if (error) { + return done(error); + } + + if (!user) { + return done(null, false); + } + + done(null, user, user.password); + }); +}, (params: http.DigestValidateOptions, done: any) => { + validateParams(params.nonce, params.cnonce, params.nc, params.opaque); + done(null, true); +})); diff --git a/passport-http/tsconfig.json b/passport-http/tsconfig.json new file mode 100644 index 0000000000..c1ff4fe0f4 --- /dev/null +++ b/passport-http/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "strictNullChecks": false, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "passport-http-tests.ts" + ] +}