forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_login.go
188 lines (162 loc) · 4.73 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
package appId
import (
"crypto/sha1"
"crypto/subtle"
"encoding/hex"
"fmt"
"net"
"reflect"
"sort"
"strings"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
func pathLogin(b *backend) *framework.Path {
return &framework.Path{
Pattern: "login",
Fields: map[string]*framework.FieldSchema{
"app_id": &framework.FieldSchema{
Type: framework.TypeString,
Description: "The unique app ID",
},
"user_id": &framework.FieldSchema{
Type: framework.TypeString,
Description: "The unique user ID",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathLogin,
},
HelpSynopsis: pathLoginSyn,
HelpDescription: pathLoginDesc,
}
}
func (b *backend) pathLogin(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
appId := data.Get("app_id").(string)
userId := data.Get("user_id").(string)
var displayName string
if dispName, resp, err := b.verifyCredentials(req, appId, userId); err != nil {
return nil, err
} else if resp != nil {
return resp, nil
} else {
displayName = dispName
}
// Get the policies associated with the app
policies, err := b.MapAppId.Policies(req.Storage, appId)
if err != nil {
return nil, err
}
// Store hashes of the app ID and user ID for the metadata
appIdHash := sha1.Sum([]byte(appId))
userIdHash := sha1.Sum([]byte(userId))
metadata := map[string]string{
"app-id": "sha1:" + hex.EncodeToString(appIdHash[:]),
"user-id": "sha1:" + hex.EncodeToString(userIdHash[:]),
}
return &logical.Response{
Auth: &logical.Auth{
InternalData: map[string]interface{}{
"app-id": appId,
"user-id": userId,
},
DisplayName: displayName,
Policies: policies,
Metadata: metadata,
LeaseOptions: logical.LeaseOptions{
Renewable: true,
},
},
}, nil
}
func (b *backend) pathLoginRenew(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
appId := req.Auth.InternalData["app-id"].(string)
userId := req.Auth.InternalData["user-id"].(string)
// Skipping CIDR verification to enable renewal from machines other than
// the ones encompassed by CIDR block.
if _, resp, err := b.verifyCredentials(req, appId, userId); err != nil {
return nil, err
} else if resp != nil {
return resp, nil
}
// Get the policies associated with the app
policies, err := b.MapAppId.Policies(req.Storage, appId)
if err != nil {
return nil, err
}
sort.Strings(req.Auth.Policies)
if !reflect.DeepEqual(policies, req.Auth.Policies) {
return logical.ErrorResponse("policies do not match"), nil
}
return framework.LeaseExtend(0, 0, b.System())(req, d)
}
func (b *backend) verifyCredentials(req *logical.Request, appId, userId string) (string, *logical.Response, error) {
// Ensure both appId and userId are provided
if appId == "" || userId == "" {
return "", logical.ErrorResponse("missing 'app_id' or 'user_id'"), nil
}
// Look up the apps that this user is allowed to access
appsMap, err := b.MapUserId.Get(req.Storage, userId)
if err != nil {
return "", nil, err
}
if appsMap == nil {
return "", logical.ErrorResponse("invalid user ID or app ID"), nil
}
// If there is a CIDR block restriction, check that
if raw, ok := appsMap["cidr_block"]; ok {
_, cidr, err := net.ParseCIDR(raw.(string))
if err != nil {
return "", nil, fmt.Errorf("invalid restriction cidr: %s", err)
}
var addr string
if req.Connection != nil {
addr = req.Connection.RemoteAddr
}
if addr == "" || !cidr.Contains(net.ParseIP(addr)) {
return "", logical.ErrorResponse("unauthorized source address"), nil
}
}
appsRaw, ok := appsMap["value"]
if !ok {
appsRaw = ""
}
apps, ok := appsRaw.(string)
if !ok {
return "", nil, fmt.Errorf("internal error: mapping is not a string")
}
// Verify that the app is in the list
found := false
appIdBytes := []byte(appId)
for _, app := range strings.Split(apps, ",") {
match := []byte(strings.TrimSpace(app))
// Protect against a timing attack with the app_id comparison
if subtle.ConstantTimeCompare(match, appIdBytes) == 1 {
found = true
}
}
if !found {
return "", logical.ErrorResponse("invalid user ID or app ID"), nil
}
// Get the raw data associated with the app
appRaw, err := b.MapAppId.Get(req.Storage, appId)
if err != nil {
return "", nil, err
}
if appRaw == nil {
return "", logical.ErrorResponse("invalid user ID or app ID"), nil
}
var displayName string
if raw, ok := appRaw["display_name"]; ok {
displayName = raw.(string)
}
return displayName, nil, nil
}
const pathLoginSyn = `
Log in with an App ID and User ID.
`
const pathLoginDesc = `
This endpoint authenticates using an application ID, user ID and potential the IP address of the connecting client.
`