MasoniteFramework / api

Masonite API package currently in development
MIT License
8 stars 4 forks source link

APIGuard usage in Masonite>=3.0 #20

Open nicolaipre opened 3 years ago

nicolaipre commented 3 years ago

What is the current status on APIs in Masonite? Is the current https://github.com/masoniteframework/api/tree/3.0 successfully up to date and useable with versions of Masonite 3?

I am currently using masonite-api==3.0.0, which I assume is compatible with Masonite 3.0.

When changing AUTH_GUARD to api in .env, and having imported ApiProvider in config/providers.py under # Third Party Providers (as stated in the docs for 2.3) I run into the following issue:

$ craft serve
Guards registered:
{'web': <class 'masonite.auth.guards.WebGuard.WebGuard'>}
------------------
Traceback (most recent call last):
  File "/Users/user/tmp/new-backend/venv/bin/craft", line 5, in <module>
    from masonite.cli import application
  File "/Users/user/tmp/new-backend/venv/lib/python3.9/site-packages/masonite/cli.py", line 16, in <module>
    from wsgi import container
  File "/Users/user/tmp/new-backend/wsgi.py", line 43, in <module>
    container.resolve(provider.boot)
  File "/Users/user/tmp/new-backend/venv/lib/python3.9/site-packages/masonite/app.py", line 227, in resolve
    return obj(*objects)
  File "/Users/user/tmp/new-backend/venv/lib/python3.9/site-packages/masonite/providers/AuthenticationProvider.py", line 20, in boot
    auth.set(config("auth.auth.defaults.guard"))
  File "/Users/user/tmp/new-backend/venv/lib/python3.9/site-packages/masonite/auth/guards/Guard.py", line 59, in set
    return self.make(key)
  File "/Users/user/tmp/new-backend/venv/lib/python3.9/site-packages/masonite/auth/guards/Guard.py", line 37, in make
    raise DriverNotFound("Could not find the guard: '{}'".format(key))
masonite.exceptions.DriverNotFound: Could not find the guard: 'api'

I noticed this happens because the AuthenticationProvider (which is imported earlier than ApiProvider) attempts to set the default guard before it is actually registered. I tried to bypass this problem by moving AuthenticationProvider below ApiProvider to force register it before it is being set by AuthentcationProvider, which results in the following error:

$ craft serve
Guards registered:
{'web': <class 'masonite.auth.guards.WebGuard.WebGuard'>, 'api': <class 'masonite.api.guards.APIGuard.APIGuard'>}
------------------
Traceback (most recent call last):
  File "/Users/user/tmp/new-backend/venv/bin/craft", line 5, in <module>
    from masonite.cli import application
  File "/Users/user/tmp/new-backend/venv/lib/python3.9/site-packages/masonite/cli.py", line 16, in <module>
    from wsgi import container
  File "/Users/user/tmp/new-backend/wsgi.py", line 43, in <module>
    container.resolve(provider.boot)
  File "/Users/user/tmp/new-backend/venv/lib/python3.9/site-packages/masonite/app.py", line 227, in resolve
    return obj(*objects)
  File "/Users/user/tmp/new-backend/venv/lib/python3.9/site-packages/masonite/providers/AuthenticationProvider.py", line 20, in boot
    auth.set(config("auth.auth.defaults.guard"))
  File "/Users/user/tmp/new-backend/venv/lib/python3.9/site-packages/masonite/auth/guards/Guard.py", line 59, in set
    return self.make(key)
  File "/Users/user/tmp/new-backend/venv/lib/python3.9/site-packages/masonite/auth/guards/Guard.py", line 34, in make
    self._guard = self.app.resolve(self.guards[key])
  File "/Users/user/tmp/new-backend/venv/lib/python3.9/site-packages/masonite/app.py", line 192, in resolve
    param = self._find_annotated_parameter(value)
  File "/Users/user/tmp/new-backend/venv/lib/python3.9/site-packages/masonite/app.py", line 317, in _find_annotated_parameter
    raise ContainerError(
masonite.exceptions.ContainerError: The dependency with the request: masonite.request.Request annotation could not be resolved by the container

I suspect the last error is due to changes in the Request handler as stated in the documented changes in 3.0: https://docs.masoniteproject.com/upgrade-guide/masonite-2.3-to-3.0#request-init-offset

Are there any plans to further update this repository, or will everything be reworked in Masonite 4?

nicolaipre commented 3 years ago

I found the problem.

Some parts of the code did not have self.app.make("Request") instead of self.request.

This seems to be present in the following files:

masonite/api/resources/Resource.py:        auth = self.request.app().make(Auth)
masonite/api/resources/Resource.py:                response = self.request.app().resolve(getattr(self, 'create'))
masonite/api/resources/Resource.py:                response = self.request.app().resolve(getattr(self, 'show'))
masonite/api/resources/Resource.py:                response = self.request.app().resolve(getattr(self, 'index'))
masonite/api/resources/Resource.py:                response = self.request.app().resolve(getattr(self, 'update'))
masonite/api/resources/Resource.py:                response = self.request.app().resolve(getattr(self, 'delete'))
masonite/api/resources/Resource.py:            self.request.status(404)
masonite/api/resources/Resource.py:            record = self.model.create(self.request.all())
masonite/api/authentication/BaseAuthentication.py:            return self.request.app().resolve(self.authenticate)
masonite/api/guards/APIJwtDriver.py:        self.request.cookie('token', token)
masonite/api/guards/APIJwtDriver.py:        self.request.delete_cookie('token')
masonite/api/guards/APIJwtDriver.py:        self.request.reset_user()
masonite/api/guards/APIGuard.py:                self.request.set_user(model)

I also noticed that AuthCookieDriver in masonite framework has been updated, while AuthJwtDriver has not.

See

masonite/drivers/authentication/AuthJwtDriver.py