-
Notifications
You must be signed in to change notification settings - Fork 4
/
session.go
86 lines (75 loc) · 2.42 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
// Content managed by Project Forge, see [projectforge.md] for details.
package cutil
import (
"context"
"github.com/valyala/fasthttp"
"projectforge.dev/projectforge/app"
"projectforge.dev/projectforge/app/controller/csession"
"projectforge.dev/projectforge/app/lib/telemetry"
"projectforge.dev/projectforge/app/lib/telemetry/httpmetrics"
"projectforge.dev/projectforge/app/lib/user"
"projectforge.dev/projectforge/app/util"
)
var initialIcons = []string{"searchbox"}
func LoadPageState(as *app.State, rc *fasthttp.RequestCtx, key string, logger util.Logger) *PageState {
parentCtx, logger := httpmetrics.ExtractHeaders(rc, logger)
ctx, span, logger := telemetry.StartSpan(parentCtx, "http:"+key, logger)
span.Attribute("path", string(rc.Request.URI().Path()))
if !telemetry.SkipControllerMetrics {
httpmetrics.InjectHTTP(rc, span)
}
session, flashes, prof := loadSession(ctx, as, rc, logger)
params := ParamSetFromRequest(rc)
return &PageState{
Method: string(rc.Method()), URI: rc.Request.URI(), Flashes: flashes, Session: session,
Profile: prof, Params: params,
Icons: initialIcons, Context: ctx, Span: span, Logger: logger,
}
}
func loadSession(_ context.Context, _ *app.State, rc *fasthttp.RequestCtx, logger util.Logger) (util.ValueMap, []string, *user.Profile) {
sessionBytes := rc.Request.Header.Cookie(util.AppKey)
session := util.ValueMap{}
if len(sessionBytes) > 0 {
dec, err := util.DecryptMessage(nil, string(sessionBytes), logger)
if err != nil {
logger.Warnf("error decrypting session: %+v", err)
}
err = util.FromJSON([]byte(dec), &session)
if err != nil {
session = util.ValueMap{}
}
}
flashes := util.StringSplitAndTrim(session.GetStringOpt(csession.WebFlashKey), ";")
if len(flashes) > 0 {
delete(session, csession.WebFlashKey)
err := csession.SaveSession(rc, session, logger)
if err != nil {
logger.Warnf("can't save session: %+v", err)
}
}
prof, err := loadProfile(session)
if err != nil {
logger.Warnf("can't load profile: %+v", err)
}
return session, flashes, prof
}
func loadProfile(session util.ValueMap) (*user.Profile, error) {
x, ok := session["profile"]
if !ok {
return user.DefaultProfile.Clone(), nil
}
s, ok := x.(string)
if !ok {
m, ok := x.(map[string]any)
if !ok {
return user.DefaultProfile.Clone(), nil
}
s = util.ToJSON(m)
}
p := &user.Profile{}
err := util.FromJSON([]byte(s), p)
if err != nil {
return nil, err
}
return p, nil
}