forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creds.go
81 lines (62 loc) · 1.33 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
package config
import (
"strings"
boshuaa "github.com/cloudfoundry/bosh-cli/uaa"
)
type Creds struct {
// Basic auth username/password or UAA client creds
Client string
ClientSecret string
// For UAA users
AccessTokenType string
AccessToken string
RefreshToken string
}
func (c Creds) IsBasicComplete() bool {
return len(c.Client) > 0 && len(c.ClientSecret) > 0
}
func (c Creds) IsUAAClient() bool {
// Clients dont use refresh tokens
return len(c.Client) > 0
}
func (c Creds) IsUAA() bool {
return c.IsUAAClient() || len(c.RefreshToken) > 0
}
func (c Creds) Description() 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
}