Hi,
I have a few questions on API's and authentication, possibly to do with the new Airlock package, but I'm really just looking for some general advice on the best way to authenticate my API, I'm new to this, so bare with me!
I have a platform where my customers sign in at clubs.domain.test, and their customers sign in at my.domain.test. Both subdomains are going to start to utilise an API at api.domain.test, neither of the apps on clubs. and my. are SPAs, they are good ol' server apps routed through and view rendered by laravel, and you sign in the traditional way. However, some of the resource index pages will use Vue to call on the API and gather data to be displayed on these pages.
I've currently implemented authentication of the API requests using the really basic way outlined at https://laravel.com/docs/6.x/api-authentication. All users have a pre-generated API token that is un-hashed. When a user is signed in their token is passed into a javascript object that's placed on the window so it can be assigned to a default axios header as the "Bearer" of each request. On the backend I just use the auth:api middleware provided by laravel to authenticate the routes, as well as the fruitcake/laravel-cors package with default config for CORS. I'll be honest, this works fine, but it niggles me that having API tokens feels overkill for my situation and I'd prefer to not expose the users API token client side.
Couple of initial questions:
- Are there any major security vulnerabilities with storing un-hashed tokens and passing these client side the way I've described above?
- And is there a better way I could be authenticating my API for such a basic basic set-up or should I be satisfied with this implementation?
These thoughts have lead me onto attempting to install Airlock and (what I think will be) improving my API authentication. I saw Airlock and thought I'd much rather authenticate using a method that is preconfigured in laravel (cookie based session auth), without having to store API tokens and have them exposed on the client. I've followed the guide at https://laravel.com/docs/master/airlock and on Mohamed's website https://divinglaravel.com/authentication-and-laravel-airlock, both great resources that are really well written and easy to follow. To confirm the set-up:
In my env file I have AIRLOCK_STATEFUL_DOMAINS="clubs.domain.test,my.domain.test".
In my Kernel I have the EnsureFrontendRequestsAreStateful middleware in the api group.
I have the fruitcake/laravel-cors package installed and "supports_credentials" set to true.
In my JS I have window.axios.defaults.withCredentials = true;.
In my env file I have SESSION_DOMAIN=".domain.test".
In my api routes file I've set my route group middleware to auth:airlock.
With all these settings configured as per the guides I can't seem to get passed the initial OPTIONS request, the GET/POST request isn't getting performed, which I guess points to a CORS problem?
Questions on Airlock:
- Could this (CORS) issue be down to the fact that I'm not performing the get initial request to the "/airlock/csrf-cookie" endpoint as my users sign in the traditional way with a full server request rather than through a SPA, or is that unrelated?
- Ideally, I don't want to configure the session cookie domain to support any subdomain as I don't want a user who signs into the clubs. subdomain to also be signed into the my. subdomain. Is Airlock un-usable without the session cookie domain configuration supporting any subdomain?
- Am I wasting my time with Airlock and is it overkill for such a simple set-up?
Thanks for looking and reading (if you've made it this far!),
Sam