Skip to content
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
24 changes: 23 additions & 1 deletion middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package authcontrol

import (
"cmp"
"context"
"errors"
"net/http"
"strings"
Expand Down Expand Up @@ -158,7 +159,7 @@ func Session(cfg Options) func(next http.Handler) http.Handler {
}
}

if accessKey != "" && sessionType < proto.SessionType_Admin {
if accessKey != "" {
ctx = WithAccessKey(ctx, accessKey)
sessionType = max(sessionType, proto.SessionType_AccessKey)
}
Expand Down Expand Up @@ -196,3 +197,24 @@ func AccessControl(acl Config[ACL], cfg Options) func(next http.Handler) http.Ha
})
}
}

// PropagateAccessKey propagates the access key from the context to other webrpc packages.
// It expectes the function `WithHTTPRequestHeaders` from the proto package that requires the access key propogation.
func PropagateAccessKey(headerContextFuncs ...func(context.Context, http.Header) (context.Context, error)) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

if accessKey, ok := GetAccessKey(ctx); ok {
h := http.Header{
HeaderAccessKey: []string{accessKey},
}
for _, fn := range headerContextFuncs {
ctx, _ = fn(ctx, h)
}
}

next.ServeHTTP(w, r.WithContext(ctx))
})
}
}
Loading