Skip to content

Commit

Permalink
fix: LRU cache for invalidated tokens
Browse files Browse the repository at this point in the history
Signed-off-by: Dario Tranchitella <dario@tranchitella.eu>
  • Loading branch information
prometherion committed Aug 6, 2024
1 parent 665c45c commit 8e400c0
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion internal/webserver/middleware/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@ 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 {
if len(token) > 0 && !invalidatedToken.Has(token) {
tr := authenticationv1.TokenReview{
TypeMeta: metav1.TypeMeta{
Kind: "TokenReview",
Expand All @@ -37,6 +40,8 @@ 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")
}
}
Expand Down

0 comments on commit 8e400c0

Please sign in to comment.