/
session.go
217 lines (195 loc) · 6.03 KB
/
session.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package api
import (
"fmt"
"log/slog"
"net/http"
"github.com/alexhokl/helper/jsonhelper"
"github.com/gin-gonic/gin"
session "github.com/go-session/session/v3"
"github.com/go-webauthn/webauthn/webauthn"
)
type oidcSession struct {
State string `json:"state"`
PKCECodeChallengeMethod string `json:"pkce_code_challenge_method"`
PKCECodeChallenge string `json:"pkce_code_challenge"`
PKCECodeVerifier string `json:"pkce_code_verifier"`
RedirectURI string `json:"redirect_uri"`
IsSignUp bool `json:"is_sign_up"`
}
const sessionEmailKey = "email"
const sessionIsAuthenticatedKey = "is_authenticated"
const sessionWebAuthnSessionKey = "webauthn_session"
const sessionRedirectURLKey = "redirect_uri"
const sessionOIDCKey = "oidc"
func getEmailFromSession(c *gin.Context) string {
return getEmailFromSessionFromRequest(c.Writer, c.Request)
}
func getEmailFromSessionFromRequest(w http.ResponseWriter, r *http.Request) string {
email, ok := getValueFromSessionFromRequest(w, r, sessionEmailKey)
if !ok {
return ""
}
return email.(string)
}
func getAuthenticatedEmailFromGinContext(c *gin.Context) string {
return getAuthenticatedEmail(c.Writer, c.Request)
}
func getAuthenticatedEmail(w http.ResponseWriter, r *http.Request) string {
emailFromSession := getEmailFromSessionFromRequest(w, r)
if emailFromSession == "" {
return ""
}
authenticatedValue, ok := getValueFromSessionFromRequest(w, r, sessionIsAuthenticatedKey)
if !ok {
return ""
}
authenticated := authenticatedValue.(bool)
if !authenticated {
return ""
}
return emailFromSession
}
func isAuthenticated(c *gin.Context) bool {
emailFromSession := getEmailFromSession(c)
if emailFromSession == "" {
return false
}
authenticated, ok := getValueFromSession(c, sessionIsAuthenticatedKey)
if !ok {
return false
}
return authenticated.(bool)
}
func setAuthenticationToSession(c *gin.Context, isAuthenticated bool) error {
if !isKeyExistInSession(c, sessionEmailKey) {
return fmt.Errorf("email is not set in session")
}
return setValuesToSession(c, map[string]interface{}{
sessionIsAuthenticatedKey: isAuthenticated,
})
}
func setEmailToSession(c *gin.Context, email string) error {
return setValuesToSession(c, map[string]interface{}{
sessionEmailKey: email,
})
}
func unsetAuthenticatedEmail(c *gin.Context) error {
store, err := getSessionStoreFromRequest(c.Writer, c.Request)
if err != nil {
return err
}
store.Delete(sessionEmailKey)
store.Delete(sessionIsAuthenticatedKey)
if err := store.Save(); err != nil {
return err
}
return nil
}
func setWebAuthnSession(c *gin.Context, webAuthnSession *webauthn.SessionData) error {
json, err := jsonhelper.GetJSONString(webAuthnSession)
if err != nil {
return err
}
return setValuesToSession(c, map[string]interface{}{
sessionWebAuthnSessionKey: json,
})
}
func getWebAuthnSession(c *gin.Context) (*webauthn.SessionData, error) {
webAuthnSessionValue, ok := getValueFromSession(c, sessionWebAuthnSessionKey)
if !ok {
return nil, fmt.Errorf("webauthn session is not set")
}
sessionValueString, ok := webAuthnSessionValue.(string)
if !ok {
return nil, fmt.Errorf("webauthn session is not a string")
}
var session webauthn.SessionData
jsonhelper.ParseJSONString(sessionValueString, &session)
return &session, nil
}
func setOIDCSession(c *gin.Context, _ string, state string, pkceCodeChallengeMethod string, pkceCodeChallenge string, pkceCodeVerifier string, redirectURI string, isSignUp bool) error {
oidcSession := &oidcSession{
State: state,
PKCECodeChallengeMethod: pkceCodeChallengeMethod,
PKCECodeChallenge: pkceCodeChallenge,
PKCECodeVerifier: pkceCodeVerifier,
RedirectURI: redirectURI,
IsSignUp: isSignUp,
}
json, err := jsonhelper.GetJSONString(oidcSession)
if err != nil {
return err
}
return setValuesToSession(c, map[string]interface{}{
sessionOIDCKey: json,
})
}
func getOIDCSession(c *gin.Context, _ string) (state string, pkceCodeChallengeMethod string, pkceCodeChallenge string, pkceCodeVerifier string, redirectURI string, isSignUp bool, err error) {
oidcSessionValue, ok := getValueFromSession(c, sessionOIDCKey)
if !ok {
err = fmt.Errorf("session [%s] is not set", sessionOIDCKey)
return
}
sessionValueString, ok := oidcSessionValue.(string)
if !ok {
err = fmt.Errorf("session [%s] is not a string", sessionOIDCKey)
return
}
var session oidcSession
jsonhelper.ParseJSONString(sessionValueString, &session)
state = session.State
pkceCodeChallengeMethod = session.PKCECodeChallengeMethod
pkceCodeChallenge = session.PKCECodeChallenge
pkceCodeVerifier = session.PKCECodeVerifier
redirectURI = session.RedirectURI
isSignUp = session.IsSignUp
return
}
func deleteOIDCSession(c *gin.Context, oidcName string) error {
store, err := getSessionStoreFromRequest(c.Writer, c.Request)
if err != nil {
return err
}
store.Delete(fmt.Sprintf("oidc_%s", oidcName))
if err := store.Save(); err != nil {
return err
}
return nil
}
func setValuesToSession(c *gin.Context, keyValues map[string]interface{}) error {
store, err := getSessionStoreFromRequest(c.Writer, c.Request)
if err != nil {
return err
}
for key, value := range keyValues {
store.Set(key, value)
}
if err := store.Save(); err != nil {
return err
}
return nil
}
func getValueFromSession(c *gin.Context, key string) (interface{}, bool) {
return getValueFromSessionFromRequest(c.Writer, c.Request, key)
}
func getValueFromSessionFromRequest(w http.ResponseWriter, r *http.Request, key string) (interface{}, bool) {
store, err := getSessionStoreFromRequest(w, r)
if err != nil {
return nil, false
}
return store.Get(key)
}
func isKeyExistInSession(c *gin.Context, key string) bool {
_, ok := getValueFromSession(c, key)
return ok
}
func getSessionStoreFromRequest(w http.ResponseWriter, r *http.Request) (session.Store, error) {
store, err := session.Start(r.Context(), w, r)
if err != nil {
slog.Error(
"failed to get session store",
slog.String("error", err.Error()),
)
}
return store, err
}