Skip to content

Commit 3ce3c3b

Browse files
committed
Use anonyamous structs as context keys to avoid collisions and ensure type safety.
1 parent 2d6e43d commit 3ce3c3b

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

pkg/context/token.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ import (
66
"github.com/github/github-mcp-server/pkg/utils"
77
)
88

9-
// tokenCtxKey is a context key for authentication token information
10-
type tokenCtx string
11-
12-
var tokenCtxKey tokenCtx = "tokenctx"
9+
type tokenCtxKey struct{}
1310

1411
type TokenInfo struct {
1512
Token string
@@ -18,29 +15,27 @@ type TokenInfo struct {
1815

1916
// WithTokenInfo adds TokenInfo to the context
2017
func WithTokenInfo(ctx context.Context, tokenInfo *TokenInfo) context.Context {
21-
return context.WithValue(ctx, tokenCtxKey, tokenInfo)
18+
return context.WithValue(ctx, tokenCtxKey{}, tokenInfo)
2219
}
2320

2421
// GetTokenInfo retrieves the authentication token from the context
2522
func GetTokenInfo(ctx context.Context) (*TokenInfo, bool) {
26-
if tokenInfo, ok := ctx.Value(tokenCtxKey).(*TokenInfo); ok {
23+
if tokenInfo, ok := ctx.Value(tokenCtxKey{}).(*TokenInfo); ok {
2724
return tokenInfo, true
2825
}
2926
return nil, false
3027
}
3128

32-
type TokenScopesKey tokenCtx
33-
34-
var tokenScopesKey TokenScopesKey = "tokenscopesctx"
29+
type tokenScopesKey struct{}
3530

3631
// WithTokenScopes adds token scopes to the context
3732
func WithTokenScopes(ctx context.Context, scopes []string) context.Context {
38-
return context.WithValue(ctx, tokenScopesKey, scopes)
33+
return context.WithValue(ctx, tokenScopesKey{}, scopes)
3934
}
4035

4136
// GetTokenScopes retrieves token scopes from the context
4237
func GetTokenScopes(ctx context.Context) ([]string, bool) {
43-
if scopes, ok := ctx.Value(tokenScopesKey).([]string); ok {
38+
if scopes, ok := ctx.Value(tokenScopesKey{}).([]string); ok {
4439
return scopes, true
4540
}
4641
return nil, false

0 commit comments

Comments
 (0)