Skip to content

Commit a204721

Browse files
Aschenalexandrebouthinon
authored andcommitted
Merge pull request #441 from kuzzleio/fix-base64-decoding-react-native
React Native does not have the Buffer class or the atob function to decode base64 payload. This PR add a standalone base64 decoding function to ensure portability across platforms.
2 parents 537ea7d + 39472a3 commit a204721

File tree

6 files changed

+28
-28
lines changed

6 files changed

+28
-28
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ jobs:
183183
184184
api_key:
185185
secure: ktfR6QVV59uCVLTfb60JSjPxzxg+UYe63pIGxU9awh1wJW89SQqQdeshHas3ojwrNoMOVybx/L6owtD8uIB4Xlau6pd7StBAA1VQ3vLa6RxU5SUr9VOz3RhZutRnYXH28Ziz/ynr6zVnwGxTzFsxBAdQ9CVS3ErBzliTgMVI+52YRNB3mBDiccSKVNGmp2APiWiaEidrmaOFkCwWQKsAxFTbBoCsDYGGAq8a8b1i2nE+BL4JWB1D2x55xzFaciDIQONXnY9YPAAbzCHBTKAne5iD6XwFj7Zg/42fHTn16LBSG+Sw+7aWyV11U6SJYKAjm/5GZEUrbvX/mwNQ7VYcvMStiP1nnt/L/s/Y7d0K3mwdsV8U8RCctElOeqLsDqoBzQYAxHTqRqJzyFNMcbcMhZgsHH5LgQDoJdKFwzP2ysYoazgm/jnr9atvlqucjWU8madRS34S8wD0zGjYf8VXH3WMUvL5mFwtb58FRCZKO3G7QJlGE8bdZ8CfJa/1tFnYY9SA0018GaXbh0RqQw0RskbmXEKTlnDBxaRd/ZjfaOjF4JcMGGZ8hsCRjxuDFY5Ki0kKDAlDV1W40fkkqPVm8k9H8Elwt0UFZvvAdjG4c+gV0zdAKHQ7P6uKvXiDlsKRHulE3Ztpo6jqGN69s7aLm6DKNVsaE4cNmmUOv3ODrM4=
186+
on:
187+
branch: master
188+
tags: true
186189

187190
- stage: Deploy Beta release on NPM
188191
if: type = push && branch =~ /^[0-9]+-dev$/

features/support/hooks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ async function clean () {
3636
}
3737
catch (error) {
3838
// rethrow to get a readable error
39-
console.error(error);
39+
console.error(error); // eslint-disable-line no-console
4040
throw error;
4141
}
4242

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kuzzle-sdk",
3-
"version": "6.2.0",
3+
"version": "6.2.1",
44
"description": "Official Javascript SDK for Kuzzle",
55
"author": "The Kuzzle Team <[email protected]>",
66
"repository": {

src/core/Jwt.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
'use strict';
22

3-
// used for unit tests
4-
const browserAtob = base64 => atob(base64);
5-
const bufferAvailable = () => typeof Buffer !== 'undefined';
3+
// atob is not available in React Native
4+
// https://stackoverflow.com/questions/42829838/react-native-atob-btoa-not-working-without-remote-js-debugging
65

7-
const decodeBase64 = base64 => {
8-
if (bufferAvailable()) {
9-
return Buffer.from(base64, 'base64').toString();
6+
const base64Charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
7+
8+
const decodeBase64 = input => {
9+
const str = input.replace(/=+$/, '');
10+
let output = '';
11+
12+
if (str.length % 4 === 1) {
13+
throw new Error('Malformed base64 string.');
14+
}
15+
16+
for (let bc = 0, bs = 0, buffer, i = 0;
17+
buffer = str.charAt(i++); // eslint-disable-line no-cond-assign
18+
19+
~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer, bc++ % 4)
20+
? output += String.fromCharCode(255 & bs >> (-2 * bc & 6))
21+
: 0
22+
) {
23+
buffer = base64Charset.indexOf(buffer);
1024
}
1125

12-
return browserAtob(base64);
26+
return output;
1327
};
1428

1529
class Jwt {
@@ -57,4 +71,4 @@ class Jwt {
5771
}
5872
}
5973

60-
module.exports = Jwt;
74+
module.exports = Jwt;

test/core/Jwt.test.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,6 @@ describe('Jwt', () => {
3737
new Jwt('this-is.not-json-payload.for-sure');
3838
}).throwError('Invalid JSON payload for JWT');
3939
});
40-
41-
it('should be able to decode the payload when Buffer is not available (browser)', () => {
42-
Jwt.__set__('browserAtob', base64 => Buffer.from(base64, 'base64').toString());
43-
Jwt.__set__('bufferAvailable', () => false);
44-
45-
const
46-
expiresAt = Date.now() + 3600 * 1000,
47-
encodedJwt = generateJwt('user-gordon', expiresAt);
48-
49-
50-
authenticationToken = new Jwt(encodedJwt);
51-
52-
should(authenticationToken.encodedJwt).be.eql(encodedJwt);
53-
should(authenticationToken.userId).be.eql('user-gordon');
54-
should(authenticationToken.expiresAt).be.eql(expiresAt);
55-
should(authenticationToken.expired).be.eql(false);
56-
});
5740
});
5841

5942
describe('#get expired', () => {

0 commit comments

Comments
 (0)