forked from heroiclabs/nakama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline_link_unlink.go
526 lines (482 loc) · 17.8 KB
/
pipeline_link_unlink.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// Copyright 2017 The Nakama Authors
//
// 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.
package server
import (
"strconv"
"github.com/uber-go/zap"
"golang.org/x/crypto/bcrypt"
"strings"
)
func (p *pipeline) linkID(logger zap.Logger, session *session, envelope *Envelope) {
// Route to correct link handler
switch envelope.GetLink().Payload.(type) {
case *TLink_Device:
p.linkDevice(logger, session, envelope)
case *TLink_Facebook:
p.linkFacebook(logger, session, envelope)
case *TLink_Google:
p.linkGoogle(logger, session, envelope)
case *TLink_GameCenter:
p.linkGameCenter(logger, session, envelope)
case *TLink_Steam:
p.linkSteam(logger, session, envelope)
case *TLink_Email:
p.linkEmail(logger, session, envelope)
case *TLink_Custom:
p.linkCustom(logger, session, envelope)
default:
logger.Error("Could not link", zap.String("error", "Invalid payload"))
session.Send(ErrorMessageBadInput(envelope.CollationId, "Invalid payload"))
return
}
}
func (p *pipeline) linkDevice(logger zap.Logger, session *session, envelope *Envelope) {
deviceID := envelope.GetLink().GetDevice()
if deviceID == "" {
session.Send(ErrorMessageBadInput(envelope.CollationId, "Device ID is required"))
return
} else if invalidCharsRegex.MatchString(deviceID) {
session.Send(ErrorMessageBadInput(envelope.CollationId, "Invalid device ID, no spaces or control characters allowed"))
return
} else if len(deviceID) < 10 || len(deviceID) > 64 {
session.Send(ErrorMessageBadInput(envelope.CollationId, "Invalid device ID, must be 10-64 bytes"))
return
}
txn, err := p.db.Begin()
if err != nil {
logger.Warn("Could not link, transaction begin error", zap.Error(err))
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Could not link"))
return
}
res, err := txn.Exec("INSERT INTO user_device (id, user_id) VALUES ($1, $2)", deviceID, session.userID.Bytes())
if err != nil {
logger.Warn("Could not link, query error", zap.Error(err))
err = txn.Rollback()
if err != nil {
logger.Warn("Could not link, transaction rollback error", zap.Error(err))
}
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Could not link"))
return
}
if count, _ := res.RowsAffected(); count == 0 {
err = txn.Rollback()
if err != nil {
logger.Warn("Could not link, transaction rollback error", zap.Error(err))
}
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Could not link"))
return
}
res, err = txn.Exec("UPDATE users SET updated_at = $1 WHERE id = $2", nowMs(), session.userID.Bytes())
if err != nil {
logger.Warn("Could not link, query error", zap.Error(err))
err = txn.Rollback()
if err != nil {
logger.Warn("Could not link, transaction rollback error", zap.Error(err))
}
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Could not link"))
return
}
if count, _ := res.RowsAffected(); count == 0 {
err = txn.Rollback()
if err != nil {
logger.Warn("Could not link, transaction rollback error", zap.Error(err))
}
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Could not link"))
return
}
err = txn.Commit()
if err != nil {
logger.Warn("Could not register, transaction commit error", zap.Error(err))
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Could not link"))
return
}
session.Send(&Envelope{CollationId: envelope.CollationId})
}
func (p *pipeline) linkFacebook(logger zap.Logger, session *session, envelope *Envelope) {
accessToken := envelope.GetLink().GetFacebook()
if accessToken == "" {
session.Send(ErrorMessageBadInput(envelope.CollationId, "Access token is required"))
return
} else if invalidCharsRegex.MatchString(accessToken) {
session.Send(ErrorMessageBadInput(envelope.CollationId, "Invalid Facebook access token, no spaces or control characters allowed"))
return
}
fbProfile, err := p.socialClient.GetFacebookProfile(accessToken)
if err != nil {
logger.Warn("Could not get Facebook profile", zap.Error(err))
session.Send(ErrorMessage(envelope.CollationId, USER_LINK_PROVIDER_UNAVAILABLE, "Could not get Facebook profile"))
return
}
userID := session.userID.Bytes()
res, err := p.db.Exec(`
UPDATE users
SET facebook_id = $2, updated_at = $3
WHERE id = $1
AND NOT EXISTS
(SELECT id
FROM users
WHERE facebook_id = $2)`,
userID, fbProfile.ID, nowMs())
if err != nil {
logger.Warn("Could not link", zap.Error(err))
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Could not link"))
return
} else if count, _ := res.RowsAffected(); count == 0 {
session.Send(ErrorMessage(envelope.CollationId, USER_LINK_INUSE, "Facebook ID in use"))
return
}
p.addFacebookFriends(logger, userID, accessToken)
session.Send(&Envelope{CollationId: envelope.CollationId})
}
func (p *pipeline) linkGoogle(logger zap.Logger, session *session, envelope *Envelope) {
accessToken := envelope.GetLink().GetGoogle()
if accessToken == "" {
session.Send(ErrorMessageBadInput(envelope.CollationId, "Access token is required"))
return
} else if invalidCharsRegex.MatchString(accessToken) {
session.Send(ErrorMessageBadInput(envelope.CollationId, "Invalid Google access token, no spaces or control characters allowed"))
return
}
googleProfile, err := p.socialClient.GetGoogleProfile(accessToken)
if err != nil {
logger.Warn("Could not get Google profile", zap.Error(err))
session.Send(ErrorMessage(envelope.CollationId, USER_LINK_PROVIDER_UNAVAILABLE, "Could not get Google profile"))
return
}
res, err := p.db.Exec(`
UPDATE users
SET google_id = $2, updated_at = $3
WHERE id = $1
AND NOT EXISTS
(SELECT id
FROM users
WHERE google_id = $2)`,
session.userID.Bytes(),
googleProfile.ID,
nowMs())
if err != nil {
logger.Warn("Could not link", zap.Error(err))
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Could not link"))
return
} else if count, _ := res.RowsAffected(); count == 0 {
session.Send(ErrorMessage(envelope.CollationId, USER_LINK_INUSE, "Google ID in use"))
return
}
session.Send(&Envelope{CollationId: envelope.CollationId})
}
func (p *pipeline) linkGameCenter(logger zap.Logger, session *session, envelope *Envelope) {
gc := envelope.GetLink().GetGameCenter()
if gc == nil || gc.PlayerId == "" || gc.BundleId == "" || gc.Timestamp == 0 || gc.Salt == "" || gc.Signature == "" || gc.PublicKeyUrl == "" {
session.Send(ErrorMessageBadInput(envelope.CollationId, "Game Center credentials required"))
return
}
_, err := p.socialClient.CheckGameCenterID(gc.PlayerId, gc.BundleId, gc.Timestamp, gc.Salt, gc.Signature, gc.PublicKeyUrl)
if err != nil {
logger.Warn("Could not get Game Center profile", zap.Error(err))
session.Send(ErrorMessage(envelope.CollationId, USER_LINK_PROVIDER_UNAVAILABLE, "Could not get Game Center profile"))
return
}
res, err := p.db.Exec(`
UPDATE users
SET gamecenter_id = $2, updated_at = $3
WHERE id = $1
AND NOT EXISTS
(SELECT id
FROM users
WHERE gamecenter_id = $2)`,
session.userID.Bytes(),
gc.PlayerId,
nowMs())
if err != nil {
logger.Warn("Could not link", zap.Error(err))
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Could not link"))
return
} else if count, _ := res.RowsAffected(); count == 0 {
session.Send(ErrorMessage(envelope.CollationId, USER_LINK_INUSE, "Game Center ID in use"))
return
}
session.Send(&Envelope{CollationId: envelope.CollationId})
}
func (p *pipeline) linkSteam(logger zap.Logger, session *session, envelope *Envelope) {
if p.config.GetSocial().Steam.PublisherKey == "" || p.config.GetSocial().Steam.AppID == 0 {
session.Send(ErrorMessage(envelope.CollationId, USER_LINK_PROVIDER_UNAVAILABLE, "Steam link not available"))
return
}
ticket := envelope.GetLink().GetSteam()
if ticket == "" {
session.Send(ErrorMessageBadInput(envelope.CollationId, "Steam ticket is required"))
return
} else if invalidCharsRegex.MatchString(ticket) {
session.Send(ErrorMessageBadInput(envelope.CollationId, "Invalid Steam ticket, no spaces or control characters allowed"))
return
}
steamProfile, err := p.socialClient.GetSteamProfile(p.config.GetSocial().Steam.PublisherKey, p.config.GetSocial().Steam.AppID, ticket)
if err != nil {
logger.Warn("Could not get Steam profile", zap.Error(err))
session.Send(ErrorMessage(envelope.CollationId, USER_LINK_PROVIDER_UNAVAILABLE, "Could not get Steam profile"))
return
}
res, err := p.db.Exec(`
UPDATE users
SET steam_id = $2, updated_at = $3
WHERE id = $1
AND NOT EXISTS
(SELECT id
FROM users
WHERE steam_id = $2)`,
session.userID.Bytes(),
strconv.FormatUint(steamProfile.SteamID, 10),
nowMs())
if err != nil {
logger.Warn("Could not link", zap.Error(err))
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Could not link"))
return
} else if count, _ := res.RowsAffected(); count == 0 {
session.Send(ErrorMessage(envelope.CollationId, USER_LINK_INUSE, "Steam ID in use"))
return
}
session.Send(&Envelope{CollationId: envelope.CollationId})
}
func (p *pipeline) linkEmail(logger zap.Logger, session *session, envelope *Envelope) {
email := envelope.GetLink().GetEmail()
if email == nil {
session.Send(ErrorMessageBadInput(envelope.CollationId, "Invalid payload"))
return
} else if email.Email == "" {
session.Send(ErrorMessageBadInput(envelope.CollationId, "Email address is required"))
return
} else if invalidCharsRegex.MatchString(email.Email) {
session.Send(ErrorMessageBadInput(envelope.CollationId, "Invalid email address, no spaces or control characters allowed"))
return
} else if !emailRegex.MatchString(email.Email) {
session.Send(ErrorMessageBadInput(envelope.CollationId, "Invalid email address format"))
return
} else if len(email.Email) < 10 || len(email.Email) > 255 {
session.Send(ErrorMessageBadInput(envelope.CollationId, "Invalid email address, must be 10-255 bytes"))
return
} else if len(email.Password) < 8 {
session.Send(ErrorMessageBadInput(envelope.CollationId, "Password must be longer than 8 characters"))
return
}
hashedPassword, _ := bcrypt.GenerateFromPassword([]byte(email.Password), bcrypt.DefaultCost)
res, err := p.db.Exec(`
UPDATE users
SET email = $2, password = $3, updated_at = $4
WHERE id = $1
AND NOT EXISTS
(SELECT id
FROM users
WHERE email = $2)`,
session.userID.Bytes(),
strings.ToLower(email.Email),
hashedPassword,
nowMs())
if err != nil {
logger.Warn("Could not link", zap.Error(err))
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Could not link"))
return
} else if count, _ := res.RowsAffected(); count == 0 {
session.Send(ErrorMessage(envelope.CollationId, USER_LINK_INUSE, "Email address in use"))
return
}
session.Send(&Envelope{CollationId: envelope.CollationId})
}
func (p *pipeline) linkCustom(logger zap.Logger, session *session, envelope *Envelope) {
customID := envelope.GetLink().GetCustom()
if customID == "" {
session.Send(ErrorMessageBadInput(envelope.CollationId, "Custom ID is required"))
return
} else if invalidCharsRegex.MatchString(customID) {
session.Send(ErrorMessageBadInput(envelope.CollationId, "Invalid custom ID, no spaces or control characters allowed"))
return
} else if len(customID) < 10 || len(customID) > 64 {
session.Send(ErrorMessageBadInput(envelope.CollationId, "Invalid custom ID, must be 10-64 bytes"))
return
}
res, err := p.db.Exec(`
UPDATE users
SET custom_id = $2, updated_at = $3
WHERE id = $1
AND NOT EXISTS
(SELECT id
FROM users
WHERE custom_id = $2)`,
session.userID.Bytes(),
customID,
nowMs())
if err != nil {
logger.Warn("Could not link", zap.Error(err))
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Could not link"))
return
} else if count, _ := res.RowsAffected(); count == 0 {
session.Send(ErrorMessage(envelope.CollationId, USER_LINK_INUSE, "Custom ID in use"))
return
}
session.Send(&Envelope{CollationId: envelope.CollationId})
}
func (p *pipeline) unlinkID(logger zap.Logger, session *session, envelope *Envelope) {
// Select correct unlink query
var query string
var param interface{}
switch envelope.GetUnlink().Payload.(type) {
case *TUnlink_Device:
txn, err := p.db.Begin()
if err != nil {
logger.Warn("Could not unlink, transaction begin error", zap.Error(err))
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Could not unlink"))
return
}
res, err := txn.Exec(`
DELETE FROM user_device WHERE id = $2 AND user_id = $1
AND (EXISTS (SELECT id FROM users WHERE id = $1 AND
(facebook_id IS NOT NULL
OR google_id IS NOT NULL
OR gamecenter_id IS NOT NULL
OR steam_id IS NOT NULL
OR email IS NOT NULL
OR custom_id IS NOT NULL))
OR EXISTS (SELECT id FROM user_device WHERE user_id = $1 AND id <> $2))`,
session.userID.Bytes(),
envelope.GetUnlink().GetDevice())
if err != nil {
logger.Warn("Could not unlink, query error", zap.Error(err))
err = txn.Rollback()
if err != nil {
logger.Warn("Could not unlink, transaction rollback error", zap.Error(err))
}
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Could not unlink"))
return
}
if count, _ := res.RowsAffected(); count == 0 {
err = txn.Rollback()
if err != nil {
logger.Warn("Could not unlink, transaction rollback error", zap.Error(err))
}
session.Send(ErrorMessage(envelope.CollationId, USER_UNLINK_DISALLOWED, "Check profile exists and is not last link"))
return
}
res, err = txn.Exec("UPDATE users SET updated_at = $2 WHERE id = $1", session.userID.Bytes(), nowMs())
if err != nil {
logger.Warn("Could not unlink, query error", zap.Error(err))
err = txn.Rollback()
if err != nil {
logger.Warn("Could not unlink, transaction rollback error", zap.Error(err))
}
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Could not unlink"))
return
}
if count, _ := res.RowsAffected(); count == 0 {
err = txn.Rollback()
if err != nil {
logger.Warn("Could not unlink, transaction rollback error", zap.Error(err))
}
session.Send(ErrorMessage(envelope.CollationId, USER_UNLINK_DISALLOWED, "Check profile exists and is not last link"))
return
}
err = txn.Commit()
if err != nil {
logger.Warn("Could not unlink, transaction commit error", zap.Error(err))
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Could not unlink"))
return
}
session.Send(&Envelope{CollationId: envelope.CollationId})
return
case *TUnlink_Facebook:
query = `UPDATE users SET facebook_id = NULL, updated_at = $3
WHERE id = $1
AND facebook_id = $2
AND ((google_id IS NOT NULL
OR gamecenter_id IS NOT NULL
OR steam_id IS NOT NULL
OR email IS NOT NULL
OR custom_id IS NOT NULL)
OR
EXISTS (SELECT id FROM user_device WHERE user_id = $1 LIMIT 1))`
param = envelope.GetUnlink().GetFacebook()
case *TUnlink_Google:
query = `UPDATE users SET google_id = NULL, updated_at = $3
WHERE id = $1
AND google_id = $2
AND ((facebook_id IS NOT NULL
OR gamecenter_id IS NOT NULL
OR steam_id IS NOT NULL
OR email IS NOT NULL
OR custom_id IS NOT NULL)
OR
EXISTS (SELECT id FROM user_device WHERE user_id = $1 LIMIT 1))`
param = envelope.GetUnlink().GetGoogle()
case *TUnlink_GameCenter:
query = `UPDATE users SET gamecenter_id = NULL, updated_at = $3
WHERE id = $1
AND gamecenter_id = $2
AND ((facebook_id IS NOT NULL
OR google_id IS NOT NULL
OR steam_id IS NOT NULL
OR email IS NOT NULL
OR custom_id IS NOT NULL)
OR
EXISTS (SELECT id FROM user_device WHERE user_id = $1 LIMIT 1))`
param = envelope.GetUnlink().GetGameCenter()
case *TUnlink_Steam:
query = `UPDATE users SET steam_id = NULL, updated_at = $3
WHERE id = $1
AND steam_id = $2
AND ((facebook_id IS NOT NULL
OR google_id IS NOT NULL
OR gamecenter_id IS NOT NULL
OR email IS NOT NULL
OR custom_id IS NOT NULL)
OR
EXISTS (SELECT id FROM user_device WHERE user_id = $1 LIMIT 1))`
param = envelope.GetUnlink().GetSteam()
case *TUnlink_Email:
query = `UPDATE users SET email = NULL, password = NULL, updated_at = $3
WHERE id = $1
AND email = $2
AND ((facebook_id IS NOT NULL
OR google_id IS NOT NULL
OR gamecenter_id IS NOT NULL
OR steam_id IS NOT NULL
OR custom_id IS NOT NULL)
OR
EXISTS (SELECT id FROM user_device WHERE user_id = $1 LIMIT 1))`
param = strings.ToLower(envelope.GetUnlink().GetEmail())
case *TUnlink_Custom:
query = `UPDATE users SET custom_id = NULL, updated_at = $3
WHERE id = $1
AND custom_id = $2
AND ((facebook_id IS NOT NULL
OR google_id IS NOT NULL
OR gamecenter_id IS NOT NULL
OR steam_id IS NOT NULL
OR email IS NOT NULL)
OR
EXISTS (SELECT id FROM user_device WHERE user_id = $1 LIMIT 1))`
param = envelope.GetUnlink().GetCustom()
default:
logger.Error("Could not unlink", zap.String("error", "Invalid payload"))
session.Send(ErrorMessageBadInput(envelope.CollationId, "Invalid payload"))
return
}
res, err := p.db.Exec(query, session.userID.Bytes(), param, nowMs())
if err != nil {
logger.Warn("Could not unlink", zap.Error(err))
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Could not unlink"))
return
} else if count, _ := res.RowsAffected(); count == 0 {
session.Send(ErrorMessage(envelope.CollationId, USER_UNLINK_DISALLOWED, "Check profile exists and is not last link"))
return
}
session.Send(&Envelope{CollationId: envelope.CollationId})
}