From afb6b70e09209ede15cf4fe319b9bb5cbd5a751d Mon Sep 17 00:00:00 2001 From: Ujjwal Gupta Date: Sat, 25 Nov 2023 18:22:30 +0400 Subject: [PATCH 1/4] create sessionstore and use it in sessionprovider --- src/abstracts/index.ts | 1 - src/abstracts/session_provider.ts | 39 ---------- src/constants/fort_global.ts | 15 ++-- src/extra/memory_session_provider.ts | 81 +++++++++++--------- src/generics/generic_session_provider.ts | 38 --------- src/generics/index.ts | 1 - src/interfaces/component_prop.ts | 4 +- src/interfaces/controller.ts | 4 +- src/interfaces/index.ts | 1 + src/interfaces/session_store.ts | 11 +++ src/models/fort.ts | 12 +-- src/providers/cookie_wall.ts | 7 +- src/providers/index.ts | 3 +- src/providers/memory_session_store.ts | 66 ++++++++++++++++ src/test_helpers/init_controller.ts | 7 +- src/types/index.ts | 4 +- tests/general/controllers/home_controller.ts | 4 +- 17 files changed, 155 insertions(+), 143 deletions(-) delete mode 100644 src/abstracts/session_provider.ts delete mode 100644 src/generics/generic_session_provider.ts create mode 100644 src/interfaces/session_store.ts create mode 100644 src/providers/memory_session_store.ts diff --git a/src/abstracts/index.ts b/src/abstracts/index.ts index 6fe0eec7..1fbf613f 100644 --- a/src/abstracts/index.ts +++ b/src/abstracts/index.ts @@ -1,6 +1,5 @@ export * from './controller'; export * from './shield'; -export * from './session_provider'; export * from './guard'; export * from './view_engine'; export * from './wall'; diff --git a/src/abstracts/session_provider.ts b/src/abstracts/session_provider.ts deleted file mode 100644 index 37776b92..00000000 --- a/src/abstracts/session_provider.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { CookieManager } from "../models"; -import * as getUniqId from "uniqid"; -import { FORT_GLOBAL } from "../constants/fort_global"; - -export abstract class SessionProvider { - - sessionId: string; - protected cookie: CookieManager; - - abstract get(key: string): Promise; - abstract isExist(key: string): Promise; - abstract getAll(): Promise<{ [key: string]: any }>; - abstract set(key: string, val: any): Promise; - abstract setMany(values: { [key: string]: any }): Promise; - abstract remove(key: string): Promise; - - abstract clear(): Promise; - - protected createSession(sessionId?) { - const now = new Date(); - this.sessionId = sessionId != null ? sessionId : getUniqId(); - this.cookie.addCookie({ - name: FORT_GLOBAL.appSessionIdentifier, - value: this.sessionId, - httpOnly: true, - path: "/", - expires: new Date(now.setMinutes(now.getMinutes() + FORT_GLOBAL.sessionTimeOut)), - maxAge: FORT_GLOBAL.sessionTimeOut * 60 - }); - } - - protected destroySession() { - const cookie = this.cookie.getCookie(FORT_GLOBAL.appSessionIdentifier); - cookie.httpOnly = true; - cookie.path = "/"; - this.cookie.removeCookie(cookie); - } -} - diff --git a/src/constants/fort_global.ts b/src/constants/fort_global.ts index ff80f261..411027cd 100644 --- a/src/constants/fort_global.ts +++ b/src/constants/fort_global.ts @@ -1,13 +1,13 @@ import { ErrorHandler, Logger } from "../models"; -import { ViewEngine, XmlParser, ComponentOption, Shield } from "../abstracts"; -import { EtagOption, FolderMap } from "../types"; -import { GenericGuard, GenericSessionProvider, GenericShield, GenericWall, GenericXmlParser } from "../generics"; +import { ViewEngine, XmlParser, ComponentOption } from "../abstracts"; +import { EtagOption, FolderMap, TSessionStore } from "../types"; +import { GenericGuard, GenericShield, GenericWall, GenericXmlParser } from "../generics"; import { MustacheViewEngine, MemorySessionProvider, DtoValidator } from "../extra"; import { APP_NAME, CURRENT_PATH } from "./index"; import * as path from "path"; import { ETAG_TYPE } from "../enums"; import { IDtoValidator } from "../interfaces"; -import { CookieEvaluatorWall, PostDataEvaluatorGuard } from "../providers"; +import { CookieEvaluatorWall, MemorySessionStore, PostDataEvaluatorGuard } from "../providers"; const isDevelopment = process.env.NODE_ENV === 'development'; const isProduction = process.env.NODE_ENV === "production"; @@ -17,7 +17,8 @@ export class FortGlobal { viewPath; shouldParseCookie = true; shouldParseBody = true; - sessionProvider: typeof GenericSessionProvider; + sessionStore: TSessionStore; + sessionProvider = MemorySessionProvider; sessionTimeOut = 60; viewEngine: ViewEngine; walls: Array = []; @@ -58,9 +59,7 @@ export class FortGlobal { this.logger = this.logger || new Logger(); } - if (this.sessionProvider == null) { - this.sessionProvider = MemorySessionProvider as any; - } + this.sessionStore = this.sessionStore || MemorySessionStore; if (this.xmlParser == null) { this.xmlParser = GenericXmlParser; diff --git a/src/extra/memory_session_provider.ts b/src/extra/memory_session_provider.ts index 915d3cc2..ef5288c4 100644 --- a/src/extra/memory_session_provider.ts +++ b/src/extra/memory_session_provider.ts @@ -1,64 +1,75 @@ -import { SessionProvider } from "../abstracts/session_provider"; -import { promiseResolve } from "../utils"; +import { FORT_GLOBAL } from "../constants"; +import { ISessonStore } from "../interfaces"; +import { CookieManager } from "../models"; +import * as getUniqId from "uniqid"; +import { TSessionStore } from "../types"; -const sessionValues: Map = new Map(); -export class MemorySessionProvider extends SessionProvider { +export class MemorySessionProvider { - private getSessionValue_() { - return sessionValues.get(this.sessionId); + sessionId: string; + protected cookie: CookieManager; + sessionStore: ISessonStore; + + constructor(cookie: CookieManager, sessionStore: TSessionStore) { + this.sessionId = cookie.cookieCollection[FORT_GLOBAL.appSessionIdentifier]; + this.sessionStore = new sessionStore(this.sessionId); + this.cookie = cookie; + } + + protected createSession(sessionId?) { + const now = new Date(); + this.sessionId = sessionId != null ? sessionId : getUniqId(); + this.cookie.addCookie({ + name: FORT_GLOBAL.appSessionIdentifier, + value: this.sessionId, + httpOnly: true, + path: "/", + expires: new Date(now.setMinutes(now.getMinutes() + FORT_GLOBAL.sessionTimeOut)), + maxAge: FORT_GLOBAL.sessionTimeOut * 60 + }); + } + + protected destroySession() { + const cookie = this.cookie.getCookie(FORT_GLOBAL.appSessionIdentifier); + cookie.httpOnly = true; + cookie.path = "/"; + this.cookie.removeCookie(cookie); } get(key: string) { - const savedValue = this.getSessionValue_() - return promiseResolve(savedValue != null ? savedValue[key] : null); + return this.sessionStore.get(key); } isExist(key: string) { - const savedValue = this.getSessionValue_() - return promiseResolve(savedValue == null ? false : savedValue[key] != null); + return this.sessionStore.isExist(key); } getAll() { - const savedValue = this.getSessionValue_(); - return promiseResolve(savedValue || {}); + return this.sessionStore.getAll(); } - set(key: string, val: any) { - const savedValue = this.getSessionValue_(); - if (savedValue == null) { + async set(key: string, val: any) { + const savedValue = await this.sessionStore.isAnyExist(); + if (savedValue === false) { this.createSession(); - sessionValues.set(this.sessionId, { - [key]: val - }); - } - else { - savedValue[key] = val; + this.sessionStore.sessionId = this.sessionId; } - return promiseResolve(null); + await this.sessionStore.set(key, val); } setMany(values: { [key: string]: any }) { - return Promise.all( - Object.keys(values).map((key) => { - return this.set(key, values[key]); - }) - ); + return this.sessionStore.setMany(values); } remove(key: string) { - const savedValue = this.getSessionValue_(); - if (savedValue != null) { - savedValue[key] = null; - } - return promiseResolve(null); + return this.sessionStore.remove(key); } - clear() { + async clear() { // remove session values - sessionValues.delete(this.sessionId); + await this.sessionStore.clear(); // expire cookie in browser this.destroySession(); - return promiseResolve(null); } } \ No newline at end of file diff --git a/src/generics/generic_session_provider.ts b/src/generics/generic_session_provider.ts deleted file mode 100644 index 23733acd..00000000 --- a/src/generics/generic_session_provider.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { SessionProvider } from "../abstracts"; -import { SessionValue } from "../types"; -import { CookieManager } from "../models"; - - -export class GenericSessionProvider extends SessionProvider { - sessionId: string; - - cookie: CookieManager; - get() { - return null; - } - - getAll() { - return null; - } - - set(key, value) { - return null; - } - - isExist(key) { - return null; - } - - remove(key) { - return null; - } - - setMany(values: SessionValue[]): Promise { - return null; - } - - // eslint-disable-next-line - clear() { - return null; - } -} \ No newline at end of file diff --git a/src/generics/index.ts b/src/generics/index.ts index 359f374a..6fbcc03b 100644 --- a/src/generics/index.ts +++ b/src/generics/index.ts @@ -1,6 +1,5 @@ export * from './generic_guard'; export * from './generic_shield'; -export * from './generic_session_provider'; export * from "./generic_wall"; export * from "./generic_controller"; export * from './generic_xml_parser'; \ No newline at end of file diff --git a/src/interfaces/component_prop.ts b/src/interfaces/component_prop.ts index 9f45a44f..2a049bef 100644 --- a/src/interfaces/component_prop.ts +++ b/src/interfaces/component_prop.ts @@ -1,14 +1,14 @@ import * as http from "http"; -import { SessionProvider } from "../abstracts"; import { CookieManager, FileManager } from "../models"; import { FortGlobal } from "../constants"; +import { MemorySessionProvider } from "../extra"; export interface IComponentProp { request: http.IncomingMessage; response: http.ServerResponse; query: { [key: string]: any }; body?: { [key: string]: any }; - session: SessionProvider; + session: MemorySessionProvider; cookie: CookieManager; param?: { [key: string]: string }; data: { [key: string]: any }; diff --git a/src/interfaces/controller.ts b/src/interfaces/controller.ts index 5738e337..f587fa4a 100644 --- a/src/interfaces/controller.ts +++ b/src/interfaces/controller.ts @@ -1,13 +1,13 @@ import { HttpRequest, HttpResponse } from "../types"; import { CookieManager, FileManager } from "../models"; -import { SessionProvider } from "../abstracts"; +import { MemorySessionProvider } from "../extra"; export interface IController { request: HttpRequest; response: HttpResponse; query: { [key: string]: string }; body?: { [key: string]: any }; - session: SessionProvider; + session: MemorySessionProvider; cookie: CookieManager; param?: { [key: string]: string }; data: { [key: string]: any }; diff --git a/src/interfaces/index.ts b/src/interfaces/index.ts index 1379301d..0de302c0 100644 --- a/src/interfaces/index.ts +++ b/src/interfaces/index.ts @@ -6,4 +6,5 @@ export * from './worker_info'; export * from './component_prop'; export * from './validator'; export * from './http_result'; +export * from './session_store'; diff --git a/src/interfaces/session_store.ts b/src/interfaces/session_store.ts new file mode 100644 index 00000000..2e49c6e1 --- /dev/null +++ b/src/interfaces/session_store.ts @@ -0,0 +1,11 @@ +export interface ISessonStore { + sessionId: string; + get(key: string): Promise; + isAnyExist(): Promise; + isExist(key: string): Promise; + getAll(): Promise<{ [key: string]: any }>; + set(key: string, val: any): Promise; + setMany(values: { [key: string]: any }): Promise; + remove(key: string): Promise; + clear(): Promise; +} \ No newline at end of file diff --git a/src/models/fort.ts b/src/models/fort.ts index c0420348..b4a87dac 100644 --- a/src/models/fort.ts +++ b/src/models/fort.ts @@ -1,12 +1,12 @@ -import { ParentRoute, EtagOption, FolderMap } from "../types"; -import { Wall, ViewEngine, SessionProvider, XmlParser, ResultMapper, Shield, Guard } from "../abstracts"; +import { ParentRoute, EtagOption, FolderMap, TSessionStore } from "../types"; +import { Wall, ViewEngine, XmlParser, ResultMapper, Shield, Guard } from "../abstracts"; import { RouteHandler, RequestHandler } from "../handlers"; import { FORT_GLOBAL } from "../constants/fort_global"; import { ErrorHandler } from "."; import * as http from "http"; import { ERROR_TYPE } from "../enums"; import { LogHelper, promise, removeLastSlash, removeFirstSlash, setResultMapper } from "../helpers"; -import { GenericSessionProvider, GenericController } from "../generics"; +import { GenericController } from "../generics"; import { isArray } from "../utils"; import { Logger } from "./logger"; import { ComponentOption } from "../abstracts/component_option"; @@ -99,13 +99,13 @@ export class Fort { } /** - * sessionProvider class, default - MemorySessionProvider + * sessionStore class, default - MemorySessionStore * * @static * @memberof Fort */ - static set sessionProvider(value: typeof SessionProvider) { - FORT_GLOBAL.sessionProvider = value as typeof GenericSessionProvider; + static set sessionStore(value: TSessionStore) { + FORT_GLOBAL.sessionStore = value; } static set resultMapper(value: typeof ResultMapper) { diff --git a/src/providers/cookie_wall.ts b/src/providers/cookie_wall.ts index 06670f97..597d5d03 100644 --- a/src/providers/cookie_wall.ts +++ b/src/providers/cookie_wall.ts @@ -14,11 +14,10 @@ export class CookieEvaluatorWall extends Wall { const request = this.request; const rawCookie = (request.headers[COOKIE] || request.headers["cookie"]) as string; const parsedCookies = parseCookie(rawCookie); - const session = new FORT_GLOBAL.sessionProvider(); - session.cookie = new CookieManager(parsedCookies); - session.sessionId = parsedCookies[FORT_GLOBAL.appSessionIdentifier]; + const cookie = new CookieManager(parsedCookies); + const session = new FORT_GLOBAL.sessionProvider(cookie, FORT_GLOBAL.sessionStore); componentProps.session = session; - componentProps.cookie = session.cookie; + componentProps.cookie = cookie; } async onIncoming(): Promise { diff --git a/src/providers/index.ts b/src/providers/index.ts index 884749c3..ceb0b1e6 100644 --- a/src/providers/index.ts +++ b/src/providers/index.ts @@ -1,2 +1,3 @@ export * from "./cookie_wall"; -export * from "./post_data_evaluator_guard"; \ No newline at end of file +export * from "./post_data_evaluator_guard"; +export * from "./memory_session_store"; \ No newline at end of file diff --git a/src/providers/memory_session_store.ts b/src/providers/memory_session_store.ts new file mode 100644 index 00000000..e0b78414 --- /dev/null +++ b/src/providers/memory_session_store.ts @@ -0,0 +1,66 @@ +import { ISessonStore } from "../interfaces"; + +const sessionValues: Map = new Map(); + +export class MemorySessionStore implements ISessonStore { + sessionId: string; + + constructor(sessionId: string) { + this.sessionId = sessionId; + } + + private getSessionValue_() { + return sessionValues.get(this.sessionId); + } + + async isAnyExist() { + return this.getSessionValue_() != null; + } + + async get(key: string): Promise { + const savedValue = this.getSessionValue_(); + return savedValue != null ? savedValue[key] : null + } + + async getAll(): Promise<{ [key: string]: any; }> { + const savedValue = this.getSessionValue_(); + return savedValue || {}; + } + + async isExist(key: string): Promise { + const savedValue = this.getSessionValue_() + return savedValue == null ? false : savedValue[key] != null; + } + + async clear(): Promise { + // remove session values + sessionValues.delete(this.sessionId); + } + + async set(key: string, val: any) { + const savedValue = this.getSessionValue_(); + if (savedValue == null) { + sessionValues.set(this.sessionId, { + [key]: val + }); + } + else { + savedValue[key] = val; + } + } + + setMany(values: { [key: string]: any }) { + return Promise.all( + Object.keys(values).map((key) => { + return this.set(key, values[key]); + }) + ); + } + + async remove(key: string) { + const savedValue = this.getSessionValue_(); + if (savedValue != null) { + savedValue[key] = null; + } + } +} \ No newline at end of file diff --git a/src/test_helpers/init_controller.ts b/src/test_helpers/init_controller.ts index dc93c908..7466c44d 100644 --- a/src/test_helpers/init_controller.ts +++ b/src/test_helpers/init_controller.ts @@ -12,10 +12,11 @@ export const initController = (controllerInstance: Controller, data?: Controller data = data || {}; const parsedCookies = data.cookieValue || {}; const headers = (data.request && data.request.headers) || {}; - const session = new FORT_GLOBAL.sessionProvider(); const cookie = new CookieManager(parsedCookies); - session.cookie = cookie; - session.sessionId = parsedCookies[FORT_GLOBAL.appSessionIdentifier]; + const session = new FORT_GLOBAL.sessionProvider( + cookie, + FORT_GLOBAL.sessionStore + ); controllerInstance['componentProp_'] = { request: new HttpRequestStub(headers) as any, response: new HttpResponseStub(headers) as any, diff --git a/src/types/index.ts b/src/types/index.ts index 371569d6..1932d2c8 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,4 +1,4 @@ -import { IHttpResult } from '../interfaces'; +import { IHttpResult, ISessonStore } from '../interfaces'; export * from './view_engine_data'; export * from './http_request'; @@ -16,3 +16,5 @@ export * from './view_read_option'; export * from './http_format_result'; export type ErrorResultMapper = (error: any) => IHttpResult; +type Class = new (...args: Args) => I; +export type TSessionStore = Class; \ No newline at end of file diff --git a/tests/general/controllers/home_controller.ts b/tests/general/controllers/home_controller.ts index a7f58782..231b0dd2 100644 --- a/tests/general/controllers/home_controller.ts +++ b/tests/general/controllers/home_controller.ts @@ -29,8 +29,8 @@ export class HomeController extends Controller { const userService = new UserService(); const user = userService.getUserByEmail(emailId); if (user != null && (this.option as MyComponentOption).timingSafeEqual(user.password, pwd)) { - this.session.set('userId', user.id); - this.session.set('emailId', emailId); + await this.session.set('userId', user.id); + await this.session.set('emailId', emailId); return textResult(`Authenticated`); } else { From e5e4a25afe1dff3d4892edad50c12ea7febbfe05 Mon Sep 17 00:00:00 2001 From: Ujjwal Gupta Date: Sat, 25 Nov 2023 18:41:51 +0400 Subject: [PATCH 2/4] move session provider to utils --- src/constants/fort_global.ts | 5 +++-- src/extra/index.ts | 5 +---- src/interfaces/component_prop.ts | 4 ++-- src/interfaces/controller.ts | 4 ++-- src/utils/index.ts | 3 ++- .../memory_session_provider.ts => utils/session_provider.ts} | 3 +-- 6 files changed, 11 insertions(+), 13 deletions(-) rename src/{extra/memory_session_provider.ts => utils/session_provider.ts} (98%) diff --git a/src/constants/fort_global.ts b/src/constants/fort_global.ts index 411027cd..3b064523 100644 --- a/src/constants/fort_global.ts +++ b/src/constants/fort_global.ts @@ -2,10 +2,11 @@ import { ErrorHandler, Logger } from "../models"; import { ViewEngine, XmlParser, ComponentOption } from "../abstracts"; import { EtagOption, FolderMap, TSessionStore } from "../types"; import { GenericGuard, GenericShield, GenericWall, GenericXmlParser } from "../generics"; -import { MustacheViewEngine, MemorySessionProvider, DtoValidator } from "../extra"; +import { MustacheViewEngine, DtoValidator } from "../extra"; import { APP_NAME, CURRENT_PATH } from "./index"; import * as path from "path"; import { ETAG_TYPE } from "../enums"; +import { SessionProvider } from "../utils"; import { IDtoValidator } from "../interfaces"; import { CookieEvaluatorWall, MemorySessionStore, PostDataEvaluatorGuard } from "../providers"; @@ -18,7 +19,7 @@ export class FortGlobal { shouldParseCookie = true; shouldParseBody = true; sessionStore: TSessionStore; - sessionProvider = MemorySessionProvider; + sessionProvider = SessionProvider; sessionTimeOut = 60; viewEngine: ViewEngine; walls: Array = []; diff --git a/src/extra/index.ts b/src/extra/index.ts index 8eae3dc2..9a09141d 100644 --- a/src/extra/index.ts +++ b/src/extra/index.ts @@ -1,5 +1,2 @@ export * from './mustache_view_engine'; -export * from "./memory_session_provider"; -export * from "./dto_validator"; -// export * from "./expect_body_guard"; -// export * from "./expect_query_shield"; \ No newline at end of file +export * from "./dto_validator"; \ No newline at end of file diff --git a/src/interfaces/component_prop.ts b/src/interfaces/component_prop.ts index 2a049bef..5b232b6a 100644 --- a/src/interfaces/component_prop.ts +++ b/src/interfaces/component_prop.ts @@ -1,14 +1,14 @@ import * as http from "http"; import { CookieManager, FileManager } from "../models"; import { FortGlobal } from "../constants"; -import { MemorySessionProvider } from "../extra"; +import { SessionProvider } from "../utils"; export interface IComponentProp { request: http.IncomingMessage; response: http.ServerResponse; query: { [key: string]: any }; body?: { [key: string]: any }; - session: MemorySessionProvider; + session: SessionProvider; cookie: CookieManager; param?: { [key: string]: string }; data: { [key: string]: any }; diff --git a/src/interfaces/controller.ts b/src/interfaces/controller.ts index f587fa4a..57346443 100644 --- a/src/interfaces/controller.ts +++ b/src/interfaces/controller.ts @@ -1,13 +1,13 @@ import { HttpRequest, HttpResponse } from "../types"; import { CookieManager, FileManager } from "../models"; -import { MemorySessionProvider } from "../extra"; +import { SessionProvider } from "../utils"; export interface IController { request: HttpRequest; response: HttpResponse; query: { [key: string]: string }; body?: { [key: string]: any }; - session: MemorySessionProvider; + session: SessionProvider; cookie: CookieManager; param?: { [key: string]: string }; data: { [key: string]: any }; diff --git a/src/utils/index.ts b/src/utils/index.ts index 1d5234ec..2d0c7f7a 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -2,4 +2,5 @@ export * from './is_null_or_empty'; export * from './is_null'; export * from './is_array'; export * from './promise_resolve'; -export * from './compare_string'; \ No newline at end of file +export * from './compare_string'; +export * from './session_provider'; \ No newline at end of file diff --git a/src/extra/memory_session_provider.ts b/src/utils/session_provider.ts similarity index 98% rename from src/extra/memory_session_provider.ts rename to src/utils/session_provider.ts index ef5288c4..a7a65da2 100644 --- a/src/extra/memory_session_provider.ts +++ b/src/utils/session_provider.ts @@ -4,8 +4,7 @@ import { CookieManager } from "../models"; import * as getUniqId from "uniqid"; import { TSessionStore } from "../types"; - -export class MemorySessionProvider { +export class SessionProvider { sessionId: string; protected cookie: CookieManager; From c8e782ec1b7eaa68da64cf8ad06a607f5a6fa1e2 Mon Sep 17 00:00:00 2001 From: Ujjwal Gupta Date: Sat, 25 Nov 2023 18:44:14 +0400 Subject: [PATCH 3/4] rename sessionprovider to sessionManager --- src/constants/fort_global.ts | 4 ++-- src/interfaces/component_prop.ts | 4 ++-- src/interfaces/controller.ts | 4 ++-- src/utils/index.ts | 2 +- src/utils/{session_provider.ts => session_manager.ts} | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) rename src/utils/{session_provider.ts => session_manager.ts} (98%) diff --git a/src/constants/fort_global.ts b/src/constants/fort_global.ts index 3b064523..5f6b559f 100644 --- a/src/constants/fort_global.ts +++ b/src/constants/fort_global.ts @@ -6,7 +6,7 @@ import { MustacheViewEngine, DtoValidator } from "../extra"; import { APP_NAME, CURRENT_PATH } from "./index"; import * as path from "path"; import { ETAG_TYPE } from "../enums"; -import { SessionProvider } from "../utils"; +import { SessionManager } from "../utils"; import { IDtoValidator } from "../interfaces"; import { CookieEvaluatorWall, MemorySessionStore, PostDataEvaluatorGuard } from "../providers"; @@ -19,7 +19,7 @@ export class FortGlobal { shouldParseCookie = true; shouldParseBody = true; sessionStore: TSessionStore; - sessionProvider = SessionProvider; + sessionProvider = SessionManager; sessionTimeOut = 60; viewEngine: ViewEngine; walls: Array = []; diff --git a/src/interfaces/component_prop.ts b/src/interfaces/component_prop.ts index 5b232b6a..dbeaf813 100644 --- a/src/interfaces/component_prop.ts +++ b/src/interfaces/component_prop.ts @@ -1,14 +1,14 @@ import * as http from "http"; import { CookieManager, FileManager } from "../models"; import { FortGlobal } from "../constants"; -import { SessionProvider } from "../utils"; +import { SessionManager } from "../utils"; export interface IComponentProp { request: http.IncomingMessage; response: http.ServerResponse; query: { [key: string]: any }; body?: { [key: string]: any }; - session: SessionProvider; + session: SessionManager; cookie: CookieManager; param?: { [key: string]: string }; data: { [key: string]: any }; diff --git a/src/interfaces/controller.ts b/src/interfaces/controller.ts index 57346443..a8962c99 100644 --- a/src/interfaces/controller.ts +++ b/src/interfaces/controller.ts @@ -1,13 +1,13 @@ import { HttpRequest, HttpResponse } from "../types"; import { CookieManager, FileManager } from "../models"; -import { SessionProvider } from "../utils"; +import { SessionManager } from "../utils"; export interface IController { request: HttpRequest; response: HttpResponse; query: { [key: string]: string }; body?: { [key: string]: any }; - session: SessionProvider; + session: SessionManager; cookie: CookieManager; param?: { [key: string]: string }; data: { [key: string]: any }; diff --git a/src/utils/index.ts b/src/utils/index.ts index 2d0c7f7a..4f5d8762 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -3,4 +3,4 @@ export * from './is_null'; export * from './is_array'; export * from './promise_resolve'; export * from './compare_string'; -export * from './session_provider'; \ No newline at end of file +export * from './session_manager'; \ No newline at end of file diff --git a/src/utils/session_provider.ts b/src/utils/session_manager.ts similarity index 98% rename from src/utils/session_provider.ts rename to src/utils/session_manager.ts index a7a65da2..35dde00b 100644 --- a/src/utils/session_provider.ts +++ b/src/utils/session_manager.ts @@ -4,7 +4,7 @@ import { CookieManager } from "../models"; import * as getUniqId from "uniqid"; import { TSessionStore } from "../types"; -export class SessionProvider { +export class SessionManager { sessionId: string; protected cookie: CookieManager; From 546b09225e4c831db66680a04e42e6f2a35bdf4d Mon Sep 17 00:00:00 2001 From: Ujjwal Gupta Date: Sat, 25 Nov 2023 18:47:05 +0400 Subject: [PATCH 4/4] rm sessionProvider from fort_global and use it directly wherever its needed --- src/constants/fort_global.ts | 2 -- src/providers/cookie_wall.ts | 3 ++- src/test_helpers/init_controller.ts | 3 ++- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/constants/fort_global.ts b/src/constants/fort_global.ts index 5f6b559f..b24f016f 100644 --- a/src/constants/fort_global.ts +++ b/src/constants/fort_global.ts @@ -6,7 +6,6 @@ import { MustacheViewEngine, DtoValidator } from "../extra"; import { APP_NAME, CURRENT_PATH } from "./index"; import * as path from "path"; import { ETAG_TYPE } from "../enums"; -import { SessionManager } from "../utils"; import { IDtoValidator } from "../interfaces"; import { CookieEvaluatorWall, MemorySessionStore, PostDataEvaluatorGuard } from "../providers"; @@ -19,7 +18,6 @@ export class FortGlobal { shouldParseCookie = true; shouldParseBody = true; sessionStore: TSessionStore; - sessionProvider = SessionManager; sessionTimeOut = 60; viewEngine: ViewEngine; walls: Array = []; diff --git a/src/providers/cookie_wall.ts b/src/providers/cookie_wall.ts index 597d5d03..26e2178b 100644 --- a/src/providers/cookie_wall.ts +++ b/src/providers/cookie_wall.ts @@ -3,6 +3,7 @@ import { COOKIE, FORT_GLOBAL } from "../constants"; import { parseCookie } from "../helpers"; import { IHttpResult } from "../interfaces"; import { CookieManager } from "../models"; +import { SessionManager } from "../utils"; export class CookieEvaluatorWall extends Wall { parseCookieFromRequest() { @@ -15,7 +16,7 @@ export class CookieEvaluatorWall extends Wall { const rawCookie = (request.headers[COOKIE] || request.headers["cookie"]) as string; const parsedCookies = parseCookie(rawCookie); const cookie = new CookieManager(parsedCookies); - const session = new FORT_GLOBAL.sessionProvider(cookie, FORT_GLOBAL.sessionStore); + const session = new SessionManager(cookie, FORT_GLOBAL.sessionStore); componentProps.session = session; componentProps.cookie = cookie; } diff --git a/src/test_helpers/init_controller.ts b/src/test_helpers/init_controller.ts index 7466c44d..0294b58f 100644 --- a/src/test_helpers/init_controller.ts +++ b/src/test_helpers/init_controller.ts @@ -5,6 +5,7 @@ import { ControllerTestData } from "../types"; import { HttpResponseStub } from "./http_response_stub"; import { HttpRequestStub } from "./http_request_stub"; import { Controller } from "../abstracts"; +import { SessionManager } from "../utils"; @@ -13,7 +14,7 @@ export const initController = (controllerInstance: Controller, data?: Controller const parsedCookies = data.cookieValue || {}; const headers = (data.request && data.request.headers) || {}; const cookie = new CookieManager(parsedCookies); - const session = new FORT_GLOBAL.sessionProvider( + const session = new SessionManager( cookie, FORT_GLOBAL.sessionStore );