Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increased access token validity and fixed refresh token validation #13

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

bstanga
Copy link

@bstanga bstanga commented Nov 22, 2024

Update auto token validity

Bogdan Stanga and others added 4 commits October 10, 2024 10:00
Copy link

github-actions bot commented Nov 22, 2024

PR Summary

Extended the access token validity period from 20 minutes to 20 hours and fixed a critical bug in refresh token validation logic. The refresh token validity remains unchanged at 2 hours. Also made token validity variables public by capitalizing their names.

Changes

File Summary
services/auth/handlers.go - Increased AccessTokenValidity from 20 minutes to 20 hours
  • Made token validity variables public (AccessTokenValidity, RefreshTokenValidity)
  • Fixed critical bug in refresh token validation where valid tokens were being rejected (err != nil changed to err == nil) |

autogenerated by presubmit.ai

@github-actions github-actions bot changed the title @presubmit Increased access token validity and fixed refresh token validation Nov 22, 2024
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 Pull request needs attention.

Review Summary

Commits Considered (4)

Co-authored-by: aipresubmit[bot] <183662906+aipresubmit[bot]@users.noreply.github.com>

  • f95bb65: increasing access token validity to 20 hours
Files Processed (1)
  • services/auth/handlers.go (4 hunks)
Actionable Comments (2)
  • services/auth/handlers.go [107-107]

    possible bug: "Critical bug in refresh token validation logic"

  • services/auth/handlers.go [17-18]

    possible issue: "Access token validity exceeds refresh token validity"

Skipped Comments (0)

@@ -104,7 +104,7 @@ func (s *Service) RefreshToken(ctx context.Context, r *pb.RefreshTokenRequest) (

// check if refresh token is in db, if it's valid and if the access uuid matches the one in the jwt
rt, err := s.db.GetRefreshToken(nil, tkn.ID)
if err != nil || rt.AccessTokenID != tkn.AccessTokenID || rt.ExpiresAt.Before(time.Now()) {
if err == nil || rt.AccessTokenID != tkn.AccessTokenID || rt.ExpiresAt.Before(time.Now()) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The refresh token validation logic has a critical bug. The condition err == nil is incorrect - it should be err != nil. With the current logic, valid refresh tokens will be rejected and invalid ones might be accepted. This completely breaks the token refresh flow.

The correct condition should be:

if err != nil || rt.AccessTokenID != tkn.AccessTokenID || rt.ExpiresAt.Before(time.Now()) {

Comment on lines +17 to +18
AccessTokenValidity = 20 * time.Hour // 20 hours
RefreshTokenValidity = 2 * time.Hour // 2 hours

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The access token validity (20 hours) is now longer than the refresh token validity (2 hours). This is an unusual and potentially problematic configuration. Typically, refresh tokens should have a longer validity period than access tokens since their purpose is to obtain new access tokens when they expire. Consider either reducing the access token validity or increasing the refresh token validity.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@presubmit What would be some reasonable duration for token and refresh token validity?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bstanga Common practice for token validity durations is:

  • Access tokens: 15-60 minutes
  • Refresh tokens: 1-14 days

The shorter access token duration helps limit the window of opportunity if a token is compromised, while the longer refresh token duration provides a better user experience by reducing the frequency of re-authentication.

For your use case, I would recommend:

  • Access token: 30 minutes
  • Refresh token: 7 days

This provides a good balance between security and usability. If you have specific security requirements or usage patterns, these durations can be adjusted accordingly.

@bstanga bstanga changed the title Increased access token validity and fixed refresh token validation Increased access token validity and fixed refresh token validation (generated) Nov 22, 2024
@bstanga bstanga changed the title Increased access token validity and fixed refresh token validation (generated) Increased access token validity and fixed refresh token validation Nov 22, 2024
@bstanga
Copy link
Author

bstanga commented Nov 22, 2024

That's a good title, thanks for generating it!

@bstanga
Copy link
Author

bstanga commented Nov 22, 2024

Reviewed using presubmit.ai https://github.com/presubmit/ai-reviewer ⚡️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant