-
Notifications
You must be signed in to change notification settings - Fork 17
/
xs_cosmos_sign_auth.go
222 lines (203 loc) · 6.77 KB
/
xs_cosmos_sign_auth.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
package auth
import (
"context"
"encoding/hex"
_ "freemasonry.cc/blockchain/client"
util2 "freemasonry.cc/blockchain/util"
"freemasonry.cc/chat/clientapi/auth/authtypes"
"freemasonry.cc/chat/clientapi/httputil"
"freemasonry.cc/chat/clientapi/jsonerror"
"freemasonry.cc/chat/clientapi/userutil"
"freemasonry.cc/chat/new_feature"
"freemasonry.cc/chat/new_feature/new_db"
"freemasonry.cc/chat/setup/config"
uapi "freemasonry.cc/chat/userapi/api"
"github.com/matrix-org/util"
"github.com/prometheus/client_golang/prometheus"
"github.com/tharsis/ethermint/crypto/ethsecp256k1"
"net/http"
"os"
"strings"
)
var AmtRegisterUsers = prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: "dendrite",
Subsystem: "client",
Name: "register_users_total",
Help: "Total number of register users on this node",
},
)
// LoginTypeCosmos describes how to authenticate with a login token.
type LoginTypeCosmos struct {
UserAPI uapi.ClientUserAPI
Config *config.ClientAPI
}
// Name implements Type.
func (t *LoginTypeCosmos) Name() string {
return authtypes.LoginTypeCosmos
}
// LoginFromJSON implements Type. The cleanup function deletes the token from
// the database on success.
func (t *LoginTypeCosmos) LoginFromJSON(ctx context.Context, reqBytes []byte) (*Login, LoginCleanupFunc, *util.JSONResponse) {
var r loginCosmosRequest
if err := httputil.UnmarshalJSON(reqBytes, &r); err != nil {
return nil, nil, err
}
login, err := t.Login(ctx, &r)
if err != nil {
return nil, nil, err
}
return login, func(context.Context, *util.JSONResponse) {}, nil
}
func (t *LoginTypeCosmos) Login(ctx context.Context, req interface{}) (*Login, *util.JSONResponse) {
r := req.(*loginCosmosRequest)
username := strings.ToLower(r.Username())
if username == "" {
return nil, &util.JSONResponse{
Code: http.StatusUnauthorized,
JSON: jsonerror.BadJSON("A username must be supplied."),
}
}
localpart, err := userutil.ParseUsernameParam(username, &t.Config.Matrix.ServerName)
if err != nil {
return nil, &util.JSONResponse{
Code: http.StatusUnauthorized,
JSON: jsonerror.InvalidUsername(err.Error()),
}
}
if os.Getenv("CHAT_SERVER_MODE") == "chain" {
_, err = new_feature.GetUserByLocal(localpart)
if err == nil { // err == nil ,
servernameOk := new_feature.CheckServername(localpart, string(t.Config.Matrix.ServerName))
if !servernameOk {
return nil, &util.JSONResponse{
Code: http.StatusUnauthorized,
JSON: jsonerror.Unknown("err servername"),
}
}
}
}
msgBytes := []byte(r.Timestamp)
sigBytes, err := hex.DecodeString(r.Sign)
if err != nil {
return nil, &util.JSONResponse{
Code: http.StatusUnauthorized,
JSON: jsonerror.BadJSON("err hex decode"),
}
}
sigChatBytes, err := hex.DecodeString(r.ChatSign)
if err != nil {
return nil, &util.JSONResponse{
Code: http.StatusUnauthorized,
JSON: jsonerror.BadJSON("err hex decode"),
}
}
//TODO ,
// username
pubKeyBytes, err := hex.DecodeString(r.PubKey)
if err != nil {
return nil, &util.JSONResponse{
Code: http.StatusUnauthorized,
JSON: jsonerror.BadJSON("err recovering pub"),
}
}
pubKeyChatBytes, err := hex.DecodeString(r.ChatPubKey)
if err != nil {
return nil, &util.JSONResponse{
Code: http.StatusUnauthorized,
JSON: jsonerror.BadJSON("err recovering chat pub"),
}
}
pubKey := ethsecp256k1.PubKey{Key: pubKeyBytes}
recoveredAddress, err := util2.GetAccountFromPub(r.PubKey)
if err != nil {
return nil, &util.JSONResponse{
Code: http.StatusUnauthorized,
JSON: jsonerror.BadJSON("err GetAccountFromPub"),
}
}
//cfg := sdk.GetConfig()
//cmdCfg.SetBech32Prefixes(cfg)
//cmdCfg.SetBip44CoinType(cfg)
//addressHex := sdk.AccAddress(pubKey.Address()).String()
//addressHex := pubKey.Address().String()
//recoveredAddress := strings.ToLower(addressHex)
pubKeyChat := ethsecp256k1.PubKey{Key: pubKeyChatBytes}
//addressHexChat := sdk.AccAddress(pubKeyChat.Address()).String()
//addressHex := pubKey.Address().String()
//recoveredChatAddress := strings.ToLower(addressHexChat)
recoveredChatAddress, err := util2.GetAccountFromPub(r.ChatPubKey)
if localpart != recoveredAddress {
util.GetLogger(ctx).Errorf("localpart=%+v recoveredAddress=%v", localpart, recoveredAddress)
return nil, &util.JSONResponse{
Code: http.StatusUnauthorized,
JSON: jsonerror.BadJSON("crypto address mismatch with recovered"),
}
}
if !pubKey.VerifySignature(msgBytes, sigBytes) {
return nil, &util.JSONResponse{
Code: http.StatusUnauthorized,
JSON: jsonerror.BadJSON("check sign err"),
}
}
if !pubKeyChat.VerifySignature(msgBytes, sigChatBytes) {
return nil, &util.JSONResponse{
Code: http.StatusUnauthorized,
JSON: jsonerror.BadJSON("check chat sign err"),
}
}
res := &uapi.QueryAccountAvailabilityResponse{}
err = t.UserAPI.QueryAccountAvailability(ctx, &uapi.QueryAccountAvailabilityRequest{
Localpart: localpart,
}, res)
if err != nil {
return nil, &util.JSONResponse{
Code: http.StatusInternalServerError,
JSON: jsonerror.Unknown("failed to check availability:" + err.Error()),
}
}
if res.Available {
res1 := &uapi.PerformAccountCreationResponse{}
err := t.UserAPI.PerformAccountCreation(ctx, &uapi.PerformAccountCreationRequest{
AccountType: uapi.AccountTypeUser,
Localpart: username,
OnConflict: uapi.ConflictAbort,
}, res1)
if err != nil {
return nil, &util.JSONResponse{
Code: http.StatusInternalServerError,
JSON: jsonerror.Unknown("failed to create account:" + err.Error()),
}
} else {
AmtRegisterUsers.Inc()
err2 := new_feature.SendServerNotice(ctx, res1.Account.UserID, "u.welcome_register", "u.welcome_register", "m.room.message")
if err2 != nil {
}
}
if err = new_db.UpsertChatBindAddr(username, recoveredChatAddress); err != nil {
return nil, &util.JSONResponse{
Code: http.StatusInternalServerError,
JSON: jsonerror.Unknown("failed to set chat_addr:" + err.Error()),
}
}
//// api
//err = t.UserAPI.InsertChainData(ctx, username, string(t.Config.Matrix.ServerName), "", "")
//if err != nil {
// return nil, &util.JSONResponse{
// Code: http.StatusInternalServerError,
// JSON: jsonerror.Unknown("failed to InsertChainData when creating account:" + err.Error()),
// }
//}
}
return &r.Login, nil
}
// loginCosmosRequest struct to hold the possible parameters from an HTTP request.
type loginCosmosRequest struct {
Login
Token string `json:"token"`
Sign string `json:"sign"`
Timestamp string `json:"timestamp"`
PubKey string `json:"pub_key"`
ChatPubKey string `json:"chat_pub_key"`
ChatSign string `json:"chat_sign"`
}