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

fix(connector): authEndpoint backward compatibility #410

Closed
wants to merge 12 commits into from
Closed
Prev Previous commit
Next Next commit
refactor(connectors/pusher): extract options code to util
  • Loading branch information
021-projects committed Jan 20, 2025
commit b5a65d9df1cd12018457920bb505183b75a063ca
70 changes: 3 additions & 67 deletions src/connector/pusher-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
PusherEncryptedPrivateChannel,
PusherPresenceChannel,
} from './../channel';
import { convertDeprecatedOptions, setAuthOptions } from '../util/pusher'

type AnyPusherChannel = PusherChannel | PusherPrivateChannel | PusherEncryptedPrivateChannel | PusherPresenceChannel;

Expand All @@ -24,76 +25,11 @@ export class PusherConnector extends Connector<PusherChannel, PusherPrivateChann

protected setOptions(options: any): any {
this.options = super.setOptions(options);
this.convertDeprecatedOptions();
this.setAuthOptions();
convertDeprecatedOptions(this.options);
setAuthOptions(this.options);
return this.options;
}

protected convertDeprecatedOptions(): any {
const options = this.options;

if (typeof options.authorizer !== 'undefined') {
// We cannot use the channelAuthorization.customHandler if the authorizer is set,
// sync the authorizer is build inside the Pusher (https://github.com/pusher/pusher-js/blob/37b057c45c403af27c79303123344c42f6da6a25/src/core/config.ts#L167)
// and require the Pusher instance to be passed to the authorizer.

// Convert all options to the deprecated format.
options.authEndpoint ||= options.channelAuthorization.endpoint;
options.authTransport ||= options.channelAuthorization.transport;
options.auth ||= {};
options.auth.headers ||= options.channelAuthorization.headers || {};
options.auth.params ||= options.channelAuthorization.params || {};

delete options.channelAuthorization;

return;
}

// Backward compatibility for the authEndpoint option.
// https://github.com/pusher/pusher-js/blob/37b057c45c403af27c79303123344c42f6da6a25/src/core/config.ts#L163
if (typeof options.authEndpoint !== 'undefined') {
options.channelAuthorization.endpoint = options.authEndpoint;
options.authEndpoint = null;
}

// Backward compatibility for the authTransport option.
// https://github.com/pusher/pusher-js/blob/37b057c45c403af27c79303123344c42f6da6a25/src/core/config.ts#L158
if (typeof options.authTransport !== 'undefined') {
options.channelAuthorization.transport = options.authTransport;
options.authTransport = null;
}

// Backward compatibility for the auth option.
// https://github.com/pusher/pusher-js/blob/37b057c45c403af27c79303123344c42f6da6a25/src/core/config.ts#L161
if (typeof options.auth !== 'undefined') {
options.channelAuthorization.headers = options.auth.headers || {};
options.channelAuthorization.params = options.auth.params || {};
options.auth = null;
}

return options;
}

protected setAuthOptions(): any {
const options = this.options;

const channelAuthKey = Object.prototype.hasOwnProperty.call(options, 'channelAuthorization')
? 'channelAuthorization'
: 'auth';

const csrfToken = this.csrfToken();
if (csrfToken) {
options[channelAuthKey].headers['X-CSRF-TOKEN'] = csrfToken;
options.userAuthentication.headers['X-CSRF-TOKEN'] = csrfToken;
}

const bearerToken = options.bearerToken;
if (bearerToken) {
options[channelAuthKey].headers['Authorization'] = `Bearer ${bearerToken}`;
options.userAuthentication.headers['Authorization'] = `Bearer ${bearerToken}`;
}
}

/**
* Create a fresh Pusher connection.
*/
Expand Down
60 changes: 60 additions & 0 deletions src/util/pusher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
export function convertDeprecatedOptions(options: Record<any, any>) {
if (typeof options.authorizer !== 'undefined') {
// We cannot use the channelAuthorization.customHandler if the authorizer is set,
// sync the authorizer is build inside the Pusher (https://github.com/pusher/pusher-js/blob/37b057c45c403af27c79303123344c42f6da6a25/src/core/config.ts#L167)
// and require the Pusher instance to be passed to the authorizer.

// Convert all options to the deprecated format.
options.authEndpoint ||= options.channelAuthorization.endpoint;
options.authTransport ||= options.channelAuthorization.transport;
options.auth ||= {};
options.auth.headers ||= options.channelAuthorization.headers || {};
options.auth.params ||= options.channelAuthorization.params || {};

delete options.channelAuthorization;

return;
}

// Backward compatibility for the authEndpoint option.
// https://github.com/pusher/pusher-js/blob/37b057c45c403af27c79303123344c42f6da6a25/src/core/config.ts#L163
if (typeof options.authEndpoint !== 'undefined') {
options.channelAuthorization.endpoint = options.authEndpoint;
options.authEndpoint = null;
}

// Backward compatibility for the authTransport option.
// https://github.com/pusher/pusher-js/blob/37b057c45c403af27c79303123344c42f6da6a25/src/core/config.ts#L158
if (typeof options.authTransport !== 'undefined') {
options.channelAuthorization.transport = options.authTransport;
options.authTransport = null;
}

// Backward compatibility for the auth option.
// https://github.com/pusher/pusher-js/blob/37b057c45c403af27c79303123344c42f6da6a25/src/core/config.ts#L161
if (typeof options.auth !== 'undefined') {
options.channelAuthorization.headers = options.auth.headers || {};
options.channelAuthorization.params = options.auth.params || {};
options.auth = null;
}

return options;
}

export function setAuthOptions(options: Record<any, any>) {
const channelAuthKey = Object.prototype.hasOwnProperty.call(options, 'channelAuthorization')
? 'channelAuthorization'
: 'auth';

const csrfToken = this.csrfToken();
if (csrfToken) {
options[channelAuthKey].headers['X-CSRF-TOKEN'] = csrfToken;
options.userAuthentication.headers['X-CSRF-TOKEN'] = csrfToken;
}

const bearerToken = options.bearerToken;
if (bearerToken) {
options[channelAuthKey].headers['Authorization'] = `Bearer ${bearerToken}`;
options.userAuthentication.headers['Authorization'] = `Bearer ${bearerToken}`;
}
}