-
Notifications
You must be signed in to change notification settings - Fork 301
/
user_access.go
206 lines (184 loc) · 7 KB
/
user_access.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
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package identityserver
import (
"context"
"github.com/jinzhu/gorm"
"go.thethings.network/lorawan-stack/v3/pkg/auth/rights"
"go.thethings.network/lorawan-stack/v3/pkg/email"
"go.thethings.network/lorawan-stack/v3/pkg/events"
"go.thethings.network/lorawan-stack/v3/pkg/identityserver/emails"
"go.thethings.network/lorawan-stack/v3/pkg/identityserver/store"
"go.thethings.network/lorawan-stack/v3/pkg/log"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
)
var (
evtCreateUserAPIKey = events.Define(
"user.api-key.create", "create user API key",
events.WithVisibility(ttnpb.RIGHT_USER_SETTINGS_API_KEYS),
events.WithAuthFromContext(),
events.WithClientInfoFromContext(),
)
evtUpdateUserAPIKey = events.Define(
"user.api-key.update", "update user API key",
events.WithVisibility(ttnpb.RIGHT_USER_SETTINGS_API_KEYS),
events.WithAuthFromContext(),
events.WithClientInfoFromContext(),
)
evtDeleteUserAPIKey = events.Define(
"user.api-key.delete", "delete user API key",
events.WithVisibility(ttnpb.RIGHT_USER_SETTINGS_API_KEYS),
events.WithAuthFromContext(),
events.WithClientInfoFromContext(),
)
)
func (is *IdentityServer) listUserRights(ctx context.Context, ids *ttnpb.UserIdentifiers) (*ttnpb.Rights, error) {
usrRights, err := rights.ListUser(ctx, *ids)
if err != nil {
return nil, err
}
return usrRights.Intersect(ttnpb.AllEntityRights.Union(ttnpb.AllOrganizationRights, ttnpb.AllUserRights)), nil
}
func (is *IdentityServer) createUserAPIKey(ctx context.Context, req *ttnpb.CreateUserAPIKeyRequest) (key *ttnpb.APIKey, err error) {
// Require that caller has rights to manage API keys.
if err = rights.RequireUser(ctx, req.UserIdentifiers, ttnpb.RIGHT_USER_SETTINGS_API_KEYS); err != nil {
return nil, err
}
// Require that caller has at least the rights of the API key.
if err = rights.RequireUser(ctx, req.UserIdentifiers, req.Rights...); err != nil {
return nil, err
}
key, token, err := generateAPIKey(ctx, req.Name, req.Rights...)
if err != nil {
return nil, err
}
err = is.withDatabase(ctx, func(db *gorm.DB) error {
return store.GetAPIKeyStore(db).CreateAPIKey(ctx, req.UserIdentifiers, key)
})
if err != nil {
return nil, err
}
key.Key = token
events.Publish(evtCreateUserAPIKey.NewWithIdentifiersAndData(ctx, req.UserIdentifiers, nil))
err = is.SendUserEmail(ctx, &req.UserIdentifiers, func(data emails.Data) email.MessageData {
data.SetEntity(req.EntityIdentifiers())
return &emails.APIKeyCreated{Data: data, Identifier: key.PrettyName(), Rights: key.Rights}
})
if err != nil {
log.FromContext(ctx).WithError(err).Error("Could not send API key created notification email")
}
return key, nil
}
func (is *IdentityServer) listUserAPIKeys(ctx context.Context, req *ttnpb.ListUserAPIKeysRequest) (keys *ttnpb.APIKeys, err error) {
if err = rights.RequireUser(ctx, req.UserIdentifiers, ttnpb.RIGHT_USER_SETTINGS_API_KEYS); err != nil {
return nil, err
}
var total uint64
ctx = store.WithPagination(ctx, req.Limit, req.Page, &total)
defer func() {
if err == nil {
setTotalHeader(ctx, total)
}
}()
keys = &ttnpb.APIKeys{}
err = is.withDatabase(ctx, func(db *gorm.DB) (err error) {
keys.APIKeys, err = store.GetAPIKeyStore(db).FindAPIKeys(ctx, req.UserIdentifiers)
return err
})
if err != nil {
return nil, err
}
for _, key := range keys.APIKeys {
key.Key = ""
}
return keys, nil
}
func (is *IdentityServer) getUserAPIKey(ctx context.Context, req *ttnpb.GetUserAPIKeyRequest) (key *ttnpb.APIKey, err error) {
if err = rights.RequireUser(ctx, req.UserIdentifiers, ttnpb.RIGHT_USER_SETTINGS_API_KEYS); err != nil {
return nil, err
}
err = is.withDatabase(ctx, func(db *gorm.DB) (err error) {
_, key, err = store.GetAPIKeyStore(db).GetAPIKey(ctx, req.KeyID)
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
key.Key = ""
return key, nil
}
func (is *IdentityServer) updateUserAPIKey(ctx context.Context, req *ttnpb.UpdateUserAPIKeyRequest) (key *ttnpb.APIKey, err error) {
// Require that caller has rights to manage API keys.
if err = rights.RequireUser(ctx, req.UserIdentifiers, ttnpb.RIGHT_USER_SETTINGS_API_KEYS); err != nil {
return nil, err
}
err = is.withDatabase(ctx, func(db *gorm.DB) (err error) {
if len(req.APIKey.Rights) > 0 {
_, key, err := store.GetAPIKeyStore(db).GetAPIKey(ctx, req.APIKey.ID)
if err != nil {
return err
}
newRights := ttnpb.RightsFrom(req.APIKey.Rights...)
existingRights := ttnpb.RightsFrom(key.Rights...)
// Require the caller to have all added rights.
if err := rights.RequireUser(ctx, req.UserIdentifiers, newRights.Sub(existingRights).GetRights()...); err != nil {
return err
}
// Require the caller to have all removed rights.
if err := rights.RequireUser(ctx, req.UserIdentifiers, existingRights.Sub(newRights).GetRights()...); err != nil {
return err
}
}
key, err = store.GetAPIKeyStore(db).UpdateAPIKey(ctx, req.UserIdentifiers, &req.APIKey)
return err
})
if err != nil {
return nil, err
}
if key == nil { // API key was deleted.
events.Publish(evtDeleteUserAPIKey.NewWithIdentifiersAndData(ctx, req.UserIdentifiers, nil))
return &ttnpb.APIKey{}, nil
}
key.Key = ""
events.Publish(evtUpdateUserAPIKey.NewWithIdentifiersAndData(ctx, req.UserIdentifiers, nil))
err = is.SendUserEmail(ctx, &req.UserIdentifiers, func(data emails.Data) email.MessageData {
data.SetEntity(req.EntityIdentifiers())
return &emails.APIKeyChanged{Data: data, Identifier: key.PrettyName(), Rights: key.Rights}
})
if err != nil {
log.FromContext(ctx).WithError(err).Error("Could not send API key update notification email")
}
return key, nil
}
type userAccess struct {
*IdentityServer
}
func (ua *userAccess) ListRights(ctx context.Context, req *ttnpb.UserIdentifiers) (*ttnpb.Rights, error) {
return ua.listUserRights(ctx, req)
}
func (ua *userAccess) GetAPIKey(ctx context.Context, req *ttnpb.GetUserAPIKeyRequest) (*ttnpb.APIKey, error) {
return ua.getUserAPIKey(ctx, req)
}
func (ua *userAccess) CreateAPIKey(ctx context.Context, req *ttnpb.CreateUserAPIKeyRequest) (*ttnpb.APIKey, error) {
return ua.createUserAPIKey(ctx, req)
}
func (ua *userAccess) ListAPIKeys(ctx context.Context, req *ttnpb.ListUserAPIKeysRequest) (*ttnpb.APIKeys, error) {
return ua.listUserAPIKeys(ctx, req)
}
func (ua *userAccess) UpdateAPIKey(ctx context.Context, req *ttnpb.UpdateUserAPIKeyRequest) (*ttnpb.APIKey, error) {
return ua.updateUserAPIKey(ctx, req)
}