8000 Test against jose instead of jsjws · davedoesdev/python-jwt@7343cb7 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Nov 13, 2023. It is now read-only.

Commit 7343cb7

Browse files
committed
Test against jose instead of jsjws
1 parent 1b4bafa commit 7343cb7

File tree

5 files changed

+42
-53
lines changed

5 files changed

+42
-53
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ python:
66
- 3.7
77
- 3.8
88
node_js:
9-
- 12
9+
- 13
1010
before_install:
11-
- nvm install 12
11+
- nvm install 13
1212
- node --version
1313
- make node_deps
1414
addons:

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ bench_gfm:
3535
for b in ./bench/*_bench.py; do $$b --gfm; done
3636

3737
node_deps:
38-
mkdir -p node_modules && npm install --python=python2.7 jsjws sinon
38+
mkdir -p node_modules && npm install --python=python2.7 jose sinon
3939

4040
dist: make_dist
4141

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Module for generating and verifying [JSON Web Tokens](http://self-issued.info/do
66
- **Note:** Versions 1.0.0 and later fix [a vulnerability](https://www.timmclean.net/2015/02/25/jwt-alg-none.html) in JSON Web Token verification so please upgrade if you're using this functionality. The API has changed so you will need to update your application. [verify_jwt](http://rawgit.davedoesdev.com/davedoesdev/python-jwt/master/docs/_build/html/index.html#python_jwt.verify_jwt) now requires you to specify which signature algorithms are allowed.
77
- Uses [jwcrypto](https://jwcrypto.readthedocs.io) to do the heavy lifting.
88
- Supports [__RS256__, __RS384__, __RS512__](http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-14#section-3.3), [__PS256__, __PS384__, __PS512__](http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-14#section-3.5), [__HS256__, __HS384__, __HS512__](http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-14#section-3.2) and [__none__](http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-14#section-3.6) signature algorithms.
9-
- Unit tests, including tests for interoperability with [node-jsjws](https://github.com/davedoesdev/node-jsjws).
9+
- Unit tests, including tests for interoperability with [jose](https://github.com/panva/jose).
1010
- Supports Python 2,7 and 3.6+. **Note:** [generate_jwt](http://rawgit.davedoesdev.com/davedoesdev/python-jwt/master/docs/_build/html/index.html#python_jwt.generate_jwt) returns the token as a Unicode string, even on Python 2.7.
1111

1212
Example:

test/fixtures.js

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,39 @@
11
/*jslint node: true */
22
"use strict";
33

4-
var sinon = require('sinon'),
5-
jsjws = require('jsjws');
6-
7-
function generate(time, header, claims, expires, not_before, key)
8-
{
9-
if (key.indexOf('-----BEGIN') === 0)
10-
{
11-
key = jsjws.createPrivateKey(key);
12-
}
13-
14-
var clock = sinon.useFakeTimers(time * 1000);
15-
16-
try
17-
{
18-
expires = new Date(expires * 1000);
19-
not_before = new Date(not_before * 1000);
20-
21-
process.stdout.write(new jsjws.JWT().generateJWTByKey(header, claims, expires, not_before, key));
22-
}
23-
finally
24-
{
4+
const crypto = require('crypto');
5+
const sinon = require('sinon');
6+
const { JWK, JWT } = require('jose');
7+
8+
function generate(time, header, claims, expires, not_before, key) {
9+
const clock = sinon.useFakeTimers(time * 1000);
10+
claims.exp = expires;
11+
claims.nbf = not_before;
12+
try {
13+
process.stdout.write(JWT.sign(claims, JWK.asKey(key), {
14+
algorithm: header.alg,
15+
jti: crypto.randomBytes(16).toString('hex'),
16+
kid: false,
17+
header: { typ: 'JWT' }
18+
}));
19+
} finally {
2520
clock.restore();
2621
}
2722
}
2823

29-
function verify(time, sjwt, iat_skew, key, alg)
30-
{
31-
if (key.indexOf('-----BEGIN') === 0)
32-
{
33-
key = jsjws.createPublicKey(key);
34-
}
35-
36-
var clock = sinon.useFakeTimers(time * 1000), jwt;
37-
38-
try
39-
{
40-
jwt = new jsjws.JWT();
41-
42-
jwt.verifyJWTByKey(sjwt, {iat_skew: iat_skew}, key, [alg]);
43-
44-
process.stdout.write(JSON.stringify([jwt.getParsedHeader(), jwt.getParsedPayload()]));
45-
}
46-
finally
47-
{
24+
function verify(time, sjwt, iat_skew, key, alg) {
25+
const clock = sinon.useFakeTimers(time * 1000);
26+
try {
27+
const { header, payload } = JWT.verify(sjwt, JWK.asKey(key), {
28+
algorithms: [ alg ],
29+
ignoreIat: true,
30+
complete: true
31+
});
32+
if (payload.iat > (Math.floor(Date.now() / 1000) + iat_skew)) {
33+
throw new Error('issued in the future');
34+
}
35+
process.stdout.write(JSON.stringify([header, payload]));
36+
} finally {
4837
clock.restore();
4938
}
5039
}

test/node_jsjws_interop_vows.py renamed to test/jose_interop_vows.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
""" test interop with node-jsjws """
1+
""" test interop with jose for node """
22

33
# pylint: disable=wrong-import-order
44
from test.common import pub_keys, priv_keys, algs, pub_key, priv_key
@@ -27,10 +27,10 @@ def spawn(cmd, parse_json):
2727

2828
#pylint: disable=W0621
2929
def generate(alg):
30-
""" return function which can generate token using node-jsjws """
30+
""" return function which can generate token using jose """
3131
key = priv_keys[alg].get('default', priv_key)
3232
def f(claims, alg, lifetime=None, expires=None, not_before=None):
33-
""" generate token using node-jsjws """
33+
""" generate token using jose """
3434
now = datetime.utcnow()
3535
return spawn(
3636
"fixtures.generate({now}, {header}, {claims}, {expires}, {not_before}, {key})".format(
@@ -44,10 +44,10 @@ def f(claims, alg, lifetime=None, expires=None, not_before=None):
4444
return f
4545

4646
def verify(alg):
47-
""" return function which can verify token using node-jsjws """
47+
""" return function which can verify token using jose """
4848
key = pub_keys[alg].get('default', pub_key)
4949
def f(sjwt, iat_skew=timedelta()):
50-
""" verify token using node-jsjws """
50+
""" verify token using jose """
5151
r = spawn(
5252
"fixtures.verify({now}, {sjwt}, {iat_skew}, {key}, {alg})".format(
5353
now=timegm(datetime.utcnow().utctimetuple()),
@@ -60,11 +60,11 @@ def f(sjwt, iat_skew=timedelta()):
6060
return f
6161

6262
for alg in algs:
63-
priv_keys[alg]['node_jsjws'] = generate(alg)
64-
pub_keys[alg]['node_jsjws'] = verify(alg)
63+
priv_keys[alg]['jose'] = generate(alg)
64+
pub_keys[alg]['jose'] = verify(alg)
6565

6666
jwt_spec.setup(['HS256', 'HS512', 'RS256', 'RS512', 'PS256', 'PS512'])
6767

6868
for alg in algs:
69-
del priv_keys[alg]['node_jsjws']
70-
del pub_keys[alg]['node_jsjws']
69+
del priv_keys[alg]['jose']
70+
del pub_keys[alg]['jose']

0 commit comments

Comments
 (0)
0