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

fix: lru cache for invalidated tokens #498

Merged
merged 1 commit into from
Sep 27, 2024
Merged
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
10 changes: 9 additions & 1 deletion internal/webserver/middleware/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@ import (
"github.com/gorilla/mux"
authenticationv1 "k8s.io/api/authentication/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/projectcapsule/capsule-proxy/internal/webserver/errors"
)

func CheckJWTMiddleware(client client.Writer) mux.MiddlewareFunc {
invalidatedToken := sets.New[string]()

return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
var err error

token := strings.ReplaceAll(request.Header.Get("Authorization"), "Bearer ", "")

if len(token) > 0 {
switch {
case len(token) > 0 && !invalidatedToken.Has(token):
tr := authenticationv1.TokenReview{
TypeMeta: metav1.TypeMeta{
Kind: "TokenReview",
Expand All @@ -37,8 +41,12 @@ func CheckJWTMiddleware(client client.Writer) mux.MiddlewareFunc {
errors.HandleError(writer, err, "cannot create TokenReview")
}
if statusErr := tr.Status.Error; len(statusErr) > 0 {
invalidatedToken.Insert(token)

errors.HandleUnauthorized(writer, fmt.Errorf(statusErr), "cannot authenticate the token due to error")
}
case invalidatedToken.Has(token):
errors.HandleUnauthorized(writer, fmt.Errorf("token is invalid"), "cannot authenticate the token due to error")
}

next.ServeHTTP(writer, request)
Expand Down
Loading