-
Notifications
You must be signed in to change notification settings - Fork 30
/
service.go
433 lines (387 loc) · 12.1 KB
/
service.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package serviceuser
import (
"context"
"crypto/rand"
"crypto/subtle"
"encoding/base64"
"encoding/hex"
"fmt"
"io"
"golang.org/x/crypto/sha3"
"golang.org/x/crypto/bcrypt"
"github.com/google/uuid"
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/lestrrat-go/jwx/v2/jwt"
"github.com/raystack/frontier/core/relation"
"github.com/raystack/frontier/internal/bootstrap/schema"
"github.com/raystack/frontier/pkg/utils"
)
type Repository interface {
List(ctx context.Context, flt Filter) ([]ServiceUser, error)
Create(ctx context.Context, serviceUser ServiceUser) (ServiceUser, error)
GetByID(ctx context.Context, id string) (ServiceUser, error)
GetByIDs(ctx context.Context, id []string) ([]ServiceUser, error)
Delete(ctx context.Context, id string) error
}
type CredentialRepository interface {
List(ctx context.Context, flt Filter) ([]Credential, error)
Create(ctx context.Context, credential Credential) (Credential, error)
Get(ctx context.Context, id string) (Credential, error)
Delete(ctx context.Context, id string) error
}
type RelationService interface {
Create(ctx context.Context, rel relation.Relation) (relation.Relation, error)
Delete(ctx context.Context, rel relation.Relation) error
LookupSubjects(ctx context.Context, rel relation.Relation) ([]string, error)
CheckPermission(ctx context.Context, rel relation.Relation) (bool, error)
}
type Service struct {
repo Repository
credRepo CredentialRepository
relService RelationService
}
func NewService(repo Repository, credRepo CredentialRepository, relService RelationService) *Service {
return &Service{
repo: repo,
credRepo: credRepo,
relService: relService,
}
}
func (s Service) List(ctx context.Context, flt Filter) ([]ServiceUser, error) {
if flt.OrgID == "" && len(flt.ServiceUserIDs) == 0 && flt.ServiceUserID == "" {
return nil, ErrInvalidID
}
return s.repo.List(ctx, flt)
}
func (s Service) Create(ctx context.Context, serviceUser ServiceUser) (ServiceUser, error) {
createdSU, err := s.repo.Create(ctx, serviceUser)
if err != nil {
return ServiceUser{}, err
}
// attach service user to organization
_, err = s.relService.Create(ctx, relation.Relation{
Object: relation.Object{
ID: serviceUser.OrgID,
Namespace: schema.OrganizationNamespace,
},
Subject: relation.Subject{
ID: createdSU.ID,
Namespace: schema.ServiceUserPrincipal,
},
RelationName: schema.MemberRelationName,
})
if err != nil {
return ServiceUser{}, err
}
_, err = s.relService.Create(ctx, relation.Relation{
Object: relation.Object{
ID: createdSU.ID,
Namespace: schema.ServiceUserPrincipal,
},
Subject: relation.Subject{
ID: serviceUser.OrgID,
Namespace: schema.OrganizationNamespace,
},
RelationName: schema.OrganizationRelationName,
})
if err != nil {
return ServiceUser{}, err
}
if len(serviceUser.CreatedByUser) > 0 {
// TODO: write authz tests that checks if the user who created the service user
// has the permission to interact with the service user
// attach user to service user who created it
_, err = s.relService.Create(ctx, relation.Relation{
Object: relation.Object{
ID: createdSU.ID,
Namespace: schema.ServiceUserPrincipal,
},
Subject: relation.Subject{
ID: serviceUser.CreatedByUser,
Namespace: schema.UserPrincipal,
},
RelationName: schema.UserRelationName,
})
if err != nil {
return ServiceUser{}, err
}
}
return createdSU, nil
}
func (s Service) Get(ctx context.Context, id string) (ServiceUser, error) {
return s.repo.GetByID(ctx, id)
}
func (s Service) GetByIDs(ctx context.Context, ids []string) ([]ServiceUser, error) {
return s.repo.GetByIDs(ctx, ids)
}
func (s Service) ListByOrg(ctx context.Context, orgID string) ([]ServiceUser, error) {
userIDs, err := s.relService.LookupSubjects(ctx, relation.Relation{
Object: relation.Object{
ID: orgID,
Namespace: schema.OrganizationNamespace,
},
Subject: relation.Subject{
Namespace: schema.ServiceUserPrincipal,
},
RelationName: schema.MembershipPermission,
})
if err != nil {
return nil, err
}
if len(userIDs) == 0 {
// no users
return []ServiceUser{}, nil
}
return s.repo.GetByIDs(ctx, userIDs)
}
func (s Service) Delete(ctx context.Context, id string) error {
// delete all of its credentials then delete the service account
creds, err := s.credRepo.List(ctx, Filter{
ServiceUserID: id,
})
if err != nil {
return err
}
for _, cred := range creds {
if err := s.credRepo.Delete(ctx, cred.ID); err != nil {
return err
}
}
// delete all of serviceuser relationships
// before deleting the serviceuser
if err := s.relService.Delete(ctx, relation.Relation{
Subject: relation.Subject{
ID: id,
Namespace: schema.ServiceUserPrincipal,
},
}); err != nil {
return err
}
return s.repo.Delete(ctx, id)
}
func (s Service) ListKeys(ctx context.Context, serviceUserID string) ([]Credential, error) {
if serviceUserID == "" {
return nil, ErrInvalidID
}
return s.credRepo.List(ctx, Filter{
ServiceUserID: serviceUserID,
IsKey: true,
})
}
// CreateKey creates a key pair for the service user
func (s Service) CreateKey(ctx context.Context, credential Credential) (Credential, error) {
credential.ID = uuid.New().String()
// generate public/private key pair
newJWK, err := utils.CreateJWKWithKID(credential.ID)
if err != nil {
return Credential{}, fmt.Errorf("failed to create key pair: %w", err)
}
jwkPEM, err := jwk.Pem(newJWK)
if err != nil {
return Credential{}, fmt.Errorf("failed to convert jwk to pem: %w", err)
}
pubKey, err := newJWK.PublicKey()
if err != nil {
return Credential{}, err
}
// using single key for now
publicKeySet := jwk.NewSet()
if err := publicKeySet.AddKey(pubKey); err != nil {
return Credential{}, err
}
credential.PublicKey = publicKeySet
// save public key in database
credential.Type = JWTCredentialType
createdCred, err := s.credRepo.Create(ctx, credential)
if err != nil {
return Credential{}, err
}
createdCred.PrivateKey = jwkPEM
return createdCred, nil
}
func (s Service) GetKey(ctx context.Context, credID string) (Credential, error) {
cred, err := s.credRepo.Get(ctx, credID)
if err != nil {
return Credential{}, err
}
if len(cred.SecretHash) > 0 || cred.PublicKey == nil || cred.PublicKey.Len() == 0 {
return Credential{}, ErrCredNotExist
}
return cred, err
}
func (s Service) DeleteKey(ctx context.Context, credID string) error {
return s.credRepo.Delete(ctx, credID)
}
// CreateSecret creates a secret for the service user
func (s Service) CreateSecret(ctx context.Context, credential Credential) (Secret, error) {
// generate a random secret
secretBytes := make([]byte, 32)
if _, err := io.ReadFull(rand.Reader, secretBytes); err != nil {
return Secret{}, err
}
secretString := base64.RawStdEncoding.EncodeToString(secretBytes)
if sHash, err := bcrypt.GenerateFromPassword([]byte(secretString), 14); err != nil {
return Secret{}, err
} else {
credential.SecretHash = string(sHash)
}
credential.Type = ClientSecretCredentialType
createdCred, err := s.credRepo.Create(ctx, credential)
if err != nil {
return Secret{}, err
}
return Secret{
ID: createdCred.ID,
Title: createdCred.Title,
Value: secretString,
CreatedAt: createdCred.CreatedAt,
}, nil
}
func (s Service) DeleteSecret(ctx context.Context, credID string) error {
return s.DeleteKey(ctx, credID)
}
func (s Service) ListSecret(ctx context.Context, serviceUserID string) ([]Credential, error) {
if serviceUserID == "" {
return nil, ErrInvalidID
}
return s.credRepo.List(ctx, Filter{
ServiceUserID: serviceUserID,
IsSecret: true,
})
}
// CreateToken creates an opaque token for the service user
func (s Service) CreateToken(ctx context.Context, credential Credential) (Token, error) {
// generate a random secret
secretBytes := make([]byte, 32)
if _, err := io.ReadFull(rand.Reader, secretBytes); err != nil {
return Token{}, err
}
// Hash the random bytes using SHA3-256
hash := sha3.Sum256(secretBytes)
credential.SecretHash = hex.EncodeToString(hash[:])
credential.Type = OpaqueTokenCredentialType
createdCred, err := s.credRepo.Create(ctx, credential)
if err != nil {
return Token{}, err
}
// encode cred val to hex bytes
credVal := hex.EncodeToString(secretBytes)
return Token{
ID: createdCred.ID,
Title: createdCred.Title,
Value: credVal,
CreatedAt: createdCred.CreatedAt,
}, nil
}
func (s Service) DeleteToken(ctx context.Context, credID string) error {
return s.DeleteKey(ctx, credID)
}
func (s Service) ListToken(ctx context.Context, serviceUserID string) ([]Credential, error) {
return s.ListSecret(ctx, serviceUserID)
}
// GetBySecret matches the secret with the secret hash stored in the database of the service user
// and if the secret matches, returns the service user
func (s Service) GetBySecret(ctx context.Context, credID string, reqSecret string) (ServiceUser, error) {
cred, err := s.credRepo.Get(ctx, credID)
if err != nil {
return ServiceUser{}, err
}
if len(cred.SecretHash) <= 0 || len(reqSecret) <= 0 {
return ServiceUser{}, ErrInvalidCred
}
if len(cred.Type) == 0 || cred.Type == ClientSecretCredentialType {
if err := bcrypt.CompareHashAndPassword([]byte(cred.SecretHash), []byte(reqSecret)); err != nil {
return ServiceUser{}, ErrInvalidCred
}
}
if cred.Type == OpaqueTokenCredentialType {
// decode the hex encoded secret
decodedReqSecret := make([]byte, 32)
if _, err := hex.Decode(decodedReqSecret, []byte(reqSecret)); err != nil {
return ServiceUser{}, err
}
reqDigest := sha3.Sum256(decodedReqSecret)
decodedCredSecret := make([]byte, 32)
if _, err := hex.Decode(decodedCredSecret, []byte(cred.SecretHash)); err != nil {
return ServiceUser{}, err
}
if subtle.ConstantTimeCompare(reqDigest[:], decodedCredSecret) == 0 {
return ServiceUser{}, ErrInvalidCred
}
}
return s.repo.GetByID(ctx, cred.ServiceUserID)
}
// GetByJWT returns the service user by verifying the token
func (s Service) GetByJWT(ctx context.Context, token string) (ServiceUser, error) {
insecureToken, err := jwt.ParseInsecure([]byte(token))
if err != nil {
return ServiceUser{}, fmt.Errorf("invalid serviceuser token: %w", err)
}
tokenKID, ok := insecureToken.Get(jwk.KeyIDKey)
if !ok {
return ServiceUser{}, fmt.Errorf("invalid key id from token")
}
cred, err := s.credRepo.Get(ctx, tokenKID.(string))
if err != nil {
return ServiceUser{}, fmt.Errorf("credential invalid of kid %s: %w", tokenKID.(string), err)
}
// verify token
_, err = jwt.Parse([]byte(token), jwt.WithKeySet(cred.PublicKey))
if err != nil {
return ServiceUser{}, fmt.Errorf("invalid serviceuser token: %w", err)
}
return s.repo.GetByID(ctx, cred.ServiceUserID)
}
// IsSudo checks platform permissions.
// Platform permissions are:
// - superuser
// - check
func (s Service) IsSudo(ctx context.Context, id string, permissionName string) (bool, error) {
return s.relService.CheckPermission(ctx, relation.Relation{
Subject: relation.Subject{
ID: id,
Namespace: schema.ServiceUserPrincipal,
},
Object: relation.Object{
ID: schema.PlatformID,
Namespace: schema.PlatformNamespace,
},
RelationName: permissionName,
})
}
// Sudo add platform permissions to user
func (s Service) Sudo(ctx context.Context, id string, relationName string) error {
currentUser, err := s.Get(ctx, id)
if err != nil {
return err
}
// check if already su
permissionName := ""
switch relationName {
case schema.MemberRelationName:
permissionName = schema.PlatformCheckPermission
case schema.AdminRelationName:
permissionName = schema.PlatformSudoPermission
}
if permissionName == "" {
return fmt.Errorf("invalid relation name, possible options are: %s, %s", schema.MemberRelationName, schema.AdminRelationName)
}
if ok, err := s.IsSudo(ctx, currentUser.ID, permissionName); err != nil {
return err
} else if ok {
return nil
}
// mark su
_, err = s.relService.Create(ctx, relation.Relation{
Object: relation.Object{
ID: schema.PlatformID,
Namespace: schema.PlatformNamespace,
},
Subject: relation.Subject{
ID: currentUser.ID,
Namespace: schema.ServiceUserPrincipal,
},
RelationName: relationName,
})
return err
}