authen-express
Advanced tools
Comparing version 0.0.1 to 0.0.2
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var AuthenticationController = (function () { | ||
function AuthenticationController(log, auth, cookie) { | ||
function AuthenticationController(log, login, cookie, decrypt) { | ||
this.log = log; | ||
this.auth = auth; | ||
this.login = login; | ||
this.cookie = cookie; | ||
this.decrypt = decrypt; | ||
this.authenticate = this.authenticate.bind(this); | ||
@@ -16,4 +17,13 @@ } | ||
} | ||
this.auth(user).then(function (result) { | ||
var account = result.user; | ||
if (this.decrypt) { | ||
var p = this.decrypt(user.password); | ||
if (p === undefined) { | ||
return res.status(401).end('cannot decrypt password'); | ||
} | ||
else { | ||
user.password = p; | ||
} | ||
} | ||
this.login(user).then(function (r) { | ||
var account = r.user; | ||
if (_this.cookie && account && account.token && account.tokenExpiredTime) { | ||
@@ -26,6 +36,6 @@ res.status(200).cookie('token', account.token, { | ||
secure: true, | ||
}).json(result).end(); | ||
}).json(r).end(); | ||
} | ||
else { | ||
res.status(200).json(result).end(); | ||
res.status(200).json(r).end(); | ||
} | ||
@@ -46,4 +56,4 @@ }).catch(function (err) { return handleError(err, res, _this.log); }); | ||
var _this = this; | ||
this.privileges().then(function (result) { | ||
res.json(result).end(); | ||
this.privileges().then(function (r) { | ||
res.json(r).end(); | ||
}).catch(function (err) { return handleError(err, res, _this.log); }); | ||
@@ -68,9 +78,4 @@ }; | ||
function toString(v) { | ||
if (typeof v === 'string') { | ||
return v; | ||
} | ||
else { | ||
return JSON.stringify(v); | ||
} | ||
return typeof v === 'string' ? v : JSON.stringify(v); | ||
} | ||
exports.toString = toString; |
{ | ||
"name": "authen-express", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "authen-express", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -52,3 +52,3 @@ import { Request, Response } from 'express'; | ||
export class AuthenticationController<T extends User> { | ||
constructor (private log: Log, private auth: (user: T) => Promise<AuthResult>, public cookie?: boolean) { | ||
constructor (public log: Log, public login: (user: T) => Promise<AuthResult>, public cookie?: boolean, public decrypt?: (cipherText: string) => string|undefined) { | ||
this.authenticate = this.authenticate.bind(this); | ||
@@ -61,4 +61,12 @@ } | ||
} | ||
this.auth(user).then(result => { | ||
const account = result.user; | ||
if (this.decrypt) { | ||
const p = this.decrypt(user.password); | ||
if (p === undefined) { | ||
return res.status(401).end('cannot decrypt password'); | ||
} else { | ||
user.password = p; | ||
} | ||
} | ||
this.login(user).then(r => { | ||
const account = r.user; | ||
if (this.cookie && account && account.token && account.tokenExpiredTime) { | ||
@@ -73,5 +81,5 @@ res.status(200).cookie( | ||
secure: true, | ||
}).json(result).end(); | ||
}).json(r).end(); | ||
} else { | ||
res.status(200).json(result).end(); | ||
res.status(200).json(r).end(); | ||
} | ||
@@ -82,2 +90,3 @@ }).catch(err => handleError(err, res, this.log)); | ||
export const AuthenticationHandler = AuthenticationController; | ||
// tslint:disable-next-line:max-classes-per-file | ||
export class PrivilegeController { | ||
@@ -88,4 +97,4 @@ constructor(private log: Log, public privileges: () => Promise<Privilege[]>) { | ||
all(req: Request, res: Response) { | ||
this.privileges().then(result => { | ||
res.json(result).end(); | ||
this.privileges().then(r => { | ||
res.json(r).end(); | ||
}).catch(err => handleError(err, res, this.log)); | ||
@@ -106,7 +115,3 @@ } | ||
export function toString(v: any): string { | ||
if (typeof v === 'string') { | ||
return v; | ||
} else { | ||
return JSON.stringify(v); | ||
} | ||
return typeof v === 'string' ? v : JSON.stringify(v); | ||
} |
7024
209