work on auth

This commit is contained in:
Tobias Oberstein
2014-09-01 20:29:43 +02:00
parent 7ed576f02a
commit 1edbb1c141
4 changed files with 71 additions and 15 deletions

View File

@@ -30,7 +30,9 @@ test:
USE_TWISTED=1 trial autobahn
test1:
USE_TWISTED=1 python -m pytest -s -v autobahn/wamp/test/test_router.py
USE_TWISTED=1 trial autobahn.wamp.test.test_auth
# USE_TWISTED=1 python -m pytest -s -v autobahn/wamp/test/test_auth.py
# USE_TWISTED=1 python -m pytest -s -v autobahn/wamp/test/test_router.py
# USE_ASYNCIO=1 python -m pytest -s -v autobahn/wamp/test/test_router.py
test2:

View File

@@ -102,6 +102,9 @@ _pack_int = Struct('>I').pack
def pbkdf2_bin(data, salt, iterations = 1000, keylen = 32, hashfunc = None):
"""
Compute
"""
hashfunc = hashfunc or hashlib.sha256
mac = hmac.new(data, None, hashfunc)
def _pseudorandom(x, mac=mac):
@@ -142,21 +145,17 @@ def derive_key(secret, salt, iterations = None, keylen = None):
def generate_wcs(short = False):
def generate_wcs(length = 12):
"""
Generates a new random secret string for use with WAMP-CRA.
:param short: If ``True``, generate string of length 6, else 12
:type short: bool
:param length: The length of the secret to generate.
:type length: int
:return: The generated secret.
:rtype: str
:rtype: unicode
"""
if short:
l = 6
else:
l = 12
return ''.join([random.choice("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_") for _ in range(l)])
return u"".join([random.choice(u"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_") for _ in range(length)])
@@ -166,12 +165,14 @@ def compute_wcs(key, challenge):
challenge and a (derived) key.
:param key: The key derived (via PBKDF2) from the secret.
:type key: str
:type key: bytes
:param challenge: The authentication challenge to sign.
:type challenge: str
:type challenge: bytes
:return: The authentication signature.
:rtype: str
:rtype: unicode
"""
assert(type(key) == bytes)
assert(type(challenge) == bytes)
sig = hmac.new(key, challenge, hashlib.sha256).digest()
return binascii.b2a_base64(sig).strip().decode('ascii')

View File

@@ -0,0 +1,53 @@
###############################################################################
##
## Copyright (C) 2014 Tavendo GmbH
##
## 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.
##
###############################################################################
from __future__ import absolute_import
#from twisted.trial import unittest
import unittest
import json
from autobahn.wamp.auth import generate_wcs, \
compute_wcs
class TestWampCra(unittest.TestCase):
def test_generate_wcs_default(self):
secret = generate_wcs()
self.assertEqual(type(secret), unicode)
self.assertEqual(len(secret), 12)
def test_generate_wcs_length(self):
length = 30
secret = generate_wcs(length)
self.assertEqual(type(secret), unicode)
self.assertEqual(len(secret), length)
def test_compute_wcs(self):
secret = u'L3L1YUE8Txlw'
challenge = json.dumps([1,2,3])
signature = compute_wcs(secret.encode('ascii'), challenge)
self.assertEqual(signature, u"1njQtmmeYO41N5EWEzD2kAjjEKRZ5kPZt/TzpYXOzR0=")
if __name__ == '__main__':
unittest.main()

View File

@@ -12,7 +12,7 @@
<script>
console.log("Ok, Autobahn loaded", autobahn.version);
if (true) {
if (false) {
var user = "peter";
var key = autobahn.auth_cra.derive_key("secret1", "salt123");
} else {
@@ -61,7 +61,7 @@
};
connection.onclose = function (reason, details) {
console.log("disconnected", reason, details.reason);
console.log("disconnected", reason, details.reason, details);
}
connection.open();