-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.go
376 lines (324 loc) · 12 KB
/
server.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
package user
import (
"context"
"time"
"github.com/sirupsen/logrus"
xrate "golang.org/x/time/rate"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
transactionpb "github.com/code-payments/code-protobuf-api/generated/go/transaction/v2"
userpb "github.com/code-payments/code-protobuf-api/generated/go/user/v1"
"github.com/code-payments/code-server/pkg/code/antispam"
auth_util "github.com/code-payments/code-server/pkg/code/auth"
"github.com/code-payments/code-server/pkg/code/common"
code_data "github.com/code-payments/code-server/pkg/code/data"
"github.com/code-payments/code-server/pkg/code/data/intent"
"github.com/code-payments/code-server/pkg/code/data/phone"
"github.com/code-payments/code-server/pkg/code/data/user"
"github.com/code-payments/code-server/pkg/code/data/user/identity"
"github.com/code-payments/code-server/pkg/code/data/user/storage"
transaction_server "github.com/code-payments/code-server/pkg/code/server/grpc/transaction/v2"
"github.com/code-payments/code-server/pkg/grpc/client"
"github.com/code-payments/code-server/pkg/rate"
)
type identityServer struct {
log *logrus.Entry
data code_data.Provider
auth *auth_util.RPCSignatureVerifier
limiter *limiter
antispamGuard *antispam.Guard
userpb.UnimplementedIdentityServer
}
func NewIdentityServer(
data code_data.Provider,
auth *auth_util.RPCSignatureVerifier,
antispamGuard *antispam.Guard,
) userpb.IdentityServer {
// todo: don't use a local rate limiter, but it's good enough for now
// todo: these rate limits are arbitrary and might need tuning
limiter := newLimiter(func(r float64) rate.Limiter {
return rate.NewLocalRateLimiter(xrate.Limit(r))
}, 1, 5)
return &identityServer{
log: logrus.StandardLogger().WithField("type", "user/server"),
data: data,
auth: auth,
limiter: limiter,
antispamGuard: antispamGuard,
}
}
func (s *identityServer) LinkAccount(ctx context.Context, req *userpb.LinkAccountRequest) (*userpb.LinkAccountResponse, error) {
log := s.log.WithField("method", "LinkAccount")
log = client.InjectLoggingMetadata(ctx, log)
ownerAccount, err := common.NewAccountFromProto(req.OwnerAccountId)
if err != nil {
log.WithError(err).Warn("owner account is invalid")
return nil, status.Error(codes.Internal, "")
}
log = log.WithField("owner_account", ownerAccount.PublicKey().ToBase58())
signature := req.Signature
req.Signature = nil
if err := s.auth.Authenticate(ctx, ownerAccount, req, signature); err != nil {
return nil, err
}
var result userpb.LinkAccountResponse_Result
var userID *user.UserID
var dataContainerID *user.DataContainerID
var metadata *userpb.PhoneMetadata
switch token := req.Token.(type) {
case *userpb.LinkAccountRequest_Phone:
log = log.WithFields(logrus.Fields{
"phone": token.Phone.PhoneNumber.Value,
"code": token.Phone.Code.Value,
})
if !s.limiter.allowPhoneLinking(ctx, token.Phone.PhoneNumber.Value) {
result = userpb.LinkAccountResponse_RATE_LIMITED
break
}
allow, err := s.antispamGuard.AllowLinkAccount(ctx, ownerAccount, token.Phone.PhoneNumber.Value)
if err != nil {
log.WithError(err).Warn("failure performing antispam checks")
return nil, status.Error(codes.Internal, "")
} else if !allow {
result = userpb.LinkAccountResponse_RATE_LIMITED
break
}
err = s.data.UsePhoneLinkingToken(ctx, token.Phone.PhoneNumber.Value, token.Phone.Code.Value)
if err == phone.ErrLinkingTokenNotFound {
result = userpb.LinkAccountResponse_INVALID_TOKEN
break
} else if err != nil {
log.WithError(err).Warn("failure using phone linking token")
return nil, status.Error(codes.Internal, "")
}
falseValue := false
err = s.data.SaveOwnerAccountPhoneSetting(ctx, token.Phone.PhoneNumber.Value, &phone.OwnerAccountSetting{
OwnerAccount: ownerAccount.PublicKey().ToBase58(),
IsUnlinked: &falseValue,
CreatedAt: time.Now(),
LastUpdatedAt: time.Now(),
})
if err != nil {
log.WithError(err).Warn("failure enabling remote send setting")
return nil, status.Error(codes.Internal, "")
}
err = s.data.SavePhoneVerification(ctx, &phone.Verification{
PhoneNumber: token.Phone.PhoneNumber.Value,
OwnerAccount: ownerAccount.PublicKey().ToBase58(),
CreatedAt: time.Now(),
LastVerifiedAt: time.Now(),
})
if err != nil {
log.WithError(err).Warn("failure saving verification record")
return nil, status.Error(codes.Internal, "")
}
newUser := identity.Record{
ID: user.NewUserID(),
View: &user.View{
PhoneNumber: &token.Phone.PhoneNumber.Value,
},
CreatedAt: time.Now(),
}
err = s.data.PutUser(ctx, &newUser)
if err != identity.ErrAlreadyExists && err != nil {
log.WithError(err).Warn("failure inserting user identity")
return nil, status.Error(codes.Internal, "")
}
newDataContainer := &storage.Record{
ID: user.NewDataContainerID(),
OwnerAccount: ownerAccount.PublicKey().ToBase58(),
IdentifyingFeatures: &user.IdentifyingFeatures{
PhoneNumber: &token.Phone.PhoneNumber.Value,
},
CreatedAt: time.Now(),
}
err = s.data.PutUserDataContainer(ctx, newDataContainer)
if err != storage.ErrAlreadyExists && err != nil {
log.WithError(err).Warn("failure inserting data container")
return nil, status.Error(codes.Internal, "")
}
existingUser, err := s.data.GetUserByPhoneView(ctx, token.Phone.PhoneNumber.Value)
if err != nil {
log.WithError(err).Warn("failure getting user identity from phone view")
return nil, status.Error(codes.Internal, "")
}
userID = existingUser.ID
log = log.WithField("user", userID.String())
existingDataContainer, err := s.data.GetUserDataContainerByPhone(ctx, ownerAccount.PublicKey().ToBase58(), token.Phone.PhoneNumber.Value)
if err != nil {
log.WithError(err).Warn("failure getting data container for phone")
return nil, status.Error(codes.Internal, "")
}
dataContainerID = existingDataContainer.ID
metadata = &userpb.PhoneMetadata{
IsLinked: true,
}
default:
return nil, status.Error(codes.InvalidArgument, "token must be set")
}
if result != userpb.LinkAccountResponse_OK {
return &userpb.LinkAccountResponse{
Result: result,
}, nil
}
return &userpb.LinkAccountResponse{
Result: result,
User: &userpb.User{
Id: userID.Proto(),
View: &userpb.View{
PhoneNumber: req.GetPhone().PhoneNumber,
},
},
DataContainerId: dataContainerID.Proto(),
Metadata: &userpb.LinkAccountResponse_Phone{
Phone: metadata,
},
}, nil
}
func (s *identityServer) UnlinkAccount(ctx context.Context, req *userpb.UnlinkAccountRequest) (*userpb.UnlinkAccountResponse, error) {
log := s.log.WithField("method", "UnlinkAccount")
log = client.InjectLoggingMetadata(ctx, log)
ownerAccount, err := common.NewAccountFromProto(req.OwnerAccountId)
if err != nil {
log.WithError(err).Warn("owner account is invalid")
return nil, status.Error(codes.Internal, "")
}
log = log.WithField("owner_account", ownerAccount.PublicKey().ToBase58())
signature := req.Signature
req.Signature = nil
if err := s.auth.Authenticate(ctx, ownerAccount, req, signature); err != nil {
return nil, err
}
result := userpb.UnlinkAccountResponse_OK
switch identifer := req.IdentifyingFeature.(type) {
case *userpb.UnlinkAccountRequest_PhoneNumber:
log = log.WithField("phone", identifer.PhoneNumber.Value)
_, err := s.data.GetPhoneVerification(ctx, ownerAccount.PublicKey().ToBase58(), identifer.PhoneNumber.Value)
if err == phone.ErrVerificationNotFound {
result = userpb.UnlinkAccountResponse_NEVER_ASSOCIATED
break
} else if err != nil {
log.WithError(err).Warn("failure getting phone verification")
return nil, status.Error(codes.Internal, "")
}
trueVal := true
err = s.data.SaveOwnerAccountPhoneSetting(ctx, identifer.PhoneNumber.Value, &phone.OwnerAccountSetting{
OwnerAccount: ownerAccount.PublicKey().ToBase58(),
IsUnlinked: &trueVal,
CreatedAt: time.Now(),
LastUpdatedAt: time.Now(),
})
if err != nil {
log.WithError(err).Warn("failure disabling remote send setting")
return nil, status.Error(codes.Internal, "")
}
default:
return nil, status.Error(codes.InvalidArgument, "identifying_feature must be set")
}
return &userpb.UnlinkAccountResponse{
Result: result,
}, nil
}
func (s *identityServer) GetUser(ctx context.Context, req *userpb.GetUserRequest) (*userpb.GetUserResponse, error) {
log := s.log.WithField("method", "GetUser")
log = client.InjectLoggingMetadata(ctx, log)
ownerAccount, err := common.NewAccountFromProto(req.OwnerAccountId)
if err != nil {
log.WithError(err).Warn("owner account is invalid")
return nil, status.Error(codes.Internal, "")
}
log = log.WithField("owner_account", ownerAccount.PublicKey().ToBase58())
signature := req.Signature
req.Signature = nil
if err := s.auth.Authenticate(ctx, ownerAccount, req, signature); err != nil {
return nil, err
}
ownerManagementState, err := common.GetOwnerManagementState(ctx, s.data, ownerAccount)
if err != nil {
log.WithError(err).Warn("failure getting owner management state")
return nil, status.Error(codes.Internal, "")
}
var result userpb.GetUserResponse_Result
var userID *user.UserID
var isStaff bool
var dataContainerID *user.DataContainerID
var metadata *userpb.PhoneMetadata
switch identifer := req.IdentifyingFeature.(type) {
case *userpb.GetUserRequest_PhoneNumber:
log = log.WithField("phone", identifer.PhoneNumber.Value)
user, err := s.data.GetUserByPhoneView(ctx, identifer.PhoneNumber.Value)
if err == identity.ErrNotFound {
result = userpb.GetUserResponse_NOT_FOUND
break
} else if err != nil {
log.WithError(err).Warn("failure getting user identity from phone view")
return nil, status.Error(codes.Internal, "")
}
userID = user.ID
log = log.WithField("user", userID.String())
isStaff = user.IsStaffUser
// todo: needs a test
if user.IsBanned {
log.Info("banned user login denied")
result = userpb.GetUserResponse_NOT_FOUND
break
}
dataContainer, err := s.data.GetUserDataContainerByPhone(ctx, ownerAccount.PublicKey().ToBase58(), identifer.PhoneNumber.Value)
if err != nil {
log.WithError(err).Warn("failure getting data container for phone")
return nil, status.Error(codes.Internal, "")
}
dataContainerID = dataContainer.ID
if ownerManagementState == common.OwnerManagementStateUnlocked {
result = userpb.GetUserResponse_UNLOCKED_TIMELOCK_ACCOUNT
break
}
isLinked, err := s.data.IsPhoneNumberLinkedToAccount(ctx, identifer.PhoneNumber.Value, ownerAccount.PublicKey().ToBase58())
if err != nil {
log.WithError(err).Warn("failure getting link status to account")
return nil, status.Error(codes.Internal, "")
}
metadata = &userpb.PhoneMetadata{
IsLinked: isLinked,
}
default:
return nil, status.Error(codes.InvalidArgument, "identifying_feature must be set")
}
if result != userpb.GetUserResponse_OK {
return &userpb.GetUserResponse{
Result: result,
}, nil
}
// todo: Start centralizing airdrop intent logic somewhere
eligibleAirdrops := []transactionpb.AirdropType{
transactionpb.AirdropType_GET_FIRST_KIN,
}
for _, intentId := range []string{
transaction_server.GetNewAirdropIntentId(transaction_server.AirdropTypeGetFirstKin, ownerAccount.PublicKey().ToBase58()),
transaction_server.GetOldAirdropIntentId(transaction_server.AirdropTypeGetFirstKin, ownerAccount.PublicKey().ToBase58()),
} {
_, err = s.data.GetIntent(ctx, intentId)
if err == nil {
eligibleAirdrops = []transactionpb.AirdropType{}
break
} else if err != intent.ErrIntentNotFound {
log.WithError(err).Warnf("failure checking %s airdrop status", transactionpb.AirdropType_GET_FIRST_KIN)
return nil, status.Error(codes.Internal, "")
}
}
return &userpb.GetUserResponse{
Result: result,
User: &userpb.User{
Id: userID.Proto(),
View: &userpb.View{
PhoneNumber: req.GetPhoneNumber(),
},
},
DataContainerId: dataContainerID.Proto(),
Metadata: &userpb.GetUserResponse_Phone{
Phone: metadata,
},
EnableInternalFlags: isStaff,
EligibleAirdrops: eligibleAirdrops,
}, nil
}