-
Notifications
You must be signed in to change notification settings - Fork 4
/
session.go
109 lines (98 loc) · 3.06 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
// Content managed by Project Forge, see [projectforge.md] for details.
package cutil
import (
"context"
"strings"
"github.com/mileusna/useragent"
"github.com/valyala/fasthttp"
"golang.org/x/exp/slices"
"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)
ua := useragent.Parse(string(rc.Request.Header.Peek("User-Agent")))
os := strings.ToLower(ua.OS)
browser := strings.ToLower(ua.Name)
platform := "unknown"
switch {
case ua.Desktop:
platform = "desktop"
case ua.Tablet:
platform = "tablet"
case ua.Mobile:
platform = "mobile"
case ua.Bot:
platform = "bot"
}
span.Attribute("browser", browser)
span.Attribute("os", os)
return &PageState{
Method: string(rc.Method()), URI: rc.Request.URI(), Flashes: flashes, Session: session,
OS: os, OSVersion: ua.OSVersion, Browser: browser, BrowserVersion: ua.Version, Platform: platform,
Profile: prof, Params: params,
Icons: slices.Clone(initialIcons), Started: util.TimeCurrent(), Logger: logger, Context: ctx, Span: span,
}
}
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
}
if p.Name == "" {
p.Name = user.DefaultProfile.Name
}
return p, nil
}