Skip to content

Conversation

slayer321
Copy link
Contributor

fixes #398

two conditions are check before getting new access token

  1. If the last updated time is less then 10 hrs we don't refresh (this is done so that we don't add any extra check if refresh is not rquired)
  2. If the current time in more than the expired time mentioned in the JWT token. We need to refresh the access token.

Copy link

github-actions bot commented Jun 4, 2025

vet Summary Report

This report is generated by vet

Policy Checks

  • ✅ Vulnerability
  • ✅ Malware
  • ✅ License
  • ✅ Popularity
  • ✅ Maintenance
  • ✅ Security Posture
  • ✅ Threats

Malicious Package Analysis

Malicious package analysis was performed using SafeDep Cloud API

Malicious Package Analysis Report
Ecosystem Package Version Status Report
ECOSYSTEM_GO github.com/golang-jwt/jwt/v5 5.2.2 🔗
  • ℹ️ 1 packages have been actively analyzed for malicious behaviour.
  • ✅ No malicious packages found.
Changed Packages

Changed Packages

  • ✅ [Go] github.com/golang-jwt/jwt/v5@5.2.2

Copy link

codecov bot commented Jun 4, 2025

Codecov Report

Attention: Patch coverage is 8.33333% with 55 lines in your changes missing coverage. Please review.

Project coverage is 17.28%. Comparing base (5a5a951) to head (93fe2d1).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
internal/auth/auth.go 9.80% 43 Missing and 3 partials ⚠️
cmd/cloud/main.go 0.00% 9 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #507      +/-   ##
==========================================
- Coverage   17.31%   17.28%   -0.04%     
==========================================
  Files         175      175              
  Lines       16790    16849      +59     
==========================================
+ Hits         2908     2913       +5     
- Misses      13659    13710      +51     
- Partials      223      226       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

// is less than equal to 10 hrs or not(Here 10hrs was taken because it was observer that most of the JWT token had similar expiry time)
// 2. It will parse the JWT token to get the expiry time and then check for it expiry
func checkIfNewAccessTokenRequired(config auth.Config) (bool, error) {
if time.Since(config.CloudAccessTokenUpdatedAt) <= 10*time.Hour {
Copy link
Member

Choose a reason for hiding this comment

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

Whats the rationale of using 10 hour as the window? Especially since later in the code we are actually checking the JWT exp

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reason for having this two level of checks was to leverage the CloudAccessTokenUpdatedAt field that we store on the user side .. so In this case

  1. Using CloudAccessTokenUpdatedAt field is current time is less than 10 hrs when the last time access token was updated .. we don't go ahead to check the JWT check token ..
  2. If the first check is not true then only we check for JWT token expiry

The reason to add the first 10 hour window check was if it is not expired there will be no need to parse JWT token and check for it expiry .

But as I was thinking more on this . I believe JWT expiry check is more future proof ..

So let me know if we want to keep both the checks or just the JWT expiry check ?

slayer321 added 3 commits June 5, 2025 13:03
Signed-off-by: sachin maurya <sachin.maurya7666@gmail.com>
Signed-off-by: sachin maurya <sachin.maurya7666@gmail.com>
Signed-off-by: sachin maurya <sachin.maurya7666@gmail.com>
@slayer321 slayer321 force-pushed the feat/cloud-session-refresh-using-refresh-token branch from 1c7cbf8 to e93e5dd Compare June 5, 2025 07:33
@@ -41,6 +42,10 @@ func NewCloudCommand() *cobra.Command {
if tenantDomain != "" {
auth.SetRuntimeCloudTenant(tenantDomain)
}
err := auth.RefreshAccessToken()
Copy link
Member

Choose a reason for hiding this comment

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

@slayer321 Have you verified the flow? It seems to me this will cause problem because it will run even first time when a user does vet cloud login and will fail, show the error and cause a poor user experience. The first time user will likely be confused with the error message.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, I check the flow. But miss the part what if user running the command for the fist time.. In my case I started with the quickstart command.. and while running login command I didn't got error . But yes if there is new user and running the login command it will be poor user experience.
I'm thinking we can add a check here to skip the login and quickstart for calling RefreshAccessToken function.

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
Copy link
Member

Choose a reason for hiding this comment

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

Is this required, especially since we have deferred resp.Body.Close()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think yes, though we are deferring the resp.Body.Close() which will close stream at the end of the function.. but what if we didn't get http statusCode as StatusOK ... in that case we just want to read the response body and return the error .

…or refreshaccesstoken check

Signed-off-by: sachin maurya <sachin.maurya7666@gmail.com>
@slayer321 slayer321 requested a review from abhisek June 5, 2025 13:05
Signed-off-by: sachin maurya <sachin.maurya7666@gmail.com>
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.

Cloud Session Refresh using Refresh Token
2 participants