Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'AuthenticatorConverterRegistry' object has no attribute 'get_security_schemes_legacy' #260

Open
mattcarp12 opened this issue Nov 4, 2021 · 5 comments
Labels
bug Something isn't working

Comments

@mattcarp12
Copy link

I have a custom authenticator that works, but I am trying to get the swagger docs to work as well but I keep getting the above exception.

Here is my custom authenticator:

class CustomAuthenticator(authenticators.Authenticator):
    def authenticate(self):
        if current_user and current_user.is_authenticated:
            pass
        elif current_app.config["AUTH_OFF"]:
            try:
                login_user(default_user)
            except AttributeError:
                raise ValueError("Trying to log into user that doesn't exist!")

This part I took directly from the flask_rebar docs:

class SwaggerAuthConverter(AuthenticatorConverter):
    AUTHENTICATOR_TYPE = CustomAuthenticator

    def get_security_schemes(self, obj, context):
        return {
            obj.name: {sw.type_: sw.api_key, sw.in_: sw.header, sw.name: obj.header}
        }

    def get_security_requirements(self, obj, context):
        return [{obj.name: []}]


custom_auth_registry = AuthenticatorConverterRegistry()
custom_auth_registry.register_type(SwaggerAuthConverter())

Finally, this is where I set up the app:

rebar = Rebar()
swagger_generator = SwaggerV2Generator(authenticator_converter_registry=custom_auth_registry)
registry = rebar.create_handler_registry(swagger_generator=swagger_generator)
registry.set_default_authenticator(CustomAuthenticator)

Here is the full backtrace:

127.0.0.1 - - [04/Nov/2021 09:33:13] "GET /swagger HTTP/1.0" 500 -
Traceback (most recent call last):
  File "/home/canopy/canopy/venv/lib/python3.6/site-packages/flask/app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/canopy/canopy/venv/lib/python3.6/site-packages/flask/app.py", line 2450, in wsgi_app
    response = self.handle_exception(e)
  File "/home/canopy/canopy/venv/lib/python3.6/site-packages/flask/app.py", line 1867, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/canopy/canopy/venv/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/canopy/canopy/venv/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/canopy/canopy/venv/lib/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/canopy/canopy/venv/lib/python3.6/site-packages/flask/app.py", line 1822, in handle_user_exception
    return handler(e)
  File "/home/canopy/canopy/venv/lib/python3.6/site-packages/flask_rebar/rebar.py", line 831, in handle_generic_error
    raise error
  File "/home/canopy/canopy/venv/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/canopy/canopy/venv/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/canopy/canopy/venv/lib/python3.6/site-packages/flask_rebar/rebar.py", line 612, in get_swagger
    registry=self, host=request.host_url.rstrip("/")
  File "/home/canopy/canopy/venv/lib/python3.6/site-packages/flask_rebar/swagger_generation/swagger_generator_v2.py", line 100, in generate_swagger
    return self.generate(registry=registry, host=host)
  File "/home/canopy/canopy/venv/lib/python3.6/site-packages/flask_rebar/swagger_generation/swagger_generator_v2.py", line 131, in generate
    self.authenticator_converter.get_security_schemes(authenticator)
  File "/home/canopy/canopy/venv/lib/python3.6/site-packages/flask_rebar/swagger_generation/authenticator_to_swagger.py", line 168, in get_security_schemes
    return self.get_security_schemes_legacy(registry=authenticator)
AttributeError: 'AuthenticatorConverterRegistry' object has no attribute 'get_security_schemes_legacy'
@barakalon
Copy link
Contributor

This certainly looks like a bug.

I'm not sure what get_security_schemes_legacy is.

https://github.com/plangrid/flask-rebar/blob/master/flask_rebar/swagger_generation/authenticator_to_swagger.py#L166

I'm not sure how that was ever supposed to work. @airstandley - do you know what's going on here?

@airstandley
Copy link
Contributor

Seems like a bug to me. It was original a legacy method marked for deprecation

def get_security_schemes_legacy(self, registry):

@airstandley
Copy link
Contributor

Definitely a bug. We missed removing those lines in #216 😭

@airstandley airstandley added bug Something isn't working good first issue Good for newcomers labels Nov 9, 2021
@airstandley
Copy link
Contributor

🤔 I think this might also be an instance vs class issue.
@mattcarp12 Do you still get an error with

rebar = Rebar()
swagger_generator = SwaggerV2Generator(authenticator_converter_registry=custom_auth_registry)
registry = rebar.create_handler_registry(swagger_generator=swagger_generator)
registry.set_default_authenticator(CustomAuthenticator())

@airstandley airstandley removed the good first issue Good for newcomers label Nov 9, 2021
@mattcarp12
Copy link
Author

@airstandley Thank you for your help with this.

I did get your above code to work. However I needed to add name and header attributes to my CustomAuthenticator class, since I am inheriting from the base class that does not include those.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants