/
credentials_userpass.go
51 lines (46 loc) · 1.48 KB
/
credentials_userpass.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
package credentials
import (
"fmt"
"strings"
)
// UsernamePasswordCredentials represents credentials composed of a username and a password
type UsernamePasswordCredentials struct {
Base `mapstructure:",squash"`
Username string
Password string
}
// NewUsernamePassword instantiates a UsernamePasswordCredentials struct
func NewUsernamePassword() *UsernamePasswordCredentials {
cred := &UsernamePasswordCredentials{}
cred.CredType = "Username/Password"
return cred
}
// ToString prints out the content of a UsernamePasswordCredentials struct.
// If showSensitive is true, the password will be shown
func (cred *UsernamePasswordCredentials) ToString(showSensitive bool) string {
password := "********"
username := cred.Username
if showSensitive {
password = cred.Password
}
if cred.Username == "" {
username = "<empty>"
}
if password == "" {
password = "<empty>"
}
return fmt.Sprintf("%s - %s:%s", cred.BaseToString(), username, password)
}
// Validate verifies that the credentials is valid.
// A UsernamePasswordCredentials is always considered valid, as empty values are accepted.
func (cred *UsernamePasswordCredentials) Validate() error {
if cred.Username == "" && cred.Password == "" && cred.Value != "" {
splitValue := strings.Split(cred.Value, ":")
if len(splitValue) != 2 {
return fmt.Errorf("The credentials with ID %s has an invalid username:password value: %s", cred.ID, cred.Value)
}
cred.Username = splitValue[0]
cred.Password = splitValue[1]
}
return nil
}