/
user.go
193 lines (172 loc) 路 4.55 KB
/
user.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
package persistence
import (
"context"
"crypto/x509"
"encoding/hex"
"fmt"
"sort"
"strings"
"time"
"cloud.google.com/go/spanner"
"github.com/gofrs/uuid/v5"
"github.com/golang-jwt/jwt"
"google.golang.org/api/iterator"
)
type User struct {
UserId string
PublicKey string
}
func UpdateUserPublicKey(ctx context.Context, userId, publicKey string) error {
pkix, err := hex.DecodeString(publicKey)
if err != nil {
return nil
}
_, err = x509.ParsePKIXPublicKey(pkix)
if err != nil {
return nil
}
_, err = Spanner(ctx).ReadWriteTransaction(ctx, func(ctx context.Context, txn *spanner.ReadWriteTransaction) error {
it := txn.ReadUsingIndex(ctx, "users", "users_by_public_key", spanner.Key{publicKey}, []string{"user_id"})
defer it.Stop()
_, err := it.Next()
if err == iterator.Done {
} else if err != nil {
return err
} else {
return nil
}
return txn.BufferWrite([]*spanner.Mutation{spanner.InsertOrUpdateMap("users", map[string]interface{}{
"user_id": userId,
"public_key": publicKey,
})})
})
return err
}
func Authenticate(ctx context.Context, jwtToken string) (string, error) {
var userId string
token, err := jwt.Parse(jwtToken, func(token *jwt.Token) (interface{}, error) {
claims, ok := token.Claims.(jwt.MapClaims)
if !ok {
return nil, nil
}
_, ok = token.Method.(*jwt.SigningMethodECDSA)
if !ok {
_, ok = token.Method.(*EdDSASigningMethod)
if !ok {
return nil, nil
}
}
id, _ := uuid.FromString(fmt.Sprint(claims["uid"]))
if id.String() == uuid.Nil.String() {
return nil, nil
} else {
userId = id.String()
}
it := Spanner(ctx).Single().Read(ctx, "users", spanner.Key{userId}, []string{"public_key"})
defer it.Stop()
row, err := it.Next()
if err == iterator.Done {
return nil, nil
} else if err != nil {
return nil, err
}
var publicKey string
err = row.Columns(&publicKey)
if err != nil {
return nil, err
}
pkix, err := hex.DecodeString(publicKey)
if err != nil {
return nil, err
}
return x509.ParsePKIXPublicKey(pkix)
})
if err != nil && strings.Contains(err.Error(), "spanner") {
return "", err
}
if err == nil && token.Valid {
return userId, nil
}
return "", nil
}
func UserOrder(ctx context.Context, orderId, userId string) (*Order, error) {
it := Spanner(ctx).Single().Query(ctx, spanner.Statement{
SQL: "SELECT * FROM orders WHERE order_id=@order_id AND user_id=@user_id",
Params: map[string]interface{}{"order_id": orderId, "user_id": userId},
})
defer it.Stop()
row, err := it.Next()
if err == iterator.Done {
return nil, nil
} else if err != nil {
return nil, err
}
var o Order
err = row.ToStruct(&o)
return &o, err
}
func UserOrders(ctx context.Context, userId string, market, state string, offset time.Time, order string, limit int) ([]*Order, error) {
txn := Spanner(ctx).ReadOnlyTransaction()
defer txn.Close()
if limit > 100 {
limit = 100
}
cmp := "<"
if order != "DESC" {
order = "ASC"
cmp = ">"
}
base, quote := getBaseQuote(market)
query := "SELECT order_id FROM orders@{FORCE_INDEX=orders_by_user_state_created_%s} WHERE user_id=@user_id AND created_at%s=@offset AND state=@state"
query = fmt.Sprintf(query, strings.ToLower(order), cmp)
params := map[string]interface{}{"user_id": userId, "offset": offset, "state": state}
if base != "" && quote != "" {
query = query + " AND base_asset_id=@base AND quote_asset_id=@quote"
params["base"], params["quote"] = base, quote
}
query = query + " ORDER BY user_id,state,created_at " + order
query = fmt.Sprintf("%s LIMIT %d", query, limit)
iit := txn.Query(ctx, spanner.Statement{query, params})
defer iit.Stop()
var orderIds []string
for {
row, err := iit.Next()
if err == iterator.Done {
break
} else if err != nil {
return nil, err
}
var id string
err = row.Columns(&id)
if err != nil {
return nil, err
}
orderIds = append(orderIds, id)
}
oit := txn.Query(ctx, spanner.Statement{
SQL: "SELECT * FROM orders WHERE order_id IN UNNEST(@order_ids)",
Params: map[string]interface{}{"order_ids": orderIds},
})
defer oit.Stop()
var orders []*Order
for {
row, err := oit.Next()
if err == iterator.Done {
break
} else if err != nil {
return orders, err
}
var o Order
err = row.ToStruct(&o)
if err != nil {
return orders, err
}
orders = append(orders, &o)
}
if order == "DESC" {
sort.Slice(orders, func(i, j int) bool { return orders[i].CreatedAt.After(orders[j].CreatedAt) })
} else {
sort.Slice(orders, func(i, j int) bool { return orders[i].CreatedAt.Before(orders[j].CreatedAt) })
}
return orders, nil
}