Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions internal/db/token_repo_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ func (r RedisAdapter) SetAccessToken(ctx context.Context, token models.AuthToken
return r.setAuthToken(ctx, token)
}

func (r RedisAdapter) SetAccessTokenExpiry(ctx context.Context, token models.AuthToken, expiresAt time.Time) error {
func (r RedisAdapter) SetAccessTokenExpiry(ctx context.Context, token models.AuthToken, expiresAtLimit time.Time) error {
if token.Type != models.AccessTokenType {
return fmt.Errorf("token is not of the right type")
}
expiresAt := expiresAtLimit
if !token.ExpiresAt.IsZero() && token.ExpiresAt.Before(expiresAtLimit) {
expiresAt = token.ExpiresAt
}
return r.setAuthTokenExpiry(ctx, token, expiresAt)
}

Expand All @@ -57,10 +61,14 @@ func (r RedisAdapter) SetRefreshToken(ctx context.Context, token models.AuthToke
return r.setAuthToken(ctx, token)
}

func (r RedisAdapter) SetRefreshTokenExpiry(ctx context.Context, token models.AuthToken, expiresAt time.Time) error {
func (r RedisAdapter) SetRefreshTokenExpiry(ctx context.Context, token models.AuthToken, expiresAtLimit time.Time) error {
if token.Type != models.RefreshTokenType {
return fmt.Errorf("token is not of the right type")
}
expiresAt := expiresAtLimit
if !token.ExpiresAt.IsZero() && token.ExpiresAt.Before(expiresAtLimit) {
expiresAt = token.ExpiresAt
}
return r.setAuthTokenExpiry(ctx, token, expiresAt)
}

Expand All @@ -71,10 +79,14 @@ func (r RedisAdapter) SetIDToken(ctx context.Context, token models.AuthToken) er
return r.setAuthToken(ctx, token)
}

func (r RedisAdapter) SetIDTokenExpiry(ctx context.Context, token models.AuthToken, expiresAt time.Time) error {
func (r RedisAdapter) SetIDTokenExpiry(ctx context.Context, token models.AuthToken, expiresAtLimit time.Time) error {
if token.Type != models.IDTokenType {
return fmt.Errorf("token is not of the right type")
}
expiresAt := expiresAtLimit
if !token.ExpiresAt.IsZero() && token.ExpiresAt.Before(expiresAtLimit) {
expiresAt = token.ExpiresAt
}
return r.setAuthTokenExpiry(ctx, token, expiresAt)
}

Expand Down
6 changes: 3 additions & 3 deletions internal/models/token_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type AccessTokenGetter interface {

type AccessTokenSetter interface {
SetAccessToken(ctx context.Context, token AuthToken) error
SetAccessTokenExpiry(ctx context.Context, token AuthToken, expiresAt time.Time) error
SetAccessTokenExpiry(ctx context.Context, token AuthToken, expiresAtLimit time.Time) error
}

type AccessTokenRemover interface {
Expand All @@ -37,7 +37,7 @@ type RefreshTokenGetter interface {

type RefreshTokenSetter interface {
SetRefreshToken(ctx context.Context, token AuthToken) error
SetRefreshTokenExpiry(ctx context.Context, token AuthToken, expiresAt time.Time) error
SetRefreshTokenExpiry(ctx context.Context, token AuthToken, expiresAtLimit time.Time) error
}

type RefreshTokenRemover interface {
Expand All @@ -50,7 +50,7 @@ type IDTokenGetter interface {

type IDTokenSetter interface {
SetIDToken(ctx context.Context, token AuthToken) error
SetIDTokenExpiry(ctx context.Context, token AuthToken, expiresAt time.Time) error
SetIDTokenExpiry(ctx context.Context, token AuthToken, expiresAtLimit time.Time) error
}

type IDTokenRemover interface {
Expand Down
4 changes: 2 additions & 2 deletions internal/sessions/session_maker.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ func (sm *SessionMakerImpl) NewSession() (models.Session, error) {
if session.IdleTTL() == time.Duration(0) {
session.ExpiresAt = time.Time{}
} else if session.MaxTTL() == time.Duration(0) {
session.ExpiresAt = session.CreatedAt.Add(session.MaxTTL())
} else {
session.ExpiresAt = session.CreatedAt.Add(session.IdleTTL())
} else {
session.ExpiresAt = session.CreatedAt.Add(session.MaxTTL())
}
slog.Info("NEW SESSION", "session", session)
return session, nil
Expand Down
9 changes: 2 additions & 7 deletions internal/sessions/token_handling.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (sessions *SessionStore) SaveTokens(c echo.Context, session *models.Session
session.TokenIDs = models.SerializableMap{}
}
session.TokenIDs[providerID] = tokens.AccessToken.ID
expiresAt := sessions.getTokenStorageExpiration(tokens, *session)
expiresAt := sessions.getTokenStorageExpiration(*session)
err = sessions.tokenStore.SetAccessToken(c.Request().Context(), tokens.AccessToken)
if err != nil {
return err
Expand Down Expand Up @@ -175,11 +175,6 @@ func (*SessionStore) idTokenKey(tokenID string) string {
return IDTokenCtxKey + ":" + tokenID
}

// getTokenStorageExpiration returns the max session expiration unless the provider is Renku or GitLab, in which case there is no expiration
func (*SessionStore) getTokenStorageExpiration(tokens models.AuthTokenSet, session models.Session) time.Time {
providerID := tokens.AccessToken.ProviderID
if providerID == "renku" || providerID == "gitlab" {
return time.Time{}
Copy link
Member

Choose a reason for hiding this comment

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

This change will break renku v1 if I understand it correctly.

Copy link
Member

Choose a reason for hiding this comment

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

Forever tokens are needed for some v1 functionality.

Copy link
Member Author

Choose a reason for hiding this comment

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

this is purely for redis expiration/ttl, not the live time of the token itself or anything like that.

Copy link
Member

Choose a reason for hiding this comment

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

By doing this change, a TTL will be set on Keycloak and GitLab tokens, which some parts of renku v1 expect to be available forever (even if the user does no log in after session expiry). Feel free to run this as an experiment, but this is the reason redis is remembering tokens forever; I did not want to break renku v1 with the gateway refactor.

Copy link
Member

Choose a reason for hiding this comment

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

So I am just adding this here after our discussion. The main issue is that SSH sessions would be affected if we started to evict tokens. Because for these users do not need to be logged in once they have started.

}
func (*SessionStore) getTokenStorageExpiration(session models.Session) time.Time {
return session.CreatedAt.Add(session.MaxTTL())
}