initial commit

This commit is contained in:
Yorick
2016-04-01 14:09:55 +00:00
commit 2e04121fef
2175 changed files with 537519 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
.vimrc*
.git*
.travis.yml
+3
View File
@@ -0,0 +1,3 @@
## 0.2.0
* Add the RS256 alg #7 [thedufer]
+22
View File
@@ -0,0 +1,22 @@
(The MIT License)
Copyright (c) 2011 Kazuhito Hokamura <k.hokamura@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+31
View File
@@ -0,0 +1,31 @@
# node-jwt-simple
[JWT(JSON Web Token)](http://self-issued.info/docs/draft-jones-json-web-token.html) encode and decode module for node.js.
JWT is used by [Google In-App Payments](http://code.google.com/intl/en/apis/inapppayments/docs/index.html).
## Install
$ npm install jwt-simple
## Usage
var jwt = require('jwt-simple');
var payload = { foo: 'bar' };
var secret = 'xxx';
// encode
var token = jwt.encode(payload, secret);
// decode
var decoded = jwt.decode(token, secret);
console.log(decoded); //=> { foo: 'bar' }
### Algorithms
By default the algorithm to encode is `HS256`.
The supported algorithms for encoding and decoding are `HS256`, `HS384`, `HS512` and `RS256`.
// encode using HS512
jwt.encode(playload, secret, 'HS512')
+1
View File
@@ -0,0 +1 @@
module.exports = require('./lib/jwt');
+178
View File
@@ -0,0 +1,178 @@
/*
* jwt-simple
*
* JSON Web Token encode and decode module for node.js
*
* Copyright(c) 2011 Kazuhito Hokamura
* MIT Licensed
*/
/**
* module dependencies
*/
var crypto = require('crypto');
/**
* support algorithm mapping
*/
var algorithmMap = {
HS256: 'sha256',
HS384: 'sha384',
HS512: 'sha512',
RS256: 'RSA-SHA256'
};
/**
* Map algorithm to hmac or sign type, to determine which crypto function to use
*/
var typeMap = {
HS256: 'hmac',
HS384: 'hmac',
HS512: 'hmac',
RS256: 'sign'
};
/**
* expose object
*/
var jwt = module.exports;
/**
* version
*/
jwt.version = '0.2.0';
/**
* Decode jwt
*
* @param {Object} token
* @param {String} key
* @param {Boolean} noVerify
* @return {Object} payload
* @api public
*/
jwt.decode = function jwt_decode(token, key, noVerify) {
// check seguments
var segments = token.split('.');
if (segments.length !== 3) {
throw new Error('Not enough or too many segments');
}
// All segment should be base64
var headerSeg = segments[0];
var payloadSeg = segments[1];
var signatureSeg = segments[2];
// base64 decode and parse JSON
var header = JSON.parse(base64urlDecode(headerSeg));
var payload = JSON.parse(base64urlDecode(payloadSeg));
if (!noVerify) {
var signingMethod = algorithmMap[header.alg];
var signingType = typeMap[header.alg];
if (!signingMethod || !signingType) {
throw new Error('Algorithm not supported');
}
// verify signature. `sign` will return base64 string.
var signingInput = [headerSeg, payloadSeg].join('.');
if (!verify(signingInput, key, signingMethod, signingType, signatureSeg)) {
throw new Error('Signature verification failed');
}
}
return payload;
};
/**
* Encode jwt
*
* @param {Object} payload
* @param {String} key
* @param {String} algorithm
* @return {String} token
* @api public
*/
jwt.encode = function jwt_encode(payload, key, algorithm) {
// Check key
if (!key) {
throw new Error('Require key');
}
// Check algorithm, default is HS256
if (!algorithm) {
algorithm = 'HS256';
}
var signingMethod = algorithmMap[algorithm];
var signingType = typeMap[algorithm];
if (!signingMethod || !signingType) {
throw new Error('Algorithm not supported');
}
// header, typ is fixed value.
var header = { typ: 'JWT', alg: algorithm };
// create segments, all segment should be base64 string
var segments = [];
segments.push(base64urlEncode(JSON.stringify(header)));
segments.push(base64urlEncode(JSON.stringify(payload)));
segments.push(sign(segments.join('.'), key, signingMethod, signingType));
return segments.join('.');
}
/**
* private util functions
*/
function verify(input, key, method, type, signature) {
if(type === "hmac") {
return (signature === sign(input, key, method, type));
}
else if(type == "sign") {
return crypto.createVerify(method)
.update(input)
.verify(key, base64urlUnescape(signature), 'base64');
}
else {
throw new Error('Algorithm type not recognized');
}
}
function sign(input, key, method, type) {
var base64str;
if(type === "hmac") {
base64str = crypto.createHmac(method, key).update(input).digest('base64');
}
else if(type == "sign") {
base64str = crypto.createSign(method).update(input).sign(key, 'base64');
}
else {
throw new Error('Algorithm type not recognized');
}
return base64urlEscape(base64str);
}
function base64urlDecode(str) {
return new Buffer(base64urlUnescape(str), 'base64').toString();
}
function base64urlUnescape(str) {
str += Array(5 - str.length % 4).join('=');
return str.replace(/\-/g, '+').replace(/_/g, '/');
}
function base64urlEncode(str) {
return base64urlEscape(new Buffer(str).toString('base64'));
}
function base64urlEscape(str) {
return str.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
}
+60
View File
@@ -0,0 +1,60 @@
{
"name": "jwt-simple",
"description": "JWT(JSON Web Token) encode and decode module",
"version": "0.2.0",
"author": {
"name": "Kazuhito Hokamura",
"email": "k.hokamura@gmail.com"
},
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/hokaccha/node-jwt-simple.git"
},
"devDependencies": {
"mocha": "*",
"expect.js": "*"
},
"scripts": {
"test": "mocha test/*.js"
},
"licenses": [
{
"type": "MIT",
"url": "https://raw.github.com/hokaccha/node-jwt-simple/master/LICENSE"
}
],
"engines": {
"node": ">= 0.4.0"
},
"keywords": [
"jwt",
"encode",
"decode"
],
"main": "./index",
"bugs": {
"url": "https://github.com/hokaccha/node-jwt-simple/issues"
},
"_id": "jwt-simple@0.2.0",
"dist": {
"shasum": "71e4e49b45cf2e2820d4576868bc0a8e373a9670",
"tarball": "http://registry.npmjs.org/jwt-simple/-/jwt-simple-0.2.0.tgz"
},
"_from": "jwt-simple@>=0.2.0 <0.3.0",
"_npmVersion": "1.3.11",
"_npmUser": {
"name": "hokaccha",
"email": "k.hokamura@gmail.com"
},
"maintainers": [
{
"name": "hokaccha",
"email": "k.hokamura@gmail.com"
}
],
"directories": {},
"_shasum": "71e4e49b45cf2e2820d4576868bc0a8e373a9670",
"_resolved": "https://registry.npmjs.org/jwt-simple/-/jwt-simple-0.2.0.tgz",
"readme": "ERROR: No README data found!",
"homepage": "https://github.com/hokaccha/node-jwt-simple#readme"
}
+57
View File
@@ -0,0 +1,57 @@
var jwt = require('../index');
var expect = require('expect.js');
var fs = require('fs');
describe('method and property', function() {
it('jwt has version property', function() {
expect(jwt.version).to.be.a('string');
});
it('jwt has encode and decode method', function() {
expect(jwt.encode).to.be.a('function');
expect(jwt.decode).to.be.a('function');
});
});
describe('encode and decode', function() {
it('encode token', function() {
var token = jwt.encode({ foo: 'bar' }, 'key');
expect(token).to.be.a('string');
expect(token.split('.')).to.have.length(3);
});
it('key is required', function() {
var fn = jwt.encode.bind(null, { foo: 'bar' });
expect(fn).to.throwException();
});
it('decode token', function() {
var obj = { foo: 'bar' };
var key = 'key';
var token = jwt.encode(obj, key);
var obj2 = jwt.decode(token, key);
expect(obj2).to.eql(obj);
expect(jwt.decode.bind(null, token, 'invalid_key')).to.throwException();
});
it('decode no verify', function() {
var obj = { foo: 'bar' };
var key = 'key';
var token = jwt.encode(obj, key);
var fn1 = jwt.decode.bind(null, token, null);
var fn2 = jwt.decode.bind(null, token, null, true);
expect(fn1).to.throwException();
expect(fn2()).to.eql(obj);
});
it('RS256', function() {
var obj = { foo: 'bar' };
var pem = fs.readFileSync(__dirname + '/test.pem').toString('ascii');
var cert = fs.readFileSync(__dirname + '/test.crt').toString('ascii');
var alg = 'RS256';
var token = jwt.encode(obj, pem, alg);
var obj2 = jwt.decode(token, cert);
expect(obj2).to.eql(obj);
expect(jwt.decode.bind(null, token, 'invalid_key')).to.throwException();
});
});
+13
View File
@@ -0,0 +1,13 @@
-----BEGIN CERTIFICATE-----
MIICATCCAWoCCQDoLzF89AVR9jANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJB
VTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0
cyBQdHkgTHRkMB4XDTE0MDMxODA5MzgzOVoXDTE1MDMxODA5MzgzOVowRTELMAkG
A1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUxITAfBgNVBAoTGEludGVybmV0
IFdpZGdpdHMgUHR5IEx0ZDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAxkf+
aQuof/FiI1ejRl/385JhGbOq9ZUD0Ma7FELpkW+Wb9k3dxFRXjIeZgbMr5kUtzGv
jMA+IJpMPmqHOLMUG731xxmXoHphlhWKV1TTR8OXduIxRB+frVhYfp0nOAZroO+5
sXBrGwCcFFjsDBhLLf7R1d9WdkS/LQ0rBi7GvaMCAwEAATANBgkqhkiG9w0BAQUF
AAOBgQCNxfthoxLOFZidvviG6aFjFgY35eFqv3RLHWAVBWQBHfjczph/r5mlT06z
AOKO7yp23Gi2dyBYaeq1u6n7iyMp9htYee8Y+Erlp6vurvi9S+/8mNVAPBtQ1kNw
KvzMTvylD2zWjopwMb9bfSKKT5pe7pZ7CS6Y5T8lM9yZlMBhHQ==
-----END CERTIFICATE-----
+15
View File
@@ -0,0 +1,15 @@
-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDGR/5pC6h/8WIjV6NGX/fzkmEZs6r1lQPQxrsUQumRb5Zv2Td3
EVFeMh5mBsyvmRS3Ma+MwD4gmkw+aoc4sxQbvfXHGZegemGWFYpXVNNHw5d24jFE
H5+tWFh+nSc4Bmug77mxcGsbAJwUWOwMGEst/tHV31Z2RL8tD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