Path: blob/main/components/gitpod-protocol/src/util/gitpod-cookie.ts
2500 views
/**1* Copyright (c) 2021 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/5import * as cookie from "cookie";67/**8* This cookie indicates whether the connected client is a Gitpod user (= "has logged in within the last year") or not.9* This is used by "gitpod.io" and "www.gitpod.io" to display different content/buttons.10*/11export const NAME = "gitpod-user";12export const VALUE = "true";1314/**15* @param domain The domain the Gitpod installation is installed onto16* @returns17*/18export function options(domain: string): cookie.CookieSerializeOptions {19// Reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies20return {21path: "/", // make sure we send the cookie to all sub-pages22httpOnly: false,23secure: false,24maxAge: 60 * 60 * 24 * 365, // 1 year25sameSite: "lax", // default: true. "Lax" needed to ensure we see cookies from users that navigate to gitpod.io from external sites26domain: `.${domain}`, // explicitly include subdomains to not only cover "gitpod.io", but also "www.gitpod.io" or workspaces27};28}2930export function generateCookie(domain: string): string {31return cookie.serialize(NAME, VALUE, options(domain));32}3334export function isPresent(cookies: string): boolean {35// needs to match the old (gitpod-user=loggedIn) and new (gitpod-user=true) values to ensure a smooth transition during rollout.36return !!cookies.match(`${NAME}=`);37}383940