Skip to content
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"dependencies": {
"@babel/runtime": "^7.3.1",
"axios": "^0.18.1",
"base64-js": "^1.3.1",
"chai-arrays": "^2.0.0",
"cross-fetch": "^3.0.0",
"form-data": "^2.3.3",
Expand Down
57 changes: 57 additions & 0 deletions src/base64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const base64 = require('base64-js');

// source - https://github.com/beatgammit/base64-js/blob/master/test/convert.js#L72
const map = (arr, callback) => {
const res = [];
let kValue, mappedValue;

for (let k = 0, len = arr.length; k < len; k++) {
if (typeof arr === 'string' && !!arr.charAt(k)) {
kValue = arr.charAt(k);
mappedValue = callback(kValue, k, arr);
res[k] = mappedValue;
} else if (typeof arr !== 'string' && k in arr) {
kValue = arr[k];
mappedValue = callback(kValue, k, arr);
res[k] = mappedValue;
}
}
return res;
};

export function encodeBase64(data) {
return base64.fromByteArray(
map(data, function(char) {
return char.charCodeAt(0);
}),
);
}

// base-64 decoder throws exception if encoded string is not padded by '=' to make string length
// in multiples of 4. So gonna use our own method for this purpose to keep backwards compatibility
// https://github.com/beatgammit/base64-js/blob/master/index.js#L26
export function decodeBase64(s) {
const e = {},
w = String.fromCharCode,
L = s.length;
let i,
b = 0,
c,
x,
l = 0,
a,
r = '';
const A = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
for (i = 0; i < 64; i++) {
e[A.charAt(i)] = i;
}
for (x = 0; x < L; x++) {
c = e[s.charAt(x)];
b = (b << 6) + c;
l += 6;
while (l >= 8) {
((a = (b >>> (l -= 8)) & 0xff) || x < L - 2) && (r += w(a));
}
}
return r;
}
7 changes: 7 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
export { StreamChat } from './client';
export * from './client_state';
export { logChatPromiseExecution } from './utils';
export * from './channel';
export * from './channel_state';
export * from './permissions';
export * from './events';
export * from './signing';
export * from './base64.js';
35 changes: 1 addition & 34 deletions src/signing.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import jwt from 'jsonwebtoken';
import crypto from 'crypto';
import { encodeBase64, decodeBase64 } from './base64';

/**
* Creates the JWT token that can be used for a UserSession
Expand Down Expand Up @@ -42,32 +43,6 @@ export function JWTServerToken(apiSecret, jwtOptions = {}) {
return jwt.sign(payload, apiSecret, opts);
}

function decodeBase64(s) {
const e = {},
w = String.fromCharCode,
L = s.length;
let i,
b = 0,
c,
x,
l = 0,
a,
r = '';
const A = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
for (i = 0; i < 64; i++) {
e[A.charAt(i)] = i;
}
for (x = 0; x < L; x++) {
c = e[s.charAt(x)];
b = (b << 6) + c;
l += 6;
while (l >= 8) {
((a = (b >>> (l -= 8)) & 0xff) || x < L - 2) && (r += w(a));
}
}
return r;
}

/**
* @return {string}
*/
Expand All @@ -82,14 +57,6 @@ export function UserFromToken(token) {
return data.user_id;
}

function encodeBase64(s) {
if (typeof window !== 'undefined' && window.btoa) {
return window.btoa(s);
} else {
return Buffer.from(s.toString(), 'binary').toString('base64');
}
}

/**
*
* @param userId {string} the id of the user
Expand Down
21 changes: 20 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import chaiLike from 'chai-like';
import Immutable from 'seamless-immutable';
import { StreamChat } from '../src';
import { StreamChat, decodeBase64, encodeBase64 } from '../src';
import { expectHTTPErrorCode } from './utils';
import fs from 'fs';
import assertArrays from 'chai-arrays';
Expand Down Expand Up @@ -586,6 +586,25 @@ describe('Chat', function() {
});
});

describe('base64', function() {
it('encodes correctly', function() {
const testCases = [
'vishal',
'vishy',
'jaap',
'eugene',
'luke=',
'Marcello!?',
];

testCases.forEach(name => {
const b64str = encodeBase64(name);
const str = decodeBase64(b64str);
expect(str).to.equal(name);
});
});
});

describe('Auth', function() {
it('Token based auth', async function() {
const token = createUserToken('daenerys');
Expand Down
6 changes: 5 additions & 1 deletion types/stream-chat/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ export class Channel {
unbanUser(targetUserID: string): Promise<UnbanUserAPIResponse>;
on(callbackOrString: string, callbackOrNothing: any): void;
off(callbackOrString: string, callbackOrNothing: any): void;
hide(userId?: string, clearHistory?: bool): Promise<APIResponse>;
hide(userId?: string, clearHistory?: boolean): Promise<APIResponse>;
show(userId?: string): Promise<APIResponse>;
}

Expand Down Expand Up @@ -431,6 +431,10 @@ export function DevToken(userId: string): string;

export function CheckSignature(body: any, secret: string, signature: string): boolean;

export function encodeBase64(s: string): string;

export function decodeBase64(s: string): string;

export function isValidEventType(eventType: string): boolean;

export function logChatPromiseExecution(promise: Promise<any>, name: string): void;
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1351,7 +1351,7 @@ base64-arraybuffer@^0.1.5:
resolved "https://registry.yarnpkg.com/base64-arraybuffer/-/base64-arraybuffer-0.1.5.tgz#73926771923b5a19747ad666aa5cd4bf9c6e9ce8"
integrity sha1-c5JncZI7Whl0etZmqlzUv5xunOg=

base64-js@^1.1.2:
base64-js@^1.1.2, base64-js@^1.3.1:
version "1.3.1"
resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-1.3.1.tgz#58ece8cb75dd07e71ed08c736abc5fac4dbf8df1"
integrity sha512-mLQ4i2QO1ytvGWFWmcngKO//JXAQueZvwEKtjgQFM4jIK0kU+ytMfplL8j+n5mspOfjHwoAg+9yhb7BwAHm36g==
Expand Down