-
Notifications
You must be signed in to change notification settings - Fork 402
/
service.go
187 lines (155 loc) · 4.35 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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package referrals
import (
"context"
"github.com/spacemonkeygo/monkit/v3"
"github.com/zeebo/errs"
"go.uber.org/zap"
"golang.org/x/crypto/bcrypt"
"storj.io/common/pb"
"storj.io/common/rpc"
"storj.io/common/signing"
"storj.io/common/storj"
"storj.io/common/uuid"
"storj.io/storj/satellite/console"
)
var mon = monkit.Package()
var (
// ErrUsedEmail is an error class for reporting already used emails.
ErrUsedEmail = errs.Class("email used error")
)
// Config is for referrals service.
type Config struct {
ReferralManagerURL storj.NodeURL `help:"the URL for referral manager"`
}
// Service allows communicating with the Referral Manager
//
// architecture: Service
type Service struct {
log *zap.Logger
signer signing.Signer
config Config
dialer rpc.Dialer
db console.Users
passwordCost int
}
// NewService returns a service for handling referrals information.
func NewService(log *zap.Logger, signer signing.Signer, config Config, dialer rpc.Dialer, db console.Users, passwordCost int) *Service {
return &Service{
log: log,
signer: signer,
config: config,
dialer: dialer,
db: db,
passwordCost: passwordCost,
}
}
// GetTokens returns tokens based on user ID.
func (service *Service) GetTokens(ctx context.Context, userID *uuid.UUID) (tokens []uuid.UUID, err error) {
defer mon.Task()(&ctx)(&err)
if userID.IsZero() {
return nil, errs.New("user ID is not defined")
}
conn, err := service.referralManagerConn(ctx)
if err != nil {
return nil, errs.Wrap(err)
}
defer func() {
err = conn.Close()
}()
client := pb.NewDRPCReferralManagerClient(conn)
response, err := client.GetTokens(ctx, &pb.GetTokensRequest{
OwnerUserId: userID[:],
OwnerSatelliteId: service.signer.ID(),
})
if err != nil {
return nil, errs.Wrap(err)
}
tokensInBytes := response.GetTokenSecrets()
if tokensInBytes != nil && len(tokensInBytes) == 0 {
return nil, errs.New("no available tokens")
}
tokens = make([]uuid.UUID, len(tokensInBytes))
for i := range tokensInBytes {
token, err := uuid.FromBytes(tokensInBytes[i])
if err != nil {
service.log.Debug("failed to convert bytes to UUID", zap.Error(err))
continue
}
tokens[i] = token
}
return tokens, nil
}
// CreateUser validates user's registration information and creates a new user.
func (service *Service) CreateUser(ctx context.Context, user CreateUser) (_ *console.User, err error) {
defer mon.Task()(&ctx)(&err)
if err := user.IsValid(); err != nil {
return nil, ErrValidation.Wrap(err)
}
if len(user.ReferralToken) == 0 {
return nil, errs.New("referral token is not defined")
}
_, err = service.db.GetByEmail(ctx, user.Email)
if err == nil {
return nil, ErrUsedEmail.New("")
}
userID, err := uuid.New()
if err != nil {
return nil, errs.Wrap(err)
}
err = service.redeemToken(ctx, &userID, user.ReferralToken)
if err != nil {
return nil, errs.Wrap(err)
}
hash, err := bcrypt.GenerateFromPassword([]byte(user.Password), service.passwordCost)
if err != nil {
return nil, errs.Wrap(err)
}
newUser := &console.User{
ID: userID,
Email: user.Email,
FullName: user.FullName,
ShortName: user.ShortName,
PasswordHash: hash,
}
u, err := service.db.Insert(ctx,
newUser,
)
if err != nil {
return nil, errs.Wrap(err)
}
return u, nil
}
func (service *Service) redeemToken(ctx context.Context, userID *uuid.UUID, token string) error {
conn, err := service.referralManagerConn(ctx)
if err != nil {
return errs.Wrap(err)
}
defer func() {
err = conn.Close()
}()
if userID.IsZero() || len(token) == 0 {
return errs.New("invalid argument")
}
referralToken, err := uuid.FromString(token)
if err != nil {
return errs.Wrap(err)
}
client := pb.NewDRPCReferralManagerClient(conn)
_, err = client.RedeemToken(ctx, &pb.RedeemTokenRequest{
Token: referralToken[:],
RedeemUserId: userID[:],
RedeemSatelliteId: service.signer.ID(),
})
if err != nil {
return errs.Wrap(err)
}
return nil
}
func (service *Service) referralManagerConn(ctx context.Context) (*rpc.Conn, error) {
if service.config.ReferralManagerURL.IsZero() {
return nil, errs.New("missing referral manager url configuration")
}
return service.dialer.DialNodeURL(ctx, service.config.ReferralManagerURL)
}