/
auth.go
144 lines (118 loc) · 2.97 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package config
import (
"errors"
"fmt"
"strings"
)
// ErrAuthStringEmpty is the error returned with authentication is provided,
// but empty.
var ErrAuthStringEmpty = errors.New("auth: cannot be empty")
// AuthConfig is the HTTP basic authentication data.
// Skip passwords in json output that is used for logging.
type AuthConfig struct {
Enabled *bool `mapstructure:"enabled"`
Username *string `mapstructure:"username"`
Password *string `mapstructure:"password" json:"-"`
}
// DefaultAuthConfig is the default configuration.
func DefaultAuthConfig() *AuthConfig {
return &AuthConfig{}
}
// ParseAuthConfig parses the auth into username:password.
func ParseAuthConfig(s string) (*AuthConfig, error) {
if s == "" {
return nil, ErrAuthStringEmpty
}
var a AuthConfig
if strings.Contains(s, ":") {
split := strings.SplitN(s, ":", 2)
a.Username = String(split[0])
a.Password = String(split[1])
} else {
a.Username = String(s)
}
return &a, nil
}
// Copy returns a deep copy of this configuration.
func (c *AuthConfig) Copy() *AuthConfig {
if c == nil {
return nil
}
var o AuthConfig
o.Enabled = c.Enabled
o.Username = c.Username
o.Password = c.Password
return &o
}
// Merge combines all values in this configuration with the values in the other
// configuration, with values in the other configuration taking precedence.
// Maps and slices are merged, most other values are overwritten. Complex
// structs define their own merge functionality.
func (c *AuthConfig) Merge(o *AuthConfig) *AuthConfig {
if c == nil {
if o == nil {
return nil
}
return o.Copy()
}
if o == nil {
return c.Copy()
}
r := c.Copy()
if o.Enabled != nil {
r.Enabled = o.Enabled
}
if o.Username != nil {
r.Username = o.Username
}
if o.Password != nil {
r.Password = o.Password
}
return r
}
// Finalize ensures there no nil pointers.
func (c *AuthConfig) Finalize() {
if c.Enabled == nil {
c.Enabled = Bool(false ||
StringPresent(c.Username) ||
StringPresent(c.Password))
}
if c.Username == nil {
c.Username = String("")
}
if c.Password == nil {
c.Password = String("")
}
if c.Enabled == nil {
c.Enabled = Bool(*c.Username != "" || *c.Password != "")
}
}
// GoString defines the printable version of this struct.
func (c *AuthConfig) GoString() string {
if c == nil {
return "(*AuthConfig)(nil)"
}
return fmt.Sprintf("&AuthConfig{"+
"Enabled:%s, "+
"Username:%s, "+
"Password:%s"+
"}",
BoolGoString(c.Enabled),
StringGoString(c.Username),
StringGoString(c.Password),
)
}
// String is the string representation of this authentication. If authentication
// is not enabled, this returns the empty string. The username and password will
// be separated by a colon.
func (c *AuthConfig) String() string {
if !BoolVal(c.Enabled) {
return ""
}
if c.Password != nil {
return fmt.Sprintf("%s:%s", StringVal(c.Username), StringVal(c.Password))
}
return StringVal(c.Username)
}