(request, secret, algorithm='sha256', is_rsa=False, opts={})
| 1445 | |
| 1446 | @staticmethod |
| 1447 | def jwt(request, secret, algorithm='sha256', is_rsa=False, opts={}): |
| 1448 | algos = { |
| 1449 | 'sha256': hashlib.sha256, |
| 1450 | 'sha384': hashlib.sha384, |
| 1451 | 'sha512': hashlib.sha512, |
| 1452 | } |
| 1453 | alg = ('RS' if is_rsa else 'HS') + algorithm[3:] |
| 1454 | if 'alg' in opts and opts['alg'] is not None: |
| 1455 | alg = opts['alg'] |
| 1456 | header_opts = { |
| 1457 | 'alg': alg, |
| 1458 | 'typ': 'JWT', |
| 1459 | } |
| 1460 | if 'kid' in opts and opts['kid'] is not None: |
| 1461 | header_opts['kid'] = opts['kid'] |
| 1462 | if 'nonce' in opts and opts['nonce'] is not None: |
| 1463 | header_opts['nonce'] = opts['nonce'] |
| 1464 | header = Exchange.encode(Exchange.json(header_opts)) |
| 1465 | encoded_header = Exchange.urlencode_base64(header) |
| 1466 | encoded_data = Exchange.urlencode_base64(Exchange.encode(Exchange.json(request))) |
| 1467 | token = encoded_header + '.' + encoded_data |
| 1468 | algoType = alg[0:2] |
| 1469 | if is_rsa or algoType == 'RS': |
| 1470 | signature = Exchange.base64_to_binary(Exchange.rsa(token, Exchange.decode(secret), algorithm)) |
| 1471 | elif algoType == 'ES': |
| 1472 | rawSignature = Exchange.ecdsa(token, secret, 'p256', algorithm) |
| 1473 | signature = Exchange.base16_to_binary(rawSignature['r'].rjust(64, "0") + rawSignature['s'].rjust(64, "0")) |
| 1474 | elif algoType == 'Ed': |
| 1475 | signature = Exchange.eddsa(token.encode('utf-8'), secret, 'ed25519', True) |
| 1476 | # here the signature is already a urlencoded base64-encoded string |
| 1477 | return token + '.' + signature |
| 1478 | else: |
| 1479 | signature = Exchange.hmac(Exchange.encode(token), secret, algos[algorithm], 'binary') |
| 1480 | return token + '.' + Exchange.urlencode_base64(signature) |
| 1481 | |
| 1482 | @staticmethod |
| 1483 | def rsa(request, secret, alg='sha256'): |
no test coverage detected