-
Notifications
You must be signed in to change notification settings - Fork 28
/
global_session_service.go
52 lines (42 loc) · 1.25 KB
/
global_session_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
package webapp
import (
"github.com/authgear/authgear-server/pkg/auth/webapp"
"github.com/authgear/authgear-server/pkg/lib/config"
"github.com/authgear/authgear-server/pkg/lib/infra/redis/appredis"
"github.com/authgear/authgear-server/pkg/util/clock"
)
type SessionStore interface {
Get(id string) (session *webapp.Session, err error)
Update(session *webapp.Session) (err error)
}
type GlobalSessionServiceFactory struct {
Clock clock.Clock
RedisHandle *appredis.Handle
}
func (f *GlobalSessionServiceFactory) NewGlobalSessionService(appID config.AppID) *GlobalSessionService {
return newGlobalSessionService(appID, f.Clock, f.RedisHandle)
}
type GlobalSessionService struct {
SessionStore SessionStore
Publisher *Publisher
Clock clock.Clock
}
func (s *GlobalSessionService) GetSession(sessionID string) (session *webapp.Session, err error) {
return s.SessionStore.Get(sessionID)
}
func (s *GlobalSessionService) UpdateSession(session *webapp.Session) error {
now := s.Clock.NowUTC()
session.UpdatedAt = now
err := s.SessionStore.Update(session)
if err != nil {
return err
}
msg := &WebsocketMessage{
Kind: WebsocketMessageKindRefresh,
}
err = s.Publisher.Publish(session, msg)
if err != nil {
return err
}
return nil
}