/
testGcmServer.go
1150 lines (1046 loc) · 31.7 KB
/
testGcmServer.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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"appengine"
"appengine/datastore"
"encoding/json"
"io/ioutil"
"net/http"
"time"
"strings"
"fmt"
"appengine/urlfetch"
"errors"
"bytes"
)
// Data structure got from datastore user kind
type User struct {
InstanceId string `json:"instanceid"`
RegistrationToken string `json:"registrationtoken"`
LastUpdateTime time.Time `json:"lastupdatetime"`
}
// HTTP response body from Google Instance ID authenticity service
type UserRegistrationTokenAuthenticity struct {
Application string `json:"application"`
AuthorizedEntity string `json:"authorizedEntity"`
// Other properties in the response body are "don't care"
}
// HTTP response body to user registration
type UserRegistrationResponseBody struct {
UserId string `json:"userid"`
}
// Data structure got from datastore group kind
type Group struct {
Name string `json:"name"`
Owner string `json:"owner"` // Instance ID
Members []string `json:"members"` // Instance ID list
NotificationKey string `json:"notificationkey"` // GCM device group unique ID
}
// HTTP body of joining or leaving group requests from users
type GroupUser struct {
// To authentication
InstanceId string `json:"instanceid"`
// The group
GroupName string `json:"groupname"`
}
// HTTP body to send to Google Cloud Messaging server to manage device groups
type GroupOperation struct {
Operation string `json:"operation"` // "create", "add", "remove"
Notification_key_name string `json:"notification_key_name"` // A unique group name in a Google project
Notification_key string `json:"notification_key,omitempty"` // A unique key to identify a group
Registration_ids []string `json:"registration_ids"` // APP registration tokens in the group
}
// HTTP body received from Google Cloud Messaging server
type GroupOperationResponse struct {
Notification_key string `json:"notification_key"` // A unique key to identify a group
Error string `json:"error"` // Error message
}
// HTTP body of sending a message to a user
type UserMessage struct {
// To authentication
InstanceId string `json:"instanceid"`
// To the target user
UserId string `json:"userid"` // Datastore user kind key string
Message string `json:"message"`
}
// HTTP body of sending a message to a topic
type TopicMessage struct {
// To authentication
InstanceId string `json:"instanceid"`
// To the target user
Topic string `json:"topic"`
Message string `json:"message"`
}
// HTTP body of sending a message to a group
type GroupMessage struct {
// To authentication
InstanceId string `json:"instanceid"`
// To the target user
GroupName string `json:"groupname"`
Message string `json:"message"`
}
const BaseUrl = "/api/0.1/"
const UserKind = "User"
const UserRoot = "User root"
const GroupKind = "Group"
const GroupRoot = "Group root"
const AppNamespace = "com.vernonsung.testgcmapp"
const GaeProjectId = "testgcmserver-1120"
const GaeProjectNumber = "846181647582"
// GCM server
const GcmURL = "https://gcm-http.googleapis.com/gcm/send"
const GcmGroupURL = "https://android.googleapis.com/gcm/notification"
const InstanceIdVerificationUrl = "https://iid.googleapis.com/iid/info/"
const GcmApiKey = "AIzaSyAODu6tKbQp8sAwEBDNLzW9uDCBmmluQ4A"
func init() {
http.HandleFunc(BaseUrl, rootPage)
http.HandleFunc(BaseUrl+"myself", UpdateMyself) // PUT
http.HandleFunc(BaseUrl+"groups", groups) // PUT
http.HandleFunc(BaseUrl+"groups/", groups) // DELETE
http.HandleFunc(BaseUrl+"user-messages", SendUserMessage) // POST
http.HandleFunc(BaseUrl+"topic-messages", SendTopicMessage) // POST
http.HandleFunc(BaseUrl+"group-messages", SendGroupMessage) // POST
}
func rootPage(rw http.ResponseWriter, req *http.Request) {
c := appengine.NewContext(req)
c.Debugf("This is root")
}
func groups(rw http.ResponseWriter, req *http.Request) {
switch req.Method {
// case "GET":
// listMember(rw, req)
// case "POST":
// SendMessage(rw, req)
case "PUT":
JoinGroup(rw, req)
case "DELETE":
LeaveGroup(rw, req)
default:
http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
}
}
// Receive a message from an APP instance.
// Check it's instancd ID.
// Send the message back.
// POST https://testgcmserver-1120.appspot.com/api/0.1/user-messages"
// Success: 204 No Content
// Failure: 400 Bad Request, 403 Forbidden
func SendUserMessage(rw http.ResponseWriter, req *http.Request) {
// Appengine
var c appengine.Context = appengine.NewContext(req)
// Result, 0: success, 1: failed
var r int = http.StatusNoContent
// Return code
defer func() {
// Return status. WriteHeader() must be called before call to Write
if r == http.StatusNoContent {
// Changing the header after a call to WriteHeader (or Write) has no effect.
rw.WriteHeader(http.StatusNoContent)
} else if r == http.StatusBadRequest {
// http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
http.Error(rw, `Please follow https://aaa.appspot.com/api/0.1/user-messages
{
"instanceid":""
"userid":""
"message":""
}`, http.StatusBadRequest)
} else {
http.Error(rw, http.StatusText(r), r)
}
}()
// Get body
b, err := ioutil.ReadAll(req.Body)
if err != nil {
c.Errorf("%s in reading body %s", err, b)
r = http.StatusBadRequest
return
}
var message UserMessage
if err = json.Unmarshal(b, &message); err != nil {
c.Errorf("%s in decoding body %s", err, b)
r = http.StatusBadRequest
return
}
// Authenticate sender
var isValid bool = false
isValid, err = verifyRequest(message.InstanceId, c)
if err != nil {
c.Errorf("%s in authenticating request", err)
r = http.StatusBadRequest
return
}
if isValid == false {
c.Warningf("Invalid request, ignore")
r = http.StatusForbidden
return
}
// Decode datastore key from string
key, err := datastore.DecodeKey(message.UserId)
if err != nil {
c.Errorf("%s in decoding key string", err)
r = http.StatusBadRequest
return
}
// Get target user from datastore
var dst User
if err := datastore.Get(c, key, &dst); err != nil {
c.Errorf("%s in getting entity from datastore by key %s", err, message.UserId)
r = http.StatusNotFound
return
}
// Make GCM message body
var bodyString string = fmt.Sprintf(`
{
"to":"%s",
"data": {
"message":"%s"
}
}`, dst.RegistrationToken, message.Message)
// Make a POST request for GCM
pReq, err := http.NewRequest("POST", GcmURL, strings.NewReader(bodyString))
if err != nil {
c.Errorf("%s in makeing a HTTP request", err)
r = http.StatusInternalServerError
return
}
pReq.Header.Add("Content-Type", "application/json")
pReq.Header.Add("Authorization", "key="+GcmApiKey)
// Debug
c.Infof("%s", *pReq)
// Send request
var client = urlfetch.Client(c)
resp, err := client.Do(pReq)
if err != nil {
c.Errorf("%s in sending request", err)
r = http.StatusInternalServerError
return
}
defer resp.Body.Close()
// Check response
c.Infof("%d %s", resp.StatusCode, resp.Status)
// Get response body
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
c.Errorf("%s in reading response body", err)
r = http.StatusInternalServerError
return
}
c.Infof("%s", respBody)
}
// Receive a message from an APP instance.
// Check it's instancd ID.
// Send the message to the topic.
// POST https://testgcmserver-1120.appspot.com/api/0.1/topic-messages"
// Success: 204 No Content
// Failure: 400 Bad Request, 403 Forbidden, 404 NotFound, 500 InternalError
func SendTopicMessage(rw http.ResponseWriter, req *http.Request) {
// Appengine
var c appengine.Context = appengine.NewContext(req)
// Result, 0: success, 1: failed
var r int = http.StatusNoContent
// Return code
defer func() {
// Return status. WriteHeader() must be called before call to Write
if r == http.StatusNoContent {
// Changing the header after a call to WriteHeader (or Write) has no effect.
rw.WriteHeader(http.StatusNoContent)
} else if r == http.StatusBadRequest {
// http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
http.Error(rw, `Please follow https://aaa.appspot.com/api/0.1/topic-messages
{
"instanceid":""
"topic":""
"message":""
}`, http.StatusBadRequest)
} else {
http.Error(rw, http.StatusText(r), r)
}
}()
// Get body
b, err := ioutil.ReadAll(req.Body)
if err != nil {
c.Errorf("%s in reading body %s", err, b)
r = http.StatusBadRequest
return
}
var message TopicMessage
if err = json.Unmarshal(b, &message); err != nil {
c.Errorf("%s in decoding body %s", err, b)
r = http.StatusBadRequest
return
}
// Authenticate sender
var isValid bool = false
isValid, err = verifyRequest(message.InstanceId, c)
if err != nil {
c.Errorf("%s in authenticating request", err)
r = http.StatusBadRequest
return
}
if isValid == false {
c.Warningf("Invalid request, ignore")
r = http.StatusForbidden
return
}
// Make GCM message body
var bodyString string = fmt.Sprintf(`
{
"to":"/topics/%s",
"data": {
"message":"%s"
}
}`, message.Topic, message.Message)
// Make a POST request for GCM
pReq, err := http.NewRequest("POST", GcmURL, strings.NewReader(bodyString))
if err != nil {
c.Errorf("%s in makeing a HTTP request", err)
r = http.StatusInternalServerError
return
}
pReq.Header.Add("Content-Type", "application/json")
pReq.Header.Add("Authorization", "key="+GcmApiKey)
// Debug
c.Infof("%s", *pReq)
// Send request
var client = urlfetch.Client(c)
resp, err := client.Do(pReq)
if err != nil {
c.Errorf("%s in sending request", err)
r = http.StatusInternalServerError
return
}
defer resp.Body.Close()
// Check response
c.Infof("%d %s", resp.StatusCode, resp.Status)
// Get response body
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
c.Errorf("%s in reading response body", err)
r = http.StatusInternalServerError
return
}
c.Infof("%s", respBody)
}
// Receive a message from an APP instance.
// Check it's instancd ID.
// Send the message to the gruop.
// POST https://testgcmserver-1120.appspot.com/api/0.1/group-messages"
// Success: 204 No Content
// Failure: 400 Bad Request, 403 Forbidden, 404 NotFound, 500 InternalError
func SendGroupMessage(rw http.ResponseWriter, req *http.Request) {
// Appengine
var c appengine.Context = appengine.NewContext(req)
// Result, 0: success, 1: failed
var r int = http.StatusNoContent
// Return code
defer func() {
// Return status. WriteHeader() must be called before call to Write
if r == http.StatusNoContent {
// Changing the header after a call to WriteHeader (or Write) has no effect.
rw.WriteHeader(http.StatusNoContent)
} else if r == http.StatusBadRequest {
// http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
http.Error(rw, `Please follow https://aaa.appspot.com/api/0.1/group-messages
{
"instanceid":""
"groupName":""
"message":""
}`, http.StatusBadRequest)
} else {
http.Error(rw, http.StatusText(r), r)
}
}()
// Get body
b, err := ioutil.ReadAll(req.Body)
if err != nil {
c.Errorf("%s in reading body %s", err, b)
r = http.StatusBadRequest
return
}
var message GroupMessage
if err = json.Unmarshal(b, &message); err != nil {
c.Errorf("%s in decoding body %s", err, b)
r = http.StatusBadRequest
return
}
// Authenticate sender
var isValid bool = false
isValid, err = verifyRequest(message.InstanceId, c)
if err != nil {
c.Errorf("%s in authenticating request", err)
r = http.StatusBadRequest
return
}
if isValid == false {
c.Warningf("Invalid request, ignore")
r = http.StatusForbidden
return
}
// Search for existing group
var cKey *datastore.Key
var pGroup *Group
cKey, pGroup, err = searchGroup(message.GroupName, c)
if err != nil {
c.Errorf("%s in searching existing group %s", err, message.GroupName)
r = http.StatusInternalServerError
return
}
if cKey == nil {
c.Warningf("Group %s is not found", message.GroupName)
r = http.StatusBadRequest
return
}
// Make GCM message body
var bodyString string = fmt.Sprintf(`
{
"to":"%s",
"data": {
"message":"%s"
}
}`, pGroup.NotificationKey, message.Message)
// Make a POST request for GCM
pReq, err := http.NewRequest("POST", GcmURL, strings.NewReader(bodyString))
if err != nil {
c.Errorf("%s in makeing a HTTP request", err)
r = http.StatusInternalServerError
return
}
pReq.Header.Add("Content-Type", "application/json")
pReq.Header.Add("Authorization", "key="+GcmApiKey)
// Debug
c.Infof("Send request to GCM server %s", *pReq)
// Send request
var client = urlfetch.Client(c)
resp, err := client.Do(pReq)
if err != nil {
c.Errorf("%s in sending request", err)
r = http.StatusInternalServerError
return
}
defer resp.Body.Close()
// Check response
c.Infof("%d %s", resp.StatusCode, resp.Status)
// Get response body
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
c.Errorf("%s in reading response body", err)
r = http.StatusInternalServerError
return
}
c.Infof("%s", respBody)
}
// PUT https://testgcmserver-1120.appspot.com/api/0.1/myself"
// Success: 200 OK
// Failure: 400 Bad Request
func UpdateMyself(rw http.ResponseWriter, req *http.Request) {
// Appengine
var c appengine.Context = appengine.NewContext(req)
// Result, 0: success, 1: failed
var r int = 0
var cKey *datastore.Key = nil
defer func() {
// Return status. WriteHeader() must be called before call to Write
if r == 0 {
// Return status. WriteHeader() must be called before call to Write
rw.WriteHeader(http.StatusOK)
// Return body
var dst UserRegistrationResponseBody = UserRegistrationResponseBody{ UserId:cKey.Encode() }
if err := json.NewEncoder(rw).Encode(dst); err != nil {
c.Errorf("%s in encoding result %v", err, dst)
}
} else {
http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
}
}()
// Get data from body
b, err := ioutil.ReadAll(req.Body)
if err != nil {
c.Errorf("%s in reading body %s", err, b)
r = 1
return
}
// Vernon debug
c.Debugf("Got body %s", b)
var user User
if err = json.Unmarshal(b, &user); err != nil {
c.Errorf("%s in decoding body %s", err, b)
r = 1
return
}
// Check registration token starts with instance ID. That's the rule of Google API service authenticity
// Also check registration token is official-signed by sending the token to Google token authenticity check service
if user.RegistrationToken[0:len(user.InstanceId)] != user.InstanceId || isRegistrationTokenValid(user.RegistrationToken, c) == false {
c.Errorf("Instance ID %s is invalid", user.InstanceId)
r = 1
return
}
// Set now as the creation time. Precision to a second.
user.LastUpdateTime = time.Unix(time.Now().Unix(), 0)
// Search for existing user
var pKey *datastore.Key
var pOldUser *User
pKey, pOldUser, err = searchUser(user.InstanceId, c)
if err != nil {
c.Errorf("%s in searching existing user %v", err, user)
r = 1
return
}
if pKey == nil {
// Add new user into datastore
pKey = datastore.NewKey(c, UserKind, UserRoot, 0, nil)
cKey, err = datastore.Put(c, datastore.NewIncompleteKey(c, UserKind, pKey), &user)
if err != nil {
c.Errorf("%s in storing to datastore", err)
r = 1
return
}
c.Infof("Add user %+v", user)
} else if user.RegistrationToken == pOldUser.RegistrationToken {
// Duplicate request. Do nothing to datastore and return existing key
cKey = pKey
} else {
cKey, err = datastore.Put(c, pKey, &user)
if err != nil {
c.Errorf("%s in storing to datastore", err)
r = 1
return
}
c.Infof("Update user %+v", user)
}
}
// PUT https://testgcmserver-1120.appspot.com/api/0.1/groups"
// Success: 204 No Content
// Failure: 400 Bad Request, 403 Forbidden, 500 Internal Server Error
func JoinGroup(rw http.ResponseWriter, req *http.Request) {
// Appengine
var c appengine.Context = appengine.NewContext(req)
// Result, 0: success, 1: failed
var r int = http.StatusNoContent
var cKey *datastore.Key = nil
defer func() {
if r == http.StatusNoContent {
// Changing the header after a call to WriteHeader (or Write) has no effect.
rw.Header().Set("Location", req.URL.String()+"/"+cKey.Encode())
// Return status. WriteHeader() must be called before call to Write
rw.WriteHeader(r)
} else {
http.Error(rw, http.StatusText(r), r)
}
}()
// Get data from body
b, err := ioutil.ReadAll(req.Body)
if err != nil {
c.Errorf("%s in reading body %s", err, b)
r = http.StatusInternalServerError
return
}
// Vernon debug
c.Debugf("Got body %s", b)
var user GroupUser
if err = json.Unmarshal(b, &user); err != nil {
c.Errorf("%s in decoding body %s", err, b)
r = http.StatusBadRequest
return
}
// Authenticate sender & Search for user registration token
var pUser *User
var token string
_, pUser, err = searchUser(user.InstanceId, c)
if err != nil {
c.Errorf("%s in searching user %v", err, user.InstanceId)
r = http.StatusInternalServerError
return
}
if pUser == nil {
c.Errorf("User %s not found. Invalid request. Ignore.", user.InstanceId)
r = http.StatusForbidden
return
}
token = pUser.RegistrationToken
// Search for existing group
var pKey *datastore.Key
var pGroup *Group
pKey, pGroup, err = searchGroup(user.GroupName, c)
if err != nil {
c.Errorf("%s in searching existing group %s", err, user.GroupName)
r = http.StatusInternalServerError
return
}
// Make GCM message body
var operation GroupOperation
if pKey == nil {
// Create a new group on GCM server
operation.Operation = "create"
operation.Notification_key_name = user.GroupName
operation.Registration_ids = []string{token}
if r = sendGroupOperationToGcm(&operation, c); r != http.StatusOK {
c.Errorf("Send group operation to GCM failed")
return
}
r = http.StatusNoContent
// Add new group to the datastore
pGroup = &Group {
Name: user.GroupName,
Owner: user.InstanceId,
Members: []string {user.InstanceId},
NotificationKey: operation.Notification_key,
}
pKey = datastore.NewKey(c, GroupKind, GroupRoot, 0, nil)
cKey, err = datastore.Put(c, datastore.NewIncompleteKey(c, GroupKind, pKey), pGroup)
if err != nil {
c.Errorf("%s in storing to datastore", err)
r = http.StatusInternalServerError
return
}
c.Infof("Create group %+v", pGroup)
} else {
// Add the new user to the existing group on GCM server
operation.Operation = "add"
operation.Notification_key_name = user.GroupName
operation.Notification_key = pGroup.NotificationKey
operation.Registration_ids = []string{token}
if r = sendGroupOperationToGcm(&operation, c); r != http.StatusOK {
c.Errorf("Send group operation to GCM failed")
return
}
r = http.StatusNoContent
// Modify datastore
pGroup.Members = append(pGroup.Members, token)
cKey, err = datastore.Put(c, pKey, pGroup)
if err != nil {
c.Errorf("%s in storing to datastore", err)
r = http.StatusInternalServerError
return
}
c.Infof("Add user %s to group %s", user.InstanceId, user.GroupName)
}
}
// DELETE https://testgcmserver-1120.appspot.com/api/0.1/groups/xxx", xxx: Group name
// Header {"Instance-Id":"..."}
// Success returns 204 No Content
// Failure returns 400 Bad Request, 403 Forbidden, 500 Internal Server Error
func LeaveGroup(rw http.ResponseWriter, req *http.Request) {
// Appengine
var c appengine.Context = appengine.NewContext(req)
// Result, 0: success, 1: failed
var r int = http.StatusNoContent
// Sender instance ID
var instanceId string
// Sender registration token
var registrationToken string
// Group name to leave
var groupName string
// Then operation sent to GCM server
var operation GroupOperation
// Group in datastore
var cKey *datastore.Key
var pGroup *Group
// Error
var err error
// Function to write response header
defer func() {
if r == http.StatusNoContent {
// Return status. WriteHeader() must be called before call to Write
rw.WriteHeader(r)
} else {
http.Error(rw, http.StatusText(r), r)
}
}()
// Get instance ID from header
instanceId = req.Header.Get("Instance-Id")
if instanceId == "" {
c.Warningf("Missing instance ID. Ignore the request.")
r = http.StatusBadRequest
return
}
// Authenticate sender & Search for user registration token
var pUser *User
_, pUser, err = searchUser(instanceId, c)
if err != nil {
c.Errorf("%s in searching user %v", err, instanceId)
r = http.StatusInternalServerError
return
}
if pUser == nil {
c.Errorf("User %s not found. Invalid request. Ignore.", instanceId)
r = http.StatusForbidden
return
}
registrationToken = pUser.RegistrationToken
// Get group name from URL
var tokens []string
tokens = strings.Split(req.URL.Path, "/")
for i, v := range tokens {
if v == "groups" && i + 1 < len(tokens) {
groupName = tokens[i + 1]
break
}
}
if groupName == "" {
c.Warningf("Missing group name. Ignore the request.")
r = http.StatusBadRequest
return
}
// Vernon debug
c.Debugf("User %s is going to leave group %s", instanceId, groupName)
// Search for existing group
cKey, pGroup, err = searchGroup(groupName, c)
if err != nil {
c.Errorf("%s in searching existing group %s", err, groupName)
r = http.StatusInternalServerError
return
}
if cKey == nil {
c.Infof("Group %s has been deleted already", groupName)
return
}
var returnCode int = http.StatusOK
if instanceId == pGroup.Owner {
// Vernon debug
c.Debugf("User %s owns the group %s", instanceId, groupName)
// Remove all user from GCM server so that the group will be removed at the same time
for _, v := range pGroup.Members {
// Search user registration token
_, pUser, err = searchUser(v, c)
if err != nil {
c.Warningf("%s in searching user %v", err, v)
continue
}
if pUser == nil {
c.Warningf("User %s not found. Ignore.", v)
continue
}
registrationToken = pUser.RegistrationToken
// Make operation structure
operation.Operation = "remove"
operation.Notification_key_name = pGroup.Name
operation.Notification_key = pGroup.NotificationKey
operation.Registration_ids = []string{registrationToken}
if returnCode = sendGroupOperationToGcm(&operation, c); returnCode != http.StatusOK {
c.Warningf("Failed to remove user %s from group %s because sending group operation to GCM failed", v, groupName)
r = returnCode
continue
}
c.Infof("User %s is removed from group %s", pUser.InstanceId, groupName)
}
// Modify datastore
if err = datastore.Delete(c, cKey); err != nil {
c.Errorf("%s in delete group %s from datastore", err, groupName)
r = http.StatusInternalServerError
return
}
c.Infof("User %s removed group %s", instanceId, groupName)
} else {
// Vernon debug
c.Debugf("User %s doesn't own the group %s", instanceId, groupName)
// Remove the user from the existing group on GCM server
operation.Operation = "remove"
operation.Notification_key_name = groupName
operation.Notification_key = pGroup.NotificationKey
operation.Registration_ids = []string{registrationToken}
if returnCode = sendGroupOperationToGcm(&operation, c); returnCode != http.StatusOK {
c.Errorf("Send group operation to GCM failed")
r = returnCode
return
}
// Modify datastore
a := pGroup.Members
for i, x := range a {
if x == instanceId {
a[i] = a[len(a)-1]
a[len(a)-1] = ""
a = a[:len(a)-1]
break
}
}
pGroup.Members = a
cKey, err = datastore.Put(c, cKey, pGroup)
if err != nil {
c.Errorf("%s in storing to datastore", err)
r = http.StatusInternalServerError
return
}
c.Infof("Remove user %s from group %s", instanceId, groupName)
}
}
// Send a Google Cloud Messaging Device Group operation to GCM server
// Success: 200 OK. Store the notification key from server to the operation structure
// Failure: 400 Bad Request, 403 Forbidden, 500 Internal Server Error
func sendGroupOperationToGcm(pOperation *GroupOperation, c appengine.Context) (r int) {
// Initial variables
var err error = nil
r = http.StatusOK
// Check parameters
if pOperation == nil {
c.Errorf("Parameter pOperation is nil")
r = http.StatusInternalServerError
return
}
// Make a POST request for GCM
var b []byte
b, err = json.Marshal(pOperation)
if err != nil {
c.Errorf("%s in encoding an operation as JSON", err)
r = http.StatusBadRequest
return
}
pReq, err := http.NewRequest("POST", GcmGroupURL, bytes.NewReader(b))
if err != nil {
c.Errorf("%s in makeing a HTTP request", err)
r = http.StatusInternalServerError
return
}
pReq.Header.Add("Content-Type", "application/json")
pReq.Header.Add("Authorization", "key="+GcmApiKey)
pReq.Header.Add("project_id", GaeProjectNumber)
// Debug
c.Debugf("Send request to GCM server %s", *pReq)
c.Debugf("Send body to GCM server %s", b)
// Send request
var client = urlfetch.Client(c)
resp, err := client.Do(pReq)
if err != nil {
c.Errorf("%s in sending request", err)
r = http.StatusInternalServerError
return
}
// Get response body
var respBody GroupOperationResponse
defer resp.Body.Close()
b, err = ioutil.ReadAll(resp.Body)
if err != nil {
c.Errorf("%s in reading response body", err)
r = http.StatusInternalServerError
return
}
c.Infof("%s", b)
if err = json.Unmarshal(b, &respBody); err != nil {
c.Errorf("%s in decoding JSON response body", err)
r = http.StatusInternalServerError
return
}
// Check response
c.Infof("%d %s", resp.StatusCode, resp.Status)
if resp.StatusCode == http.StatusOK {
// Success. Write Notification Key to operation structure
pOperation.Notification_key = respBody.Notification_key
return
} else {
c.Errorf("GCM server replied that %s", respBody.Error)
r = http.StatusBadRequest
return
}
}
// Send APP instance ID to Google server to verify its authenticity
func isRegistrationTokenValid(token string, c appengine.Context) (isValid bool) {
if token == "" {
c.Warningf("Instance ID is empty")
return false
}
// Make a GET request for Google Instance ID service
pReq, err := http.NewRequest("GET", InstanceIdVerificationUrl + token, nil)
if err != nil {
c.Errorf("%s in makeing a HTTP request", err)
return false
}
pReq.Header.Add("Authorization", "key="+GcmApiKey)
// Debug
c.Infof("%s", *pReq)
// Send request
pClient := urlfetch.Client(c)
var resp *http.Response
var sleepTime int
// A Google APP Engine process must end within 60 seconds. So sleep no more than 16 seconds each retry.
for sleepTime = 1; sleepTime <= 16; sleepTime *= 2 {
resp, err = pClient.Do(pReq)
if err != nil {
c.Errorf("%s in verifying instance ID %s", err, token)
return false
}
// Retry while server is temporary invalid
if resp.StatusCode != http.StatusServiceUnavailable {
break
}
time.Sleep(1 * time.Second)
}
// Check response code
if resp.StatusCode != http.StatusOK {
c.Warningf("Invalid instance ID with response code %d %s", resp.StatusCode, resp.Status)
return false
}
// Get body
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
c.Errorf("%s in reading HTTP response body")
return false
}
// Decode body as JSON
var authenticity UserRegistrationTokenAuthenticity
if err := json.Unmarshal(body, &authenticity); err != nil {
c.Warningf("%s in decoding HTTP response body %s", body)
return false
}
if authenticity.Application != AppNamespace || authenticity.AuthorizedEntity != GaeProjectNumber {
c.Warningf("Invalid instance ID with authenticity application %s and authorized entity %s",
authenticity.Application, authenticity.AuthorizedEntity)
return false
}
return true
}
func searchUser(instanceId string, c appengine.Context) (key *datastore.Key, user *User, err error) {
var v []User
// Initial variables
key = nil
user = nil
err = nil
// Query
f := datastore.NewQuery(UserKind)
f = f.Filter("InstanceId=", instanceId)
k, err := f.GetAll(c, &v)
if err != nil {
c.Errorf("%s in getting data from datastore\n", err)
err = errors.New("Datastore is temporary unavailable")
return
}