/
service.go
122 lines (100 loc) · 3.17 KB
/
service.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
package hsessionsvs
import (
"github.com/drharryhe/has/common/hconf"
cache2 "github.com/patrickmn/go-cache"
"gorm.io/gorm"
"github.com/drharryhe/has/common/herrors"
"github.com/drharryhe/has/common/hlogger"
"github.com/drharryhe/has/common/htypes"
_ "github.com/drharryhe/has/common/htypes"
"github.com/drharryhe/has/core"
"github.com/drharryhe/has/plugins/hdatabaseplugin"
"github.com/drharryhe/has/plugins/hmemcacheplugin"
"github.com/drharryhe/has/utils/hdatetime"
)
const (
defaultSessionPerUser = 1
defaultTokenExpire = 60 * 24 * 7 //minute
)
func New() *Service {
return &Service{}
}
type Service struct {
core.Service
conf SessionService
db *gorm.DB
cache *cache2.Cache
}
func (this *Service) Open(s core.IServer, instance core.IService, options htypes.Any) *herrors.Error {
if err := this.Service.Open(s, instance, options); err != nil {
return err
}
this.db = this.UsePlugin("DatabasePlugin").(*hdatabaseplugin.Plugin).Capability().(map[string]*gorm.DB)[this.conf.DatabaseKey]
this.cache = this.UsePlugin("MemCachePlugin").(*hmemcacheplugin.Plugin).GetCache(this.Class())
if this.conf.AutoMigrate {
this.db.AutoMigrate(&SvsSessionToken{})
this.conf.AutoMigrate = false
hconf.Save()
}
if this.conf.SessionsPerUser <= 0 {
this.conf.SessionsPerUser = defaultSessionPerUser
}
if this.conf.TokenExpire <= 0 {
this.conf.TokenExpire = defaultTokenExpire
}
return nil
}
func (this *Service) EntityStub() *core.EntityStub {
return core.NewEntityStub(
&core.EntityStubOptions{
Owner: this,
})
}
func (this *Service) Config() core.IEntityConf {
return &this.conf
}
func (this *Service) Objects() []interface{} {
return []interface{}{
&SvsSessionToken{},
}
}
func (this *Service) checkToken(st *SvsSessionToken, req *VerifyTokenRequest) *herrors.Error {
if this.conf.CheckIP && (req.IP == nil || st.IP != *req.IP) {
return herrors.ErrCallerInvalidRequest.New(strInvalidToken).D("invalid token IP")
}
if this.conf.CheckUser && (req.User == nil || st.User != *req.User) {
return herrors.ErrCallerInvalidRequest.New(strInvalidToken).D("invalid token user")
}
if this.conf.CheckAgent && (req.Agent == nil || st.Agent != *req.Agent) {
return herrors.ErrCallerInvalidRequest.New(strInvalidToken).D("invalid token agent")
}
if st.Validity.Format("2006-01-02 15:04:05") < hdatetime.Now() {
return herrors.ErrCallerInvalidRequest.New(strInvalidToken).D("token expired")
}
return nil
}
func (this *Service) saveToken(t *SvsSessionToken) *herrors.Error {
if err := this.db.Save(t).Error; err != nil {
return herrors.ErrSysInternal.New(err.Error())
}
return nil
}
func (this *Service) updateToken(t *SvsSessionToken) *herrors.Error {
this.cache.Set(t.Value, t, 0)
if err := this.db.Save(t).Error; err != nil {
return herrors.ErrSysInternal.New(err.Error())
}
return nil
}
func (this *Service) deleteToken(token string) {
this.cache.Delete(token)
if err := this.db.Where("value = ?", token).Delete(&SvsSessionToken{}).Error; err != nil {
hlogger.Error(herrors.ErrSysInternal.New(err.Error()))
}
}
func (this *Service) isMagicToken(token string) bool {
if this.conf.MagicToken == "" {
return false
}
return this.conf.MagicToken == token
}