forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_login.go
142 lines (121 loc) · 3.41 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
package appId
import (
"crypto/sha1"
"crypto/subtle"
"encoding/hex"
"fmt"
"net"
"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.WriteOperation: 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)
// 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
}
// Get the policies associated with the app
policies, err := b.MapAppId.Policies(req.Storage, appId)
if err != nil {
return nil, err
}
// Check if we have a display name
var displayName string
if raw, ok := appRaw["display_name"]; ok {
displayName = raw.(string)
}
// 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{
DisplayName: displayName,
Policies: policies,
Metadata: metadata,
},
}, 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.
`