Skip to content

Commit 6c23590

Browse files
committed
add some helper function for BearerTokenAuth
1 parent 057c9c0 commit 6c23590

File tree

8 files changed

+24
-55
lines changed

8 files changed

+24
-55
lines changed

pkg/zen/auth.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package zen
22

33
import (
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+
}

svc/api/routes/chproxy_metrics/BUILD.bazel

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff 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
],

svc/api/routes/chproxy_metrics/handler.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ package chproxyMetrics
22

33
import (
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 {
3330
func (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

svc/api/routes/chproxy_ratelimits/BUILD.bazel

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff 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
],

svc/api/routes/chproxy_ratelimits/handler.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ package chproxyRatelimits
22

33
import (
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 {
3330
func (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

svc/api/routes/chproxy_verifications/BUILD.bazel

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff 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
],

svc/api/routes/chproxy_verifications/handler.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ package chproxyVerifications
22

33
import (
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 {
3330
func (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

svc/api/routes/internal_cache_invalidate/handler.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package internalCacheInvalidate
22

33
import (
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 {
3231
func (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

0 commit comments

Comments
 (0)