I am making a react application and using JWT for authentication.
As soon as a user logs in I issue a access token and set a http only cookie named jwt and value is refresh token. As per some articles I have read online it is suggested that access token have a short validity and refresh token have a long validity, so I set validity of access token to be 1 day and refresh token to be 25 days, (numbers are not very relevant). Now as soon as refresh token expires The user is automatically logged out.
Now the app I am developing is a data entry dashboard and I do not want the user to suddenly logout after entering a lot of data even if that happens once a month, so I want to know the industry standard to manage this kind of situation
There is a way to update refresh token without login-password pair:
When a refresh token expires a client have to get a new token pair (access, refresh tokens).
The client sends its access AND refresh tokens and receives a new pair (just like when authenticating with username:password).
You should track expiry of refresh tokens to prevent their reuse.
It is as safe as your flow because when a client gets logged out it understands that someone else already used its refresh token once. So it has to authenticate using username:password and invalidate the last refresh token.
I found an article explaining this flow
Related
Microsoft started to turn of basic authorization for Exchange Online and we must move to modern one.
We chose auhtorization code flow. User process authorization, we get auth code in url send it to server and get access/refresh token. Unfortunately default refresh token lifetime for SPA is 24 hours. Thats problem because we want to use refresh token later (e.g. days, months) in webjobs/api.
Our scenario:
User process authorization, scope is mail.send offline_access
We get auth code and send it to server
Server use auth code to get tokens
Tokens are valid and saved to database
Several days ago we use refresh token to gen new access token but request ends with exception (24 hour lifetime exceeded)
Our requirements in nutshell: Users allow us to send mail. Server save tokens and use them later in webjobs, api etc.
Can someone give us advice what is the proper way to get refresh token for long term use?
I'm currently working on a project with expressjs. For user authorization I use JWT tokens but until now only access tokens, because I just don't understand them together with a real example. So can somebody explain to me what the steps are the authorization goes through when someone logs into his account.
f.E.:
Refresh and Access Tokens get generated
Token gets stored into database etc.
Thank you in advance and have a great day
User sends a POST request with login credentials
Server authenticates and if successful, returns a JWT (usually in a httpOnly cookie). Server does not store JWTs in a database. The whole point of JWT is that authentication state is stored by the bearer of it.
For subsequent requests to protected endpoints, client needs to attach JWT. Server should check if JWT is expired and whether it has been altered.
Extensions that you might want to think about:
How to refresh JWTs:
There are different refreshing patterns that can be used. For
example, you can check the expiry of the JWT every time that your
server receives a request. If JWT is expiring soon, issue a fresh
JWT.
How to maintain authentication state on client-side without having to log in every time you refresh the page
How to really log a user out:
If you set the expiry as 30 minutes and a user logs out at the 15th minute mark, that JWT can technically still be used to access protected endpoints for another 15 minutes.
I am using Firebase authentication to authenticate users. Whenever, the user is logged in, I get the user's ID token with user.getIdToken(true) and set it on the local storage. With that token in the authorization header, I am requesting my back-end API.
On the back-end side, I am using Firebase admin SDK to authenticate the request and the user with the client-side ID token passed in the request authorization header.
This works for a while. But after some time I get error:
ExpiredIdTokenError: Token expired, 1620908095 < 1620915515
I saw that Firebase refreshes the ID token on its own. But I don't think that's the case. I have looked through the developer tools network tab, and there's also an observer method to check whenever the token has changed => onIdTokenChanged(), but the token is never refreshed.
I couldn't find any information on the Firebase docs either, and I was hoping if you could help me:
How can I generate a token without expiration limit to last until signed out or at least for some more time (1 week maybe)?
If I cannot the set the expiry limit of the token, what steps should I take so that I can send a valid unexpired token when I am request data from my back-end? Do I have to call user.getIdToken(true) every-time and get a fresh token before I request from my back-end API?
The idTokenChanged() observer is a bit misleading. It will fire when the token is refreshed, but the token is only refreshed automatically when you also use other Firebase products (like its database or file storage). In other cases, as you said you should call user.getIdToken(), which will refresh an expired token for you if necessary, everytime you call your API. You don't need to pass true into this method unless you want to have a completely fresh token everytime (which you most likely don't need).
To my knowledge you cannot control the expiration of tokens generated with the client SDK, for that you would need to generate your own tokens on the server.
I would like to set the session lifetime for the client to 6 hours. And if the user logged in and did not take any action during this time, then log out him and redirect him to the login page. But if the user has been making requests to the server with a valid token for 6 hours, I would like to update this token and start the token lifetime again.
If I follow the instructions according to the firebase session cookies documentation found here https://firebase.google.com/docs/auth/admin/manage-cookies?hl=en
and quickstart example
https://github.com/firebase/quickstart-nodejs/blob/master/auth-sessions/app.js
I can generate the session token on the server in exchange for a idToken and return the session ID to the client as a cookie. And after that we only use the session token between client and server as cookie, We can also check if the session has expired using verifySessionCookie.
but i can't figure out a way of refreshing the session cookie without telling the user to sign in again?
My simple application written in Javascript is using a service (which is written as a wrapper on Auth0) for authentication. On successful login, if I refresh the home page, application again goes to login page (even if I have stored the access token in cookies)
I also tried to store the access token in browser session storage.
As my index.html is launched, i am checking if my application url contains access token. If there is no access token, I redirect it to login page.
if (((window.location.hash).indexOf('access_token') < 0)) {
location.replace(redirectUrl);
}
On successful login, as url has access token in it, app works fine further.
But next time when I refresh the home page, it don't have access token in URL.
As per my understanding, as I have access token in cookies, it should not ask me for login again as long as token is valid.
It is still asking for login. What should be the strategy should I use to persist the token ?
On logout, I am setting the cookie to expire. Is there any ideal way to do log out other than this?
This can often be troubleshooted with a HAR file capture to follow the trail being left in the authentication flow. However since we don't have that option at this juncture here are a couple things to look at.
Are you using Dev keys in your auth scenario?
Expiry time
Where is the access token being stored?
https://auth0.com/docs/tokens/overview-access-tokens
Currently, you have a somewhat incomplete implementation.
As per my understanding, as I have access token in cookies, it should not ask me for login again as long as token is valid.
Saving token in the cookie storage and local storage does not have any relation with user authentication state. It is just a way of persisting the token and reading the data. In each navigation, you need to read the token and extract the information from the token to make sure user is authenticated.
The architecture you should follow:
Redirect the user to /authorize endpoint if there is no valid token in the application.
Once auth0 finish the user validation, the user will be redirected in the whitelisted callback URL. Make sure this URL is unique and save the token from URL fragments. It is very important to complete the token validation on the client side. Otherwise, it will be a major security issue. https://auth0.com/docs/tokens/guides/id-token/validate-id-token
Redirect the user to the secured URL.
I would highly recommend using auth0 SDK as it mitigates all the security issues including token signature validation using RS256 algorithm.
https://auth0.com/docs/quickstart/spa/vanillajs/01-login
You may find folloiwng thread useful
Why my auth0 token expires when refreshing page or clicking link in my Angular app?