Merge pull request #2707 from horiuchi/passport-fix

Passport fix
This commit is contained in:
Masahiro Wakame
2014-08-24 22:13:48 +09:00
5 changed files with 136 additions and 6 deletions

View File

@@ -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))

View File

@@ -0,0 +1,41 @@
/// <reference path="./express-session.d.ts" />
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 = <MySession>req.session;
if (sess.views) {
sess.views++
res.setHeader('Content-Type', 'text/html')
res.write('<p>views: ' + sess.views + '</p>')
res.write('<p>expires in: ' + (sess.cookie.maxAge / 1000) + 's</p>')
res.end()
} else {
sess.views = 1
res.end('welcome to the session demo. refresh!')
}
});

75
express-session/express-session.d.ts vendored Normal file
View File

@@ -0,0 +1,75 @@
// Type definitions for express-session
// Project: https://www.npmjs.org/package/express-session
// Definitions by: Hiroki Horiuchi <https://github.com/horiuchi/>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
/// <reference path="../express/express.d.ts" />
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;
}

View File

@@ -1,5 +1,6 @@
/// <reference path="passport.d.ts" />
/// <reference path="../express/express.d.ts" />
/// <reference path="../express-session/express-session.d.ts" />
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: '/',

View File

@@ -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;