@furystack/http-api
Advanced tools
Comparing version 0.1.8 to 0.2.8
@@ -23,3 +23,3 @@ "use strict"; | ||
hostName: "localhost", | ||
identityService: new _1.IdentityService(new core_1.InMemoryStore("", "Id")), | ||
identityService: new _1.IdentityService(), | ||
serverFactory: (listener) => new http_1.Server(listener), | ||
@@ -26,0 +26,0 @@ notFoundAction: new NotFoundAction_1.NotFoundAction(), |
/// <reference types="node" /> | ||
import { IPhysicalStore, IUser } from "@furystack/core"; | ||
import { Constructable, IPhysicalStore, IUser } from "@furystack/core"; | ||
import { Injector } from "@furystack/inject"; | ||
import { IncomingMessage, ServerResponse } from "http"; | ||
import { IExternalLoginService } from "./Models"; | ||
export declare type ILoginUser<T extends IUser> = T & { | ||
Password: string; | ||
}; | ||
export interface IIdentityServiceOptions<TUser> { | ||
users: IPhysicalStore<TUser>; | ||
cookieName: string; | ||
hashMethod: (plain: string) => string; | ||
injector: Injector; | ||
} | ||
export declare class IdentityService<TUser extends ILoginUser<IUser> = ILoginUser<IUser>> { | ||
readonly users: IPhysicalStore<TUser>; | ||
private readonly cookieName; | ||
private readonly hashMethod; | ||
readonly sessions: Map<string, number>; | ||
private hashPassword; | ||
authenticateUser(userName: string, password: string): Promise<TUser>; | ||
@@ -17,4 +21,6 @@ private getSessionIdFromRequest; | ||
cookieLogin(username: string, password: string, serverResponse: ServerResponse): Promise<TUser>; | ||
externalLogin<T extends IExternalLoginService<TUser, TArgs>, TArgs extends any[]>(service: Constructable<T>, ...args: TArgs): Promise<void>; | ||
cookieLogout(req: IncomingMessage, serverResponse: ServerResponse): Promise<void>; | ||
constructor(users: IPhysicalStore<TUser>, cookieName?: string, hashMethod?: (plainText: string) => string); | ||
readonly options: IIdentityServiceOptions<TUser>; | ||
constructor(options?: Partial<IdentityService<TUser>["options"]>); | ||
} |
@@ -12,19 +12,21 @@ "use strict"; | ||
const core_1 = require("@furystack/core"); | ||
const inject_1 = require("@furystack/inject"); | ||
const hash_js_1 = require("hash.js"); | ||
const uuid_1 = require("uuid"); | ||
class IdentityService { | ||
constructor(users, cookieName = "SENTINEL_SESSION", hashMethod = (plain) => hash_js_1.sha256().update(plain).digest("hex")) { | ||
this.users = users; | ||
this.cookieName = cookieName; | ||
this.hashMethod = hashMethod; | ||
constructor(options) { | ||
this.sessions = new Map(); | ||
this.options = { | ||
users: new core_1.InMemoryStore("", "Id"), | ||
cookieName: "fss", | ||
hashMethod: (plain) => hash_js_1.sha256().update(plain).digest("hex"), | ||
injector: inject_1.Injector.Default, | ||
}; | ||
this.options = Object.assign({}, this.options, options); | ||
} | ||
hashPassword(password) { | ||
return this.hashMethod(password); // password; | ||
} | ||
authenticateUser(userName, password) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const match = yield this.users.filter({ | ||
const match = yield this.options.users.filter({ | ||
Username: userName, | ||
Password: this.hashPassword(password), | ||
Password: this.options.hashMethod(password), | ||
}); | ||
@@ -45,3 +47,3 @@ if (match.length === 1) { | ||
}); | ||
const sessionCookie = cookies.find((c) => c.name === this.cookieName); | ||
const sessionCookie = cookies.find((c) => c.name === this.options.cookieName); | ||
if (sessionCookie) { | ||
@@ -65,3 +67,3 @@ return sessionCookie.value; | ||
const userId = this.sessions.get(sessionId); | ||
return (yield this.users.get(userId)) || core_1.visitorUser; | ||
return (yield this.options.users.get(userId)) || core_1.visitorUser; | ||
} | ||
@@ -77,3 +79,3 @@ return core_1.visitorUser; | ||
this.sessions.set(sessionId, user.Id); | ||
serverResponse.setHeader("Set-Cookie", `${this.cookieName}=${sessionId}; Path=/; Secure; HttpOnly`); | ||
serverResponse.setHeader("Set-Cookie", `${this.options.cookieName}=${sessionId}; Path=/; Secure; HttpOnly`); | ||
} | ||
@@ -83,2 +85,8 @@ return user; | ||
} | ||
externalLogin(service, ...args) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const instance = inject_1.Injector.Default.GetInstance(service); | ||
instance.login(this, ...args); | ||
}); | ||
} | ||
cookieLogout(req, serverResponse) { | ||
@@ -89,3 +97,3 @@ return __awaiter(this, void 0, void 0, function* () { | ||
this.sessions.delete(sessionId); | ||
serverResponse.setHeader("Set-Cookie", `${this.cookieName}=; HttpOnly`); | ||
serverResponse.setHeader("Set-Cookie", `${this.options.cookieName}=; HttpOnly`); | ||
} | ||
@@ -92,0 +100,0 @@ }); |
export * from "./IRequestAction"; | ||
export * from "./IExternalLoginService"; |
@@ -7,5 +7,5 @@ /// <reference types="node" /> | ||
childActions: IRequestAction[]; | ||
readonly authenticate: boolean; | ||
readonly authenticate = false; | ||
readonly authorize: never[]; | ||
readonly methodType: string; | ||
readonly methodType = ""; | ||
resolve(segments: string[], incomingMessage: IncomingMessage, serverResponse: ServerResponse): IRequestAction; | ||
@@ -12,0 +12,0 @@ abstract exec(incomingMessage: IncomingMessage, serverResponse: ServerResponse, getContext: () => IContext): Promise<void>; |
{ | ||
"name": "@furystack/http-api", | ||
"version": "0.1.8", | ||
"version": "0.2.8", | ||
"description": "HTTP Api FuryStack package", | ||
@@ -59,4 +59,4 @@ "main": "dist/index.js", | ||
"@furystack/core": "^0.1.8", | ||
"@sensenet/client-utils": "^1.0.1", | ||
"@types/node": "^10.5.7", | ||
"@sensenet/client-utils": "^1.1.0", | ||
"@types/node": "^10.12.0", | ||
"hash.js": "^1.1.5", | ||
@@ -66,11 +66,11 @@ "uuid": "^3.3.2" | ||
"devDependencies": { | ||
"@types/chai": "^4.1.4", | ||
"@types/chai": "^4.1.6", | ||
"@types/mocha": "^5.2.5", | ||
"@types/uuid": "^3.4.3", | ||
"chai": "^4.1.2", | ||
"@types/uuid": "^3.4.4", | ||
"chai": "^4.2.0", | ||
"mocha": "^5.2.0", | ||
"nyc": "^12.0.2", | ||
"nyc": "^13.1.0", | ||
"rimraf": "^2.6.1", | ||
"tslint": "^5.11.0", | ||
"typescript": "^3.0.1" | ||
"typescript": "^3.1.3" | ||
}, | ||
@@ -83,2 +83,2 @@ "config": { | ||
"typings": "./dist/index.d.ts" | ||
} | ||
} |
@@ -5,2 +5,2 @@ # http-api | ||
[![Build Status](https://travis-ci.org/furystack/http-api.svg?branch=master)](https://travis-ci.org/furystack/http-api) | ||
[![codecov](https://codecov.io/gh/furystack/http-api/branch/master/graph/badge.svg)](https://codecov.io/gh/furystack/http-api) | ||
[![codecov](https://codecov.io/gh/furystack/http-api/branch/master/graph/badge.svg)](https://codecov.io/gh/furystack/http-api) [![Greenkeeper badge](https://badges.greenkeeper.io/furystack/http-api.svg)](https://greenkeeper.io/) |
@@ -29,3 +29,3 @@ import { IApi, InMemoryStore, IUser, LoggerCollection } from "@furystack/core"; | ||
hostName: "localhost", | ||
identityService: new IdentityService(new InMemoryStore<ILoginUser<IUser>>("", "Id")), | ||
identityService: new IdentityService(), | ||
serverFactory: (listener) => new HttpServer(listener), | ||
@@ -32,0 +32,0 @@ notFoundAction: new NotFoundAction(), |
@@ -1,18 +0,22 @@ | ||
import { IPhysicalStore, IUser, visitorUser } from "@furystack/core"; | ||
import { Constructable, InMemoryStore, IPhysicalStore, IUser, visitorUser } from "@furystack/core"; | ||
import { Injector } from "@furystack/inject"; | ||
import { sha256 } from "hash.js"; | ||
import { IncomingMessage, ServerResponse } from "http"; | ||
import { v1 } from "uuid"; | ||
import { IExternalLoginService } from "./Models"; | ||
export type ILoginUser<T extends IUser> = T & { Password: string }; | ||
export interface IIdentityServiceOptions<TUser> { | ||
users: IPhysicalStore<TUser>; | ||
cookieName: string; | ||
hashMethod: (plain: string) => string; | ||
injector: Injector; | ||
} | ||
export class IdentityService<TUser extends ILoginUser<IUser> = ILoginUser<IUser>> { | ||
public readonly sessions: Map<string, number> = new Map(); | ||
private hashPassword(password: string): string { | ||
return this.hashMethod(password); // password; | ||
} | ||
public async authenticateUser(userName: string, password: string): Promise<TUser> { | ||
const match = await this.users.filter({ | ||
const match = await this.options.users.filter({ | ||
Username: userName, | ||
Password: this.hashPassword(password), | ||
Password: this.options.hashMethod(password), | ||
} as Partial<TUser>); | ||
@@ -33,3 +37,3 @@ if (match.length === 1) { | ||
}); | ||
const sessionCookie = cookies.find((c) => c.name === this.cookieName); | ||
const sessionCookie = cookies.find((c) => c.name === this.options.cookieName); | ||
if (sessionCookie) { | ||
@@ -54,3 +58,3 @@ return sessionCookie.value; | ||
const userId = this.sessions.get(sessionId); | ||
return await this.users.get(userId as any) || visitorUser as TUser; | ||
return await this.options.users.get(userId as any) || visitorUser as TUser; | ||
} | ||
@@ -66,3 +70,3 @@ | ||
this.sessions.set(sessionId, user.Id); | ||
serverResponse.setHeader("Set-Cookie", `${this.cookieName}=${sessionId}; Path=/; Secure; HttpOnly`); | ||
serverResponse.setHeader("Set-Cookie", `${this.options.cookieName}=${sessionId}; Path=/; Secure; HttpOnly`); | ||
} | ||
@@ -72,2 +76,7 @@ return user; | ||
public async externalLogin<T extends IExternalLoginService<TUser, TArgs>, TArgs extends any[]>(service: Constructable<T>, ...args: TArgs) { | ||
const instance = Injector.Default.GetInstance(service); | ||
instance.login(this, ...args); | ||
} | ||
public async cookieLogout(req: IncomingMessage, serverResponse: ServerResponse) { | ||
@@ -77,8 +86,16 @@ const sessionId = this.getSessionIdFromRequest(req); | ||
this.sessions.delete(sessionId); | ||
serverResponse.setHeader("Set-Cookie", `${this.cookieName}=; HttpOnly`); | ||
serverResponse.setHeader("Set-Cookie", `${this.options.cookieName}=; HttpOnly`); | ||
} | ||
} | ||
constructor(public readonly users: IPhysicalStore<TUser>, private readonly cookieName: string = "SENTINEL_SESSION", private readonly hashMethod: (plainText: string) => string = (plain) => sha256().update(plain).digest("hex")) { } | ||
public readonly options: IIdentityServiceOptions<TUser> = { | ||
users: new InMemoryStore<ILoginUser<TUser>>("", "Id"), | ||
cookieName: "fss", | ||
hashMethod: (plain) => sha256().update(plain).digest("hex"), | ||
injector: Injector.Default, | ||
}; | ||
constructor(options?: Partial<IdentityService<TUser>["options"]>) { | ||
this.options = { ...this.options, ...options }; | ||
} | ||
} |
export * from "./IRequestAction"; | ||
export * from "./IExternalLoginService"; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
115487
99
1630
Updated@types/node@^10.12.0