Skip to content

Commit f5ebb9d

Browse files
authored
Propagate Access Key (#26)
* Always set access key * Propagate middleware * Add comment
1 parent 0f42df9 commit f5ebb9d

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

middleware.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package authcontrol
22

33
import (
44
"cmp"
5+
"context"
56
"errors"
67
"net/http"
78
"strings"
@@ -158,7 +159,7 @@ func Session(cfg Options) func(next http.Handler) http.Handler {
158159
}
159160
}
160161

161-
if accessKey != "" && sessionType < proto.SessionType_Admin {
162+
if accessKey != "" {
162163
ctx = WithAccessKey(ctx, accessKey)
163164
sessionType = max(sessionType, proto.SessionType_AccessKey)
164165
}
@@ -196,3 +197,24 @@ func AccessControl(acl Config[ACL], cfg Options) func(next http.Handler) http.Ha
196197
})
197198
}
198199
}
200+
201+
// PropagateAccessKey propagates the access key from the context to other webrpc packages.
202+
// It expectes the function `WithHTTPRequestHeaders` from the proto package that requires the access key propogation.
203+
func PropagateAccessKey(headerContextFuncs ...func(context.Context, http.Header) (context.Context, error)) func(next http.Handler) http.Handler {
204+
return func(next http.Handler) http.Handler {
205+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
206+
ctx := r.Context()
207+
208+
if accessKey, ok := GetAccessKey(ctx); ok {
209+
h := http.Header{
210+
HeaderAccessKey: []string{accessKey},
211+
}
212+
for _, fn := range headerContextFuncs {
213+
ctx, _ = fn(ctx, h)
214+
}
215+
}
216+
217+
next.ServeHTTP(w, r.WithContext(ctx))
218+
})
219+
}
220+
}

0 commit comments

Comments
 (0)