def jws_sign(account_id=None, account_kid=None, jws_object=None, jwk_object=None, jwk_public_json=None, alg="ES256"): """ Signs JWS with JWK. :param account_id: User account ID :param account_kid: Key ID for user's key :param jws_object: JWS object :param jwk_object: JWK object :param jwk_public_json: JSON presentation of public part of JWK :param alg: Signature algorithm to use, Defaults to ES256 :return: Signed JWS object """ if account_id is None: raise AttributeError("Provide account_id as parameter") if account_kid is None: raise AttributeError("Provide account_kid as parameter") if jws_object is None: raise AttributeError("Provide jws_object as parameter") if jwk_object is None: raise AttributeError("Provide jwk_object as parameter") if jwk_public_json is None: raise AttributeError("Provide jwk_public_json as parameter") if alg is None: raise AttributeError("Provide alg as parameter") try: unprotected_header = {'kid': account_kid, 'jwk': json.loads(jwk_public_json)} protected_header = {'alg': alg} unprotected_header_json = json.dumps(unprotected_header) protected_header_json = json.dumps(protected_header) except Exception as exp: exp = append_description_to_exception(exp=exp, description='Could not create headers') logger.error('Could not create headers: ' + repr(exp)) raise else: logger.info("Created headers") log_dict_as_json(data=unprotected_header) log_dict_as_json(data=protected_header) try: logger.debug("Signing JWS with following") log_dict_as_json(lineno=get_current_line_no(), data={'jws_object': repr(jws_object.__dict__)}) log_dict_as_json(lineno=get_current_line_no(), data={'alg': alg}) log_dict_as_json(lineno=get_current_line_no(), data={'unprotected_header_json': unprotected_header}) log_dict_as_json(lineno=get_current_line_no(), data={'protected_header_json': protected_header}) jws_object.add_signature(jwk_object, alg=alg, header=unprotected_header_json, protected=protected_header_json) except Exception as exp: exp = append_description_to_exception(exp=exp, description='Could not sign JWS with JWK') logger.error('Could not sign JWS with JWK: ' + repr(exp)) #log_dict_as_json(data={'msg': 'Could not sign JWS with JWK', 'unprotected_header': unprotected_header, 'protected_header': protected_header}) raise else: logger.info("Signed JWS with JWK") logger.debug("Signed jws_object: " + str(jws_object.__dict__)) return jws_object
def jws_generate(payload=None): if payload is None: raise AttributeError("Provide payload as parameter") payload_json = json.dumps(payload) logger.debug('payload_json: ' + payload_json) try: jws_object = jws.JWS(payload=payload_json) except Exception as exp: exp = append_description_to_exception(exp=exp, description='Could not generate JWS object with payload') logger.error('Could not generate JWS object with payload: ' + repr(exp)) log_dict_as_json(data={'payload': repr(payload)}) raise else: logger.debug('jws_object: ' + str(jws_object)) log_dict_as_json(lineno=get_current_line_no(), data={'jws_object': jws_object.__dict__}) logger.info('JWS object created') return jws_object