-
Notifications
You must be signed in to change notification settings - Fork 11
feat: add getCookieOptions utility function #184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dtoxvanilla1991
wants to merge
9
commits into
kinde-oss:main
Choose a base branch
from
dtoxvanilla1991:feat/getCookiesOptions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9524dd0
feat: add getCookieOptions utility and related types; update exports
dtoxvanilla1991 0c1c8c0
refactor: update fetch mock implementation and response handling in e…
dtoxvanilla1991 13a637a
Merge remote-tracking branch 'origin/main' into feat/getCookiesOptions
dtoxvanilla1991 9d615c1
test: update fetch mock to use mockResolvedValueOnce for async response
dtoxvanilla1991 8420809
test: refactor fetch mock implementation for exchangeAuthCode tests
dtoxvanilla1991 5fbb6be
refactor: simplify cookie options types and remove unused code
dtoxvanilla1991 557652d
style: format getCookieOptions function for improved readability
dtoxvanilla1991 2875e93
docs: enhance documentation for GLOBAL_COOKIE_OPTIONS and getCookieOp…
dtoxvanilla1991 70e9189
refactor: update cookie options to use storageSettings for maxCookieL…
dtoxvanilla1991 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
|
|
||
| import { | ||
| getCookieOptions, | ||
| TWENTY_NINE_DAYS, | ||
| GLOBAL_COOKIE_OPTIONS, | ||
| } from "./getCookieOptions"; | ||
| import { storageSettings } from "../sessionManager/index"; | ||
|
|
||
| describe("getCookieOptions", () => { | ||
| it("returns the default configuration when no options provided", () => { | ||
| const result = getCookieOptions(); | ||
|
|
||
| expect(result).toMatchObject({ | ||
| maxAge: TWENTY_NINE_DAYS, | ||
| maxCookieLength: storageSettings.maxLength, | ||
| sameSite: "lax", | ||
| httpOnly: true, | ||
| path: "/", | ||
| }); | ||
| }); | ||
|
|
||
| it("allows consumers to override default options", () => { | ||
| const result = getCookieOptions({ | ||
| secure: true, | ||
| sameSite: "none", | ||
| path: "/custom", | ||
| maxAge: 60, | ||
| domain: "example.com", | ||
| }); | ||
|
|
||
| expect(result.secure).toBe(true); | ||
| expect(result.sameSite).toBe("none"); | ||
| expect(result.path).toBe("/custom"); | ||
| expect(result.maxAge).toBe(60); | ||
| expect(result.domain).toBe("example.com"); | ||
| }); | ||
|
|
||
| it("preserves GLOBAL_COOKIE_OPTIONS when not overridden", () => { | ||
| const result = getCookieOptions({ domain: "test.com" }); | ||
|
|
||
| expect(result.httpOnly).toBe(GLOBAL_COOKIE_OPTIONS.httpOnly); | ||
| expect(result.sameSite).toBe(GLOBAL_COOKIE_OPTIONS.sameSite); | ||
| expect(result.path).toBe(GLOBAL_COOKIE_OPTIONS.path); | ||
| expect(result.maxAge).toBe(GLOBAL_COOKIE_OPTIONS.maxAge); | ||
| expect(result.maxCookieLength).toBe(GLOBAL_COOKIE_OPTIONS.maxCookieLength); | ||
| expect(result.domain).toBe("test.com"); | ||
| }); | ||
|
|
||
| it("user options take precedence over defaults", () => { | ||
| const result = getCookieOptions({ | ||
| httpOnly: false, | ||
| maxAge: 1000, | ||
| }); | ||
|
|
||
| expect(result.httpOnly).toBe(false); | ||
| expect(result.maxAge).toBe(1000); | ||
| // Other defaults remain | ||
| expect(result.sameSite).toBe("lax"); | ||
| expect(result.path).toBe("/"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("GLOBAL_COOKIE_OPTIONS", () => { | ||
| it("contains secure defaults", () => { | ||
| expect(GLOBAL_COOKIE_OPTIONS.httpOnly).toBe(true); | ||
| expect(GLOBAL_COOKIE_OPTIONS.sameSite).toBe("lax"); | ||
| expect(GLOBAL_COOKIE_OPTIONS.path).toBe("/"); | ||
| expect(GLOBAL_COOKIE_OPTIONS.maxAge).toBe(TWENTY_NINE_DAYS); | ||
| expect(GLOBAL_COOKIE_OPTIONS.maxCookieLength).toBe( | ||
| storageSettings.maxLength, | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import { storageSettings } from "../sessionManager/index.js"; | ||
|
|
||
| export interface CookieOptions { | ||
| maxAge?: number; | ||
| domain?: string; | ||
| maxCookieLength?: number; | ||
| sameSite?: string; | ||
| httpOnly?: boolean; | ||
| secure?: boolean; | ||
| path?: string; | ||
| } | ||
|
|
||
| export const TWENTY_NINE_DAYS = 2505600; | ||
|
|
||
| /** | ||
| * Default cookie options used across Kinde SDKs. | ||
| * | ||
| * **Security Note:** The `secure` flag is intentionally omitted to support: | ||
| * - Framework-agnostic usage across different environments | ||
| * - Local development over HTTP (localhost) | ||
| * | ||
| * Warning: For production deployments using HTTPS, consumers must explicitly | ||
| * set `secure: true` via `getCookieOptions({ secure: true })` to ensure | ||
| * cookies are only transmitted over secure connections. | ||
| */ | ||
| export const GLOBAL_COOKIE_OPTIONS: CookieOptions = { | ||
| maxAge: TWENTY_NINE_DAYS, | ||
| maxCookieLength: storageSettings.maxLength, | ||
| sameSite: "lax", | ||
| httpOnly: true, | ||
| path: "/", | ||
| }; | ||
|
|
||
| /** | ||
| * Returns cookie options by merging provided options with secure defaults. | ||
| * | ||
| * @param options - Custom cookie options to override defaults | ||
| * @returns Merged cookie options with GLOBAL_COOKIE_OPTIONS as base | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * // Development (HTTP) | ||
| * const devOptions = getCookieOptions(); | ||
| * | ||
| * // Production (HTTPS) - must set secure: true | ||
| * const prodOptions = getCookieOptions({ secure: true, domain: ".example.com" }); | ||
| * ``` | ||
| * | ||
| * **Security Warning:** Always set `secure: true` in production environments | ||
| * using HTTPS to prevent cookie transmission over insecure connections. | ||
| */ | ||
| export const getCookieOptions = ( | ||
| options: CookieOptions = {}, | ||
| ): CookieOptions => { | ||
| return { | ||
| ...GLOBAL_COOKIE_OPTIONS, | ||
| ...options, | ||
| }; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.