Skip to content

Commit 587843f

Browse files
committed
Initial commit v1.0.0
0 parents  commit 587843f

File tree

15 files changed

+581
-0
lines changed

15 files changed

+581
-0
lines changed

.editorconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
3+
root = true
4+
5+
[*.js]
6+
charset = utf-8
7+
end_of_line = lf
8+
indent_style = space
9+
indent_size = 2
10+
insert_final_newline = false
11+
12+
[*.json]
13+
indent_size = 2
14+
15+
[*.jade]
16+
indent_size = 2

.eslintignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
**/_package/**
2+
**/node_modules/**
3+
**/coverage/**
4+
**/.idea/**

.eslintrc.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"plugins": [
3+
"prettier"
4+
],
5+
"rules": {
6+
"prettier/prettier": "error",
7+
"no-var": "error",
8+
"quotes": [
9+
"error",
10+
"single",
11+
{
12+
"avoidEscape": true
13+
}
14+
],
15+
"one-var": [
16+
"error",
17+
"never"
18+
],
19+
"prefer-const": "error"
20+
},
21+
"parserOptions": {
22+
"ecmaVersion": 8,
23+
"sourceType": "module"
24+
}
25+
}

.gitignore

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# Bower dependency directory (https://bower.io/)
27+
bower_components
28+
29+
# node-waf configuration
30+
.lock-wscript
31+
32+
# Compiled binary addons (http://nodejs.org/api/addons.html)
33+
build/Release
34+
35+
# Dependency directories
36+
node_modules/
37+
jspm_packages/
38+
39+
# Typescript v1 declaration files
40+
typings/
41+
42+
# Optional npm cache directory
43+
.npm
44+
45+
# Optional eslint cache
46+
.eslintcache
47+
48+
# Optional REPL history
49+
.node_repl_history
50+
51+
# Output of 'npm pack'
52+
*.tgz
53+
54+
# Yarn Integrity file
55+
.yarn-integrity
56+
57+
# dotenv environment variables file
58+
.env
59+
60+
61+
**/_package/**
62+
**/coverage/**

.prettierrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"singleQuote": true
3+
}

.travis.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: node_js
2+
3+
matrix:
4+
include:
5+
- node_js: "6"
6+
- node_js: "8"
7+
8+
after_script:
9+
- npm run coverage
10+
11+
notifications:
12+
email: false

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 OpenComponents community
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# oc-plugin-jwt [![Greenkeeper badge](https://badges.greenkeeper.io/opencomponents/oc-plugin-jwt.svg)](https://greenkeeper.io/) [![Build Status](https://travis-ci.org/opencomponents/oc-plugin-jwt.svg?branch=master)](https://travis-ci.org/opencomponents/oc-plugin-jwt)
2+
3+
4+
[OpenComponents](https://github.com/opentable/oc) plugin for validating [JSON Web Token (JWT)](https://tools.ietf.org/html/rfc7519) inside OC components.
5+
6+
## Requirements
7+
* Node version: min **6**
8+
* [OC Registry](https://github.com/opentable/oc)
9+
10+
## Install
11+
12+
```bash
13+
npm i oc-plugin-jwt --save
14+
```
15+
16+
## Registry setup
17+
18+
More info about integrating OC plugins: [here](https://github.com/opentable/oc/wiki/Registry#plugins)
19+
20+
21+
Registering using the simple in-memory keystore.
22+
23+
```js
24+
const registry = oc.registry(configuration);
25+
26+
registry.register(
27+
{
28+
name: 'jwtVerify',
29+
register: require('oc-plugin-jwt').verify,
30+
options: {
31+
keys: {
32+
'key-id-1': {
33+
publicKey: fs.readFileSync('certificate.pem')
34+
},
35+
'key-id-2': {
36+
secret: 'super-secret-password'
37+
}
38+
}
39+
}
40+
},
41+
err => {
42+
if (err) {
43+
console.log('plugin initialisation failed:', err);
44+
} else {
45+
console.log('jwt verify now available');
46+
}
47+
}
48+
);
49+
50+
registry.start(callback);
51+
```
52+
53+
Or custom using a custom keystore
54+
```js
55+
const registry = oc.registry(configuration);
56+
57+
registry.register(
58+
{
59+
name: 'jwtVerify',
60+
register: require('oc-plugin-jwt').verify,
61+
options: {
62+
keyStore: {
63+
getSecretOrPublicKey(keyId, callback) {
64+
// Get the public key or secret by some method
65+
return callback(null, key);
66+
}
67+
}
68+
}
69+
},
70+
err => {
71+
if (err) {
72+
console.log('plugin initialisation failed:', err);
73+
} else {
74+
console.log('jwt verify now available');
75+
}
76+
}
77+
);
78+
79+
registry.start(callback);
80+
```
81+
82+
## Using it inside components
83+
84+
Example for a component's server.js:
85+
86+
```js
87+
module.exports.data = (context, callback) => {
88+
const exampleToken =
89+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImtleS1pZC0yIn0.' +
90+
'eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.' +
91+
'bQVxleqAX7NQzI_RkIPFVfTl44-iEY0UYPUBm10789o';
92+
context.plugins.jwtVerify(exampleToken, (error, verifiedToken) => {
93+
if (error) {
94+
// Handle token verification errors
95+
callback(error);
96+
}
97+
callback(null, { verifiedToken: verifiedToken });
98+
});
99+
};
100+
```
101+
102+
## Generating Tokens
103+
104+
* [See Here](https://github.com/opencomponents/oc-plugin-jwt-examples)

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
module.exports.verify = require('./lib/verify');
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`verify when a key is not found in the keystore it returns an error with no token 1`] = `[Error: Key no-key not found]`;
4+
5+
exports[`verify when a using a custom keystore returns the verified token with no errors 1`] = `
6+
Object {
7+
"admin": true,
8+
"name": "John Doe",
9+
"sub": "1234567890",
10+
}
11+
`;
12+
13+
exports[`verify when given a string that is not a token it returns an error with no token 1`] = `[Error: Invalid Token]`;
14+
15+
exports[`verify when given a token that has been tampered with it returns an error with no token 1`] = `[JsonWebTokenError: invalid signature]`;
16+
17+
exports[`verify when given a token that is missing the key id it returns an error with no token 1`] = `[Error: Token missing key id (kid) in header]`;
18+
19+
exports[`verify when given a valid public key signed token returns the verified token with no errors 1`] = `
20+
Object {
21+
"admin": true,
22+
"name": "John Doe",
23+
"sub": "1234567890",
24+
}
25+
`;
26+
27+
exports[`verify when given a valid secret signed token returns the verified token with no errors 1`] = `
28+
Object {
29+
"admin": true,
30+
"name": "John Doe",
31+
"sub": "1234567890",
32+
}
33+
`;

0 commit comments

Comments
 (0)