-
Notifications
You must be signed in to change notification settings - Fork 29
/
logout.go
131 lines (114 loc) · 3.83 KB
/
logout.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
// Copyright 2017 Vector Creations Ltd
//
// 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.
//
//
// Modifications copyright (C) 2020 Finogeeks Co., Ltd
package routing
import (
"context"
"net/http"
"time"
"github.com/finogeeks/ligase/common"
"github.com/finogeeks/ligase/common/filter"
"github.com/finogeeks/ligase/core"
"github.com/finogeeks/ligase/model/service"
"github.com/finogeeks/ligase/model/types"
"github.com/finogeeks/ligase/skunkworks/log"
"github.com/finogeeks/ligase/storage/model"
)
// Logout handles POST /logout
func Logout(
ctx context.Context,
deviceDB model.DeviceDatabase,
userID, deviceID string,
cache service.Cache,
encryptDB model.EncryptorAPIDatabase,
syncDB model.SyncAPIDatabase,
tokenFilter *filter.Filter,
rpcClient *common.RpcClient,
) (int, core.Coder) {
LogoutDevice(ctx, userID, deviceID, deviceDB, cache, encryptDB, syncDB, tokenFilter, rpcClient)
return http.StatusOK, nil
}
func LogoutDevice(
ctx context.Context,
userID,
deviceID string,
deviceDB model.DeviceDatabase,
cache service.Cache,
encryptDB model.EncryptorAPIDatabase,
syncDB model.SyncAPIDatabase,
tokenFilter *filter.Filter,
rpcClient *common.RpcClient,
) {
createdTimeMS := time.Now().UnixNano() / 1000000
log.Infof("logout user %s device %s", userID, deviceID)
if err := deviceDB.RemoveDevice(ctx, deviceID, userID, createdTimeMS); err != nil {
log.Errorf("Log out remove device error, device: %s , user: %s , error: %v", deviceID, userID, err)
}
err := encryptDB.DeleteDeviceKeys(ctx, deviceID, userID)
if err != nil {
log.Errorf("Log out remove device keys error, device: %s , user: %s , error: %v", deviceID, userID, err)
}
cache.DeleteDeviceOneTimeKey(userID, deviceID)
cache.DeleteDeviceKey(userID, deviceID)
err = syncDB.DeleteDeviceStdMessage(ctx, userID, deviceID)
if err != nil {
log.Errorf("Log out remove device std message, device: %s , user: %s , error: %v", deviceID, userID, err)
}
content := types.KeyUpdateContent{
Type: types.ONETIMEKEYUPDATE,
OneTimeKeyChangeUserId: userID,
OneTimeKeyChangeDeviceId: deviceID,
}
bytes, err := json.Marshal(content)
if err == nil {
rpcClient.Pub(types.KeyUpdateTopicDef, bytes)
} else {
log.Errorf("Log out pub key update, device: %s , user: %s , error: %v", deviceID, userID, err)
}
pubLogoutToken(userID, deviceID, rpcClient)
}
func pubLogoutToken(userID string, deviceID string, rpcClient *common.RpcClient) {
content := types.FilterTokenContent{
UserID: userID,
DeviceID: deviceID,
FilterType: types.FILTERTOKENDEL,
}
bytes, err := json.Marshal(content)
if err == nil {
log.Infof("pub logout filter token info %s", string(bytes))
rpcClient.Pub(types.FilterTokenTopicDef, bytes)
} else {
log.Errorf("pub logout filter token info Marshal err %v", err)
}
}
// LogoutAll handles POST /logout/all
func LogoutAll(
ctx context.Context,
deviceDB model.DeviceDatabase,
userID, deviceID string,
cache service.Cache,
encryptDB model.EncryptorAPIDatabase,
syncDB model.SyncAPIDatabase,
tokenFilter *filter.Filter,
rpcClient *common.RpcClient,
) (int, core.Coder) {
log.Infof("logout all user %s device %s", userID, deviceID)
devs := cache.GetDevicesByUserID(userID)
for _, dev := range *devs {
LogoutDevice(ctx, dev.UserID, dev.ID, deviceDB, cache, encryptDB, syncDB, tokenFilter, rpcClient)
}
return http.StatusOK, nil
}