def raw_jwt_to_proto(raw_jwt: jwt.RawJwt) -> testing_api_pb2.JwtToken: """Converts a jwt.RawJwt into a proto.""" raw_token = testing_api_pb2.JwtToken() if raw_jwt.has_issuer(): raw_token.issuer.value = raw_jwt.issuer() if raw_jwt.has_subject(): raw_token.subject.value = raw_jwt.subject() if raw_jwt.has_audiences(): raw_token.audiences.extend(raw_jwt.audiences()) if raw_jwt.has_jwt_id(): raw_token.jwt_id.value = raw_jwt.jwt_id() if raw_jwt.has_expiration(): raw_token.expiration.seconds = int(raw_jwt.expiration().timestamp()) if raw_jwt.has_not_before(): raw_token.not_before.seconds = int(raw_jwt.not_before().timestamp()) if raw_jwt.has_issued_at(): raw_token.issued_at.seconds = int(raw_jwt.issued_at().timestamp()) for name in raw_jwt.custom_claim_names(): value = raw_jwt.custom_claim(name) if value is None: raw_token.custom_claims[ name].null_value = testing_api_pb2.NULL_VALUE if isinstance(value, (int, float)): raw_token.custom_claims[name].number_value = value if isinstance(value, Text): raw_token.custom_claims[name].string_value = value if isinstance(value, bool): raw_token.custom_claims[name].bool_value = value if isinstance(value, dict): raw_token.custom_claims[name].json_object_value = json.dumps(value) if isinstance(value, list): raw_token.custom_claims[name].json_array_value = json.dumps(value) return raw_token
def verifiedjwt_to_proto( verified_jwt: jwt.VerifiedJwt) -> testing_api_pb2.JwtToken: """Converts a jwt.VerifiedJwt into a proto JwtToken.""" token = testing_api_pb2.JwtToken() if verified_jwt.has_issuer(): token.issuer.value = verified_jwt.issuer() if verified_jwt.has_subject(): token.subject.value = verified_jwt.subject() if verified_jwt.has_audiences(): token.audiences.extend(verified_jwt.audiences()) if verified_jwt.has_jwt_id(): token.jwt_id.value = verified_jwt.jwt_id() if verified_jwt.has_expiration(): token.expiration.seconds = _to_timestamp(verified_jwt.expiration()) if verified_jwt.has_not_before(): token.not_before.seconds = _to_timestamp(verified_jwt.not_before()) if verified_jwt.has_issued_at(): token.issued_at.seconds = _to_timestamp(verified_jwt.issued_at()) for name in verified_jwt.custom_claim_names(): value = verified_jwt.custom_claim(name) if value is None: token.custom_claims[name].null_value = testing_api_pb2.NULL_VALUE elif isinstance(value, bool): token.custom_claims[name].bool_value = value elif isinstance(value, (int, float)): token.custom_claims[name].number_value = value elif isinstance(value, Text): token.custom_claims[name].string_value = value elif isinstance(value, dict): token.custom_claims[name].json_object_value = json.dumps(value) elif isinstance(value, list): token.custom_claims[name].json_array_value = json.dumps(value) else: raise ValueError('claim %s has unknown type' % name) return token