Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-protocol/src/util/gitpod-cookie.ts
2500 views
1
/**
2
* Copyright (c) 2021 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
import * as cookie from "cookie";
7
8
/**
9
* This cookie indicates whether the connected client is a Gitpod user (= "has logged in within the last year") or not.
10
* This is used by "gitpod.io" and "www.gitpod.io" to display different content/buttons.
11
*/
12
export const NAME = "gitpod-user";
13
export const VALUE = "true";
14
15
/**
16
* @param domain The domain the Gitpod installation is installed onto
17
* @returns
18
*/
19
export function options(domain: string): cookie.CookieSerializeOptions {
20
// Reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies
21
return {
22
path: "/", // make sure we send the cookie to all sub-pages
23
httpOnly: false,
24
secure: false,
25
maxAge: 60 * 60 * 24 * 365, // 1 year
26
sameSite: "lax", // default: true. "Lax" needed to ensure we see cookies from users that navigate to gitpod.io from external sites
27
domain: `.${domain}`, // explicitly include subdomains to not only cover "gitpod.io", but also "www.gitpod.io" or workspaces
28
};
29
}
30
31
export function generateCookie(domain: string): string {
32
return cookie.serialize(NAME, VALUE, options(domain));
33
}
34
35
export function isPresent(cookies: string): boolean {
36
// needs to match the old (gitpod-user=loggedIn) and new (gitpod-user=true) values to ensure a smooth transition during rollout.
37
return !!cookies.match(`${NAME}=`);
38
}
39
40