All files / src/operation load-account-for-request.ts

60% Statements 24/40
37.5% Branches 3/8
71.43% Functions 5/7
60% Lines 24/40

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      18x 18x 18x 18x 18x   18x 18x 18x               28x   28x       28x   28x       28x       28x 28x   28x 28x                           28x                                                                                     28x   28x         28x       28x   28x       18x  
import HTTP from 'http';
 
import Node from 'type/node';
import SystemId from 'system/enum/id';
import HttpHeader from 'http/enum/header';
import parseCookie from 'http/utility/parse-cookie';
import getHeaderValue from 'http/utility/get-header-value';
import CookieAttribute from 'http/enum/cookie-attribute';
import BasicAuthCredentials from 'http/type/basic-auth-credentials';
import FetchAccountOperation from 'operation/fetch-account';
import LoadNodeFromUrlOperation from 'operation/load-node-from-url';
import Operation, { OperationInput } from 'operation';
 
interface Input extends OperationInput {
	readonly request: HTTP.IncomingMessage;
}
 
class LoadAccountForRequestOperation extends Operation<Input, Node> {
	protected async performInternal(): Promise<Node> {
		const session_id = this.getSessionId();
 
		Iif (session_id !== undefined) {
			return this.loadFromSessionId(session_id);
		}
 
		const basic_auth_credentials = this.getBasicAuthCredentials();
 
		Iif (basic_auth_credentials !== undefined) {
			return this.loadFromBasicAuthCredentials(basic_auth_credentials);
		}
 
		return this.loadAnonymousAccount();
	}
 
	private getSessionId(): string | undefined {
		const request = this.getRequest();
		const header = getHeaderValue(request, HttpHeader.COOKIE);
 
		Eif (header === undefined) {
			return undefined;
		}
 
		const parsed_cookie = parseCookie(header);
 
		return parsed_cookie[CookieAttribute.SESSION] as string | undefined;
	}
 
	private getBasicAuthCredentials(): BasicAuthCredentials | undefined {
		/*
		const request = this.getRequest();
		const header = getHeaderValue(request, HttpHeader.AUTHORIZATION);
		*/
 
		return undefined;
	}
 
	private async loadFromSessionId(session_id: string): Promise<Node> {
		const repository = this.getRepository();
 
		const session = await repository.fetchNode(
			SystemId.SESSION_TYPE,
			session_id
		);
 
		if (session === undefined) {
			return this.loadAnonymousAccount();
		}
 
		const url = session.account as string;
		const account = this.getAccount();
 
		const operation = new LoadNodeFromUrlOperation({
			repository,
			account,
			url
		});
 
		return operation.perform();
	}
 
	private loadFromBasicAuthCredentials(
		credentials: BasicAuthCredentials
	): Promise<Node> {
		const repository = this.getRepository();
		const account = this.getAccount();
 
		const operation = new FetchAccountOperation({
			repository,
			account,
			credentials
		});
 
		return operation.perform();
	}
 
	private async loadAnonymousAccount(): Promise<Node> {
		const repository = this.getRepository();
 
		const node = await repository.fetchNode(
			SystemId.ACCOUNT_TYPE,
			SystemId.ANONYMOUS_ACCOUNT
		);
 
		return node as Node;
	}
 
	private getRequest(): HTTP.IncomingMessage {
		const input = this.getInput();
 
		return input.request;
	}
}
 
export default LoadAccountForRequestOperation;