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

[ECO-4995] Fix base64 url encoding/decoding #43

Merged
merged 2 commits into from
Sep 25, 2024
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
35 changes: 27 additions & 8 deletions src/channel/ably/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
// Get Token Header
const base64HeaderUrl = jwtToken.split('.')[0];
const base64Header = base64HeaderUrl.replace('-', '+').replace('_', '/');
const header = JSON.parse(toText(base64Header));
const header = JSON.parse(fromBase64UrlEncoded(base64Header));
// Get Token payload
const base64Url = jwtToken.split('.')[1];
const base64 = base64Url.replace('-', '+').replace('_', '/');
const payload = JSON.parse(toText(base64));
const payload = JSON.parse(fromBase64UrlEncoded(base64));
return { header, payload };
};

Expand All @@ -33,18 +33,37 @@

const isBrowser = typeof window === 'object';

export const toText = (base64: string) => {
/**
* Helper method to decode base64 url encoded string
* https://stackoverflow.com/a/78178053
* @param base64 base64 url encoded string
* @returns decoded text string
*/
export const fromBase64UrlEncoded = (base64: string) => {
const base64Encoded = base64.replace(/-/g, '+').replace(/_/g, '/');
const padding = base64.length % 4 === 0 ? '' : '='.repeat(4 - (base64.length % 4));
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not sure if we really need to add padding as per
https://gist.github.com/tatsuyasusukida/ce71456081748242a0bd4cbfcfe44eb7

Copy link

Choose a reason for hiding this comment

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

I believe padding it's not necessary, message will be decoded without padding, but I think it's better to keep it, since padding is a part of base64 spec

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

thanks, this is helpful

const base64WithPadding = base64Encoded + padding;

if (isBrowser) {
return atob(base64);
return atob(base64WithPadding);
}
return Buffer.from(base64, 'base64').toString('binary');
return Buffer.from(base64WithPadding, 'base64').toString('binary');
};

export const toBase64 = (text: string) => {
/**
* Helper method to encode text into base64 url encoded string
* https://stackoverflow.com/a/78178053
* @param base64 text
* @returns base64 url encoded string
*/
export const toBase64UrlEncoded = (text: string) => {
let encoded = ''
if (isBrowser) {
return btoa(text);
encoded = btoa(text);
} else {
encoded = Buffer.from(text, 'binary').toString('base64');
}
return Buffer.from(text, 'binary').toString('base64');
return encoded.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
Dismissed Show dismissed Hide dismissed
};

const isAbsoluteUrl = (url: string) => (url && url.indexOf('http://') === 0) || url.indexOf('https://') === 0;
Expand Down
6 changes: 3 additions & 3 deletions src/connector/ably-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Connector } from './connector';

import { AblyChannel, AblyPrivateChannel, AblyPresenceChannel, AblyAuth } from './../channel';
import { AblyRealtime, TokenDetails } from '../../typings/ably';
import { toBase64 } from '../channel/ably/utils';
import { toBase64UrlEncoded } from '../channel/ably/utils';

/**
* This class creates a connector to Ably.
Expand Down Expand Up @@ -119,14 +119,14 @@ export class AblyConnector extends Connector {

/**
* Get the socket ID for the connection.
* For ably, returns base64 encoded json with keys {connectionKey, clientId}
* For ably, returns base64 url encoded json with keys {connectionKey, clientId}
*/
socketId(): string {
let socketIdObject = {
connectionKey : this.ably.connection.key,
clientId : this.ably.auth.clientId ?? null,
}
return toBase64(JSON.stringify(socketIdObject));
return toBase64UrlEncoded(JSON.stringify(socketIdObject));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/echo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export default class Echo {

/**
* Get the Socket ID for the connection.
* For ably, returns base64 encoded json with keys {connectionKey, clientId}
* For ably, returns base64 url encoded json with keys {connectionKey, clientId}
*/
socketId(): string {
return this.connector.socketId();
Expand Down
Loading