-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add some tests and fix some bugs found by the tests
- Loading branch information
Showing
8 changed files
with
220 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright 2017 Gehirn Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import json | ||
from unittest import TestCase | ||
|
||
from jwt.jwa import ( | ||
HS256, | ||
none, | ||
) | ||
from jwt.jwk import jwk_from_dict | ||
from jwt.utils import b64decode | ||
|
||
from .helper import load_testdata | ||
|
||
|
||
class NoneTest(TestCase): | ||
|
||
def setUp(self): | ||
self.message = ( | ||
b'eyJhbGciOiJub25lIn0' | ||
b'.' | ||
b'eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFt' | ||
b'cGxlLmNvbS9pc19yb290Ijp0cnVlfQ' | ||
) | ||
|
||
def test_sign(self): | ||
signature = none.sign(self.message, None) | ||
self.assertEqual(signature, b'') | ||
|
||
def test_verify(self): | ||
self.assertTrue(none.verify(self.message, None, b'')) | ||
|
||
|
||
class HS256Test(TestCase): | ||
|
||
def setUp(self): | ||
self.key = jwk_from_dict(json.loads(load_testdata('oct.json', 'r'))) | ||
self.signature = b64decode( | ||
'dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk' | ||
) | ||
|
||
self.message = ( | ||
b'eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9' | ||
b'.' | ||
b'eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFt' | ||
b'cGxlLmNvbS9pc19yb290Ijp0cnVlfQ' | ||
) | ||
|
||
def test_sign(self): | ||
signature = HS256.sign(self.message, self.key) | ||
self.assertEqual(signature, self.signature) | ||
|
||
def test_verify(self): | ||
self.assertTrue(HS256.verify(self.message, self.key, self.signature)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright 2017 Gehirn Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import json | ||
from unittest import TestCase | ||
|
||
from jwt.jws import JWS | ||
from jwt.jwk import jwk_from_dict | ||
|
||
from .helper import load_testdata | ||
|
||
|
||
class JWSTest(TestCase): | ||
|
||
def setUp(self): | ||
self.inst = JWS() | ||
self.key = jwk_from_dict( | ||
json.loads(load_testdata('rsa_privkey.json', 'r'))) | ||
self.pubkey = jwk_from_dict( | ||
json.loads(load_testdata('rsa_pubkey.json', 'r'))) | ||
|
||
self.message = ( | ||
b'{"iss":"joe",\r\n' | ||
b' "exp":1300819380,\r\n' | ||
b' "http://example.com/is_root":true}' | ||
) | ||
self.compact_jws = ( | ||
'eyJhbGciOiJSUzI1NiJ9' | ||
'.' | ||
'eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFt' | ||
'cGxlLmNvbS9pc19yb290Ijp0cnVlfQ' | ||
'.' | ||
'cC4hiUPoj9Eetdgtv3hF80EGrhuB__dzERat0XF9g2VtQgr9PJbu3XOiZj5RZmh7' | ||
'AAuHIm4Bh-0Qc_lF5YKt_O8W2Fp5jujGbds9uJdbF9CUAr7t1dnZcAcQjbKBYNX4' | ||
'BAynRFdiuB--f_nZLgrnbyTyWzO75vRK5h6xBArLIARNPvkSjtQBMHlb1L07Qe7K' | ||
'0GarZRmB_eSN9383LcOLn6_dO--xi12jzDwusC-eOkHWEsqtFZESc6BfI7noOPqv' | ||
'hJ1phCnvWh6IeYI2w9QOYEUipUTI8np6LbgGY9Fs98rqVt5AXLIhWkWywlVmtVrB' | ||
'p0igcN_IoypGlUPQGe77Rw' | ||
) | ||
|
||
def test_encode(self): | ||
compact_jws = self.inst.encode(self.message, self.key, alg='RS256') | ||
self.assertEqual(compact_jws, self.compact_jws) | ||
|
||
def test_decode(self): | ||
message = self.inst.decode(self.compact_jws, self.key) | ||
self.assertEqual(message, self.message) | ||
|
||
def test_decode_pubkey(self): | ||
message = self.inst.decode(self.compact_jws, self.pubkey) | ||
self.assertEqual(message, self.message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright 2017 Gehirn Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import json | ||
from unittest import TestCase | ||
|
||
from jwt.jwk import jwk_from_dict | ||
from jwt.jwt import JWT | ||
|
||
from .helper import load_testdata | ||
|
||
|
||
class JWTTest(TestCase): | ||
|
||
def setUp(self): | ||
self.inst = JWT() | ||
self.key = jwk_from_dict( | ||
json.loads(load_testdata('oct.json', 'r'))) | ||
|
||
self.message = { | ||
'iss': 'joe', | ||
'exp': 1300819380, | ||
'http://example.com/is_root': True, | ||
} | ||
|
||
self.compact_jws = ( | ||
'eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9' | ||
'.' | ||
'eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFt' | ||
'cGxlLmNvbS9pc19yb290Ijp0cnVlfQ' | ||
'.' | ||
'dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk' | ||
) | ||
|
||
def test_decode(self): | ||
message = self.inst.decode(self.compact_jws, self.key) | ||
self.assertEqual(message, self.message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters