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

Adds document attribute for disable tcpcheck #4008

Merged
merged 7 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 2 additions & 6 deletions client/luigi-client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,14 +594,10 @@ export declare interface StorageManager {
/**
* Registers a listener called with the context object and the Luigi Core domain as soon as Luigi is instantiated. Defer your application bootstrap if you depend on authentication data coming from Luigi.
* @param {Lifecycle~initListenerCallback} initFn the function that is called once Luigi is initialized, receives current context and origin as parameters
* @param {boolean} disableTpcCheck if set to `true` third party cookie check will be disabled via LuigiClient.
* @memberof Lifecycle
*/
export function addInitListener(initFn: (context: Context, origin?: string) => void, disableTpcCheck?: boolean): number;
export type addInitListener = (
initFn: (context: Context, origin?: string) => void,
disableTpcCheck?: boolean
Copy link
Contributor

Choose a reason for hiding this comment

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

please keep the disableTcpCheck argument, it should still be possible to disable via js, e.g. in a test environment.

) => number;
export function addInitListener(initFn: (context: Context, origin?: string) => void): number;
export type addInitListener = (initFn: (context: Context, origin?: string) => void) => number;

/**
* Callback of the addInitListener
Expand Down
42 changes: 26 additions & 16 deletions client/src/lifecycleManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ class LifecycleManager extends LuigiClientBase {
/** @private */
constructor() {
super();
this.disableTpcCheck = false;
this.luigiInitialized = false;
this.defaultContextKeys = ['context', 'internal', 'nodeParams', 'pathParams', 'searchParams'];
this.setCurrentContext(
this.defaultContextKeys.reduce(function(acc, key) {
this.defaultContextKeys.reduce(function (acc, key) {
acc[key] = {};
return acc;
}, {})
Expand All @@ -38,6 +37,15 @@ class LifecycleManager extends LuigiClientBase {
return window.document.head.hasAttribute('defer-luigi-init');
}

/**
* Check if the html head element contains the attribute "disable-tpc-check"
* @private
* @memberof Lifecycle
*/
_isTpcCheckDisabled() {
return window.document.head.hasAttribute('disable-tpc-check');
}

/**
* Check if LuigiClient is initialized
* @returns {boolean} client initialized state
Expand Down Expand Up @@ -66,7 +74,7 @@ class LifecycleManager extends LuigiClientBase {
* Save context data every time navigation to a different node happens
* @private
*/
const setContext = rawData => {
const setContext = (rawData) => {
for (let index = 0; index < this.defaultContextKeys.length; index++) {
let key = this.defaultContextKeys[index];
try {
Expand All @@ -80,13 +88,13 @@ class LifecycleManager extends LuigiClientBase {
this.setCurrentContext(rawData);
};

const setAuthData = eventPayload => {
const setAuthData = (eventPayload) => {
if (eventPayload) {
this.authData = eventPayload;
}
};

helpers.addEventListener('luigi.init', e => {
helpers.addEventListener('luigi.init', (e) => {
setContext(e.data);
setAuthData(e.data.authData);
helpers.setLuigiCoreDomain(e.origin);
Expand All @@ -96,15 +104,15 @@ class LifecycleManager extends LuigiClientBase {
helpers.sendPostMessageToLuigiCore({ msg: 'luigi.init.ok' });
});

helpers.addEventListener('luigi-client.inactive-microfrontend', e => {
helpers.addEventListener('luigi-client.inactive-microfrontend', (e) => {
this._notifyInactive(e.origin);
});

helpers.addEventListener('luigi.auth.tokenIssued', e => {
helpers.addEventListener('luigi.auth.tokenIssued', (e) => {
setAuthData(e.data.authData);
});

helpers.addEventListener('luigi.navigate', e => {
helpers.addEventListener('luigi.navigate', (e) => {
setContext(e.data);
if (!this.currentContext.internal.isNavigateBack && !this.currentContext.withoutSync) {
const previousHash = window.location.hash;
Expand Down Expand Up @@ -140,17 +148,21 @@ class LifecycleManager extends LuigiClientBase {
}

_tpcCheck() {
if (this.currentContext?.internal?.thirdPartyCookieCheck?.disabled || this.disableTpcCheck) {
const tpcCheckDisabled =
this._isTpcCheckDisabled() || this.currentContext?.internal?.thirdPartyCookieCheck?.disabled;
Copy link
Contributor

Choose a reason for hiding this comment

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

moving the check
this.currentContext?.internal?.thirdPartyCookieCheck?.disabled;
into _isTpcCheckDisabled() could save one LOC ;)


if (tpcCheckDisabled) {
return;
}

let tpc = 'enabled';
let cookies = document.cookie;
let luigiCookie;
if (cookies) {
luigiCookie = cookies
.split(';')
.map(cookie => cookie.trim())
.find(cookie => cookie === 'luigiCookie=true');
.map((cookie) => cookie.trim())
.find((cookie) => cookie === 'luigiCookie=true');
}
if (luigiCookie === 'luigiCookie=true') {
document.cookie = 'luigiCookie=; Max-Age=-99999999; SameSite=None; Secure';
Expand All @@ -160,8 +172,8 @@ class LifecycleManager extends LuigiClientBase {
if (cookies) {
luigiCookie = cookies
.split(';')
.map(cookie => cookie.trim())
.find(cookie => cookie === 'luigiCookie=true');
.map((cookie) => cookie.trim())
.find((cookie) => cookie === 'luigiCookie=true');
}
if (luigiCookie === 'luigiCookie=true') {
document.cookie = 'luigiCookie=; Max-Age=-99999999; SameSite=None; Secure';
Expand Down Expand Up @@ -225,13 +237,11 @@ class LifecycleManager extends LuigiClientBase {
/**
* Registers a listener called with the context object and the Luigi Core domain as soon as Luigi is instantiated. Defer your application bootstrap if you depend on authentication data coming from Luigi.
* @param {Lifecycle~initListenerCallback} initFn the function that is called once Luigi is initialized, receives current context and origin as parameters
* @param {boolean} disableTpcCheck if set to `true` third party cookie check will be disabled via LuigiClient.
* @memberof Lifecycle
* @example
* const initListenerId = LuigiClient.addInitListener((context) => storeContextToMF(context))
*/
addInitListener(initFn, disableTpcCheck) {
this.disableTpcCheck = disableTpcCheck;
addInitListener(initFn) {
Copy link
Contributor

Choose a reason for hiding this comment

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

please keep the disableTcpCheck argument, it should still be possible to disable via js, e.g. in a test environment.
if it is true, just call document.head.setAttribute('disable-tpc-check') here instead of this.disableTpcCheck=true

const id = helpers.getRandomId();
this._onInitFns[id] = initFn;
if (this.luigiInitialized && helpers.isFunction(initFn)) {
Expand Down
4 changes: 2 additions & 2 deletions client/src/luigi-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class LuigiClient {
}
}

addInitListener(initFn, disableTpcCheck) {
return lifecycleManager.addInitListener(initFn, disableTpcCheck);
addInitListener(initFn) {
Copy link
Contributor

Choose a reason for hiding this comment

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

see above

return lifecycleManager.addInitListener(initFn);
}
removeInitListener(id) {
return lifecycleManager.removeInitListener(id);
Expand Down
12 changes: 12 additions & 0 deletions docs/authorization-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ When Luigi fails to renew the token and then logs the user out, it adds the `?re

Use these parameters to set a logout page.

<!-- add-attribute:class:success -->
>**TIP:** There's an option to disable third party cookie check declaratively - in your micro frontend HTML that serves as entry file, you must add the `disable-tpc-check` attribute into the `<head>` element as follows:

```html
<html>
<head disable-tpc-check>
....
</head>
.....
</html>
```

## OAuth2 Implicit Grant configuration

This code snippet demonstrates how to configure authorization using OAuth2 Implicit Grant in Luigi. Note that you must install the [OAuth2 Plugin](auth-oauth2.md) first.
Expand Down
1 change: 0 additions & 1 deletion docs/luigi-client-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ Registers a listener called with the context object and the Luigi Core domain as
##### Parameters

* `initFn` **[Lifecycle~initListenerCallback](#lifecycleinitlistenercallback)** the function that is called once Luigi is initialized, receives current context and origin as parameters
* `disableTpcCheck` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** if set to `true` third party cookie check will be disabled via LuigiClient.

##### Examples

Expand Down
1 change: 0 additions & 1 deletion docs/luigi-compound-container-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ meta -->
This document outlines the parameters provided by the Luigi Compound Container. Luigi Compound Container provides the possibility to insert multiple webcomponent-based microfrontends in one container.<br/>
In addition you can use standard `addEventListener` function to react on events emmitted by the Luigi Compound Container. The list of events and their meaning can be found [here](https://github.com/SAP/luigi/blob/main/container/src/constants/communication.ts).


## API Reference

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
Expand Down