This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
forked from mattermost/mattermost
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tokens_store.go
78 lines (62 loc) · 2.48 KB
/
tokens_store.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
// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package sqlstore
import (
"database/sql"
"net/http"
l4g "github.com/alecthomas/log4go"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/store"
)
type SqlTokenStore struct {
SqlStore
}
func NewSqlTokenStore(sqlStore SqlStore) store.TokenStore {
s := &SqlTokenStore{sqlStore}
for _, db := range sqlStore.GetAllConns() {
table := db.AddTableWithName(model.Token{}, "Tokens").SetKeys(false, "Token")
table.ColMap("Token").SetMaxSize(64)
table.ColMap("Type").SetMaxSize(64)
table.ColMap("Extra").SetMaxSize(128)
}
return s
}
func (s SqlTokenStore) CreateIndexesIfNotExists() {
}
func (s SqlTokenStore) Save(token *model.Token) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
if result.Err = token.IsValid(); result.Err != nil {
return
}
if err := s.GetMaster().Insert(token); err != nil {
result.Err = model.NewAppError("SqlTokenStore.Save", "store.sql_recover.save.app_error", nil, "", http.StatusInternalServerError)
}
})
}
func (s SqlTokenStore) Delete(token string) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
if _, err := s.GetMaster().Exec("DELETE FROM Tokens WHERE Token = :Token", map[string]interface{}{"Token": token}); err != nil {
result.Err = model.NewAppError("SqlTokenStore.Delete", "store.sql_recover.delete.app_error", nil, "", http.StatusInternalServerError)
}
})
}
func (s SqlTokenStore) GetByToken(tokenString string) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
token := model.Token{}
if err := s.GetReplica().SelectOne(&token, "SELECT * FROM Tokens WHERE Token = :Token", map[string]interface{}{"Token": tokenString}); err != nil {
if err == sql.ErrNoRows {
result.Err = model.NewAppError("SqlTokenStore.GetByToken", "store.sql_recover.get_by_code.app_error", nil, err.Error(), http.StatusBadRequest)
} else {
result.Err = model.NewAppError("SqlTokenStore.GetByToken", "store.sql_recover.get_by_code.app_error", nil, err.Error(), http.StatusInternalServerError)
}
}
result.Data = &token
})
}
func (s SqlTokenStore) Cleanup() {
l4g.Debug("Cleaning up token store.")
deltime := model.GetMillis() - model.MAX_TOKEN_EXIPRY_TIME
if _, err := s.GetMaster().Exec("DELETE FROM Tokens WHERE CreateAt < :DelTime", map[string]interface{}{"DelTime": deltime}); err != nil {
l4g.Error("Unable to cleanup token store.")
}
}