forked from couchbase/gocb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
87 lines (72 loc) · 2.19 KB
/
auth.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
package gocb
// Authenticator provides an interface to authenticate to each service.
type Authenticator interface {
clusterMgmt() userPassPair
clusterN1ql() []userPassPair
clusterFts() []userPassPair
bucketMemd(bucket string) string
bucketMgmt(bucket string) userPassPair
bucketViews(bucket string) userPassPair
bucketN1ql(bucket string) []userPassPair
bucketFts(bucket string) []userPassPair
}
// Provides a password for a single bucket.
type BucketAuthenticator struct {
Password string
}
type userPassPair struct {
Username string `json:"user"`
Password string `json:"pass"`
}
// BucketAuthenticatorMap is a map of bucket name to BucketAuthenticator.
type BucketAuthenticatorMap map[string]BucketAuthenticator
// Authenticator which uses a list of buckets and passwords.
type ClusterAuthenticator struct {
Buckets BucketAuthenticatorMap
Username string
Password string
}
func (ca ClusterAuthenticator) clusterMgmt() userPassPair {
return userPassPair{ca.Username, ca.Password}
}
func (ca ClusterAuthenticator) clusterAll() []userPassPair {
userPassList := make([]userPassPair, len(ca.Buckets))
for bucket, auth := range ca.Buckets {
userPassList = append(userPassList, userPassPair{
Username: bucket,
Password: auth.Password,
})
}
return userPassList
}
func (ca ClusterAuthenticator) clusterN1ql() []userPassPair {
return ca.clusterAll()
}
func (ca ClusterAuthenticator) clusterFts() []userPassPair {
return ca.clusterAll()
}
func (ca ClusterAuthenticator) bucketAll(bucket string) userPassPair {
if bucketAuth, ok := ca.Buckets[bucket]; ok {
return userPassPair{bucket, bucketAuth.Password}
}
return userPassPair{"", ""}
}
func (ca ClusterAuthenticator) bucketMemd(bucket string) string {
return ca.bucketAll(bucket).Password
}
func (ca ClusterAuthenticator) bucketMgmt(bucket string) userPassPair {
return ca.bucketAll(bucket)
}
func (ca ClusterAuthenticator) bucketViews(bucket string) userPassPair {
return ca.bucketAll(bucket)
}
func (ca ClusterAuthenticator) bucketN1ql(bucket string) []userPassPair {
return []userPassPair{
ca.bucketAll(bucket),
}
}
func (ca ClusterAuthenticator) bucketFts(bucket string) []userPassPair {
return []userPassPair{
ca.bucketAll(bucket),
}
}