Skip to content

Commit 311f7e4

Browse files
authored
Forward origin header from incoming requests (#100)
1 parent b349f7e commit 311f7e4

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

rpc/access/middleware.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ func accessKeyMiddleware(next http.Handler) http.Handler {
5151
if accessKey != "" {
5252
ctx = tenant.WithAccessKey(ctx, accessKey)
5353
}
54+
origin := r.Header.Get("origin")
55+
if origin != "" {
56+
ctx = tenant.WithOrigin(ctx, origin)
57+
}
5458
next.ServeHTTP(w, r.WithContext(ctx))
5559
})
5660
}

rpc/tenant/context.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type contextKeyType string
1010

1111
var (
1212
accessKeyCtxKey = contextKeyType("access-key")
13+
originKeyCtxKey = contextKeyType("origin")
1314
tenantCtxKey = contextKeyType("tenant-data")
1415
)
1516

@@ -26,3 +27,12 @@ func AccessKeyFromContext(ctx context.Context) string {
2627
func WithAccessKey(ctx context.Context, accessKey string) context.Context {
2728
return context.WithValue(ctx, accessKeyCtxKey, accessKey)
2829
}
30+
31+
func OriginFromContext(ctx context.Context) string {
32+
v, _ := ctx.Value(originKeyCtxKey).(string)
33+
return v
34+
}
35+
36+
func WithOrigin(ctx context.Context, origin string) context.Context {
37+
return context.WithValue(ctx, originKeyCtxKey, origin)
38+
}

rpc/waasapi/waasapi.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ func Context(ctx context.Context, optJwtToken ...string) context.Context {
2121
waasHeader := http.Header{}
2222
waasHeader.Set("Authorization", "BEARER "+jwtToken)
2323

24-
accessKey := tenant.AccessKeyFromContext(ctx)
25-
if accessKey != "" {
24+
if accessKey := tenant.AccessKeyFromContext(ctx); accessKey != "" {
2625
waasHeader.Set("X-Access-Key", accessKey)
2726
}
27+
if origin := tenant.OriginFromContext(ctx); origin != "" {
28+
waasHeader.Set("Origin", origin)
29+
}
2830

2931
waasCtx, err := proto_wallet.WithHTTPRequestHeaders(ctx, waasHeader)
3032
if err != nil {

0 commit comments

Comments
 (0)