Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 2x 2x 2x 1x 1x 2x 1x 2x 11x 11x 1x 10x 10x 10x 10x 10x 10x 1x 1x 1x 9x 9x 9x 1x 22x 2x 2x 2x 20x 1x 33x 3x 3x 30x 1x 11x 1x 1x 10x 10x 10x 10x 10x 9x 9x 9x 1x 1x 11x 1x 1x 1x 10x 1x 11x 1x 1x 10x 1x | import { CtxtHandle } from '../../lib/api'; import http from 'http'; import { parseCookies } from './cookies'; import dbg from 'debug'; import { SSOMethod } from './SSO'; import { CookieToken } from './interfaces'; const debug = dbg('node-expose-sspi:schManager'); type IPromiseFn = (value?: unknown) => void; interface AuthItem { resolve: IPromiseFn; reject: IPromiseFn; timeout?: NodeJS.Timeout; } interface ContextInfo { serverContextHandle: CtxtHandle; method: SSOMethod; } const COOKIE_KEY = 'NEGOTIATE_ID'; const COOKIE_PREFIX_VALUE = 'NEGOTIATE_'; export class ServerContextHandleManager { private serverContextHandle: CtxtHandle; private queue: AuthItem[] = []; private authItem: AuthItem; private sessionMap = new Map<string, ContextInfo>(); private method: SSOMethod; constructor(private IdelayMax = 20000) {} initCookie(req: http.IncomingMessage, res: http.ServerResponse): CookieToken { debug('initCookie'); let cookieToken = parseCookies(req)[COOKIE_KEY]; if (!cookieToken) { cookieToken = COOKIE_PREFIX_VALUE + Math.floor(1e10 * Math.random()); // create a session cookie (without expiration specified) res.setHeader('Set-Cookie', COOKIE_KEY + '=' + cookieToken); } if (!this.sessionMap.has(cookieToken)) { this.sessionMap.set(cookieToken, { method: undefined, serverContextHandle: undefined, } as ContextInfo); } return cookieToken; } waitForReleased(cookieToken: CookieToken): Promise<void> { if (cookieToken) { return Promise.resolve(); } debug('waitForReleased: start'); return new Promise((resolve, reject) => { debug('waitForReleased: start promise'); // if nobody else is currently authenticating then go now. const authItem = { resolve, reject }; const timeout = setTimeout(() => { this.tooLate(authItem); }, this.delayMax); if (this.authItem === undefined) { debug( 'waitForReleased: no other authentication ongoing: we can start now.' ); this.authItem = { resolve, reject, timeout }; return this.authItem.resolve(); } debug( 'someone is currently authenticating, go in the queue and wait for your turn.' ); this.queue.push({ resolve, reject, timeout }); debug('queue length', this.queue.length); }); } set(serverContextHandle: CtxtHandle, cookieToken: CookieToken): void { if (cookieToken) { const contextInfo = this.sessionMap.get(cookieToken); contextInfo.serverContextHandle = serverContextHandle; return; } this.serverContextHandle = serverContextHandle; } getServerContextHandle(cookieToken: CookieToken): CtxtHandle { if (cookieToken) { const contextInfo = this.sessionMap.get(cookieToken); return contextInfo.serverContextHandle; } return this.serverContextHandle; } release(cookieToken?: CookieToken): void { if (cookieToken) { this.sessionMap.delete(cookieToken); return; } Eif (this.authItem) { clearTimeout(this.authItem.timeout); } this.serverContextHandle = undefined; this.authItem = undefined; if (this.queue.length > 0) { // it means another client B was waiting for authenticating. // so we start authenticating this client B. this.authItem = this.queue.shift(); debug('releasing. queue length', this.queue.length); this.authItem.resolve(); } } /** * Used only when a negotiate connection * does not go to its final state before timeout. * * Note: Do not interfer with cookies. * * @param {AuthItem} authItem * @returns * @memberof ServerContextHandleManager */ tooLate(authItem: AuthItem): void { while (this.queue.length > 0) { const ai = this.queue.shift(); clearTimeout(ai.timeout); this.authItem.reject(); } this.authItem = authItem; this.authItem.resolve(); } setMethod(method: SSOMethod, cookieToken: CookieToken): void { if (cookieToken) { const contextInfo = this.sessionMap.get(cookieToken); contextInfo.method = method; return; } this.method = method; } getMethod(cookieToken: CookieToken): SSOMethod { if (cookieToken) { const contextInfo = this.sessionMap.get(cookieToken); return contextInfo.method; } return this.method; } } |