-
Notifications
You must be signed in to change notification settings - Fork 4
/
session.go
97 lines (83 loc) · 2.7 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
// 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 {
ctx, logger := httpmetrics.ExtractHeaders(rc, logger)
traceCtx, span, logger := telemetry.StartSpan(ctx, "http:"+key, logger)
span.Attribute("path", string(rc.Request.URI().Path()))
httpmetrics.InjectHTTP(rc, span)
session, flashes, prof, accts := loadSession(ctx, as, rc, logger)
params := ParamSetFromRequest(rc)
isAuthed, _ := user.Check("/", accts)
isAdmin, _ := user.Check("/admin", accts)
return &PageState{
Method: string(rc.Method()), URI: rc.Request.URI(), Flashes: flashes, Session: session,
Profile: prof, Accounts: accts, Authed: isAuthed, Admin: isAdmin, Params: params,
Icons: initialIcons, Context: traceCtx, Span: span, Logger: logger,
}
}
func loadSession(ctx context.Context, as *app.State, rc *fasthttp.RequestCtx, logger util.Logger) (util.ValueMap, []string, *user.Profile, user.Accounts) {
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)
}
var accts user.Accounts
authX, ok := session[csession.WebAuthKey]
if ok {
authS, ok := authX.(string)
if ok {
accts = user.AccountsFromString(authS)
}
}
return session, flashes, prof, accts
}
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
}