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

[main] add withCredentials config #2373

Merged
merged 13 commits into from
Jul 12, 2024
16 changes: 16 additions & 0 deletions channels/1ds-post-js/src/DataModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,22 @@ export interface IChannelConfiguration {
* @since 4.1.0
*/
excludeCsMetaData?: boolean;

/**
siyuniu-ms marked this conversation as resolved.
Show resolved Hide resolved
* [Optional] Specify whether cross-site Access-Control fetch requests should include credentials such as cookies,
* authentication headers, or TLS client certificates.
*
* Possible values:
* - "omit": never send credentials in the request or include credentials in the response.
* - "include": always include credentials, even cross-origin.
* - "same-origin": only send and include credentials for same-origin requests.
*
* If not set, the default value will be "include".
*
* For more information, refer to:
* - [Fetch API - Using Fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#including_credentials)
*/
fetchCredentials?: RequestCredentials;
}

/**
Expand Down
6 changes: 5 additions & 1 deletion channels/1ds-post-js/src/HttpManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export class HttpManager {
let _timeoutWrapper: ITimeoutOverrideWrapper;
let _excludeCsMetaData: boolean;
let _sendPostMgr: SenderPostManager;
let _fetchCredentials: RequestCredentials;

dynamicProto(HttpManager, this, (_self) => {
_initDefaults();
Expand Down Expand Up @@ -238,7 +239,9 @@ export class HttpManager {
if (!isNullOrUndefined(channelConfig.useSendBeacon)) {
_useBeacons = !!channelConfig.useSendBeacon;
}

if (channelConfig.fetchCredentials){
_fetchCredentials= channelConfig.fetchCredentials;
}
let sendPostConfig = _getSendPostMgrConfig();
// only init it once
if (!_sendPostMgr) {
Expand Down Expand Up @@ -426,6 +429,7 @@ export class HttpManager {
enableSendPromise: false,
isOneDs: true,
disableCredentials: !_sendCredentials,
fetchCredentials: _fetchCredentials,
disableXhr: false,
disableBeacon: !_useBeacons,
disableBeaconSync: !_useBeacons,
Expand Down
10 changes: 8 additions & 2 deletions channels/offline-channel-js/src/Sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class Sender {
let _isOneDs: boolean;
let _sendPostMgr: SenderPostManager;
let _disableCredentials: boolean;
let _fetchCredentials: RequestCredentials;


dynamicProto(Sender, this, (_self, _base) => {
Expand Down Expand Up @@ -83,14 +84,18 @@ export class Sender {
utlSetStoragePrefix(config.storagePrefix);
}
let ctx = createProcessTelemetryContext(null, config, core);

let offlineCfg = ctx.getExtCfg(DefaultOfflineIdentifier) as IOfflineChannelConfiguration;
_onlineChannelId = channelId || BreezeChannelIdentifier;
let senderConfig = ctx.getExtCfg(_onlineChannelId, {}) as any;
let offlineSenderCfg = offlineCfg.senderCfg || {} as IOfflineSenderConfig;

_fetchCredentials = null;
if (_onlineChannelId == PostChannelId) {
_isOneDs = true;
let channelConfig = ctx.getExtCfg(PostChannelId);
if (channelConfig && channelConfig["fetchCredentials"]) {
_fetchCredentials = channelConfig["fetchCredentials"];
}
}

_alwaysUseCustomSend = offlineSenderCfg.alwaysUseXhrOverride;
Expand Down Expand Up @@ -163,6 +168,7 @@ export class Sender {
enableSendPromise: _enableSendPromise,
isOneDs: _isOneDs,
disableCredentials: _disableCredentials,
fetchCredentials: _fetchCredentials,
senderOnCompleteCallBack: _getOnCompleteFuncs()
} as _ISendPostMgrConfig;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,22 @@ export interface _ISendPostMgrConfig {
*/
addNoResponse?: boolean;

/**
* [Optional] Specify whether cross-site Access-Control fetch requests should include credentials such as cookies,
* authentication headers, or TLS client certificates.
*
* Possible values:
* - "omit": never send credentials in the request or include credentials in the response.
* - "include": always include credentials, even cross-origin.
* - "same-origin": only send and include credentials for same-origin requests.
*
* If not set, the default value will be "include".
*
* For more information, refer to:
* - [Fetch API - Using Fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#including_credentials)
*/
fetchCredentials?: RequestCredentials;

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class SenderPostManager {
let _isOneDs: boolean;
let _onCompleteFuncs: _ISenderOnComplete;
let _disableCredentials: boolean;
let _fetchCredentials: RequestCredentials;
let _fallbackInst: IXHROverride;
let _disableXhr: boolean;
let _disableBeacon: boolean;
Expand Down Expand Up @@ -83,6 +84,7 @@ export class SenderPostManager {
try {
_onCompleteFuncs = config.senderOnCompleteCallBack || {};
_disableCredentials = !!config.disableCredentials;
_fetchCredentials = config.fetchCredentials;
_isOneDs = !!config.isOneDs;
_enableSendPromise = !!config.enableSendPromise;
_disableXhr = !! config.disableXhr;
Expand Down Expand Up @@ -379,7 +381,9 @@ export class SenderPostManager {
}


if (_sendCredentials && _isOneDs) {
if (_fetchCredentials) { // if user passed in this value via post channel (1ds), then use it
init.credentials = _fetchCredentials;
} else if (_sendCredentials && _isOneDs) {
// for 1ds, Don't send credentials when URL is file://
init.credentials = "include";
siyuniu-ms marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down Expand Up @@ -607,6 +611,7 @@ export class SenderPostManager {
_isOneDs = null;
_onCompleteFuncs = null;
_disableCredentials = null;
_fetchCredentials = null;
_fallbackInst = null;
_disableXhr = false;
_disableBeacon = false;
Expand Down
Loading