def jwt_validator_to_proto( validator: jwt.JwtValidator) -> testing_api_pb2.JwtValidator: """Converts a jwt.JwtValidator into a proto JwtValidator.""" proto_validator = testing_api_pb2.JwtValidator() if validator.has_expected_type_header(): proto_validator.expected_type_header.value = validator.expected_type_header( ) if validator.has_expected_issuer(): proto_validator.expected_issuer.value = validator.expected_issuer() if validator.has_expected_subject(): proto_validator.expected_subject.value = validator.expected_subject() if validator.has_expected_audience(): proto_validator.expected_audience.value = validator.expected_audience() proto_validator.ignore_type_header = validator.ignore_type_header() proto_validator.ignore_issuer = validator.ignore_issuer() proto_validator.ignore_subject = validator.ignore_subject() proto_validator.ignore_audience = validator.ignore_audiences() proto_validator.allow_missing_expiration = validator.allow_missing_expiration( ) proto_validator.clock_skew.seconds = validator.clock_skew().seconds if validator.has_fixed_now(): seconds, nanos = split_datetime(validator.fixed_now()) proto_validator.now.seconds = seconds proto_validator.now.nanos = nanos return proto_validator
def jwt_validator_to_proto( validator: jwt.JwtValidator) -> testing_api_pb2.JwtValidator: """Converts a jwt.JwtValidator into a proto JwtValidator.""" proto_validator = testing_api_pb2.JwtValidator() if validator.has_issuer(): proto_validator.issuer.value = validator.issuer() if validator.has_subject(): proto_validator.subject.value = validator.subject() if validator.has_audience(): proto_validator.audience.value = validator.audience() proto_validator.clock_skew.seconds = validator.clock_skew().seconds if validator.has_fixed_now(): proto_validator.now.seconds = int(validator.fixed_now().timestamp()) return proto_validator
def test_jwt_validator_to_proto(self): now = datetime.datetime.fromtimestamp(1234567, datetime.timezone.utc) validator = jwt.new_validator( issuer='issuer', subject='subject', audience='audience', clock_skew=datetime.timedelta(seconds=123), fixed_now=now) proto = _primitives.jwt_validator_to_proto(validator) expected = testing_api_pb2.JwtValidator() expected.issuer.value = 'issuer' expected.subject.value = 'subject' expected.audience.value = 'audience' expected.clock_skew.seconds = 123 expected.now.seconds = 1234567 self.assertEqual(proto, expected)
def test_jwt_validator_to_proto(self): now = datetime.datetime.fromtimestamp(1234567.125, datetime.timezone.utc) validator = jwt.new_validator( expected_type_header='type_header', expected_issuer='issuer', expected_audience='audience', clock_skew=datetime.timedelta(seconds=123), fixed_now=now) proto = _primitives.jwt_validator_to_proto(validator) expected = testing_api_pb2.JwtValidator() expected.expected_type_header.value = 'type_header' expected.expected_issuer.value = 'issuer' expected.expected_audience.value = 'audience' expected.clock_skew.seconds = 123 expected.now.seconds = 1234567 expected.now.nanos = 125000000 self.assertEqual(proto, expected)
def test_empty_jwt_validator_to_proto(self): validator = jwt.new_validator() proto = _primitives.jwt_validator_to_proto(validator) expected = testing_api_pb2.JwtValidator() expected.clock_skew.seconds = 0 self.assertEqual(proto, expected)