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

Angular Support: To make node-jose work in angular apps #264

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ A JavaScript implementation of the JSON Object Signing and Encryption (JOSE) for
- [Converting to Buffer](#converting-to-buffer)
- [URI-Safe Base64](#uri-safe-base64)
- [Random Bytes](#random-bytes)
- [Angular Usage](#angular-usage)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

Expand Down Expand Up @@ -794,3 +795,36 @@ This function uses:
* `crypto.randomBytes()` on node.js
* `crypto.getRandomValues()` on modern browsers
* A PRNG based on AES and SHA-1 for older platforms


### Angular Usage ###

Import and use as any other package. All the methods will be supported in the browser, just as in node.js.

```javascript
import * as jose from 'node-jose';
```

The following changes are required to make few node-modules available in the browser

- angular compilerOptions for stream

In `tsconfig.json` , *compilerOptions* add
```json
"paths": {
"stream": ["../node_modules/stream-browserify/index.js"]
}
```
This is to avoid the below error
> ERROR in ./node_modules/browserify-zlib/lib/index.js.
> Module not found: Error: Can't resolve 'stream' in '***/node_modules/browserify-zlib/lib'

- polyfil for global object

In `polyfills.ts` add
```
// Polyfill for node-jose
(window as any)['global'] = window;
```
This is to avoid the below error
> Uncaught ReferenceError: global is not defined
12 changes: 12 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ if (typeof Promise === "undefined") {
require("es6-promise").polyfill();
}

try { // for browsers
var global = global || window;
// Browser Support: make buffer, that emulates node's Buffer API, available globally in the browser
global.Buffer = global.Buffer || require("buffer").Buffer;
// Browser Support: make process, that emulates node's Process API, available globally in the browser
global.process = global.process || require("process");
}
catch (e) {
// "window is not defined" for node.js
}


var JWS = require("./jws");

module.exports = {
Expand Down
11 changes: 9 additions & 2 deletions lib/jwe/decrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@
var base64url = require("../util/base64url"),
AlgConfig = require("../util/algconfig"),
JWK = require("../jwk"),
merge = require("../util/merge"),
zlib = require("zlib");
merge = require("../util/merge");

var zlib;
// Browser Support: If 'zlib' can't be resolved use browserify-zlib that emulates Node's zlib module for the browser
try {
zlib = require("zlib")
} catch(e) {
zlib = require("browserify-zlib");
}

var DEFAULT_OPTIONS = {
algorithms: "*"
Expand Down
9 changes: 8 additions & 1 deletion lib/jwe/encrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ var lodash = require("lodash"),
generateCEK = require("./helpers").generateCEK,
JWK = require("../jwk"),
slice = require("./helpers").slice,
zlib = require("zlib"),
CONSTANTS = require("../algorithms/constants");

var zlib;
// Browser Support: If 'zlib' can't be resolved use browserify-zlib that emulates Node's zlib module for the browser
try {
zlib = require("zlib")
} catch(e) {
zlib = require("browserify-zlib");
}

var assign = lodash.assign;
var clone = lodash.clone;

Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-jose",
"version": "1.1.3",
"version": "1.1.4",
"description": "A JavaScript implementation of the JSON Object Signing and Encryption (JOSE) for current web browsers and node.js-based servers",
"keywords": [
"crypto",
Expand Down Expand Up @@ -31,10 +31,14 @@
},
"dependencies": {
"base64url": "^3.0.1",
"browserify-zlib": "^0.2.0",
"buffer": "^5.4.2",
"es6-promise": "^4.2.6",
"lodash": "^4.17.11",
"long": "^4.0.0",
"node-forge": "^0.8.1",
"process": "^0.11.10",
"stream-browserify": "^2.0.2",
"uuid": "^3.3.2"
},
"devDependencies": {
Expand Down Expand Up @@ -73,4 +77,4 @@
"webpack-stream": "^4.0.0",
"yargs": "^11.1.0"
}
}
}