-
Notifications
You must be signed in to change notification settings - Fork 1
/
session.go
52 lines (42 loc) · 1.2 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
package oidclogin
import (
"context"
"fmt"
"net/http"
"time"
"github.com/alexedwards/scs/v2"
)
func (svc *OIDC) withSession(w http.ResponseWriter, r *http.Request, tx func(session context.Context) error) error {
session, err := svc.getSession(r)
if err != nil {
return fmt.Errorf("get session: %w", err)
}
err = tx(session)
if err != nil {
return err
}
return svc.commitSession(session, w)
}
func (svc *OIDC) getSession(r *http.Request) (context.Context, error) {
var token string
cookie, err := r.Cookie(svc.config.SessionManager.Cookie.Name)
if err == nil {
token = cookie.Value
}
return svc.config.SessionManager.Load(r.Context(), token)
}
func (svc *OIDC) commitSession(session context.Context, writer http.ResponseWriter) error {
writer.Header().Add("Vary", "Cookie")
//nolint:exhaustive
switch svc.config.SessionManager.Status(session) {
case scs.Modified:
token, expiry, err := svc.config.SessionManager.Commit(session)
if err != nil {
return fmt.Errorf("commit: %w", err)
}
svc.config.SessionManager.WriteSessionCookie(session, writer, token, expiry)
case scs.Destroyed:
svc.config.SessionManager.WriteSessionCookie(session, writer, "", time.Time{})
}
return nil
}