Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

ThomasK33
Copy link
Member

Implement OAuth2 Refresh Token Expiry Configuration

This PR adds a dedicated configuration option for OAuth2 refresh token lifetimes, allowing refresh tokens to outlive their associated access tokens. This is important for OAuth2 flows where refresh tokens should remain valid after access tokens expire.

Key changes:

  • Added RefreshDefaultDuration to SessionLifetime struct to control refresh token expiry
  • Modified token generation to use this new setting when creating refresh tokens
  • Added a test to verify refresh tokens outlive access tokens
  • Set default refresh token lifetime to 30 days

The PR ensures that refresh tokens can have a longer lifetime than access tokens, which is a standard OAuth2 pattern that allows clients to obtain new access tokens without requiring user re-authentication.

Copy link
Member Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@ThomasK33 ThomasK33 force-pushed the thomask33/09-10-feat_oauth2_add_configurable_refresh_token_lifetime branch 2 times, most recently from c43e727 to 459f676 Compare September 10, 2025 19:50
@ThomasK33 ThomasK33 marked this pull request as ready for review September 11, 2025 08:42
Comment on lines +570 to +572
// This should generally be longer than access token lifetimes to allow
// refreshing after access token expiry.
RefreshDefaultDuration serpent.Duration `json:"refresh_default_duration,omitempty" typescript:",notnull"`
Copy link
Member

Choose a reason for hiding this comment

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

In what case would it make sense to set the default refresh duration to be the same as the access token lifetime? In that case you may as well just disable refresh tokens entirely. If we allow setting this equal to or very close to access token lifetime, I foresee customers running into issues. Would it make sense to validate that RefreshDefaultDuration is strictly greater than access token lifetime, and raise an error if this is not the case?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, that makes sense. I've added a validation to catch those cases.

It errors out if the constraint of refresh token lifetime < access token lifetime, since debugging why refresh tokens aren't issued would become even harder if we fail silently.

@ThomasK33 ThomasK33 force-pushed the thomask33/09-10-feat_oauth2_add_configurable_refresh_token_lifetime branch 2 times, most recently from 932dbe8 to 0baaed6 Compare September 11, 2025 10:58
@@ -3223,6 +3238,27 @@ type LinkConfig struct {
Icon string `json:"icon" yaml:"icon" enums:"bug,chat,docs"`
}

// Validate checks cross-field constraints for deployment values.
// It should be called after all values are loaded from flags/env/YAML.
Copy link
Member

Choose a reason for hiding this comment

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

s/should/must

It would also be nice to know if there were a way to catch if this were not the case, and error if so.

For now, I suppose we could just check for a zero-valued DeploymentValues and complain?

Copy link
Member Author

@ThomasK33 ThomasK33 Sep 11, 2025

Choose a reason for hiding this comment

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

The closest I can think of is checking if a session's default duration is not equal to 0 (the default)?

Since a session duration of 0 doesn't make sense conceptually.

So prepending something like

// Check if values appear uninitialized
      if access == 0 {
          return xerrors.New("sessions configuration
   appears uninitialized - ensure all values are 
  loaded before validation")
      }

@ThomasK33 ThomasK33 force-pushed the thomask33/09-10-feat_oauth2_add_configurable_refresh_token_lifetime branch from 0baaed6 to 720a83d Compare September 11, 2025 11:13
@ThomasK33 ThomasK33 force-pushed the thomask33/09-10-feat_oauth2_add_configurable_refresh_token_lifetime branch from 720a83d to ceb24de Compare September 12, 2025 07:35
Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

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

LGTM, some comments below. Recommend a second approval as well as my OAuth knowledge is rusty.


// Check if values appear uninitialized
if access == 0 {
return xerrors.New("sessions configuration appears uninitialized - ensure all values are loaded before validation")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return xerrors.New("sessions configuration appears uninitialized - ensure all values are loaded before validation")
return xerrors.New("developer error: sessions configuration appears uninitialized - ensure all values are loaded before validation")

dv := mk(1*time.Hour, 48*time.Hour)
err := dv.Validate()
require.NoError(t, err)
})
Copy link
Member

Choose a reason for hiding this comment

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

Don't forget to also add a test to hit the code path you added to check for uninitialized default duration

@ThomasK33 ThomasK33 force-pushed the thomask33/09-10-feat_oauth2_add_configurable_refresh_token_lifetime branch from ceb24de to 0fb122c Compare September 12, 2025 11:47
Change-Id: I988093e8fc7328a09d2a0b2c5d476bad75e064c8
Signed-off-by: Thomas Kosiewski <[email protected]>
@ThomasK33 ThomasK33 force-pushed the thomask33/09-10-feat_oauth2_add_configurable_refresh_token_lifetime branch from 0fb122c to 805bc4d Compare September 12, 2025 12:12
@ThomasK33 ThomasK33 merged commit 088d149 into main Sep 13, 2025
37 checks passed
@ThomasK33 ThomasK33 deleted the thomask33/09-10-feat_oauth2_add_configurable_refresh_token_lifetime branch September 13, 2025 06:57
@github-actions github-actions bot locked and limited conversation to collaborators Sep 13, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants