36 lines
812 B
TypeScript
36 lines
812 B
TypeScript
|
import { run_regex } from '@sapphirecode/utilities';
|
||
|
import { debug } from './debug';
|
||
|
|
||
|
const logger = debug ('cookies');
|
||
|
|
||
|
function build_cookie (name: string, value: string): string {
|
||
|
return `${name}=${value}; Secure; HttpOnly; SameSite=Strict`;
|
||
|
}
|
||
|
|
||
|
function extract_cookie (
|
||
|
name: string|undefined,
|
||
|
header: string|undefined
|
||
|
): string| null {
|
||
|
logger (`extracting cookie ${name}`);
|
||
|
|
||
|
const cookie_regex = /(?:^|;)\s*(?<name>[^;=]+)=(?<value>[^;]+)/gu;
|
||
|
|
||
|
let result = null;
|
||
|
|
||
|
run_regex (
|
||
|
cookie_regex,
|
||
|
header,
|
||
|
(res: RegExpMatchArray) => {
|
||
|
logger ('parsing cookie %s', res.groups?.name);
|
||
|
if (res.groups?.name === name) {
|
||
|
logger ('found cookie');
|
||
|
result = res.groups?.value as string;
|
||
|
}
|
||
|
}
|
||
|
);
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
export { build_cookie, extract_cookie };
|