2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-04-27 19:07:11 -04:00
|
|
|
// Structured similarly to Go's cookie.go
|
|
|
|
// https://github.com/golang/go/blob/master/src/net/http/cookie.go
|
|
|
|
import { ServerRequest, Response } from "./server.ts";
|
2020-06-07 09:20:33 -04:00
|
|
|
import { assert } from "../_util/assert.ts";
|
2019-04-27 19:07:11 -04:00
|
|
|
import { toIMF } from "../datetime/mod.ts";
|
2019-04-24 07:38:52 -04:00
|
|
|
|
2019-04-27 19:07:11 -04:00
|
|
|
export interface Cookies {
|
2019-04-24 07:38:52 -04:00
|
|
|
[key: string]: string;
|
|
|
|
}
|
|
|
|
|
2019-04-27 19:07:11 -04:00
|
|
|
export interface Cookie {
|
2020-04-28 17:26:31 -04:00
|
|
|
/** Name of the cookie. */
|
2019-04-27 19:07:11 -04:00
|
|
|
name: string;
|
2020-04-28 17:26:31 -04:00
|
|
|
/** Value of the cookie. */
|
2019-04-27 19:07:11 -04:00
|
|
|
value: string;
|
2020-04-28 17:26:31 -04:00
|
|
|
/** Expiration date of the cookie. */
|
2019-04-27 19:07:11 -04:00
|
|
|
expires?: Date;
|
2020-04-28 17:26:31 -04:00
|
|
|
/** Max-Age of the Cookie. Must be integer superior to 0. */
|
2019-04-27 19:07:11 -04:00
|
|
|
maxAge?: number;
|
2020-04-28 17:26:31 -04:00
|
|
|
/** Specifies those hosts to which the cookie will be sent. */
|
2019-04-27 19:07:11 -04:00
|
|
|
domain?: string;
|
2020-04-28 17:26:31 -04:00
|
|
|
/** Indicates a URL path that must exist in the request. */
|
2019-04-27 19:07:11 -04:00
|
|
|
path?: string;
|
2020-04-28 17:26:31 -04:00
|
|
|
/** Indicates if the cookie is made using SSL & HTTPS. */
|
2019-04-27 19:07:11 -04:00
|
|
|
secure?: boolean;
|
2020-05-17 13:24:39 -04:00
|
|
|
/** Indicates that cookie is not accessible via JavaScript. **/
|
2019-04-27 19:07:11 -04:00
|
|
|
httpOnly?: boolean;
|
2020-04-28 17:26:31 -04:00
|
|
|
/** Allows servers to assert that a cookie ought not to
|
|
|
|
* be sent along with cross-site requests. */
|
2019-04-27 19:07:11 -04:00
|
|
|
sameSite?: SameSite;
|
2020-04-28 17:26:31 -04:00
|
|
|
/** Additional key value pairs with the form "key=value" */
|
2019-04-27 19:07:11 -04:00
|
|
|
unparsed?: string[];
|
|
|
|
}
|
|
|
|
|
2020-04-10 10:12:42 -04:00
|
|
|
export type SameSite = "Strict" | "Lax" | "None";
|
2019-04-27 19:07:11 -04:00
|
|
|
|
|
|
|
function toString(cookie: Cookie): string {
|
2020-04-10 10:12:42 -04:00
|
|
|
if (!cookie.name) {
|
|
|
|
return "";
|
|
|
|
}
|
2019-04-27 19:07:11 -04:00
|
|
|
const out: string[] = [];
|
|
|
|
out.push(`${cookie.name}=${cookie.value}`);
|
|
|
|
|
|
|
|
// Fallback for invalid Set-Cookie
|
|
|
|
// ref: https://tools.ietf.org/html/draft-ietf-httpbis-cookie-prefixes-00#section-3.1
|
|
|
|
if (cookie.name.startsWith("__Secure")) {
|
|
|
|
cookie.secure = true;
|
|
|
|
}
|
|
|
|
if (cookie.name.startsWith("__Host")) {
|
|
|
|
cookie.path = "/";
|
|
|
|
cookie.secure = true;
|
|
|
|
delete cookie.domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cookie.secure) {
|
|
|
|
out.push("Secure");
|
|
|
|
}
|
|
|
|
if (cookie.httpOnly) {
|
|
|
|
out.push("HttpOnly");
|
|
|
|
}
|
2020-02-19 15:36:18 -05:00
|
|
|
if (typeof cookie.maxAge === "number" && Number.isInteger(cookie.maxAge)) {
|
2020-02-07 02:23:38 -05:00
|
|
|
assert(cookie.maxAge > 0, "Max-Age must be an integer superior to 0");
|
2019-04-27 19:07:11 -04:00
|
|
|
out.push(`Max-Age=${cookie.maxAge}`);
|
|
|
|
}
|
|
|
|
if (cookie.domain) {
|
|
|
|
out.push(`Domain=${cookie.domain}`);
|
|
|
|
}
|
|
|
|
if (cookie.sameSite) {
|
|
|
|
out.push(`SameSite=${cookie.sameSite}`);
|
|
|
|
}
|
|
|
|
if (cookie.path) {
|
|
|
|
out.push(`Path=${cookie.path}`);
|
|
|
|
}
|
|
|
|
if (cookie.expires) {
|
2019-10-05 12:02:34 -04:00
|
|
|
const dateString = toIMF(cookie.expires);
|
2019-04-27 19:07:11 -04:00
|
|
|
out.push(`Expires=${dateString}`);
|
|
|
|
}
|
|
|
|
if (cookie.unparsed) {
|
|
|
|
out.push(cookie.unparsed.join("; "));
|
|
|
|
}
|
|
|
|
return out.join("; ");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the cookies of the Server Request
|
|
|
|
* @param req Server Request
|
|
|
|
*/
|
|
|
|
export function getCookies(req: ServerRequest): Cookies {
|
2020-02-07 02:23:38 -05:00
|
|
|
const cookie = req.headers.get("Cookie");
|
|
|
|
if (cookie != null) {
|
2019-04-27 19:07:11 -04:00
|
|
|
const out: Cookies = {};
|
2020-02-07 02:23:38 -05:00
|
|
|
const c = cookie.split(";");
|
2019-04-24 07:38:52 -04:00
|
|
|
for (const kv of c) {
|
2020-02-07 02:23:38 -05:00
|
|
|
const [cookieKey, ...cookieVal] = kv.split("=");
|
|
|
|
assert(cookieKey != null);
|
|
|
|
const key = cookieKey.trim();
|
2019-04-29 10:49:50 -04:00
|
|
|
out[key] = cookieVal.join("=");
|
2019-04-24 07:38:52 -04:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
2019-04-27 19:07:11 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the cookie header properly in the Response
|
|
|
|
* @param res Server Response
|
|
|
|
* @param cookie Cookie to set
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* setCookie(response, { name: 'deno', value: 'runtime',
|
|
|
|
* httpOnly: true, secure: true, maxAge: 2, domain: "deno.land" });
|
|
|
|
*/
|
|
|
|
export function setCookie(res: Response, cookie: Cookie): void {
|
|
|
|
if (!res.headers) {
|
|
|
|
res.headers = new Headers();
|
|
|
|
}
|
|
|
|
// TODO (zekth) : Add proper parsing of Set-Cookie headers
|
|
|
|
// Parsing cookie headers to make consistent set-cookie header
|
|
|
|
// ref: https://tools.ietf.org/html/rfc6265#section-4.1.1
|
2020-04-10 10:12:42 -04:00
|
|
|
const v = toString(cookie);
|
|
|
|
if (v) {
|
|
|
|
res.headers.append("Set-Cookie", v);
|
|
|
|
}
|
2019-04-27 19:07:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the cookie header properly in the Response to delete it
|
|
|
|
* @param res Server Response
|
|
|
|
* @param name Name of the cookie to Delete
|
|
|
|
* Example:
|
|
|
|
*
|
2020-06-07 07:53:36 -04:00
|
|
|
* deleteCookie(res,'foo');
|
2019-04-27 19:07:11 -04:00
|
|
|
*/
|
2020-06-07 07:53:36 -04:00
|
|
|
export function deleteCookie(res: Response, name: string): void {
|
2019-04-27 19:07:11 -04:00
|
|
|
setCookie(res, {
|
|
|
|
name: name,
|
|
|
|
value: "",
|
2020-03-28 13:03:49 -04:00
|
|
|
expires: new Date(0),
|
2019-04-27 19:07:11 -04:00
|
|
|
});
|
|
|
|
}
|