-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadministrator.go
174 lines (160 loc) · 5.19 KB
/
administrator.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
package main
import (
"context"
"log"
"time"
"go.mongodb.org/mongo-driver/bson"
)
// CheckExpiries checks the expiries of the given collection and deletes the expired documents
func CheckExpiries(execute chan string) {
//execute the cleaning of a collection when the channel is triggered
db, err := connectToDB()
if err != nil {
log.Printf("[ERROR] Error connecting to database: %v\n", err)
return
}
defer db.Client().Disconnect(context.Background())
oauthStatesCollection := db.Collection("oauthStates")
refreshTokensCollection := db.Collection("refreshTokens")
usersTokensCollections := db.Collection("users")
pollingIDsCollection := db.Collection("pollingIDs")
for {
collection := <-execute
switch collection {
case "oauthStates":
log.Println("[DEBUG] Cleaning oauthStates collection")
stateCur, err := oauthStatesCollection.Find(context.Background(), bson.M{})
if err != nil {
log.Printf("[ERROR] Error getting the oauth states: %v\n", err)
continue
}
var states []State
err = stateCur.All(context.TODO(), &states)
if err != nil {
log.Println("[ERROR] Error getting all the states")
continue
}
for _, s := range states {
if s.ExpirationDate.Before(time.Now()) {
_, err = oauthStatesCollection.DeleteOne(context.Background(), bson.M{"_id": s.Id})
if err != nil {
log.Printf("[ERROR] Error deleting the state %s: %v\n", s.State, err)
continue
}
log.Printf("[INFO] Deleted state %s\n", s.State)
}
}
log.Println("[DEBUG] oauthStates collection cleaned")
case "refreshTokens":
log.Println("[DEBUG] Cleaning refreshTokens collection")
refreshTokensCur, err := refreshTokensCollection.Find(context.Background(), bson.M{})
if err != nil {
log.Printf("[ERROR] Error getting the refresh tokens: %v\n", err)
continue
}
var refreshTokens []RefreshToken
err = refreshTokensCur.All(context.TODO(), &refreshTokens)
if err != nil {
log.Println("[ERROR] Error getting all the refresh tokens")
continue
}
for _, r := range refreshTokens {
if r.Expiration.Before(time.Now()) {
_, err = refreshTokensCollection.DeleteOne(context.Background(), bson.M{"_id": r.ID})
if err != nil {
log.Printf("[ERROR] Error deleting the refresh token (%s) for %d: %v\n", r.Token, r.UserID, err)
continue
}
log.Printf("[INFO] Deleted refresh token (%s) for %d\n", r.Token, r.UserID)
}
}
log.Println("[DEBUG] refreshTokens collection cleaned")
case "users":
log.Println("[DEBUG] Cleaning users collection")
usersCur, err := usersTokensCollections.Find(context.Background(), bson.M{"isMock": true})
if err != nil {
log.Printf("[ERROR] Error getting the users: %v\n", err)
continue
}
var users []struct {
UserID int `bson:"userID"`
Password string `bson:"password"`
Name string `bson:"name"`
Pfp string `bson:"pfp"`
CreationDate time.Time `bson:"creationDate"`
IsMock bool `bson:"isMock"`
}
err = usersCur.All(context.Background(), &users)
if err != nil {
log.Println("[ERROR] Error getting all the users")
continue
}
for _, u := range users {
if u.CreationDate.AddDate(0, 0, 1).Before(time.Now()) {
_, err = usersTokensCollections.DeleteOne(context.Background(), bson.M{"userID": u.UserID})
if err != nil {
log.Printf("[ERROR] Error deleting the user (%d): %v\n", u.UserID, err)
continue
}
log.Printf("[INFO] Deleted mock user %s [%d] \n", u.Name, u.UserID)
}
}
log.Println("[DEBUG] users collection cleaned")
case "pollingIDs":
log.Println("[DEBUG] Cleaning pollingIDs collection")
pollingIDsCur, err := pollingIDsCollection.Find(context.Background(), bson.M{})
if err != nil {
log.Printf("[ERROR] Error getting the polling IDs: %v\n", err)
continue
}
var pollingIDs []Polling
err = pollingIDsCur.All(context.Background(), &pollingIDs)
if err != nil {
log.Printf("[ERROR] Error getting the polling IDs: %v\n", err)
continue
}
for _, p := range pollingIDs {
if p.ExpDate.Before(time.Now()) {
_, err = pollingIDsCollection.DeleteOne(context.Background(), bson.M{"_id": p.DBId})
if err != nil {
log.Printf("[ERROR] Error deleting the polling ID (%s): %v\n", p.DBId.String(), err)
continue
}
success := "failed"
if p.LoginSuccessful {
success = "success"
}
log.Printf("[INFO] Deleted polling ID %s of a %s login", p.RandomId, success)
}
}
log.Println("[DEBUG] pollingIDs collection cleaned")
}
}
}
// RunExecutor runs CheckExpiries function every given interval sending the event on the channel
func RunExecutor(oauthStateCleaningInterval, refreshTokensCleaningInterval, usersCleaningInterval, pollingIDsCleaningInterval time.Duration, execute chan string) {
go func() {
for {
execute <- "oauthStates"
time.Sleep(oauthStateCleaningInterval)
}
}()
go func() {
for {
execute <- "refreshTokens"
time.Sleep(refreshTokensCleaningInterval)
}
}()
go func() {
for {
execute <- "users"
time.Sleep(usersCleaningInterval)
}
}()
go func() {
for {
execute <- "pollingIDs"
time.Sleep(pollingIDsCleaningInterval)
}
}()
}