def validate(self, attrs): request = self.context.get("request") client_ip, is_routable = get_client_ip(request) is_valid, instance = ScopedAPIKey.objects.is_valid_with_logging( api_key=attrs['api_key'], extra={'ip': client_ip}) if not is_valid or not instance: raise AuthenticationFailed('Api key has expired or is invalid') jwt_backend_class = JWTBackendRegistry.get_instance().get_backend( instance.jwt_backend_name) if not jwt_backend_class: raise AuthenticationFailed( 'Unknown jwt backend could not authenticate') backend = jwt_backend_class() return backend.make_authenticate_success_response( base_payload={ **instance.base_jwt_payload, 'api_key_id': instance.id })
def ready(self): from ievv_auth.ievv_jwt.backends.backend_registry import JWTBackendRegistry from ievv_auth.ievv_jwt.backends.api_key_backend import ApiKeyBackend registry = JWTBackendRegistry.get_instance() registry.set_backend(ApiKeyBackend)
def test_unknown_backend(self): registry = JWTBackendRegistry.get_instance() backend_class = registry.get_backend('unknown') self.assertIsNone(backend_class)
def test_sanity(self): registry = JWTBackendRegistry.get_instance() backend_class = registry.get_backend('api-key') self.assertEqual(backend_class, ApiKeyBackend)
def setUp(self): registry = JWTBackendRegistry.get_instance() registry.set_backend(ApiKeyBackend)
def __init__(self, *args, **kwargs): super(ScopedAPIKeyForm, self).__init__(*args, **kwargs) self.fields[ 'jwt_backend_name'].choices = JWTBackendRegistry.get_instance( ).get_backend_choices()