-
Notifications
You must be signed in to change notification settings - Fork 162
/
creds.go
90 lines (68 loc) · 1.43 KB
/
creds.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
package config
import (
"strings"
boshuaa "github.com/cloudfoundry/bosh-cli/uaa"
)
type Creds struct {
Username string
Password string
// For UAA clients
Client string
ClientSecret string
// For UAA users
RefreshToken string
}
func (c Creds) IsBasic() bool {
return len(c.Username) > 0
}
func (c Creds) IsBasicComplete() bool {
return len(c.Username) > 0 && len(c.Password) > 0
}
func (c Creds) IsUAA() bool {
return len(c.Client) > 0 || len(c.RefreshToken) > 0
}
func (c Creds) IsUAAClient() bool {
// Clients dont use refresh tokens
return len(c.Client) > 0
}
func (c Creds) Description() string {
if len(c.Username) > 0 {
return credsDesc{name: c.Username, kind: "user"}.String()
}
if len(c.RefreshToken) > 0 {
info, err := boshuaa.NewTokenInfoFromValue(c.RefreshToken)
if err != nil {
return credsDesc{name: "?"}.String()
}
desc := credsDesc{
name: info.Username,
kind: "user",
misc: strings.Join(info.Scopes, ", "),
}
return desc.String()
}
if len(c.Client) > 0 {
return credsDesc{name: c.Client, kind: "client"}.String()
}
return credsDesc{full: "anonymous user"}.String()
}
type credsDesc struct {
name string
kind string
misc string
full string
}
func (c credsDesc) String() string {
if len(c.full) > 0 {
return c.full
}
str := ""
if len(c.kind) > 0 {
str = c.kind + " "
}
str += "'" + c.name + "'"
if len(c.misc) > 0 {
str += " (" + c.misc + ")"
}
return str
}