In this post I will show you how you can use PKCE(Proof Key for Code Exchange) for authentication. I will use Nuxt.js, because that's what I use in...
For further actions, you may consider blocking this person and/or reporting abuse
Hi,
SOLVED:
Everything work fine, but now I get
When logging in, when returning to nuxt auth.vue I get:
x-access-token undefined localhost / Session 23
Is it a backend problem?
Problem was the client id in the frontend that was wrong in two files. login and auth
Hi, when i want to retrive data (articles) when i'm loggedIn i get this error:
Access to XMLHttpRequest at 'domain.com/api/trials/' from origin 'app.domain.com/' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.
// nuxt -> index.vue (works local but not on the domains)
async asyncData({ $axios }) {
const trials = await $axios.$get(process.env.LARAVEL_ENDPOINT + "/api/trials/");
return { trials };
},
......
// laravel api route /api/trials
Route::group(['middleware' => 'cors'], function()
{
Route::group(['middleware' => ['auth:api']], function () {
Route::resource('trials', 'API\TrialController')
->only(['index', 'store', 'show', 'edit', 'update','destroy']);
});
.......
// trialcontroller
class TrialController extends Controller
{
public function __construct()
{
$this->middleware('auth:api');
}
public function index()
{
$trials = Auth::user()->parentTrials()->get();
........
If you are using laravel version 6.x, then you should add CORS middleware, if you are using laravel 7.x, you should just setup the CORS that's already there.
I use this in the backend: "fruitcake/laravel-cors": "^2.0",
I can retrive data with postman if I use the header: Authorization: Bearer eyJ0eXAiOiJKV1Q.....
Do i need to set some headers to the nuxt request?
async asyncData({ $axios }) {
const trials = await $axios.$get(process.env.LARAVEL_ENDPOINT + "/api/trials/");
return { trials };
},
if yes how? i see many others have the same problem
You need to send Bearer header with every request
Do you have an example?
I'm trying with this but I cant get the token
async asyncData({ $axios }) {
async asyncData (context) {
const trials = await $axios.$get(process.env.LARAVEL_ENDPOINT + "/api/trials/", {}, { headers: {"Authorization" :
Bearer ${context.app.$auth.getToken('local')}
} })I can se the cookie x-access-token in chrome developer tool under application
Now I got the access_token:
const access_token = cookies.get('x-access-token');
console.log(access_token);
But still no content, only errors:
Access to XMLHttpRequest at 'domain.com/api/trials/' from origin 'app.domain.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.
cf5ce039ccd82a3e879b.js:1 GET domain.com/api/trials/ net::ERR_FAILED
Request URL: domain.com/api/trials/
Referrer Policy: no-referrer-when-downgrade
Provisional headers are shown
You should do something like this
this.$axios.setToken(access_token, token_type);
Hi again,
Now I need to "update a Post" and the error comes again, but only on the domain not local:
Console error:
Access to XMLHttpRequest at 'domain.com/api/trials/' from origin 'app.domain.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.
600bbbde120028ba59aa.js:1 POST domain.com/api/trials/ net::ERR_FAILED
Laravel log error:
[2020-06-22 16:05:59] local.ERROR: The resource owner or authorization server denied the request. {"exception":"object
[stacktrace].....\TokenGuard.php(149): Laravel\Passport\Guards\TokenGuard->getPsrRequestViaBearerToken
My update submit:
UpdateForm(key) {
event.preventDefault();
var app = this;
You need to setup the correct domain for the CORS
But it works for GET (posts) and get users but not update content (post)
See if the post request has been enabled for CORS
how can I see that? i'm new to cors, its hard to debug when it works local but not on the server
Read the documentation for fruitcake/laravel-cors
When I read the docs I can't find info on what to change to get post to work . Do you know where to se an example on a Update with "post" have been used, with Nuxt/Passport? almost all examples are just GET
Now it works. :)
My post looks like:
I also removed a / from /api/trials (is was /api/trials/)
I will try later to remove some of the code to see if it all have to be there.
Thank you man! You saved my night. The league oauth2 server used hash() function as binary output. But most of JS examples can not produce as I expected. I was stucked at creating codeVerifier as they follows. Your solution saved me!
glad I could help
Firstly, Thank you for your great work!
I've been searching for a long time on how to authenticate Vue SPAs with Laravel and found Laravel Passport but it seems that Laravel Passport wasn't made for this purpose!
and PKCE is breaking the UX, actually, this prompt is a bit confuses the users.
I'm looking for something simple for SPAs.
I found an alternative which is Sanctum, I read the intro about it in the docs and found that Scantum was mainly built for SPA.
My question about best practices and security, do you recommend using Sanctum instead?
If your both applications are on the same top-level domain, yes, it's best to use Sanctrum. If they are not on the same top-level domain, you can't use Sanctrum.
Thanks for informative post.
I wanted to know if front and back on two domains, can't we use sanctum laravel.com/docs/7.x/sanctum#api-t... api token authentication?
You can, but I haven't used it yet. I think it's not as flexible as passport.
Thank you for the fast response <3
Why not using @nuxtjs/auth-next module ?
If it's not flexible enough, you can make your own scheme like this:
schemes/laravelPassport.js
:And then use it like this in
nuxt.config.js
:What you did here is almost the same as if you have done it custom, without using the nuxtjs/auth-next module. So why should we introduce another package in our project if the code is similar?!
With your method here, I'm logged in the Laravel backend.
Is it an expected result ? Is there a way to prevent from logging in the backend ?
I have the same issue with @nuxtjs/auth (and @nuxtjs/auth-next module).
Maybe it's something I'm misunderstanding about using Laravel Passport.
You must be logged in the backend if you want to make a request to the backend.
Also, why not using Passport Grant Token ?
We are using Passport Grant Token
Sorry to disturb again.
I'm still having problems with, how to get the users info (like name) to show on page (like in the footer). If reloading the webapp I still have the access token but the name I set with this.$auth.setUser(resp.name) is gone. Do you have and example on how to get the webapp to save the user name on reload? I can do it with a cookie, but think that its not safe.
You can use the cookies to persist the state or use some package that does that. Don't worry about security, because the user name is not something that should not be publicly visible.
Can other users not just change the userId in the local cookie to something else and then get other users info from the api? In my case todos
Well, if you've set up your back-end properly, they won't be able to do that
i have a question. so this pkce thing that u make can only be used by user_id 1?
I assume that you are talking about client_id? Or am I wrong?
Which user ID should the client be assigned to? -> 1
you can put any id there
Ya. So that client_id only belong to user_id 1 am i right? So for each user need to create 1 client_id for this pkce thing?
no, you will need just one client
So in db there is user_id 1. That part will be ignored by laravel?
how do i login with pkce to user_id 2?
the login flow is the same for every user
Hi, one more question
I'm new to passport so what about all the auth tokens in the db? I can see many of them, Are they delete automatic by Laravel passport?
SELECT * FROM
oauth_access_tokens
5fea03842964060c10590e072ce5571478c4270705caf5c447...
22
1
authToken
[]
0
2020-05-14 11:41:19
2020-05-14 11:41:19
2021-05-14 11:41:19
874657bfbafafa674aeac5f5c01b967fa7b95dff71ef4c2c1e...
22
1
.................
No, they are not deleted automatically
Thank you for the excellent tutorial. This is the best tutorial I have found of using Passports PKCE functionality and I would have not been able to figure it out myself. I was able to port your solution today into a React front-end with Laravel back-end.
I just have a few questions about using this technique if you have time.
1) If the OAuth2 server knows when the access token expires and therefore won't allow access of an expired token, it seems that its fine to store the access token not in a cookie since our main protection is the quick timeout? (I'm planning on using Redux which is basically session)
2) Does a registration page necessarily now need to send a user to the PKCE flow after registration rather than issue a token using password grant on initial registration?
3) Does PKCE matter for the refresh token exchange? As in, after we have gone to all this trouble to get the access token through PKCE do we shoot ourselves in the foot if we are not using the same precaution on a subsequent refresh token?
I'm always storing my short-lived
access_token
in the cookie, and my long-livedrefresh_token
in thehttponly
cookie, also Laravel has a CSRF protection out of the box. That way I'm protected from potential XSS and CSRF attacks.I think you must use the PKCE flow, because the
client_id
s are not the same. But I've never tested this, maybe you could try it out and comment here if you could make it work.No, we are having a separate route in which we're refreshing out token. My
refresh_token
s expiry time is 10 days, but the userrefresh_token
is newing up every time newaccess_token
is requested. So the user will have to go through the whole PKCE flow if they weren't active for 10 days straight.So I have been reading RFCs today...because quarantine and I am actually attempting to understand what "the standard" is. I think the best documents are currently OAuth 2.0 for Browser-Based Apps and Proof Key for Code Exchange by OAuth Public Clients. The interesting part (section 6) in the best practices document (1st link) there is great discussion about choosing an OAuth2 solution based off the architecture you're working with, which makes a lot of sense. Unlocking this critical aspect essentially answered some of my questions.
The most important being to realize that if your architecture and requirements don't "need" redirects then there is probably a more secure way to accomplish the task without PKCE, essentially that PKCE would be a less secure option if one can get it done without needing redirects. Maybe that was super obvious but I was just assuming that PKCE is the new standard so everyone needs to switch to it which was causing me to try to jam all the concepts together.
1) Regarding Storing Tokens. I think you were very close (if not 100%) to what is suggested in the best practices with your previous post Secure authentication in Nuxt SPA with Laravel as back-end
2) Regarding User Registration. Based off the above logic an application that also worries about initial user registration probably is in a position architecturally to not need PKCE so it is out of bounds.
3) Regarding Refresh Tokens and PKCE. If one is truly working with a public client (that you really need PKCE) it is the case that issuing refresh tokens is indeed more risk than the access token. In my opinion the separate time expiry you suggested would not increase security, but there are some interesting suggestions for refresh tokens with PKCE in the best practices, including not using them, or using a decreasing expiration refresh token that still needs to be reacquired every 24 hours.
Yes, the part with decreasing the
refresh_token
expiration time is very interesting, and I might try that.Hi! Very helpful tutorial!
The only inconvenient that I have is that the session starts in the login, and when I tried to revoke the token (the route is under auth:api middleware) the session is not over and when i triy to login again, it skips the login form and jumps to the authorization prompt or just to callback page. I tried to create a route in the web middleware which kills the session but always stores the cookie 'laravel_session' and 'XSRF-TOKEN' and can't delete them.
Did this happen to you? Thanks in advance.
How do you revoke the token?
Yes, not only using
but also like indicates the docs:
Laravel - revoking tokens
This is not working for me because uses the
laravel_session
cookie data to persist login and return a newaccess_token
without ask for credentials again, redirecting to the callback page directly.Laravel destroy the session after a while or when the browser is closed but it's a problem when I want to change user to login because I have to wait or close everything.
Maybe the problem is the session based login, but there is no much info about it.
I would like to know if it has happened to you and if anyone could solve it.
Sorry about my english, is not my mother tongue. And thanks again!
Maybe you should try to revoke the token and clear the users session, maybe that will do it. But I don't know if this is the right way to logout some user...
After several trials, I came up with a solution (not an elegant one I guess) that works.
It's a mix from logout from the API guard (
api.php
routes withauth:api
middleware), revoking the token:And in the web guard (
web.php
routes), kill the session:In the frontend I send an axios post request to the
logoutAPI
route and then call thelogoutSession
route. Here is the code using the@nuxtjs/auth-next
module.This way, every time I logout from the app and login again, the credentials are required and doesn't persists.
Thanks for your replies, I hope this helps someone!
Hi again;
Now im at: logout
I can logout on the frontend but the backend (laravel is still logget ind) so if i click login on the frontend the user will get logget ind again.
How to logout on the laravel backend so the user have to write the email and password again to login?
This dont work: the user is not logget out in the backend
Api route:
Route::middleware(['auth:api'])->group(function () {
Route::post('/logout', 'Auth\AuthWepAppController@logoutApi');
});
AuthWepAppController:
public function logoutApi (Request $request) {
Hi again Stefan,
With the code you have written, the user logges out when refreshing the page, howto handle so the user don't logges out on page refresh?
The attached screendump is what I have after page refresh
dev-to-uploads.s3.amazonaws.com/i/...
On page reload, check for the token in cookies, and if there is a token in the cookies, just set it in axios
I did:
In authIt.js
const access_token = cookies.get('x-access-token');
if
no token redirect to /login
else
store.$axios.$get(process.env.LARAVEL_ENDPOINT+'/api/user')
.then(resp => {
store.$auth.setUser(resp)
......
Hi again,
Question about logout:
I can do this in the frontend: this.$auth.logout();
But how to logout the laravel backend?
I have tried:
Nuxt:
logout() {
this.$axios.$post(process.env.LARAVEL_ENDPOINT+'/api/logout')
.then(response => {.....
Laravel backend:
API.php *******************
Route::middleware('auth:api')->group(function () {
Route::post('logout', 'Auth\AuthWepAppController@logoutApi')->name('logout');
});
AuthWepAppController *******************
It gives me this error:
message": "Call to undefined method Illuminate\Support\Facades\Request::user()",
What am I missing? thanks
Hi again Stefan
I'm trying again to figure out how to logout from the backend. How do you logout on the backend?
This dont work: the user is not logget out in the backend
Api route:
Route::middleware(['auth:api'])->group(function () {
Route::post('/logout', 'Auth\AuthWepAppController@logoutApi');
});
AuthWepAppController:
public function logoutApi (Request $request) {
$request->session()->flush();
}
Just get the users token and delete it.
Auth::user()->token()->delete
Great tutorial. I'm new to Passport and PKCE.
What is the "best practice" way on checking if a user is logged in frontend? so instead there will be a logout button and not a login button.
You should check if the token exists in the cookies or in the localStorage (wherever you have stored it)
I have not stored it yet. The way you store it here is that the best way? dev.to/stefant123/secure-authentic...
I think yes, or I still haven't found any better way to do it.
Question: we actually DON'T want the user to be stored in the session - because we want the login prompt to be shown each time the user hits oauth/authorize in the client... is this an option?
TBH I don't know really, but I guess you could do something like this. For example, when the user hits
oauth/authorize
you can run some middleware that would delete the session for that user, and the login prompt would be shown. Maybe this is the way you could do it, but then again I'm not sure if this would work and why would you want this.Hello, although it's been a while.
What if the client is first party mobile app, is it possible to use session?
Without session, the authorize route can't be accessed right?