Access token and refresh token.
# support
g
Hello, I've noticed that in the code, the refresh token is set with a duration of two weeks. However, based on my observation, the user is logged out after an hour of inactivity. Could you help clarify where the session timeout period for the end user is defined in your application? A screenshot or git-repo link would be helpful, Thanks in advance! https://github.com/medplum/medplum/blob/af97ce40b135d1ef64a658f056cc945a9970e613/packages/server/src/oauth/keys.ts#L219C1-L231C2
a
Hi @Guoyi Z (Empallo) , I am not sure I fully understand the question, but are you looking for these 3600 seconds (aka one hour)? https://github.com/medplum/medplum/blob/af97ce40b135d1ef64a658f056cc945a9970e613/packages/server/src/oauth/token.ts#L566
g
Hi @andrei_60331 , thanks for the information. Just to clarify, does this one-hour period imply that if the user remains inactive on the client side, the session will expire? Specifically, I’m interested in understanding where the code logic enforces a forced logout after a period of inactivity. Could you point me to the section in the code that handles this aspect? Thank you!
Based on my understanding, this code(https://github.com/medplum/medplum/blob/9d1cc0ff89b618c7f8fe9d567bee8d73c1de19fe/packages/server/src/oauth/authorize.ts#L152C1-L173C2) explains why a user is automatically logged out after an hour of inactivity on the client side. The system tracks the user's session duration when there is no active interaction. As long as the user remains active and the access token is valid, they can stay logged in. If the access token expires but the refresh token is still valid, the system will use the refresh token to obtain a new access token, extending the session (by another hour, please correct me if I was wrong). However, if the user is inactive for over an hour, this code will execute a check. Specifically, it retrieves login information through either the getExistingLoginFromIdTokenHint or getExistingLoginFromCookie methods and verifies the authTime to determine the last time the user actively interacted with the system. If the time elapsed since authTime exceeds the maximum session duration (e.g., one hour), the user is forced to re-authenticate, even if the refresh token is still valid. In other words, this code manages session validity based on user activity duration, rather than solely relying on token expiration. As long as the user remains active and the tokens can be renewed, they can stay logged maximum of 2 weeks. But once the session’s idle time surpasses the allowed threshold, even with a valid refresh token, the user will be required to log in again. If my understanding is incorrect, please let me know. Thanks!
Hi @reshma @rahul1 @andrei_60331 , Would appreciate comment on this. In order to pass a security test, we want to know which code segment defines the timeout period for terminating a session within the application for the end user.
a
Hi @Guoyi Z (Empallo) , (sorry if I am responding too late), I think you are correct in general understanding. However there are a lot of nuances as Medplum supports multiple authentication methods as per https://www.medplum.com/docs/auth/methods/oauth-auth-code. If you are asking specifically about Application https://github.com/medplum/medplum/tree/af97ce40b135d1ef64a658f056cc945a9970e613/packages/app then I can see that it expires user session and redirects to login page when Medplum server https://github.com/medplum/medplum/tree/af97ce40b135d1ef64a658f056cc945a9970e613/packages/server responds with 401 (more specifically here is the corresponding code https://github.com/medplum/medplum/blob/9d1cc0ff89b618c7f8fe9d567bee8d73c1de19fe/packages/core/src/client.ts#L3118) As for the server - it responds with 401 after one hour when JWT doesn't pass verification, in your case it will be "exp" claim timestamp check failed'. Here is where the corresponding exception will be raised https://github.com/medplum/medplum/blob/9d1cc0ff89b618c7f8fe9d567bee8d73c1de19fe/packages/server/src/oauth/utils.ts#L817
g
Thanks, @andrei_60331 , this is super helpful!
150 Views