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

Only Require OIDC Refresh Token If Access Token Expired #899

Merged
merged 2 commits into from
Jan 23, 2025
Merged
Changes from 1 commit
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
32 changes: 10 additions & 22 deletions backend/pkg/auth/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,6 @@ func (oa *oidcAuth) ValidateToken(c echo.Context) error {
return nil
}

// If refresh token is not available in the session
// mark the request as unauthorized so that the session
// can be recreated with refresh_token
session := echosessions.GetSession(c)
refreshToken := session.Get("refresh_token")
if refreshToken == nil {
logger.Debug().Str("request_id", requestID).Msg("ValidateToken, Refresh token not found in session")
httpError(c, http.StatusUnauthorized)
return nil
}

_, err := oa.verifier.Verify(ctx, token)
if err != nil {
logger.Error().Str("request_id", requestID).AnErr("error", err).Msg("ValidateToken, Token verification error")
Expand Down Expand Up @@ -375,23 +364,22 @@ func (oa *oidcAuth) Authenticate(c echo.Context) (teamID string, replied bool) {
return "", true
}

// If refresh token is not available in the session
// mark the request as unauthorized so that the session
// can be recreated with refresh_token
session := echosessions.GetSession(c)
refreshToken := session.Get("refresh_token")
if refreshToken == nil {
logger.Debug().Str("request_id", requestID).Msg("Refresh token not found in session")
httpError(c, http.StatusUnauthorized)
return "", true
}

// Verify Token
tk, err := oa.verifier.Verify(ctx, token)
if err != nil {
// If token is expired, use the refresh_token to fetch a new token
// and set the new id_token in response header
if strings.Contains(err.Error(), "token is expired") {
// If refresh token is not available in the session
// mark the request as unauthorized so that the session
// can be recreated with refresh_token
session := echosessions.GetSession(c)
refreshToken := session.Get("refresh_token")
if refreshToken == nil {
justdan96 marked this conversation as resolved.
Show resolved Hide resolved
logger.Debug().Str("request_id", requestID).Msg("Refresh token not found in session")
httpError(c, http.StatusUnauthorized)
return "", true
}
ts := oa.oauthConfig.TokenSource(ctx, &oauth2.Token{RefreshToken: refreshToken.(string)})
newToken, err := ts.Token()
if err != nil {
Expand Down