diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 10ebf956da..bb0933af5e 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -79,6 +79,7 @@ All definitions files include a header with the author and editors, so at some p * [expect.js](https://github.com/LearnBoost/expect.js) (by [Teppei Sato](https://github.com/teppeis)) * [expectations](https://github.com/spmason/expectations) (by [vvakame](https://github.com/vvakame)) * [Express](http://expressjs.com/) (by [Boris Yankov](https://github.com/borisyankov)) +* [express-session](https://www.npmjs.org/package/express-session) (by [Hiroki Horiuchi](https://github.com/horiuchi/)) * [Ext JS](http://www.sencha.com/products/extjs/) (by [Brian Kotek](https://github.com/brian428)) * [Fabric.js](http://fabricjs.com/) (by [Oliver Klemencic](https://github.com/oklemencic/)) * [Fancybox](http://fancybox.net/) (by [Boris Yankov](https://github.com/borisyankov)) diff --git a/express-session/express-session-tests.ts b/express-session/express-session-tests.ts new file mode 100644 index 0000000000..1e1f909b14 --- /dev/null +++ b/express-session/express-session-tests.ts @@ -0,0 +1,41 @@ +/// + +import express = require('express'); +import session = require('express-session'); + +var app = express(); + +app.use(session({ + secret: 'keyboard cat' +})); +app.use(session({ + secret: 'keyboard cat', + name: 'connect.sid', + store: new session.MemoryStore(), + cookie: { path: '/', httpOnly: true, secure: false, maxAge: null }, + genid: (req: express.Request): string => { return ''; }, + rolling: false, + resave: true, + proxy: true, + saveUninitialized: true, + unset: 'keep' +})); + + +interface MySession extends Express.Session { + views: number; +} +app.use(function(req, res, next) { + var sess = req.session; + if (sess.views) { + sess.views++ + res.setHeader('Content-Type', 'text/html') + res.write('

views: ' + sess.views + '

') + res.write('

expires in: ' + (sess.cookie.maxAge / 1000) + 's

') + res.end() + } else { + sess.views = 1 + res.end('welcome to the session demo. refresh!') + } +}); + diff --git a/express-session/express-session.d.ts b/express-session/express-session.d.ts new file mode 100644 index 0000000000..5e32e3b7ec --- /dev/null +++ b/express-session/express-session.d.ts @@ -0,0 +1,75 @@ +// Type definitions for express-session +// Project: https://www.npmjs.org/package/express-session +// Definitions by: Hiroki Horiuchi +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module Express { + + export interface Request { + session?: Session; + } + + export interface Session { + [key: string]: any; + + regenerate: (callback: (err: any) => void) => void; + destroy: (callback: (err: any) => void) => void; + reload: (callback: (err: any) => void) => void; + save: (callback: (err: any) => void) => void; + touch: (callback: (err: any) => void) => void; + + cookie: SessionCookie; + } + export interface SessionCookie { + originalMaxAge: number; + path: string; + maxAge: number; + secure?: boolean; + httpOnly: boolean; + domain?: string; + expires: Date; + serialize: (name: string, value: string) => string; + } +} + +declare module "express-session" { + import express = require('express'); + + function session(options?: session.SessionOptions): express.RequestHandler; + + module session { + export interface SessionOptions { + secret: string; + name?: string; + store?: Store; + cookie?: express.CookieOptions; + genid?: (req: express.Request) => string; + rolling?: boolean; + resave?: boolean; + proxy?: boolean; + saveUninitialized?: boolean; + unset?: string; + } + + export interface Store { + get: (sid: string, callback: (err: any, session: Express.Session) => void) => void; + set: (sid: string, session: Express.Session, callback: (err: any) => void) => void; + destroy: (sid: string, callback: (err: any) => void) => void; + length?: (callback: (err: any, length: number) => void) => void; + clear?: (callback: (err: any) => void) => void; + } + export class MemoryStore implements Store { + get: (sid: string, callback: (err: any, session: Express.Session) => void) => void; + set: (sid: string, session: Express.Session, callback: (err: any) => void) => void; + destroy: (sid: string, callback: (err: any) => void) => void; + all: (callback: (err: any, obj: { [sid: string]: Express.Session; }) => void) => void; + length: (callback: (err: any, length: number) => void) => void; + clear: (callback: (err: any) => void) => void; + } + } + + export = session; +} + diff --git a/passport/passport-tests.ts b/passport/passport-tests.ts index 7542e2e0ed..423d925b4c 100644 --- a/passport/passport-tests.ts +++ b/passport/passport-tests.ts @@ -1,5 +1,6 @@ /// /// +/// import express = require('express'); import passport = require('passport'); @@ -37,7 +38,7 @@ app.post('/login', function(req, res, next) { passport.authenticate('local', function(err: any, user: { username: string; }, info: { message: string; }) { if (err) { return next(err) } if (!user) { - req.session.error = info.message; + req.session['error'] = info.message; return res.redirect('/login') } req.logIn(user, function(err) { @@ -52,6 +53,9 @@ app.get('/logout', function(req, res) { res.redirect('/'); }); +app.post('/auth/token', passport.authenticate(['basic', 'oauth2-client-password'], { session: false })); + + function authSetting(): void { var authOption = { successRedirect: '/', diff --git a/passport/passport.d.ts b/passport/passport.d.ts index 977323140f..6528c393d3 100644 --- a/passport/passport.d.ts +++ b/passport/passport.d.ts @@ -7,7 +7,6 @@ declare module Express { export interface Request { - session?: any; authInfo?: any; // These declarations are merged into express's Request type @@ -36,7 +35,12 @@ declare module 'passport' { function authenticate(strategy: string, callback?: Function): express.Handler; function authenticate(strategy: string, options: Object, callback?: Function): express.Handler; - function authorize(strategy: string, options: Object, callback?: express.Handler): express.Handler; + function authenticate(strategies: string[], callback?: Function): express.Handler; + function authenticate(strategies: string[], options: Object, callback?: Function): express.Handler; + function authorize(strategy: string, callback?: Function): express.Handler; + function authorize(strategy: string, options: Object, callback?: Function): express.Handler; + function authorize(strategies: string[], callback?: Function): express.Handler; + function authorize(strategies: string[], options: Object, callback?: Function): express.Handler; function serializeUser(fn: (user: any, done: (err: any, id: any) => void) => void): void; function deserializeUser(fn: (id: any, done: (err: any, user: any) => void) => void): void; function transformAuthInfo(fn: (info: any, done: (err: any, info: any) => void) => void): void; @@ -49,9 +53,14 @@ declare module 'passport' { initialize(options?: { userProperty: string; }): express.Handler; session(options?: { pauseStream: boolean; }): express.Handler; - authenticate(strategy: string, callback2: (err: any, user: any, info: any) => void): express.Handler; - authenticate(strategy: string, options: Object, callback?: express.Handler): express.Handler; - authorize(strategy: string, options: Object, callback?: express.Handler): express.Handler; + authenticate(strategy: string, callback?: Function): express.Handler; + authenticate(strategy: string, options: Object, callback?: Function): express.Handler; + authenticate(strategies: string[], callback?: Function): express.Handler; + authenticate(strategies: string[], options: Object, callback?: Function): express.Handler; + authorize(strategy: string, callback?: Function): express.Handler; + authorize(strategy: string, options: Object, callback?: Function): express.Handler; + authorize(strategies: string[], callback?: Function): express.Handler; + authorize(strategies: string[], options: Object, callback?: Function): express.Handler; serializeUser(fn: (user: any, done: (err: any, id: any) => void) => void): void; deserializeUser(fn: (id: any, done: (err: any, user: any) => void) => void): void; transformAuthInfo(fn: (info: any, done: (err: any, info: any) => void) => void): void;