From 042968c7351f7bc0ab69702c16d7fd5b6320bd7c Mon Sep 17 00:00:00 2001 From: Horiuchi_H Date: Fri, 22 Aug 2014 20:19:14 +0900 Subject: [PATCH 1/6] add express-session type file --- express-session/express-session-tests.ts | 41 ++++++++++++++ express-session/express-session.d.ts | 71 ++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 express-session/express-session-tests.ts create mode 100644 express-session/express-session.d.ts 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..672e3a1c23 --- /dev/null +++ b/express-session/express-session.d.ts @@ -0,0 +1,71 @@ +// 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 { + 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?: { + secret: string; + name?: string; + store?: session.Store; + cookie?: express.CookieOptions; + genid?: (req: express.Request) => string; + rolling?: boolean; + resave?: boolean; + proxy?: boolean; + saveUninitialized?: boolean; + unset?: string; + }): express.RequestHandler; + + module session { + 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; +} + From 432545c4d99dba86a5229bed0e500f66f43b1baa Mon Sep 17 00:00:00 2001 From: Horiuchi_H Date: Fri, 22 Aug 2014 20:19:28 +0900 Subject: [PATCH 2/6] modify passport type file --- passport/passport-tests.ts | 3 +++ passport/passport.d.ts | 19 ++++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/passport/passport-tests.ts b/passport/passport-tests.ts index 7542e2e0ed..f579010a70 100644 --- a/passport/passport-tests.ts +++ b/passport/passport-tests.ts @@ -52,6 +52,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; From 2c47792b0c48d32266e58f680f46420ccf608c1f Mon Sep 17 00:00:00 2001 From: Horiuchi_H Date: Fri, 22 Aug 2014 20:25:48 +0900 Subject: [PATCH 3/6] export SessionOptions interface --- express-session/express-session.d.ts | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/express-session/express-session.d.ts b/express-session/express-session.d.ts index 672e3a1c23..3f091289d8 100644 --- a/express-session/express-session.d.ts +++ b/express-session/express-session.d.ts @@ -35,20 +35,22 @@ declare module Express { declare module "express-session" { import express = require('express'); - function session(options?: { - secret: string; - name?: string; - store?: session.Store; - cookie?: express.CookieOptions; - genid?: (req: express.Request) => string; - rolling?: boolean; - resave?: boolean; - proxy?: boolean; - saveUninitialized?: boolean; - unset?: string; - }): express.RequestHandler; + 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; From 95a310df643f240a5ad1ce7abb39625b9e57a993 Mon Sep 17 00:00:00 2001 From: Horiuchi_H Date: Sat, 23 Aug 2014 23:23:49 +0900 Subject: [PATCH 4/6] modify unittest error --- passport/passport-tests.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/passport/passport-tests.ts b/passport/passport-tests.ts index f579010a70..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) { From da2bb07e037a61e6ead149ae89063b00892f6929 Mon Sep 17 00:00:00 2001 From: Horiuchi_H Date: Sun, 24 Aug 2014 00:11:05 +0900 Subject: [PATCH 5/6] modify unit test --- express-session/express-session.d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/express-session/express-session.d.ts b/express-session/express-session.d.ts index 3f091289d8..5e32e3b7ec 100644 --- a/express-session/express-session.d.ts +++ b/express-session/express-session.d.ts @@ -12,6 +12,8 @@ declare module Express { } export interface Session { + [key: string]: any; + regenerate: (callback: (err: any) => void) => void; destroy: (callback: (err: any) => void) => void; reload: (callback: (err: any) => void) => void; From 215537df6712aed82c4ba6e1a59e7a0c6bf83a2f Mon Sep 17 00:00:00 2001 From: Horiuchi_H Date: Sun, 24 Aug 2014 22:12:05 +0900 Subject: [PATCH 6/6] append contributors for express-session --- CONTRIBUTORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 99864cf671..c327952ab6 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))