-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.go
134 lines (110 loc) · 4.02 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
package device
import (
"context"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
commonpb "github.com/code-payments/code-protobuf-api/generated/go/common/v1"
devicepb "github.com/code-payments/code-protobuf-api/generated/go/device/v1"
"github.com/code-payments/code-server/pkg/grpc/client"
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/login"
"github.com/code-payments/code-server/pkg/code/data/phone"
)
type server struct {
log *logrus.Entry
data code_data.Provider
auth *auth_util.RPCSignatureVerifier
devicepb.UnimplementedDeviceServer
}
func NewDeviceServer(data code_data.Provider, auth *auth_util.RPCSignatureVerifier) devicepb.DeviceServer {
return &server{
log: logrus.StandardLogger().WithField("type", "device/server"),
data: data,
auth: auth,
}
}
func (s *server) RegisterLoggedInAccounts(ctx context.Context, req *devicepb.RegisterLoggedInAccountsRequest) (*devicepb.RegisterLoggedInAccountsResponse, error) {
log := s.log.WithFields(logrus.Fields{
"method": "RegisterLoggedInAccounts",
"app_install": req.AppInstall.Value,
})
log = client.InjectLoggingMetadata(ctx, log)
if len(req.Owners) != len(req.Signatures) {
return nil, status.Error(codes.InvalidArgument, "")
}
signatures := req.Signatures
req.Signatures = nil
var validOwners []string
var invalidOwners []*commonpb.SolanaAccountId
for i, protoOwner := range req.Owners {
owner, err := common.NewAccountFromProto(protoOwner)
if err != nil {
log.WithError(err).Warn("invalid owner account")
return nil, status.Error(codes.Internal, "")
}
if err := s.auth.Authenticate(ctx, owner, req, signatures[i]); err != nil {
return nil, err
}
_, err = s.data.GetLatestPhoneVerificationForAccount(ctx, owner.PublicKey().ToBase58())
if err == phone.ErrVerificationNotFound {
invalidOwners = append(invalidOwners, protoOwner)
} else if err != nil {
log.WithError(err).Warn("failure checking phone verification status")
return nil, status.Error(codes.Internal, "")
}
validOwners = append(validOwners, owner.PublicKey().ToBase58())
}
if len(invalidOwners) > 0 {
return &devicepb.RegisterLoggedInAccountsResponse{
Result: devicepb.RegisterLoggedInAccountsResponse_INVALID_OWNER,
InvalidOwners: invalidOwners,
}, nil
}
loginRecord := &login.MultiRecord{
AppInstallId: req.AppInstall.Value,
Owners: validOwners,
}
err := s.data.SaveLogins(ctx, loginRecord)
if err != nil {
log.WithError(err).Warn("failure updating login records")
return nil, status.Error(codes.Internal, "")
}
return &devicepb.RegisterLoggedInAccountsResponse{
Result: devicepb.RegisterLoggedInAccountsResponse_OK,
}, nil
}
func (s *server) GetLoggedInAccounts(ctx context.Context, req *devicepb.GetLoggedInAccountsRequest) (*devicepb.GetLoggedInAccountsResponse, error) {
log := s.log.WithFields(logrus.Fields{
"method": "GetLoggedInAccounts",
"app_install": req.AppInstall.Value,
})
log = client.InjectLoggingMetadata(ctx, log)
var protoOwners []*commonpb.SolanaAccountId
loginRecord, err := s.data.GetLoginsByAppInstall(ctx, req.AppInstall.Value)
switch err {
case nil:
for _, owner := range loginRecord.Owners {
parsed, err := common.NewAccountFromPublicKeyString(owner)
if err != nil {
log.WithError(err).Warn("invalid owner account")
return nil, status.Error(codes.Internal, "")
}
protoOwners = append(protoOwners, parsed.ToProto())
}
case login.ErrLoginNotFound:
default:
log.WithError(err).Warn("failure getting login records")
return nil, status.Error(codes.Internal, "")
}
if len(protoOwners) > 1 {
log.Warn("unexpectedly have more than 1 owner logged into same app install")
return nil, status.Error(codes.Internal, "")
}
return &devicepb.GetLoggedInAccountsResponse{
Result: devicepb.GetLoggedInAccountsResponse_OK,
Owners: protoOwners,
}, nil
}