-
Notifications
You must be signed in to change notification settings - Fork 258
/
static_token_provider.go
135 lines (118 loc) · 4.34 KB
/
static_token_provider.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2020-2024 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bufconnect
import (
"errors"
"fmt"
"strings"
"github.com/bufbuild/buf/private/pkg/app"
)
// NewTokenProviderFromContainer creates a singleTokenProvider from the BUF_TOKEN environment variable
func NewTokenProviderFromContainer(container app.EnvContainer) (TokenProvider, error) {
return newTokenProviderFromString(container.Env(TokenEnvKey), true)
}
// NewTokenProviderFromString creates a singleTokenProvider by the token provided
func NewTokenProviderFromString(token string) (TokenProvider, error) {
return newTokenProviderFromString(token, false)
}
// newTokenProviderFromString returns a TokenProvider with auth keys from the provided token. The
// remote token is in the format: token1@remote1,token2@remote2.
// The special characters `@` and `,` are used as the splitters. The tokens and remote addresses
// do not contain these characters since they are enforced by the rules in BSR.
func newTokenProviderFromString(token string, isFromEnvVar bool) (TokenProvider, error) {
if token == "" {
return nopTokenProvider{}, nil
}
// Tokens for different remotes are separated by `,`. Using strings.Split to separate the string into remote tokens.
tokens := strings.Split(token, ",")
if len(tokens) == 1 {
if strings.Contains(tokens[0], "@") {
return newMultipleTokenProvider(tokens, isFromEnvVar)
}
return newSingleTokenProvider(tokens[0], isFromEnvVar)
}
return newMultipleTokenProvider(tokens, isFromEnvVar)
}
// singleTokenProvider is used to provide set of authentication tokenToAuthKey.
type singleTokenProvider struct {
// true: the tokenSet is generated from environment variable tokenEnvKey
// false: otherwise
setBufTokenEnvVar bool
token string
}
func newSingleTokenProvider(token string, isFromEnvVar bool) (*singleTokenProvider, error) {
if strings.Contains(token, "@") {
return nil, errors.New("token cannot contain special character `@`")
}
if strings.Contains(token, ",") {
return nil, errors.New("token cannot contain special character `,`")
}
if token == "" {
return nil, errors.New("single token cannot be empty")
}
return &singleTokenProvider{
setBufTokenEnvVar: isFromEnvVar,
token: token,
}, nil
}
// RemoteToken finds the token by the remote address
func (t *singleTokenProvider) RemoteToken(address string) string {
return t.token
}
func (t *singleTokenProvider) IsFromEnvVar() bool {
return t.setBufTokenEnvVar
}
type multipleTokenProvider struct {
addressToToken map[string]string
isFromEnvVar bool
}
func newMultipleTokenProvider(tokens []string, isFromEnvVar bool) (*multipleTokenProvider, error) {
addressToToken := make(map[string]string)
for _, token := range tokens {
split := strings.Split(token, "@")
if len(split) != 2 {
return nil, fmt.Errorf("invalid token: %s", token)
}
if split[0] == "" || split[1] == "" {
return nil, fmt.Errorf("invalid token: %s", token)
}
if strings.Contains(split[0], ":") {
return nil, fmt.Errorf("invalid token: %s, token cannot contain special character `:`", token)
}
if strings.Contains(split[0], ",") {
return nil, fmt.Errorf("invalid token: %s, token cannot contain special character `,`", token)
}
if _, ok := addressToToken[split[1]]; ok {
return nil, fmt.Errorf("invalid token: %s, repeated remote adddress: %s", token, split[1])
}
addressToToken[split[1]] = split[0]
}
return &multipleTokenProvider{
addressToToken: addressToToken,
isFromEnvVar: isFromEnvVar,
}, nil
}
func (m *multipleTokenProvider) RemoteToken(address string) string {
return m.addressToToken[address]
}
func (m *multipleTokenProvider) IsFromEnvVar() bool {
return m.isFromEnvVar
}
type nopTokenProvider struct{}
func (nopTokenProvider) RemoteToken(string) string {
return ""
}
func (nopTokenProvider) IsFromEnvVar() bool {
return false
}