-
Notifications
You must be signed in to change notification settings - Fork 393
/
regtokens.go
105 lines (84 loc) · 2.91 KB
/
regtokens.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
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package satellitedb
import (
"context"
"github.com/skyrings/skyring-common/tools/uuid"
"github.com/zeebo/errs"
"storj.io/storj/satellite/console"
dbx "storj.io/storj/satellite/satellitedb/dbx"
)
// registrationTokens is an implementation of RegistrationTokens interface using spacemonkeygo/dbx orm
type registrationTokens struct {
db dbx.Methods
}
// Create creates new registration token
func (rt *registrationTokens) Create(ctx context.Context, projectLimit int) (_ *console.RegistrationToken, err error) {
defer mon.Task()(&ctx)(&err)
secret, err := console.NewRegistrationSecret()
if err != nil {
return nil, err
}
regToken, err := rt.db.Create_RegistrationToken(
ctx,
dbx.RegistrationToken_Secret(secret[:]),
dbx.RegistrationToken_ProjectLimit(projectLimit),
dbx.RegistrationToken_Create_Fields{},
)
if err != nil {
return nil, err
}
return registrationTokenFromDBX(ctx, regToken)
}
// GetBySecret retrieves RegTokenInfo with given Secret
func (rt *registrationTokens) GetBySecret(ctx context.Context, secret console.RegistrationSecret) (_ *console.RegistrationToken, err error) {
defer mon.Task()(&ctx)(&err)
regToken, err := rt.db.Get_RegistrationToken_By_Secret(ctx, dbx.RegistrationToken_Secret(secret[:]))
if err != nil {
return nil, err
}
return registrationTokenFromDBX(ctx, regToken)
}
// GetByOwnerID retrieves RegTokenInfo by ownerID
func (rt *registrationTokens) GetByOwnerID(ctx context.Context, ownerID uuid.UUID) (_ *console.RegistrationToken, err error) {
defer mon.Task()(&ctx)(&err)
regToken, err := rt.db.Get_RegistrationToken_By_OwnerId(ctx, dbx.RegistrationToken_OwnerId(ownerID[:]))
if err != nil {
return nil, err
}
return registrationTokenFromDBX(ctx, regToken)
}
// UpdateOwner updates registration token's owner
func (rt *registrationTokens) UpdateOwner(ctx context.Context, secret console.RegistrationSecret, ownerID uuid.UUID) (err error) {
defer mon.Task()(&ctx)(&err)
_, err = rt.db.Update_RegistrationToken_By_Secret(
ctx,
dbx.RegistrationToken_Secret(secret[:]),
dbx.RegistrationToken_Update_Fields{
OwnerId: dbx.RegistrationToken_OwnerId(ownerID[:]),
},
)
return err
}
// registrationTokenFromDBX is used for creating RegistrationToken entity from autogenerated dbx.RegistrationToken struct
func registrationTokenFromDBX(ctx context.Context, regToken *dbx.RegistrationToken) (_ *console.RegistrationToken, err error) {
if regToken == nil {
return nil, errs.New("token parameter is nil")
}
var secret [32]byte
copy(secret[:], regToken.Secret)
result := &console.RegistrationToken{
Secret: secret,
OwnerID: nil,
ProjectLimit: regToken.ProjectLimit,
CreatedAt: regToken.CreatedAt,
}
if regToken.OwnerId != nil {
ownerID, err := bytesToUUID(regToken.OwnerId)
if err != nil {
return nil, err
}
result.OwnerID = &ownerID
}
return result, nil
}