Merge pull request #6958 from mozilla/tweak-array-buf

fix(pairing): conver to Uint8Array for pairing channel compatibility
This commit is contained in:
Vlad Filippov 2019-02-13 17:03:12 -05:00 коммит произвёл GitHub
Родитель 0a91eb5406 8b364659d1
Коммит 6d41d21759
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 18 добавлений и 18 удалений

Просмотреть файл

@ -3,11 +3,11 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Converts base64url to an ArrayBuffer
* Converts base64url to a Uint8Array
* @param {String} base64
* @returns {ArrayBufferLike}
* @returns {Uint8Array}
*/
function base64urlToArrayBuffer(base64 = '') {
function base64urlToUint8Array(base64 = '') {
base64 = base64.replace(/-/g, '+'); // 62nd char of encoding
base64 = base64.replace(/_/g, '/'); // 63rd char of encoding
const binaryString = atob(base64);
@ -16,9 +16,9 @@ function base64urlToArrayBuffer(base64 = '') {
for (let i = 0; i < len; i++){
bytes[i] = binaryString.charCodeAt(i);
}
return bytes.buffer;
return bytes;
}
module.exports = {
base64urlToArrayBuffer
base64urlToUint8Array
};

Просмотреть файл

@ -5,7 +5,7 @@
import PairingChannelClientErrors from './pairing-channel-client-errors';
import { Model } from 'backbone';
import { pick } from 'underscore';
import { base64urlToArrayBuffer } from './crypto/util';
import { base64urlToUint8Array } from './crypto/util';
import Raven from 'raven';
import Vat from 'lib/vat';
@ -61,7 +61,7 @@ export default class PairingChannelClient extends Model {
throw PairingChannelClientErrors.toError('INVALID_CONFIGURATION');
}
const psk = base64urlToArrayBuffer(channelKey);
const psk = base64urlToUint8Array(channelKey);
return FxAccountsPairingChannel.PairingChannel.connect(channelServerUri, channelId, psk).then((channel) => {
this.channel = channel;

Просмотреть файл

@ -5,25 +5,25 @@
'use strict';
import {assert} from 'chai';
import {base64urlToArrayBuffer} from 'lib/crypto/util';
import {base64urlToUint8Array} from 'lib/crypto/util';
describe('lib/crypto/util/base64urlToArrayBuffer', () => {
describe('lib/crypto/util/base64urlToUint8Array', () => {
it('should decode base64url undefined as empty string', () => {
const arrayBuffer = base64urlToArrayBuffer();
assert.equal(arrayBuffer.constructor.name, 'ArrayBuffer', 'is an array buffer');
assert.equal(new Int8Array(arrayBuffer)[0], undefined, 'is correct value for empty string');
const uint8array = base64urlToUint8Array();
assert.equal(uint8array.constructor.name, 'Uint8Array', 'is a Uint8Array');
assert.equal(new Int8Array(uint8array)[0], undefined, 'is correct value for empty string');
});
it('should decode base64url empty string', () => {
const arrayBuffer = base64urlToArrayBuffer('');
assert.equal(arrayBuffer.constructor.name, 'ArrayBuffer', 'is an array buffer');
assert.equal(new Int8Array(arrayBuffer)[0], undefined, 'is correct value for empty string');
const uint8array = base64urlToUint8Array('');
assert.equal(uint8array.constructor.name, 'Uint8Array', 'is a Uint8Array');
assert.equal(new Int8Array(uint8array)[0], undefined, 'is correct value for empty string');
});
it('should decode base64url test', () => {
const arrayBuffer = base64urlToArrayBuffer('dGVzdA==');
assert.equal(arrayBuffer.constructor.name, 'ArrayBuffer', 'is an array buffer');
const int8Array = new Int8Array(arrayBuffer);
const uint8array = base64urlToUint8Array('dGVzdA==');
assert.equal(uint8array.constructor.name, 'Uint8Array', 'is a Uint8Array');
const int8Array = new Int8Array(uint8array);
assert.equal(int8Array[0], 116);
assert.equal(int8Array[1], 101);
assert.equal(int8Array[2], 115);