-
Notifications
You must be signed in to change notification settings - Fork 157
/
suiteTicket.go
66 lines (49 loc) · 1.41 KB
/
suiteTicket.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
package auth
import (
"errors"
"github.com/ArtisanCloud/PowerLibs/v3/cache"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
"time"
)
type SuiteTicket struct {
App *kernel.ApplicationInterface
*kernel.InteractsWithCache
}
func NewSuiteTicket(app *kernel.ApplicationInterface) (*SuiteTicket, error) {
config := (*app).GetContainer().GetConfig()
var cacheClient cache.CacheInterface = nil
if (*config)["cache"] != nil {
cacheClient = (*config)["cache"].(cache.CacheInterface)
}
ticket := &SuiteTicket{
App: app,
InteractsWithCache: kernel.NewInteractsWithCache(cacheClient),
}
return ticket, nil
}
func (verifyTicket *SuiteTicket) SetTicket(ticket string) (err error) {
cacheHandle := verifyTicket.GetCache()
cacheKey := verifyTicket.getCacheKey()
err = cacheHandle.Set(cacheKey, ticket, 3600*time.Second)
if err != nil {
return err
}
if !cacheHandle.Has(cacheKey) {
return errors.New("failed to cache suite ticket")
}
return nil
}
func (verifyTicket *SuiteTicket) GetTicket() (ticket string, err error) {
cacheHandle := verifyTicket.GetCache()
cacheKey := verifyTicket.getCacheKey()
cached, err := cacheHandle.Get(cacheKey, "")
if err != nil {
return "", err
}
ticket = cached.(string)
return ticket, nil
}
func (verifyTicket *SuiteTicket) getCacheKey() string {
config := (*verifyTicket.App).GetConfig()
return "powerwechat.open_work.suite_ticket." + config.GetString("suite_id", "")
}