Skip to content

Commit 7800931

Browse files
committed
Breaking: use external implementation of base64url
1 parent d1267b2 commit 7800931

File tree

4 files changed

+32
-85
lines changed

4 files changed

+32
-85
lines changed

README.md

+4-8
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ var jose = require('node-jose');
5858

5959
This library uses [Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) for nearly every operation.
6060

61-
This library supports [Browserify](http://browserify.org/). To use in a web browser, `require('node-kms')` and bundle with the rest of your app.
61+
This library supports [Browserify](http://browserify.org/). To use in a web browser, `require('node-jose')` and bundle with the rest of your app.
6262

6363
The content to be signed/encrypted or returned from being verified/decrypted are [Buffer](https://nodejs.org/api/buffer.html) objects.
6464

@@ -448,6 +448,8 @@ buff = jose.util.asBuffer(input);
448448

449449
### URI-Safe Base64 ###
450450

451+
This exposes [base64url](https://github.com/brianloveswords/base64url)'s `encode` and `toBuffer` methods as `encode` and `decode` (respectively).
452+
451453
To convert from a Buffer to a base64uri-encoded String:
452454

453455
```
@@ -460,7 +462,7 @@ To convert a String to a base64uri-encoded String:
460462
// explicit encoding
461463
output = jose.util.base64url.encode(input, "utf8");
462464
463-
// implied "binary" encoding
465+
// implied "utf8" encoding
464466
output = jose.util.base64url.encode(input);
465467
```
466468

@@ -470,12 +472,6 @@ To convert a base64uri-encoded String to a Buffer:
470472
var output = jose.util.base64url.decode(input);
471473
```
472474

473-
To convert a base64uri-encoded String to a String:
474-
475-
```
476-
output = jose.util.base64url.decode(input, "utf8");
477-
```
478-
479475
### Random Bytes ###
480476

481477
To generate a Buffer of octets, regardless of platform:

lib/util/base64url.js

+24-58
Original file line numberDiff line numberDiff line change
@@ -5,69 +5,35 @@
55
*/
66
"use strict";
77

8+
var impl = require("base64url");
9+
810
/**
911
* @namespace base64url
1012
* @description
1113
* Provides methods to encode and decode data according to the
1214
* base64url alphabet.
1315
*/
14-
var base64url = exports;
15-
/**
16-
* Encodes the input to base64url.
17-
*
18-
* If {input} is a Buffer, then {encoding} is ignored. Otherwise,
19-
* {encoding} can be one of "binary", "base64", "hex", "utf8".
20-
*
21-
* @param {Buffer|String} input The data to encode.
22-
* @param {String} [encoding = binary] The input encoding format.
23-
* @returns {String} the base64url encoding of {input}.
24-
*/
25-
base64url.encode = function(input, encoding) {
26-
var fn = function(match) {
27-
switch(match) {
28-
case "+": return "-";
29-
case "/": return "_";
30-
case "=": return "";
31-
}
32-
// should never happen
33-
};
34-
35-
encoding = encoding || "binary";
36-
if (Buffer.isBuffer(input)) {
37-
input = input.toString("base64");
38-
} else {
39-
if ("undefined" !== typeof ArrayBuffer && input instanceof ArrayBuffer) {
40-
input = new Uint8Array(input);
41-
}
42-
input = new Buffer(input, encoding).toString("base64");
43-
}
44-
45-
return input.replace(/\+|\/|\=/g, fn);
16+
var base64url = {
17+
/**
18+
* @function
19+
* Encodes the input to base64url.
20+
*
21+
* If {input} is a Buffer, then {encoding} is ignored. Otherwise,
22+
* {encoding} can be one of "binary", "base64", "hex", "utf8".
23+
*
24+
* @param {Buffer|String} input The data to encode.
25+
* @param {String} [encoding = binary] The input encoding format.
26+
* @returns {String} the base64url encoding of {input}.
27+
*/
28+
encode: impl.encode,
29+
/**
30+
* @function
31+
* Decodes the input from base64url.
32+
*
33+
* @param {String} input The data to decode.
34+
* @returns {Buffer|String} the base64url decoding of {input}.
35+
*/
36+
decode: impl.toBuffer
4637
};
47-
/**
48-
* Decodes the input from base64url.
49-
*
50-
* If {encoding} is not specified, then this method returns a Buffer.
51-
* Othewise, {encoding} can be one of "binary", "base64", "hex", "utf8";
52-
* this method then returns a string matching the given encoding.
53-
*
54-
* @param {String} input The data to decode.
55-
* @param {String} [encoding] The output encoding format.
56-
* @returns {Buffer|String} the base64url decoding of {input}.
57-
*/
58-
base64url.decode = function(input, encoding) {
59-
var fn = function(match) {
60-
switch(match) {
61-
case "-": return "+";
62-
case "_": return "/";
63-
}
64-
// should never happen
65-
};
6638

67-
input = input.replace(/\-|\_/g, fn);
68-
var output = new Buffer(input, "base64");
69-
if (encoding) {
70-
output = output.toString(encoding);
71-
}
72-
return output;
73-
};
39+
module.exports = base64url;

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
],
2626
"license": "Apache-2.0",
2727
"dependencies": {
28+
"base64url": "^1.0.4",
2829
"es6-promise": "^2.0.1",
2930
"jsbn": "git+https://github.com/andyperlitch/jsbn.git",
3031
"lodash.assign": "^3.2.0",

test/util/base64url-test.js

+3-19
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ describe("util/base64url", function() {
3737
assert.equal(output, "4oC5aGVsbG8gd29ybGQh4oC6");
3838
});
3939

40-
it("should encode a (binary) string", function() {
41-
var input = "\xe2\x80\xb9hello world!\xe2\x80\xba";
40+
it("should encode a (utf8) string", function() {
41+
var input = "‹hello world!";
4242
var output = utils.base64url.encode(input);
4343
assert.equal(output, "4oC5aGVsbG8gd29ybGQh4oC6");
4444
});
@@ -84,27 +84,11 @@ describe("util/base64url", function() {
8484
assert.deepEqual(output, expected);
8585
});
8686

87-
it("should decode a string to a string", function() {
88-
var input, output;
89-
90-
input = "4oC5aGVsbG8gd29ybGQh4oC6";
91-
output = utils.base64url.decode(input, "binary");
92-
assert.equal(output, "\xe2\x80\xb9hello world!\xe2\x80\xba");
93-
94-
input = "4oC5aGVsbG8gd29ybGQh4oC6";
95-
output = utils.base64url.decode(input, "utf8");
96-
assert.equal(output, "‹hello world!›");
97-
98-
input = "4oC5aGVsbG8gd29ybGQh4oC6";
99-
output = utils.base64url.decode(input, "hex");
100-
assert.equal(output, "e280b968656c6c6f20776f726c6421e280ba");
101-
});
102-
10387
it("should decode the rainbow!", function() {
10488
var input, output;
10589

10690
input = "Pfv_Oeu-Ndt9Mcs8Lbr7Kaq6JZp5IYo4HXn3GWm2FVl1EUk0DTjzCSiyBRhxAQgw";
107-
output = utils.base64url.decode(input, "hex");
91+
output = utils.base64url.decode(input).toString("hex");
10892
assert.equal(output, "3dfbff39ebbe35db7d31cb3c2dbafb29aaba259a79218a381d79f71969b61559751149340d38f30928b2051871010830");
10993
});
11094
});

0 commit comments

Comments
 (0)