-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
path_login.go
223 lines (189 loc) · 5.8 KB
/
path_login.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package radius
import (
"context"
"fmt"
"net"
"strconv"
"strings"
"time"
"layeh.com/radius"
. "layeh.com/radius/rfc2865"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/helper/cidrutil"
"github.com/hashicorp/vault/sdk/helper/policyutil"
"github.com/hashicorp/vault/sdk/logical"
)
func pathLogin(b *backend) *framework.Path {
return &framework.Path{
Pattern: "login" + framework.OptionalParamRegex("urlusername"),
Fields: map[string]*framework.FieldSchema{
"urlusername": {
Type: framework.TypeString,
Description: "Username to be used for login. (URL parameter)",
},
"username": {
Type: framework.TypeString,
Description: "Username to be used for login. (POST request body)",
},
"password": {
Type: framework.TypeString,
Description: "Password for this user.",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathLogin,
logical.AliasLookaheadOperation: b.pathLoginAliasLookahead,
},
HelpSynopsis: pathLoginSyn,
HelpDescription: pathLoginDesc,
}
}
func (b *backend) pathLoginAliasLookahead(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
username := d.Get("username").(string)
if username == "" {
return nil, fmt.Errorf("missing username")
}
return &logical.Response{
Auth: &logical.Auth{
Alias: &logical.Alias{
Name: username,
},
},
}, nil
}
func (b *backend) pathLogin(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
cfg, err := b.Config(ctx, req)
if err != nil {
return nil, err
}
if cfg == nil {
return logical.ErrorResponse("radius backend not configured"), nil
}
// Check for a CIDR match.
if len(cfg.TokenBoundCIDRs) > 0 {
if req.Connection == nil {
b.Logger().Warn("token bound CIDRs found but no connection information available for validation")
return nil, logical.ErrPermissionDenied
}
if !cidrutil.RemoteAddrIsOk(req.Connection.RemoteAddr, cfg.TokenBoundCIDRs) {
return nil, logical.ErrPermissionDenied
}
}
username := d.Get("username").(string)
password := d.Get("password").(string)
if username == "" {
username = d.Get("urlusername").(string)
if username == "" {
return logical.ErrorResponse("username cannot be empty"), nil
}
}
if password == "" {
return logical.ErrorResponse("password cannot be empty"), nil
}
policies, resp, err := b.RadiusLogin(ctx, req, username, password)
// Handle an internal error
if err != nil {
return nil, err
}
if resp != nil {
// Handle a logical error
if resp.IsError() {
return resp, nil
}
}
auth := &logical.Auth{
Metadata: map[string]string{
"username": username,
"policies": strings.Join(policies, ","),
},
InternalData: map[string]interface{}{
"password": password,
},
DisplayName: username,
Alias: &logical.Alias{
Name: username,
},
}
cfg.PopulateTokenAuth(auth)
resp.Auth = auth
if policies != nil {
resp.Auth.Policies = append(resp.Auth.Policies, policies...)
}
return resp, nil
}
func (b *backend) pathLoginRenew(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
cfg, err := b.Config(ctx, req)
if err != nil {
return nil, err
}
if cfg == nil {
return logical.ErrorResponse("radius backend not configured"), nil
}
username := req.Auth.Metadata["username"]
password := req.Auth.InternalData["password"].(string)
var resp *logical.Response
var loginPolicies []string
loginPolicies, resp, err = b.RadiusLogin(ctx, req, username, password)
if err != nil || (resp != nil && resp.IsError()) {
return resp, err
}
finalPolicies := cfg.TokenPolicies
if loginPolicies != nil {
finalPolicies = append(finalPolicies, loginPolicies...)
}
if !policyutil.EquivalentPolicies(finalPolicies, req.Auth.TokenPolicies) {
return nil, fmt.Errorf("policies have changed, not renewing")
}
req.Auth.Period = cfg.TokenPeriod
req.Auth.TTL = cfg.TokenTTL
req.Auth.MaxTTL = cfg.TokenMaxTTL
return &logical.Response{Auth: req.Auth}, nil
}
func (b *backend) RadiusLogin(ctx context.Context, req *logical.Request, username string, password string) ([]string, *logical.Response, error) {
cfg, err := b.Config(ctx, req)
if err != nil {
return nil, nil, err
}
if cfg == nil || cfg.Host == "" || cfg.Secret == "" {
return nil, logical.ErrorResponse("radius backend not configured"), nil
}
hostport := net.JoinHostPort(cfg.Host, strconv.Itoa(cfg.Port))
packet := radius.New(radius.CodeAccessRequest, []byte(cfg.Secret))
UserName_SetString(packet, username)
UserPassword_SetString(packet, password)
if cfg.NasIdentifier != "" {
NASIdentifier_AddString(packet, cfg.NasIdentifier)
}
packet.Add(5, radius.NewInteger(uint32(cfg.NasPort)))
client := radius.Client{
Dialer: net.Dialer{
Timeout: time.Duration(cfg.DialTimeout) * time.Second,
},
}
clientCtx, cancelFunc := context.WithTimeout(ctx, time.Duration(cfg.ReadTimeout)*time.Second)
received, err := client.Exchange(clientCtx, packet, hostport)
cancelFunc()
if err != nil {
return nil, logical.ErrorResponse(err.Error()), nil
}
if received.Code != radius.CodeAccessAccept {
return nil, logical.ErrorResponse("access denied by the authentication server"), nil
}
policies := cfg.UnregisteredUserPolicies
// Retrieve user entry from storage
user, err := b.user(ctx, req.Storage, username)
if err != nil {
return nil, logical.ErrorResponse("could not retrieve user entry from storage"), err
}
if user != nil {
policies = user.Policies
}
return policies, &logical.Response{}, nil
}
const pathLoginSyn = `
Log in with a username and password.
`
const pathLoginDesc = `
This endpoint authenticates using a username and password. Please be sure to
read the note on escaping from the path-help for the 'config' endpoint.
`