Skip to content
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

Save consent method ("accept", "reject", "save", etc.) to fides_consent cookie as extra metadata #4529

Merged
merged 17 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ The types of changes are:

### Changed
- Upgrade to use Fideslang `3.0.0` and remove associated concepts [#4502](https://github.com/ethyca/fides/pull/4502)

### Changed
- `fides.js` now sets `supportsOOB` to `false` [#4516](https://github.com/ethyca/fides/pull/4516)
- Save consent method ("accept", "reject", "save", etc.) to `fides_consent` cookie as extra metadata [#4529](https://github.com/ethyca/fides/pull/4529)

## [2.26.0](https://github.com/ethyca/fides/compare/2.25.0...main)

Expand Down
2 changes: 1 addition & 1 deletion clients/admin-ui/src/types/api/models/ConsentMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* An enumeration.
*/
export enum ConsentMethod {
BUTTON = "button",
BUTTON = "button", // deprecated- keeping for backwards-compatibility
NevilleS marked this conversation as resolved.
Show resolved Hide resolved
REJECT = "reject",
ACCEPT = "accept",
SAVE = "save",
Expand Down
54 changes: 49 additions & 5 deletions clients/fides-js/__tests__/lib/cookie.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as uuid from "uuid";
import { CookieAttributes } from "typescript-cookie/dist/types";
import {
CookieKeyConsent,
CookieMeta,
FidesCookie,
getOrMakeFidesCookie,
isNewFidesCookie,
Expand Down Expand Up @@ -96,6 +97,7 @@ describe("getOrMakeFidesCookie", () => {
it("makes and returns a default cookie", () => {
const cookie: FidesCookie = getOrMakeFidesCookie();
expect(cookie.consent).toEqual({});
expect(cookie.fides_meta.consentMethod).toEqual(undefined);
expect(cookie.fides_meta.createdAt).toEqual(MOCK_DATE);
expect(cookie.fides_meta.updatedAt).toEqual("");
expect(cookie.identity.fides_user_device_id).toEqual(MOCK_UUID);
Expand All @@ -109,23 +111,47 @@ describe("getOrMakeFidesCookie", () => {
const SAVED_CONSENT = { data_sales: false, performance: true };

describe("in v0.9.0 format", () => {
const V090_COOKIE = JSON.stringify({
const V090_COOKIE_OBJECT: FidesCookie = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the type here to ensure we're keeping this test accurate - and then updated it to include the required tcf_consent field 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be a good idea to make this change to other places in this test file that still use const V090_COOKIE = JSON.stringify({...?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion! Updated

consent: SAVED_CONSENT,
identity: { fides_user_device_id: SAVED_UUID },
fides_meta: {
createdAt: CREATED_DATE,
updatedAt: UPDATED_DATE,
version: "0.9.0",
},
});
beforeEach(() => mockGetCookie.mockReturnValue(V090_COOKIE));
tcf_consent: {},
};

it("returns the saved cookie", () => {
mockGetCookie.mockReturnValue(JSON.stringify(V090_COOKIE_OBJECT));
const cookie: FidesCookie = getOrMakeFidesCookie();
expect(cookie.consent).toEqual(SAVED_CONSENT);
expect(cookie.fides_meta.consentMethod).toEqual(undefined);
expect(cookie.fides_meta.createdAt).toEqual(CREATED_DATE);
expect(cookie.fides_meta.updatedAt).toEqual(UPDATED_DATE);
expect(cookie.identity.fides_user_device_id).toEqual(SAVED_UUID);
expect(cookie.tcf_consent).toEqual({});
});

it("returns the saved cookie including optional fides_meta details like consentMethod", () => {
// extend the cookie object with some extra details on fides_meta
const extendedFidesMeta: CookieMeta = {
...V090_COOKIE_OBJECT.fides_meta,
...{ consentMethod: "accept", otherMetadata: "foo" },
NevilleS marked this conversation as resolved.
Show resolved Hide resolved
};
const cookieObject = {
...V090_COOKIE_OBJECT,
...{ fides_meta: extendedFidesMeta },
};
mockGetCookie.mockReturnValue(JSON.stringify(cookieObject));
const cookie: FidesCookie = getOrMakeFidesCookie();
expect(cookie.consent).toEqual(SAVED_CONSENT);
expect(cookie.fides_meta.consentMethod).toEqual("accept");
expect(cookie.fides_meta.otherMetadata).toEqual("foo");
expect(cookie.fides_meta.createdAt).toEqual(CREATED_DATE);
expect(cookie.fides_meta.updatedAt).toEqual(UPDATED_DATE);
expect(cookie.identity.fides_user_device_id).toEqual(SAVED_UUID);
expect(cookie.tcf_consent).toEqual({});
});
});

Expand All @@ -137,8 +163,10 @@ describe("getOrMakeFidesCookie", () => {
it("returns the saved cookie and converts to new 0.9.0 format", () => {
const cookie: FidesCookie = getOrMakeFidesCookie();
expect(cookie.consent).toEqual(SAVED_CONSENT);
expect(cookie.fides_meta.consentMethod).toEqual(undefined);
expect(cookie.fides_meta.createdAt).toEqual(MOCK_DATE);
expect(cookie.identity.fides_user_device_id).toEqual(MOCK_UUID);
expect(cookie.tcf_consent).toEqual({});
});
});
});
Expand All @@ -154,6 +182,18 @@ describe("saveFidesCookie", () => {
expect(cookie.fides_meta.updatedAt).toEqual(MOCK_DATE);
});

it("saves optional fides_meta details like consentMethod", () => {
const cookie: FidesCookie = getOrMakeFidesCookie();
cookie.fides_meta.consentMethod = "dismiss";
saveFidesCookie(cookie);
expect(mockSetCookie.mock.calls).toHaveLength(1);
expect(mockSetCookie.mock.calls[0][0]).toEqual("fides_consent"); // name
const cookieValue = mockSetCookie.mock.calls[0][1];
const cookieParsed = JSON.parse(cookieValue);
expect(cookieParsed.fides_meta).toHaveProperty("consentMethod");
expect(cookieParsed.fides_meta.consentMethod).toEqual("dismiss");
});

it("sets a cookie on the root domain with 1 year expiry date", () => {
const cookie: FidesCookie = getOrMakeFidesCookie();
saveFidesCookie(cookie);
Expand Down Expand Up @@ -274,19 +314,23 @@ describe("isNewFidesCookie", () => {
});

describe("when a saved cookie exists", () => {
const CONSENT_METHOD = "accept";
const CREATED_DATE = "2022-12-24T12:00:00.000Z";
const UPDATED_DATE = "2022-12-25T12:00:00.000Z";
const SAVED_UUID = "8a46c3ee-d6c3-4518-9b6c-074528b7bfd0";
const SAVED_CONSENT = { data_sales: false, performance: true };
const V090_COOKIE = JSON.stringify({
const V090_COOKIE_OBJECT: FidesCookie = {
consent: SAVED_CONSENT,
identity: { fides_user_device_id: SAVED_UUID },
fides_meta: {
consentMethod: CONSENT_METHOD,
createdAt: CREATED_DATE,
updatedAt: UPDATED_DATE,
version: "0.9.0",
},
});
tcf_consent: {},
};
const V090_COOKIE = JSON.stringify(V090_COOKIE_OBJECT);
beforeEach(() => mockGetCookie.mockReturnValue(V090_COOKIE));

it("returns false for saved cookies", () => {
Expand Down
6 changes: 3 additions & 3 deletions clients/fides-js/src/components/ConsentButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,22 @@ export const NoticeConsentButtons = ({

const handleAcceptAll = () => {
onSave(
ConsentMethod.accept,
ConsentMethod.ACCEPT,
NevilleS marked this conversation as resolved.
Show resolved Hide resolved
notices.map((n) => n.notice_key)
);
};

const handleRejectAll = () => {
onSave(
ConsentMethod.reject,
ConsentMethod.REJECT,
notices
.filter((n) => n.consent_mechanism === ConsentMechanism.NOTICE_ONLY)
.map((n) => n.notice_key)
);
};

const handleSave = () => {
onSave(ConsentMethod.save, enabledKeys);
onSave(ConsentMethod.SAVE, enabledKeys);
};

if (isAcknowledge) {
Expand Down
2 changes: 1 addition & 1 deletion clients/fides-js/src/components/notices/NoticeOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ const NoticeOverlay: FunctionComponent<OverlayProps> = ({
}, [cookie, options.debug]);

const handleDismiss = useCallback(() => {
handleUpdatePreferences(ConsentMethod.dismiss, initialEnabledNoticeKeys);
handleUpdatePreferences(ConsentMethod.DISMISS, initialEnabledNoticeKeys);
}, [handleUpdatePreferences, initialEnabledNoticeKeys]);

if (!experience.experience_config) {
Expand Down
4 changes: 2 additions & 2 deletions clients/fides-js/src/components/tcf/TcfConsentButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const TcfConsentButtons = ({
...(experience.tcf_system_legitimate_interests || []),
]),
};
onSave(ConsentMethod.accept, allIds);
onSave(ConsentMethod.ACCEPT, allIds);
};
const handleRejectAll = () => {
const emptyIds: EnabledIds = {
Expand All @@ -60,7 +60,7 @@ export const TcfConsentButtons = ({
vendorsConsent: [],
vendorsLegint: [],
};
onSave(ConsentMethod.reject, emptyIds);
onSave(ConsentMethod.REJECT, emptyIds);
};

return (
Expand Down
4 changes: 2 additions & 2 deletions clients/fides-js/src/components/tcf/TcfOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ const TcfOverlay: FunctionComponent<OverlayProps> = ({
}, [cookie, options.debug]);

const handleDismiss = useCallback(() => {
handleUpdateAllPreferences(ConsentMethod.dismiss, initialEnabledIds);
handleUpdateAllPreferences(ConsentMethod.DISMISS, initialEnabledIds);
}, [handleUpdateAllPreferences, initialEnabledIds]);

if (!experience.experience_config) {
Expand Down Expand Up @@ -415,7 +415,7 @@ const TcfOverlay: FunctionComponent<OverlayProps> = ({
<Button
buttonType={ButtonType.SECONDARY}
label={experience.experience_config?.save_button_label}
onClick={() => onSave(ConsentMethod.save, draftIds)}
onClick={() => onSave(ConsentMethod.SAVE, draftIds)}
className="fides-save-button"
/>
}
Expand Down
14 changes: 7 additions & 7 deletions clients/fides-js/src/lib/consent-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,13 +420,13 @@ export enum ButtonType {
}

export enum ConsentMethod {
button = "button", // deprecated- keeping for backwards-compatibility
reject = "reject",
accept = "accept",
save = "save",
dismiss = "dismiss",
gpc = "gpc",
individual_notice = "api",
BUTTON = "button", // deprecated- keeping for backwards-compatibility
REJECT = "reject",
ACCEPT = "accept",
SAVE = "save",
DISMISS = "dismiss",
GPC = "gpc",
INDIVIDUAL_NOTICE = "individual_notice",
NevilleS marked this conversation as resolved.
Show resolved Hide resolved
NevilleS marked this conversation as resolved.
Show resolved Hide resolved
}

export type PrivacyPreferencesRequest = {
Expand Down
11 changes: 9 additions & 2 deletions clients/fides-js/src/lib/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,21 @@ declare global {
}
}

/**
* Defines the type of "extra" details that can be optionally added to certain
* events. This is intentionally vague, but constrained to be basic (primitive)
* values for simplicity.
*/
export type FidesEventExtraDetails = Record<string, string | number | boolean>;

/**
* Defines the properties available on event.detail. Currently the FidesCookie
* and an extra field `meta` for any other details that the event wants to pass
* around.
*/
export type FidesEventDetail = FidesCookie & {
debug?: boolean;
extraDetails?: Record<string, string | boolean>;
extraDetails?: FidesEventExtraDetails;
NevilleS marked this conversation as resolved.
Show resolved Hide resolved
};

export type FidesEvent = CustomEvent<FidesEventDetail>;
Expand All @@ -59,7 +66,7 @@ export const dispatchFidesEvent = (
type: FidesEventType,
cookie: FidesCookie,
debug: boolean,
extraDetails?: Record<string, string | boolean>
extraDetails?: FidesEventExtraDetails
) => {
if (typeof window !== "undefined" && typeof CustomEvent !== "undefined") {
const event = new CustomEvent(type, {
Expand Down
2 changes: 1 addition & 1 deletion clients/fides-js/src/lib/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const automaticallyApplyGPCPreferences = ({
updateConsentPreferences({
consentPreferencesToSave,
experience: effectiveExperience,
consentMethod: ConsentMethod.gpc,
consentMethod: ConsentMethod.GPC,
options: fidesOptions,
userLocationString: fidesRegionString || undefined,
cookie,
Expand Down
10 changes: 7 additions & 3 deletions clients/fides-js/src/lib/preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
removeCookiesFromBrowser,
saveFidesCookie,
} from "./cookie";
import { dispatchFidesEvent } from "./events";
import { dispatchFidesEvent, FidesEventExtraDetails } from "./events";
import { patchUserPreference } from "../services/api";
import { TcfSavePreferences } from "./tcf/types";

Expand Down Expand Up @@ -83,9 +83,13 @@ export const updateConsentPreferences = async ({
tcf?: TcfSavePreferences;
updateCookie: (oldCookie: FidesCookie) => Promise<FidesCookie>;
}) => {
// 1. Update the cookie object based on new preferences
// Collect any "extra" details that should be recorded on the cookie & event
const extraDetails: FidesEventExtraDetails = { consentMethod };

// 1. Update the cookie object based on new preferences & extra details
const updatedCookie = await updateCookie(cookie);
Object.assign(cookie, updatedCookie);
Object.assign(cookie.fides_meta, extraDetails); // save extra details to meta (i.e. consentMethod)
NevilleS marked this conversation as resolved.
Show resolved Hide resolved

// 2. Update the window.Fides object
debugLog(options.debug, "Updating window.Fides");
Expand Down Expand Up @@ -131,5 +135,5 @@ export const updateConsentPreferences = async ({
}

// 6. Dispatch a "FidesUpdated" event
dispatchFidesEvent("FidesUpdated", cookie, options.debug, { consentMethod });
dispatchFidesEvent("FidesUpdated", cookie, options.debug, extraDetails);
};
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ const NoticeDrivenConsent = () => {
preferences,
user_geography: region,
privacy_experience_id: experience?.id,
method: ConsentMethod.BUTTON,
method: ConsentMethod.SAVE,
NevilleS marked this conversation as resolved.
Show resolved Hide resolved
code: verificationCode,
};

Expand Down Expand Up @@ -232,6 +232,7 @@ const NoticeDrivenConsent = () => {
const consentCookieKey: CookieKeyConsent = Object.fromEntries(noticeKeyMap);
window.Fides.consent = consentCookieKey;
const updatedCookie = { ...cookie, consent: consentCookieKey };
updatedCookie.fides_meta.consentMethod = ConsentMethod.SAVE; // include the consentMethod as extra metadata
NevilleS marked this conversation as resolved.
Show resolved Hide resolved
saveFidesCookie(updatedCookie);
toast({
title: "Your consent preferences have been saved",
Expand Down
Loading