mirror of
https://github.com/coder/coder.git
synced 2025-07-06 15:41:45 +00:00
fix: redirect to login upon authentication error (#9134)
This commit is contained in:
@ -308,6 +308,9 @@ func ExtractAPIKey(rw http.ResponseWriter, r *http.Request, cfg ExtractAPIKeyCon
|
||||
}
|
||||
|
||||
// Checking if the key is expired.
|
||||
// NOTE: The `RequireAuth` React component depends on this `Detail` to detect when
|
||||
// the users token has expired. If you change the text here, make sure to update it
|
||||
// in site/src/components/RequireAuth/RequireAuth.tsx as well.
|
||||
if key.ExpiresAt.Before(now) {
|
||||
return optionalWrite(http.StatusUnauthorized, codersdk.Response{
|
||||
Message: SignedOutErrorMessage,
|
||||
|
@ -3,11 +3,13 @@ package httpmw_test
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
@ -197,6 +199,11 @@ func TestAPIKey(t *testing.T) {
|
||||
res := rw.Result()
|
||||
defer res.Body.Close()
|
||||
require.Equal(t, http.StatusUnauthorized, res.StatusCode)
|
||||
|
||||
var apiRes codersdk.Response
|
||||
dec := json.NewDecoder(res.Body)
|
||||
_ = dec.Decode(&apiRes)
|
||||
require.True(t, strings.HasPrefix(apiRes.Detail, "API key expired"))
|
||||
})
|
||||
|
||||
t.Run("Valid", func(t *testing.T) {
|
||||
|
Reference in New Issue
Block a user