-
Notifications
You must be signed in to change notification settings - Fork 10
/
credentials.go
121 lines (103 loc) · 2.94 KB
/
credentials.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
package ydb
import (
"context"
"errors"
)
var (
// ErrCredentialsNoCredentials may be returned by Credentials implementations to
// make driver act as if there no Credentials at all. That is, driver will
// not send any token meta information during request.
ErrCredentialsNoCredentials = errors.New("ydb: credentials: no credentials")
)
// Credentials is an interface that contains options used to authorize a
// client.
type Credentials interface {
Token(context.Context) (string, error)
}
// CredentialsFunc is an adapter to allow the use of ordinary functions as
// Credentials.
type CredentialsFunc func(context.Context) (string, error)
// Token implements Credentials.
func (f CredentialsFunc) Token(ctx context.Context) (string, error) {
return f(ctx)
}
// Token implements Credentials.
func (f CredentialsFunc) String() string {
return "CredentialsFunc"
}
// AuthTokenCredentials implements Credentials interface with static
// authorization parameters.
type AuthTokenCredentials struct {
AuthToken string
sourceInfo string
}
func NewAuthTokenCredentials(authToken string, sourceInfo string) *AuthTokenCredentials {
return &AuthTokenCredentials{
AuthToken: authToken,
sourceInfo: sourceInfo,
}
}
// Token implements Credentials.
func (a AuthTokenCredentials) Token(_ context.Context) (string, error) {
return a.AuthToken, nil
}
// Token implements Credentials.
func (a AuthTokenCredentials) String() string {
if a.sourceInfo == "" {
return "AuthTokenCredentials"
}
return "AuthTokenCredentials created from " + a.sourceInfo
}
// anonymousCredentials implements Credentials interface with anonymous access
type anonymousCredentials struct {
sourceInfo string
}
func NewAnonymousCredentials(sourceInfo string) *anonymousCredentials {
return &anonymousCredentials{
sourceInfo: sourceInfo,
}
}
// Token implements Credentials.
func (a anonymousCredentials) Token(_ context.Context) (string, error) {
return "", nil
}
// Token implements Credentials.
func (a anonymousCredentials) String() string {
if a.sourceInfo == "" {
return "anonymousCredentials"
}
return "anonymousCredentials created from " + a.sourceInfo
}
type multiCredentials struct {
cs []Credentials
}
func (m *multiCredentials) Token(ctx context.Context) (token string, err error) {
for _, c := range m.cs {
token, err = c.Token(ctx)
if err == nil {
return
}
}
if err == nil {
err = ErrCredentialsNoCredentials
}
return
}
// MultiCredentials creates Credentials which represents multiple ways of
// obtaining token.
// Its Token() method proxies call to the underlying credentials in order.
// When first successful call met, it returns. If there are no successful
// calls, it returns last error.
func MultiCredentials(cs ...Credentials) Credentials {
all := make([]Credentials, 0, len(cs))
for _, c := range cs {
if m, ok := c.(*multiCredentials); ok {
all = append(all, m.cs...)
} else {
all = append(all, c)
}
}
return &multiCredentials{
cs: all,
}
}