File tree Expand file tree Collapse file tree 8 files changed +24
-55
lines changed
internal_cache_invalidate Expand file tree Collapse file tree 8 files changed +24
-55
lines changed Original file line number Diff line number Diff line change 11package zen
22
33import (
4+ "crypto/subtle"
45 "strings"
56
67 "github.com/unkeyed/unkey/pkg/codes"
@@ -50,3 +51,22 @@ func Bearer(s *Session) (string, error) {
5051
5152 return bearer , nil
5253}
54+
55+ // BearerTokenAuth extracts the Bearer token from the session and compares it
56+ // against the expected token using constant-time comparison. Returns nil if
57+ // the token matches, or an appropriate error otherwise.
58+ func BearerTokenAuth (s * Session , expected string ) error {
59+ token , err := Bearer (s )
60+ if err != nil {
61+ return err
62+ }
63+
64+ if subtle .ConstantTimeCompare ([]byte (token ), []byte (expected )) != 1 {
65+ return fault .New ("invalid bearer token" ,
66+ fault .Code (codes .Auth .Authentication .KeyNotFound .URN ()),
67+ fault .Internal ("bearer token does not match" ),
68+ fault .Public ("The provided token is invalid." ))
69+ }
70+
71+ return nil
72+ }
Original file line number Diff line number Diff line change @@ -8,8 +8,6 @@ go_library(
88 deps = [
99 "//pkg/clickhouse" ,
1010 "//pkg/clickhouse/schema" ,
11- "//pkg/codes" ,
12- "//pkg/fault" ,
1311 "//pkg/prometheus/metrics" ,
1412 "//pkg/zen" ,
1513 ],
Original file line number Diff line number Diff line change @@ -2,13 +2,10 @@ package chproxyMetrics
22
33import (
44 "context"
5- "crypto/subtle"
65 "net/http"
76
87 "github.com/unkeyed/unkey/pkg/clickhouse"
98 "github.com/unkeyed/unkey/pkg/clickhouse/schema"
10- "github.com/unkeyed/unkey/pkg/codes"
11- "github.com/unkeyed/unkey/pkg/fault"
129 "github.com/unkeyed/unkey/pkg/prometheus/metrics"
1310 "github.com/unkeyed/unkey/pkg/zen"
1411)
@@ -33,19 +30,10 @@ func (h *Handler) Path() string {
3330func (h * Handler ) Handle (ctx context.Context , s * zen.Session ) error {
3431 s .DisableClickHouseLogging ()
3532
36- // Authenticate using Bearer token
37- token , err := zen .Bearer (s )
38- if err != nil {
33+ if err := zen .BearerTokenAuth (s , h .Token ); err != nil {
3934 return err
4035 }
4136
42- if subtle .ConstantTimeCompare ([]byte (token ), []byte (h .Token )) != 1 {
43- return fault .New ("invalid chproxy token" ,
44- fault .Code (codes .Auth .Authentication .KeyNotFound .URN ()),
45- fault .Internal ("chproxy token does not match" ),
46- fault .Public ("The provided token is invalid." ))
47- }
48-
4937 events , err := zen.BindBody [[]schema.ApiRequest ](s )
5038 if err != nil {
5139 return err
Original file line number Diff line number Diff line change @@ -8,8 +8,6 @@ go_library(
88 deps = [
99 "//pkg/clickhouse" ,
1010 "//pkg/clickhouse/schema" ,
11- "//pkg/codes" ,
12- "//pkg/fault" ,
1311 "//pkg/prometheus/metrics" ,
1412 "//pkg/zen" ,
1513 ],
Original file line number Diff line number Diff line change @@ -2,13 +2,10 @@ package chproxyRatelimits
22
33import (
44 "context"
5- "crypto/subtle"
65 "net/http"
76
87 "github.com/unkeyed/unkey/pkg/clickhouse"
98 "github.com/unkeyed/unkey/pkg/clickhouse/schema"
10- "github.com/unkeyed/unkey/pkg/codes"
11- "github.com/unkeyed/unkey/pkg/fault"
129 "github.com/unkeyed/unkey/pkg/prometheus/metrics"
1310 "github.com/unkeyed/unkey/pkg/zen"
1411)
@@ -33,19 +30,10 @@ func (h *Handler) Path() string {
3330func (h * Handler ) Handle (ctx context.Context , s * zen.Session ) error {
3431 s .DisableClickHouseLogging ()
3532
36- // Authenticate using Bearer token
37- token , err := zen .Bearer (s )
38- if err != nil {
33+ if err := zen .BearerTokenAuth (s , h .Token ); err != nil {
3934 return err
4035 }
4136
42- if subtle .ConstantTimeCompare ([]byte (token ), []byte (h .Token )) != 1 {
43- return fault .New ("invalid chproxy token" ,
44- fault .Code (codes .Auth .Authentication .KeyNotFound .URN ()),
45- fault .Internal ("chproxy token does not match" ),
46- fault .Public ("The provided token is invalid." ))
47- }
48-
4937 events , err := zen.BindBody [[]schema.Ratelimit ](s )
5038 if err != nil {
5139 return err
Original file line number Diff line number Diff line change @@ -8,8 +8,6 @@ go_library(
88 deps = [
99 "//pkg/clickhouse" ,
1010 "//pkg/clickhouse/schema" ,
11- "//pkg/codes" ,
12- "//pkg/fault" ,
1311 "//pkg/prometheus/metrics" ,
1412 "//pkg/zen" ,
1513 ],
Original file line number Diff line number Diff line change @@ -2,13 +2,10 @@ package chproxyVerifications
22
33import (
44 "context"
5- "crypto/subtle"
65 "net/http"
76
87 "github.com/unkeyed/unkey/pkg/clickhouse"
98 "github.com/unkeyed/unkey/pkg/clickhouse/schema"
10- "github.com/unkeyed/unkey/pkg/codes"
11- "github.com/unkeyed/unkey/pkg/fault"
129 "github.com/unkeyed/unkey/pkg/prometheus/metrics"
1310 "github.com/unkeyed/unkey/pkg/zen"
1411)
@@ -33,19 +30,10 @@ func (h *Handler) Path() string {
3330func (h * Handler ) Handle (ctx context.Context , s * zen.Session ) error {
3431 s .DisableClickHouseLogging ()
3532
36- // Authenticate using Bearer token
37- token , err := zen .Bearer (s )
38- if err != nil {
33+ if err := zen .BearerTokenAuth (s , h .Token ); err != nil {
3934 return err
4035 }
4136
42- if subtle .ConstantTimeCompare ([]byte (token ), []byte (h .Token )) != 1 {
43- return fault .New ("invalid chproxy token" ,
44- fault .Code (codes .Auth .Authentication .KeyNotFound .URN ()),
45- fault .Internal ("chproxy token does not match" ),
46- fault .Public ("The provided token is invalid." ))
47- }
48-
4937 events , err := zen.BindBody [[]schema.KeyVerification ](s )
5038 if err != nil {
5139 return err
Original file line number Diff line number Diff line change @@ -2,7 +2,6 @@ package internalCacheInvalidate
22
33import (
44 "context"
5- "crypto/subtle"
65 "net/http"
76
87 "github.com/unkeyed/unkey/internal/services/caches"
@@ -32,18 +31,10 @@ func (h *Handler) Path() string {
3231func (h * Handler ) Handle (ctx context.Context , s * zen.Session ) error {
3332 s .DisableClickHouseLogging ()
3433
35- token , err := zen .Bearer (s )
36- if err != nil {
34+ if err := zen .BearerTokenAuth (s , h .Token ); err != nil {
3735 return err
3836 }
3937
40- if subtle .ConstantTimeCompare ([]byte (token ), []byte (h .Token )) != 1 {
41- return fault .New ("invalid dashboard token" ,
42- fault .Code (codes .Auth .Authentication .KeyNotFound .URN ()),
43- fault .Internal ("dashboard token does not match" ),
44- fault .Public ("The provided token is invalid." ))
45- }
46-
4738 req , err := zen.BindBody [request ](s )
4839 if err != nil {
4940 return err
You can’t perform that action at this time.
0 commit comments