-
Notifications
You must be signed in to change notification settings - Fork 349
/
auth.signUp_handler.go
256 lines (230 loc) · 8.49 KB
/
auth.signUp_handler.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
// Copyright 2022 Teamgram Authors
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: teamgramio (teamgram.io@gmail.com)
//
package core
import (
"context"
"math/rand"
"github.com/teamgram/marmota/pkg/threading2"
"github.com/teamgram/proto/mtproto"
"github.com/teamgram/proto/mtproto/crypto"
"github.com/teamgram/teamgram-server/app/bff/authorization/internal/logic"
"github.com/teamgram/teamgram-server/app/bff/authorization/internal/model"
msgpb "github.com/teamgram/teamgram-server/app/messenger/msg/msg/msg"
"github.com/teamgram/teamgram-server/app/service/authsession/authsession"
userpb "github.com/teamgram/teamgram-server/app/service/biz/user/user"
"github.com/teamgram/teamgram-server/pkg/env2"
"github.com/teamgram/teamgram-server/pkg/phonenumber"
)
/*
Android client auth.signUp#1b067634, handler error
if (error.text.contains("PHONE_NUMBER_INVALID")) {
needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("InvalidPhoneNumber", R.string.InvalidPhoneNumber));
} else if (error.text.contains("PHONE_CODE_EMPTY") || error.text.contains("PHONE_CODE_INVALID")) {
needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("InvalidCode", R.string.InvalidCode));
} else if (error.text.contains("PHONE_CODE_EXPIRED")) {
needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("CodeExpired", R.string.CodeExpired));
} else if (error.text.contains("FIRSTNAME_INVALID")) {
needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("InvalidFirstName", R.string.InvalidFirstName));
} else if (error.text.contains("LASTNAME_INVALID")) {
needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("InvalidLastName", R.string.InvalidLastName));
} else {
needShowAlert(LocaleController.getString("AppName", R.string.AppName), error.text);
}
*/
// AuthSignUp
// auth.signUp#80eee427 phone_number:string phone_code_hash:string first_name:string last_name:string = auth.Authorization;
func (c *AuthorizationCore) AuthSignUp(in *mtproto.TLAuthSignUp) (*mtproto.Auth_Authorization, error) {
if c.svcCtx.Plugin != nil {
c.svcCtx.Plugin.OnAuthAction(c.ctx,
c.MD.PermAuthKeyId,
c.MD.ClientMsgId,
c.MD.ClientAddr,
in.PhoneNumber,
logic.GetActionType(in),
"auth.signUp")
}
// 1. check phone_code empty
var (
phoneCode *string = nil
)
// 3. check number
// 3.1. empty
if in.PhoneNumber == "" {
c.Logger.Errorf("check phone_number error - empty")
err := mtproto.ErrPhoneNumberInvalid
return nil, err
}
// 3.2. check phone_number
// 客户端发送的手机号格式为: "+86 111 1111 1111",归一化
// We need getRegionCode from phone_number
pNumber, err := phonenumber.MakePhoneNumberHelper(in.PhoneNumber, "")
if err != nil {
c.Logger.Errorf("check phone_number error - %v", err)
err = mtproto.ErrPhoneNumberInvalid
return nil, err
}
phoneNumber := pNumber.GetNormalizeDigits()
if in.PhoneCodeHash == "" {
c.Logger.Errorf("check phone_code_hash error - empty")
err = mtproto.ErrPhoneCodeHashEmpty
return nil, err
}
// TODO(@benqi): register name ruler
// check first name invalid
if in.FirstName == "" {
c.Logger.Errorf("check first_name error - empty")
err = mtproto.ErrFirstnameInvalid
return nil, err
}
// TODO(@benqi): PHONE_NUMBER_FLOOD
// <string name="PhoneNumberFlood">Sorry, you have deleted and re-created your account too many times recently.
// Please wait for a few days before signing up again.</string>
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var (
codeData *model.PhoneCodeTransaction
)
// phoneRegistered := auth.CheckPhoneNumberExist(phoneNumber)
codeData, err = c.svcCtx.AuthLogic.DoAuthSignUp(c.ctx, c.MD.AuthId, phoneNumber, phoneCode, in.PhoneCodeHash)
if err != nil {
c.Logger.Errorf(err.Error())
return nil, err
} else {
_ = codeData
}
var (
user *mtproto.ImmutableUser
)
key := crypto.CreateAuthKey()
_, err = c.svcCtx.Dao.AuthsessionClient.AuthsessionSetAuthKey(c.ctx, &authsession.TLAuthsessionSetAuthKey{
AuthKey: &mtproto.AuthKeyInfo{
AuthKeyId: key.AuthKeyId(),
AuthKey: key.AuthKey(),
AuthKeyType: mtproto.AuthKeyTypePerm,
PermAuthKeyId: key.AuthKeyId(),
TempAuthKeyId: 0,
MediaTempAuthKeyId: 0,
},
FutureSalt: nil,
})
if err != nil {
c.Logger.Errorf("create user secret key error")
return nil, err
}
var (
firstName = in.FirstName
lastName = in.LastName
username string
predefinedUser *mtproto.PredefinedUser
)
if env2.PredefinedUser {
predefinedUser, _ = c.svcCtx.Dao.UserClient.UserGetPredefinedUser(c.ctx, &userpb.TLUserGetPredefinedUser{
Phone: phoneNumber,
})
if predefinedUser == nil {
c.Logger.Errorf("check predefinedUsers error - %v", err)
err = mtproto.ErrPhoneNumberInvalid
return nil, err
}
firstName = predefinedUser.GetFirstName().GetValue()
lastName = predefinedUser.GetLastName().GetValue()
username = predefinedUser.GetUsername().GetValue()
}
// Create new user
if user, err = c.svcCtx.UserClient.UserCreateNewUser(c.ctx, &userpb.TLUserCreateNewUser{
SecretKeyId: key.AuthKeyId(),
Phone: phoneNumber,
CountryCode: pNumber.GetRegionCode(),
FirstName: firstName,
LastName: lastName,
}); err != nil {
c.Logger.Errorf("createNewUser error: %v", err)
return nil, err
}
if env2.PredefinedUser {
c.svcCtx.Dao.UserClient.UserPredefinedBindRegisteredUserId(c.ctx, &userpb.TLUserPredefinedBindRegisteredUserId{
Phone: phoneNumber,
RegisteredUserId: user.User.Id,
})
if username != "" {
// TODO(@benqi): setUsername
//s.UserFacade.UpdateUsername(ctx, user.Id, username)
//s.UsernameFacade.UpdateUsername(ctx, model2.PEER_USER, user.Id, username)
//s.UserFacade.UpdateVerified(ctx, user.Id, predefinedUser.Verified)
}
}
// TODO(@benqi): remove to createNewUser
// user.Self = true
// bind auth_key and user_id
_, err = c.svcCtx.Dao.AuthsessionClient.AuthsessionBindAuthKeyUser(c.ctx, &authsession.TLAuthsessionBindAuthKeyUser{
AuthKeyId: c.MD.AuthId,
UserId: user.User.Id,
})
if err != nil {
c.Logger.Errorf("bindAuthKeyUser error: %v", err)
err = mtproto.ErrInternelServerError
return nil, err
}
return threading2.WrapperGoFunc(
c.ctx,
mtproto.MakeTLAuthAuthorization(&mtproto.Auth_Authorization{
User: user.ToSelfUser(),
}).To_Auth_Authorization(),
func(ctx context.Context) {
// on event
c.svcCtx.AuthLogic.DeletePhoneCode(ctx, c.MD.AuthId, phoneNumber, in.PhoneCodeHash)
// c.pushSignInMessage(ctx, user.Id(), codeData.PhoneCode)
c.onContactSignUp(ctx, c.MD.AuthId, user.Id(), phoneNumber)
},
).(*mtproto.Auth_Authorization), nil
}
func (c *AuthorizationCore) onContactSignUp(ctx context.Context, authKeyId, userId int64, phone string) {
importers, _ := c.svcCtx.Dao.UserClient.UserGetImportersByPhone(ctx, &userpb.TLUserGetImportersByPhone{
Phone: phone,
})
for _, c2 := range importers.GetDatas() {
c.Logger.Infof("importer: %v", c2)
v, _ := c.svcCtx.Dao.UserClient.UserGetContactSignUpNotification(ctx, &userpb.TLUserGetContactSignUpNotification{
UserId: c2.ClientId,
})
_ = authKeyId
if mtproto.FromBool(v) {
c.svcCtx.Dao.MsgClient.MsgPushUserMessage(
context.Background(),
&msgpb.TLMsgPushUserMessage{
UserId: userId,
AuthKeyId: 0,
PeerType: mtproto.PEER_USER,
PeerId: c2.ClientId,
PushType: 1,
Message: msgpb.MakeTLOutboxMessage(&msgpb.OutboxMessage{
NoWebpage: false,
Background: false,
RandomId: rand.Int63(),
Message: mtproto.MakeContactSignUpMessage(userId, c2.ClientId),
ScheduleDate: nil,
}).To_OutboxMessage(),
})
} else {
c.Logger.Infof("not setting contactSignUpNotification")
}
}
c.svcCtx.Dao.UserClient.UserDeleteImportersByPhone(ctx, &userpb.TLUserDeleteImportersByPhone{
Phone: phone,
})
}