-
Notifications
You must be signed in to change notification settings - Fork 0
/
filestore.go
192 lines (151 loc) · 4.31 KB
/
filestore.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
package cookies
import (
"bytes"
"errors"
"parkme-api/security"
"parkme-api/util/jsonutil"
"io/ioutil"
"os"
"gopkg.in/mgo.v2/bson"
)
// FileCookieStore manages cookies using files on the local drive
type FileCookieStore struct {
location string
}
// ReadCookie fetches a cookie from the cookie store
func (store *FileCookieStore) ReadCookie(key string) (*Session, error) {
fileName := fileLocation(store.location, key)
encryptedData, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, err
}
jsonData, err := security.Decrypt(encryptedData)
if err != nil {
return nil, err
}
var session *Session
err = jsonutil.DeserializeJSON(jsonData, session)
return session, err
}
// WriteCookie writes a cookie in the cookie store. If that cookie already exists,
// it is overwritten
func (store *FileCookieStore) WriteCookie(cookie *Session) error {
fileName := fileLocation(store.location, cookie.Token)
jsonData, err := jsonutil.SerializeJSON(cookie)
if err != nil {
return err
}
encryptedData, err := security.Encrypt(jsonData)
if err != nil {
return err
}
err = addUserToken(store.location, cookie.UserID, cookie.Token)
if err != nil {
return err
}
return ioutil.WriteFile(fileName, encryptedData, os.ModeDevice)
}
// DeleteCookie deletes a cookie from the cookie storage
func (store *FileCookieStore) DeleteCookie(cookie *Session) error {
fileName := fileLocation(store.location, cookie.Token)
err := removeUserToken(store.location, cookie.UserID, cookie.Token)
if err != nil {
return err
}
return os.Remove(fileName)
}
// GetAllUserCookies returns all the cookies that a certain user has
func (store *FileCookieStore) GetAllUserCookies(userID bson.ObjectId) ([]*Session, error) {
userTokens, err := getUserTokens(store.location, userID)
if err != nil {
return nil, err
}
var cookies []*Session
for _, token := range userTokens {
session, err := store.ReadCookie(token)
if err != nil {
return nil, err
}
cookies = append(cookies, session)
}
return cookies, err
}
// Init initializes the cookie store
func (store *FileCookieStore) Init() {
if _, err := os.Stat(store.location); os.IsNotExist(err) {
err = os.Mkdir(store.location, os.ModeDevice)
if err != nil {
panic(ErrInitializationFailed)
}
}
}
// cookieLocation computes and returns the location of a certain cookie
func fileLocation(storeLocation, key string) string {
var buffer bytes.Buffer
buffer.WriteString(storeLocation)
buffer.WriteRune('/')
buffer.WriteString(key)
return buffer.String()
}
// NewFileCookieStore creates a new NewFileCookieStore pointer entity
func NewFileCookieStore(storeLocation string) *FileCookieStore {
store := &FileCookieStore{location: storeLocation}
return store
}
func addUserToken(storeLocation string, userID bson.ObjectId, token string) error {
userTokens, err := getUserTokens(storeLocation, userID)
if err != nil {
return err
}
var isAlreadyAdded bool
for _, userToken := range userTokens {
if userToken == token {
isAlreadyAdded = true
break
}
}
if !isAlreadyAdded {
userTokens = append(userTokens, token)
}
return saveUserTokens(storeLocation, userID, userTokens)
}
func removeUserToken(storeLocation string, userID bson.ObjectId, token string) error {
userTokens, err := getUserTokens(storeLocation, userID)
if err != nil {
return err
}
tokenIndex := -1
for index, userToken := range userTokens {
if userToken == token {
tokenIndex = index
break
}
}
if tokenIndex == -1 {
return errors.New("User token does not exist")
}
userTokens = append(userTokens[:tokenIndex], userTokens[tokenIndex+1:]...)
return saveUserTokens(storeLocation, userID, userTokens)
}
func getUserTokens(storeLocation string, userID bson.ObjectId) ([]string, error) {
userIndexFile := fileLocation(storeLocation, userID.Hex())
fileContent, err := ioutil.ReadFile(userIndexFile)
if err != nil {
return nil, err
}
var userTokens []string
err = jsonutil.DeserializeJSON(fileContent, userTokens)
if err != nil {
return nil, err
}
return userTokens, nil
}
func saveUserTokens(storeLocation string, userID bson.ObjectId, tokens []string) error {
userIndexFile := fileLocation(storeLocation, userID.Hex())
jsonData, err := jsonutil.SerializeJSON(tokens)
if err != nil {
return err
}
err = ioutil.WriteFile(userIndexFile, jsonData, os.ModeDevice)
return err
}