-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathtoken.go
More file actions
74 lines (62 loc) · 1.95 KB
/
token.go
File metadata and controls
74 lines (62 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package context
import (
"context"
"github.com/github/github-mcp-server/pkg/utils"
)
type tokenCtxKey struct{}
type TokenInfo struct {
Token string
TokenType utils.TokenType
}
// WithTokenInfo adds TokenInfo to the context
func WithTokenInfo(ctx context.Context, tokenInfo *TokenInfo) context.Context {
return context.WithValue(ctx, tokenCtxKey{}, tokenInfo)
}
// GetTokenInfo retrieves the authentication token from the context
func GetTokenInfo(ctx context.Context) (*TokenInfo, bool) {
if tokenInfo, ok := ctx.Value(tokenCtxKey{}).(*TokenInfo); ok {
return tokenInfo, true
}
return nil, false
}
type tokenScopesKey struct{}
type tokenScopesValue struct {
Token string
Scopes []string
}
// WithTokenScopes adds token scopes to the context
func WithTokenScopes(ctx context.Context, scopes []string) context.Context {
return context.WithValue(ctx, tokenScopesKey{}, scopes)
}
// WithTokenScopesForToken adds token scopes and the associated token to the context.
func WithTokenScopesForToken(ctx context.Context, token string, scopes []string) context.Context {
return context.WithValue(ctx, tokenScopesKey{}, tokenScopesValue{
Token: token,
Scopes: scopes,
})
}
// GetTokenScopes retrieves token scopes from the context
func GetTokenScopes(ctx context.Context) ([]string, bool) {
if scoped, ok := ctx.Value(tokenScopesKey{}).(tokenScopesValue); ok {
return scoped.Scopes, true
}
if scopes, ok := ctx.Value(tokenScopesKey{}).([]string); ok {
return scopes, true
}
return nil, false
}
// GetTokenScopesForToken retrieves token scopes only when they are bound to the active token.
func GetTokenScopesForToken(ctx context.Context, token string) ([]string, bool) {
if scoped, ok := ctx.Value(tokenScopesKey{}).(tokenScopesValue); ok {
if scoped.Token == token {
return scoped.Scopes, true
}
return nil, false
}
if token == "" {
if scopes, ok := ctx.Value(tokenScopesKey{}).([]string); ok {
return scopes, true
}
}
return nil, false
}