/
store.go
159 lines (129 loc) · 3.58 KB
/
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
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
package authenticationflow
import (
"context"
"encoding/json"
"errors"
"fmt"
goredis "github.com/go-redis/redis/v8"
"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/duration"
)
const Lifetime = duration.UserInteraction
type StoreImpl struct {
Redis *appredis.Handle
AppID config.AppID
Context context.Context
}
func (s *StoreImpl) CreateFlow(flow *Flow) error {
bytes, err := json.Marshal(flow)
if err != nil {
return err
}
return s.Redis.WithConn(func(conn *goredis.Conn) error {
flowKey := redisFlowKey(s.AppID, flow.FlowID)
stateKey := redisFlowStateKey(s.AppID, flow.StateToken)
ttl := Lifetime
_, err := conn.SetEX(s.Context, flowKey, []byte(flowKey), ttl).Result()
if err != nil {
return err
}
_, err = conn.SetEX(s.Context, stateKey, bytes, ttl).Result()
if err != nil {
return err
}
return nil
})
}
func (s *StoreImpl) GetFlowByStateToken(stateToken string) (*Flow, error) {
stateKey := redisFlowStateKey(s.AppID, stateToken)
var flow Flow
err := s.Redis.WithConn(func(conn *goredis.Conn) error {
bytes, err := conn.Get(s.Context, stateKey).Bytes()
if errors.Is(err, goredis.Nil) {
return ErrFlowNotFound
}
if err != nil {
return err
}
err = json.Unmarshal(bytes, &flow)
if err != nil {
return err
}
flowKey := redisFlowKey(s.AppID, flow.FlowID)
_, err = conn.Get(s.Context, flowKey).Result()
if errors.Is(err, goredis.Nil) {
return ErrFlowNotFound
}
if err != nil {
return err
}
return nil
})
return &flow, err
}
func (s *StoreImpl) DeleteFlow(flow *Flow) error {
// We do not delete the states because there are many of them.
// Deleting the flowID is enough to make GetFlowByStateToken to return ErrFlowNotFound.
return s.Redis.WithConn(func(conn *goredis.Conn) error {
flowKey := redisFlowKey(s.AppID, flow.FlowID)
_, err := conn.Del(s.Context, flowKey).Result()
if err != nil {
return err
}
return nil
})
}
func (s *StoreImpl) CreateSession(session *Session) error {
bytes, err := json.Marshal(session)
if err != nil {
return err
}
return s.Redis.WithConn(func(conn *goredis.Conn) error {
sessionKey := redisFlowSessionKey(s.AppID, session.FlowID)
ttl := Lifetime
_, err := conn.SetEX(s.Context, sessionKey, bytes, ttl).Result()
if err != nil {
return err
}
return nil
})
}
func (s *StoreImpl) GetSession(flowID string) (*Session, error) {
sessionKey := redisFlowSessionKey(s.AppID, flowID)
var session Session
err := s.Redis.WithConn(func(conn *goredis.Conn) error {
bytes, err := conn.Get(s.Context, sessionKey).Bytes()
if errors.Is(err, goredis.Nil) {
return ErrFlowNotFound
}
if err != nil {
return err
}
err = json.Unmarshal(bytes, &session)
if err != nil {
return err
}
return nil
})
return &session, err
}
func (s *StoreImpl) DeleteSession(session *Session) error {
return s.Redis.WithConn(func(conn *goredis.Conn) error {
sessionKey := redisFlowSessionKey(s.AppID, session.FlowID)
_, err := conn.Del(s.Context, sessionKey).Result()
if err != nil {
return err
}
return nil
})
}
func redisFlowKey(appID config.AppID, flowID string) string {
return fmt.Sprintf("app:%s:authenticationflow_flow:%s", appID, flowID)
}
func redisFlowStateKey(appID config.AppID, stateToken string) string {
return fmt.Sprintf("app:%s:authenticationflow_state:%s", appID, stateToken)
}
func redisFlowSessionKey(appID config.AppID, flowID string) string {
return fmt.Sprintf("app:%s:authenticationflow_session:%s", appID, flowID)
}