- commit
- 88726f8
- parent
- 3d03d7b
- author
- Antonio Mika
- date
- 2024-10-08 22:53:23 +0000 UTC
Refactor shared utilities
18 files changed,
+150,
-193
+2,
-2
1@@ -13,7 +13,7 @@ import (
2 "github.com/charmbracelet/wish"
3 "github.com/picosh/pico/db/postgres"
4 "github.com/picosh/pico/filehandlers"
5- "github.com/picosh/pico/filehandlers/util"
6+ "github.com/picosh/pico/shared"
7 "github.com/picosh/pico/shared/storage"
8 wsh "github.com/picosh/pico/wish"
9 "github.com/picosh/send/auth"
10@@ -83,7 +83,7 @@ func StartSshServer() {
11 }
12 handler := filehandlers.NewFileHandlerRouter(cfg, dbh, fileMap)
13
14- sshAuth := util.NewSshAuthHandler(dbh, logger, cfg)
15+ sshAuth := shared.NewSshAuthHandler(dbh, logger, cfg)
16 s, err := wish.NewServer(
17 wish.WithAddress(fmt.Sprintf("%s:%s", host, port)),
18 wish.WithHostKeyPath("ssh_data/term_info_ed25519"),
+6,
-8
1@@ -16,8 +16,6 @@ import (
2 "github.com/charmbracelet/ssh"
3 "github.com/charmbracelet/wish"
4 "github.com/picosh/pico/db"
5- "github.com/picosh/pico/filehandlers/util"
6- futil "github.com/picosh/pico/filehandlers/util"
7 "github.com/picosh/pico/shared"
8 "github.com/picosh/pico/shared/storage"
9 "github.com/picosh/pobj"
10@@ -118,7 +116,7 @@ func (h *UploadAssetHandler) GetLogger() *slog.Logger {
11 }
12
13 func (h *UploadAssetHandler) Read(s ssh.Session, entry *sendutils.FileEntry) (os.FileInfo, sendutils.ReaderAtCloser, error) {
14- user, err := futil.GetUser(s.Context())
15+ user, err := shared.GetUser(s.Context())
16 if err != nil {
17 return nil, nil, err
18 }
19@@ -152,7 +150,7 @@ func (h *UploadAssetHandler) Read(s ssh.Session, entry *sendutils.FileEntry) (os
20 func (h *UploadAssetHandler) List(s ssh.Session, fpath string, isDir bool, recursive bool) ([]os.FileInfo, error) {
21 var fileList []os.FileInfo
22
23- user, err := futil.GetUser(s.Context())
24+ user, err := shared.GetUser(s.Context())
25 if err != nil {
26 return fileList, err
27 }
28@@ -194,7 +192,7 @@ func (h *UploadAssetHandler) List(s ssh.Session, fpath string, isDir bool, recur
29 }
30
31 func (h *UploadAssetHandler) Validate(s ssh.Session) error {
32- user, err := util.GetUser(s.Context())
33+ user, err := shared.GetUser(s.Context())
34 if err != nil {
35 return err
36 }
37@@ -245,7 +243,7 @@ func (h *UploadAssetHandler) findDenylist(bucket sst.Bucket, project *db.Project
38 }
39
40 func (h *UploadAssetHandler) Write(s ssh.Session, entry *sendutils.FileEntry) (string, error) {
41- user, err := futil.GetUser(s.Context())
42+ user, err := shared.GetUser(s.Context())
43 if err != nil {
44 h.Cfg.Logger.Error("user not found in ctx", "err", err.Error())
45 return "", err
46@@ -311,7 +309,7 @@ func (h *UploadAssetHandler) Write(s ssh.Session, entry *sendutils.FileEntry) (s
47 return "", err
48 }
49
50- featureFlag, err := futil.GetFeatureFlag(s.Context())
51+ featureFlag, err := shared.GetFeatureFlag(s.Context())
52 if err != nil {
53 return "", err
54 }
55@@ -403,7 +401,7 @@ func (h *UploadAssetHandler) Write(s ssh.Session, entry *sendutils.FileEntry) (s
56 }
57
58 func (h *UploadAssetHandler) Delete(s ssh.Session, entry *sendutils.FileEntry) error {
59- user, err := futil.GetUser(s.Context())
60+ user, err := shared.GetUser(s.Context())
61 if err != nil {
62 h.Cfg.Logger.Error("user not found in ctx", "err", err.Error())
63 return err
+4,
-5
1@@ -14,7 +14,6 @@ import (
2 "github.com/charmbracelet/ssh"
3 exifremove "github.com/neurosnap/go-exif-remove"
4 "github.com/picosh/pico/db"
5- "github.com/picosh/pico/filehandlers/util"
6 "github.com/picosh/pico/shared"
7 "github.com/picosh/pico/shared/storage"
8 "github.com/picosh/pobj"
9@@ -49,7 +48,7 @@ func NewUploadImgHandler(dbpool db.DB, cfg *shared.ConfigSite, storage storage.S
10 }
11
12 func (h *UploadImgHandler) Read(s ssh.Session, entry *sendutils.FileEntry) (os.FileInfo, sendutils.ReaderAtCloser, error) {
13- user, err := util.GetUser(s.Context())
14+ user, err := shared.GetUser(s.Context())
15 if err != nil {
16 return nil, nil, err
17 }
18@@ -89,7 +88,7 @@ func (h *UploadImgHandler) Read(s ssh.Session, entry *sendutils.FileEntry) (os.F
19
20 func (h *UploadImgHandler) Write(s ssh.Session, entry *sendutils.FileEntry) (string, error) {
21 logger := h.Cfg.Logger
22- user, err := util.GetUser(s.Context())
23+ user, err := shared.GetUser(s.Context())
24 if err != nil {
25 logger.Error("could not get user from ctx", "err", err.Error())
26 return "", err
27@@ -146,7 +145,7 @@ func (h *UploadImgHandler) Write(s ssh.Session, entry *sendutils.FileEntry) (str
28 logger.Info("unable to find image, continuing", "filename", nextPost.Filename, "err", err.Error())
29 }
30
31- featureFlag, err := util.GetFeatureFlag(s.Context())
32+ featureFlag, err := shared.GetFeatureFlag(s.Context())
33 if err != nil {
34 return "", err
35 }
36@@ -193,7 +192,7 @@ func (h *UploadImgHandler) Write(s ssh.Session, entry *sendutils.FileEntry) (str
37 }
38
39 func (h *UploadImgHandler) Delete(s ssh.Session, entry *sendutils.FileEntry) error {
40- user, err := util.GetUser(s.Context())
41+ user, err := shared.GetUser(s.Context())
42 if err != nil {
43 return err
44 }
+1,
-2
1@@ -8,7 +8,6 @@ import (
2
3 "github.com/charmbracelet/ssh"
4 "github.com/picosh/pico/db"
5- "github.com/picosh/pico/filehandlers/util"
6 "github.com/picosh/pico/shared"
7 sendutils "github.com/picosh/send/utils"
8 "github.com/picosh/utils"
9@@ -81,7 +80,7 @@ func (h *UploadImgHandler) writeImg(s ssh.Session, data *PostMetaData) error {
10 if !valid {
11 return err
12 }
13- user, err := util.GetUser(s.Context())
14+ user, err := shared.GetUser(s.Context())
15 if err != nil {
16 return err
17 }
+3,
-4
1@@ -12,7 +12,6 @@ import (
2
3 "github.com/charmbracelet/ssh"
4 "github.com/picosh/pico/db"
5- "github.com/picosh/pico/filehandlers/util"
6 "github.com/picosh/pico/shared"
7 "github.com/picosh/pico/shared/storage"
8 sendutils "github.com/picosh/send/utils"
9@@ -48,7 +47,7 @@ func NewScpPostHandler(dbpool db.DB, cfg *shared.ConfigSite, hooks ScpFileHooks,
10 }
11
12 func (h *ScpUploadHandler) Read(s ssh.Session, entry *sendutils.FileEntry) (os.FileInfo, sendutils.ReaderAtCloser, error) {
13- user, err := util.GetUser(s.Context())
14+ user, err := shared.GetUser(s.Context())
15 if err != nil {
16 return nil, nil, err
17 }
18@@ -77,7 +76,7 @@ func (h *ScpUploadHandler) Read(s ssh.Session, entry *sendutils.FileEntry) (os.F
19
20 func (h *ScpUploadHandler) Write(s ssh.Session, entry *sendutils.FileEntry) (string, error) {
21 logger := h.Cfg.Logger
22- user, err := util.GetUser(s.Context())
23+ user, err := shared.GetUser(s.Context())
24 if err != nil {
25 logger.Error("error getting user from ctx", "err", err.Error())
26 return "", err
27@@ -264,7 +263,7 @@ func (h *ScpUploadHandler) Write(s ssh.Session, entry *sendutils.FileEntry) (str
28
29 func (h *ScpUploadHandler) Delete(s ssh.Session, entry *sendutils.FileEntry) error {
30 logger := h.Cfg.Logger
31- user, err := util.GetUser(s.Context())
32+ user, err := shared.GetUser(s.Context())
33 if err != nil {
34 logger.Error("could not get user from ctx", "err", err.Error())
35 return err
+2,
-3
1@@ -10,7 +10,6 @@ import (
2
3 "github.com/charmbracelet/ssh"
4 "github.com/picosh/pico/db"
5- "github.com/picosh/pico/filehandlers/util"
6 "github.com/picosh/pico/shared"
7 "github.com/picosh/send/utils"
8 )
9@@ -83,7 +82,7 @@ func (r *FileHandlerRouter) Read(s ssh.Session, entry *utils.FileEntry) (os.File
10
11 func BaseList(s ssh.Session, fpath string, isDir bool, recursive bool, spaces []string, dbpool db.DB) ([]os.FileInfo, error) {
12 var fileList []os.FileInfo
13- user, err := util.GetUser(s.Context())
14+ user, err := shared.GetUser(s.Context())
15 if err != nil {
16 return fileList, err
17 }
18@@ -154,7 +153,7 @@ func (r *FileHandlerRouter) GetLogger() *slog.Logger {
19 }
20
21 func (r *FileHandlerRouter) Validate(s ssh.Session) error {
22- user, err := util.GetUser(s.Context())
23+ user, err := shared.GetUser(s.Context())
24 if err != nil {
25 return err
26 }
+0,
-64
1@@ -1,64 +0,0 @@
2-package util
3-
4-import (
5- "log/slog"
6-
7- "github.com/charmbracelet/ssh"
8- "github.com/picosh/pico/db"
9- "github.com/picosh/pico/shared"
10- "github.com/picosh/utils"
11-)
12-
13-type SshAuthHandler struct {
14- DBPool db.DB
15- Logger *slog.Logger
16- Cfg *shared.ConfigSite
17-}
18-
19-func NewSshAuthHandler(dbpool db.DB, logger *slog.Logger, cfg *shared.ConfigSite) *SshAuthHandler {
20- return &SshAuthHandler{
21- DBPool: dbpool,
22- Logger: logger,
23- Cfg: cfg,
24- }
25-}
26-
27-func (r *SshAuthHandler) PubkeyAuthHandler(ctx ssh.Context, key ssh.PublicKey) bool {
28- shared.SetPublicKeyCtx(ctx, key)
29-
30- pubkey := utils.KeyForKeyText(key)
31-
32- user, err := r.DBPool.FindUserForKey(ctx.User(), pubkey)
33- if err != nil {
34- r.Logger.Error(
35- "could not find user for key",
36- "key", key,
37- "err", err,
38- )
39- return false
40- }
41-
42- if user.Name == "" {
43- r.Logger.Error("username is not set")
44- return false
45- }
46-
47- ff, _ := r.DBPool.FindFeatureForUser(user.ID, "plus")
48- // we have free tiers so users might not have a feature flag
49- // in which case we set sane defaults
50- if ff == nil {
51- ff = db.NewFeatureFlag(
52- user.ID,
53- "plus",
54- r.Cfg.MaxSize,
55- r.Cfg.MaxAssetSize,
56- )
57- }
58- // this is jank
59- ff.Data.StorageMax = ff.FindStorageMax(r.Cfg.MaxSize)
60- ff.Data.FileMax = ff.FindFileMax(r.Cfg.MaxAssetSize)
61-
62- SetUser(ctx, user)
63- SetFeatureFlag(ctx, ff)
64- return true
65-}
+0,
-35
1@@ -1,35 +0,0 @@
2-package util
3-
4-import (
5- "fmt"
6-
7- "github.com/charmbracelet/ssh"
8- "github.com/picosh/pico/db"
9-)
10-
11-type ctxUserKey struct{}
12-type ctxFeatureFlagKey struct{}
13-
14-func GetUser(ctx ssh.Context) (*db.User, error) {
15- user, ok := ctx.Value(ctxUserKey{}).(*db.User)
16- if !ok {
17- return user, fmt.Errorf("user not set on `ssh.Context()` for connection")
18- }
19- return user, nil
20-}
21-
22-func SetUser(ctx ssh.Context, user *db.User) {
23- ctx.SetValue(ctxUserKey{}, user)
24-}
25-
26-func GetFeatureFlag(ctx ssh.Context) (*db.FeatureFlag, error) {
27- ff, ok := ctx.Value(ctxFeatureFlagKey{}).(*db.FeatureFlag)
28- if !ok || ff.Name == "" {
29- return ff, fmt.Errorf("feature flag not set on `ssh.Context()` for connection")
30- }
31- return ff, nil
32-}
33-
34-func SetFeatureFlag(ctx ssh.Context, ff *db.FeatureFlag) {
35- ctx.SetValue(ctxFeatureFlagKey{}, ff)
36-}
+2,
-2
1@@ -13,7 +13,7 @@ import (
2 "github.com/charmbracelet/wish"
3 "github.com/picosh/pico/db/postgres"
4 "github.com/picosh/pico/filehandlers"
5- "github.com/picosh/pico/filehandlers/util"
6+ "github.com/picosh/pico/shared"
7 "github.com/picosh/pico/shared/storage"
8 wsh "github.com/picosh/pico/wish"
9 "github.com/picosh/send/auth"
10@@ -81,7 +81,7 @@ func StartSshServer() {
11 "fallback": filehandlers.NewScpPostHandler(dbh, cfg, hooks, st),
12 }
13 handler := filehandlers.NewFileHandlerRouter(cfg, dbh, fileMap)
14- sshAuth := util.NewSshAuthHandler(dbh, logger, cfg)
15+ sshAuth := shared.NewSshAuthHandler(dbh, logger, cfg)
16 s, err := wish.NewServer(
17 wish.WithAddress(fmt.Sprintf("%s:%s", host, port)),
18 wish.WithHostKeyPath("ssh_data/term_info_ed25519"),
+1,
-2
1@@ -14,7 +14,6 @@ import (
2 "github.com/picosh/pico/db"
3 "github.com/picosh/pico/db/postgres"
4 uploadassets "github.com/picosh/pico/filehandlers/assets"
5- "github.com/picosh/pico/filehandlers/util"
6 "github.com/picosh/pico/shared"
7 "github.com/picosh/pico/shared/storage"
8 wsh "github.com/picosh/pico/wish"
9@@ -97,7 +96,7 @@ func StartSshServer() {
10 HttpHandler: createHttpHandler(apiConfig),
11 }
12
13- sshAuth := util.NewSshAuthHandler(dbpool, logger, cfg)
14+ sshAuth := shared.NewSshAuthHandler(dbpool, logger, cfg)
15 s, err := wish.NewServer(
16 wish.WithAddress(fmt.Sprintf("%s:%s", host, port)),
17 wish.WithHostKeyPath("ssh_data/term_info_ed25519"),
+3,
-3
1@@ -38,7 +38,7 @@ func createHttpHandler(apiConfig *shared.ApiConfig) CtxHttpBridge {
2 "impersonating", asUser,
3 )
4
5- pubkey, err := shared.GetPublicKeyCtx(ctx)
6+ pubkey, err := shared.GetPublicKey(ctx)
7 if err != nil {
8 log.Error(err.Error(), "subdomain", subdomain)
9 return http.HandlerFunc(shared.UnauthorizedHandler)
10@@ -88,7 +88,7 @@ func createHttpHandler(apiConfig *shared.ApiConfig) CtxHttpBridge {
11 requester, _ = dbh.FindUserForName(asUser)
12 }
13
14- shared.SetUserCtx(ctx, requester)
15+ shared.SetUser(ctx, requester)
16
17 if !HasProjectAccess(project, owner, requester, pubkey) {
18 log.Error("no access")
19@@ -101,7 +101,7 @@ func createHttpHandler(apiConfig *shared.ApiConfig) CtxHttpBridge {
20 // special API endpoint for tunnel users accessing site
21 shared.NewCorsRoute("GET", "/api/current_user", func(w http.ResponseWriter, r *http.Request) {
22 w.Header().Set("Content-Type", "application/json")
23- user, err := shared.GetUserCtx(ctx)
24+ user, err := shared.GetUser(ctx)
25 if err != nil {
26 logger.Error("could not find user", "err", err.Error())
27 shared.JSONError(w, err.Error(), http.StatusNotFound)
+4,
-5
1@@ -14,7 +14,6 @@ import (
2 "github.com/charmbracelet/ssh"
3 "github.com/charmbracelet/wish"
4 "github.com/picosh/pico/db"
5- "github.com/picosh/pico/filehandlers/util"
6 "github.com/picosh/pico/shared"
7 sendutils "github.com/picosh/send/utils"
8 "github.com/picosh/utils"
9@@ -57,7 +56,7 @@ func (h *UploadHandler) Delete(s ssh.Session, entry *sendutils.FileEntry) error
10 }
11
12 func (h *UploadHandler) Read(s ssh.Session, entry *sendutils.FileEntry) (os.FileInfo, sendutils.ReaderAtCloser, error) {
13- user, err := util.GetUser(s.Context())
14+ user, err := shared.GetUser(s.Context())
15 if err != nil {
16 return nil, nil, err
17 }
18@@ -81,7 +80,7 @@ func (h *UploadHandler) Read(s ssh.Session, entry *sendutils.FileEntry) (os.File
19
20 func (h *UploadHandler) List(s ssh.Session, fpath string, isDir bool, recursive bool) ([]os.FileInfo, error) {
21 var fileList []os.FileInfo
22- user, err := util.GetUser(s.Context())
23+ user, err := shared.GetUser(s.Context())
24 if err != nil {
25 return fileList, err
26 }
27@@ -136,7 +135,7 @@ func (h *UploadHandler) Validate(s ssh.Session) error {
28 return fmt.Errorf("must have username set")
29 }
30
31- util.SetUser(s.Context(), user)
32+ shared.SetUser(s.Context(), user)
33 return nil
34 }
35
36@@ -277,7 +276,7 @@ func (h *UploadHandler) ProcessAuthorizedKeys(text []byte, logger *slog.Logger,
37
38 func (h *UploadHandler) Write(s ssh.Session, entry *sendutils.FileEntry) (string, error) {
39 logger := h.Cfg.Logger
40- user, err := util.GetUser(s.Context())
41+ user, err := shared.GetUser(s.Context())
42 if err != nil {
43 logger.Error(err.Error())
44 return "", err
+1,
-1
1@@ -28,7 +28,7 @@ import (
2 )
3
4 func authHandler(ctx ssh.Context, key ssh.PublicKey) bool {
5- shared.SetPublicKeyCtx(ctx, key)
6+ shared.SetPublicKey(ctx, key)
7 return true
8 }
9
+2,
-2
1@@ -14,7 +14,7 @@ import (
2 "github.com/picosh/pico/db/postgres"
3 "github.com/picosh/pico/filehandlers"
4 uploadimgs "github.com/picosh/pico/filehandlers/imgs"
5- "github.com/picosh/pico/filehandlers/util"
6+ "github.com/picosh/pico/shared"
7 "github.com/picosh/pico/shared/storage"
8 wsh "github.com/picosh/pico/wish"
9 "github.com/picosh/send/auth"
10@@ -92,7 +92,7 @@ func StartSshServer() {
11 DBPool: dbh,
12 }
13
14- sshAuth := util.NewSshAuthHandler(dbh, logger, cfg)
15+ sshAuth := shared.NewSshAuthHandler(dbh, logger, cfg)
16 s, err := wish.NewServer(
17 wish.WithAddress(fmt.Sprintf("%s:%s", host, port)),
18 wish.WithHostKeyPath("ssh_data/term_info_ed25519"),
+2,
-2
1@@ -12,7 +12,7 @@ import (
2 "github.com/charmbracelet/promwish"
3 "github.com/charmbracelet/wish"
4 "github.com/picosh/pico/db/postgres"
5- "github.com/picosh/pico/filehandlers/util"
6+ "github.com/picosh/pico/shared"
7 wsh "github.com/picosh/pico/wish"
8 psub "github.com/picosh/pubsub"
9 "github.com/picosh/utils"
10@@ -40,7 +40,7 @@ func StartSshServer() {
11 Waiters: syncmap.New[string, []string](),
12 }
13
14- sshAuth := util.NewSshAuthHandler(dbh, logger, cfg)
15+ sshAuth := shared.NewSshAuthHandler(dbh, logger, cfg)
16 s, err := wish.NewServer(
17 wish.WithAddress(fmt.Sprintf("%s:%s", host, port)),
18 wish.WithHostKeyPath("ssh_data/term_info_ed25519"),
1@@ -1,7 +1,105 @@
2 package shared
3
4-import "github.com/charmbracelet/ssh"
5+import (
6+ "fmt"
7+ "log/slog"
8
9-func PubkeyAuthHandler(ctx ssh.Context, key ssh.PublicKey) bool {
10+ "github.com/charmbracelet/ssh"
11+ "github.com/picosh/pico/db"
12+ "github.com/picosh/utils"
13+)
14+
15+type ctxUserKey struct{}
16+type ctxFeatureFlagKey struct{}
17+
18+func GetUser(ctx ssh.Context) (*db.User, error) {
19+ user, ok := ctx.Value(ctxUserKey{}).(*db.User)
20+ if !ok {
21+ return user, fmt.Errorf("user not set on `ssh.Context()` for connection")
22+ }
23+ return user, nil
24+}
25+
26+func SetUser(ctx ssh.Context, user *db.User) {
27+ ctx.SetValue(ctxUserKey{}, user)
28+}
29+
30+func GetFeatureFlag(ctx ssh.Context) (*db.FeatureFlag, error) {
31+ ff, ok := ctx.Value(ctxFeatureFlagKey{}).(*db.FeatureFlag)
32+ if !ok || ff.Name == "" {
33+ return ff, fmt.Errorf("feature flag not set on `ssh.Context()` for connection")
34+ }
35+ return ff, nil
36+}
37+
38+func SetFeatureFlag(ctx ssh.Context, ff *db.FeatureFlag) {
39+ ctx.SetValue(ctxFeatureFlagKey{}, ff)
40+}
41+
42+type ctxPublicKey struct{}
43+
44+func GetPublicKey(ctx ssh.Context) (ssh.PublicKey, error) {
45+ pk, ok := ctx.Value(ctxPublicKey{}).(ssh.PublicKey)
46+ if !ok {
47+ return nil, fmt.Errorf("public key not set on `ssh.Context()` for connection")
48+ }
49+ return pk, nil
50+}
51+
52+func SetPublicKey(ctx ssh.Context, pk ssh.PublicKey) {
53+ ctx.SetValue(ctxPublicKey{}, pk)
54+}
55+
56+type SshAuthHandler struct {
57+ DBPool db.DB
58+ Logger *slog.Logger
59+ Cfg *ConfigSite
60+}
61+
62+func NewSshAuthHandler(dbpool db.DB, logger *slog.Logger, cfg *ConfigSite) *SshAuthHandler {
63+ return &SshAuthHandler{
64+ DBPool: dbpool,
65+ Logger: logger,
66+ Cfg: cfg,
67+ }
68+}
69+
70+func (r *SshAuthHandler) PubkeyAuthHandler(ctx ssh.Context, key ssh.PublicKey) bool {
71+ SetPublicKey(ctx, key)
72+
73+ pubkey := utils.KeyForKeyText(key)
74+
75+ user, err := r.DBPool.FindUserForKey(ctx.User(), pubkey)
76+ if err != nil {
77+ r.Logger.Error(
78+ "could not find user for key",
79+ "key", key,
80+ "err", err,
81+ )
82+ return false
83+ }
84+
85+ if user.Name == "" {
86+ r.Logger.Error("username is not set")
87+ return false
88+ }
89+
90+ ff, _ := r.DBPool.FindFeatureForUser(user.ID, "plus")
91+ // we have free tiers so users might not have a feature flag
92+ // in which case we set sane defaults
93+ if ff == nil {
94+ ff = db.NewFeatureFlag(
95+ user.ID,
96+ "plus",
97+ r.Cfg.MaxSize,
98+ r.Cfg.MaxAssetSize,
99+ )
100+ }
101+ // this is jank
102+ ff.Data.StorageMax = ff.FindStorageMax(r.Cfg.MaxSize)
103+ ff.Data.FileMax = ff.FindFileMax(r.Cfg.MaxAssetSize)
104+
105+ SetUser(ctx, user)
106+ SetFeatureFlag(ctx, ff)
107 return true
108 }
1@@ -1,35 +1 @@
2 package shared
3-
4-import (
5- "fmt"
6-
7- "github.com/charmbracelet/ssh"
8- "github.com/picosh/pico/db"
9-)
10-
11-type ctxPublicKey struct{}
12-type ctxUserKey struct{}
13-
14-func GetPublicKeyCtx(ctx ssh.Context) (ssh.PublicKey, error) {
15- pk, ok := ctx.Value(ctxPublicKey{}).(ssh.PublicKey)
16- if !ok {
17- return nil, fmt.Errorf("public key not set on `ssh.Context()` for connection")
18- }
19- return pk, nil
20-}
21-
22-func SetPublicKeyCtx(ctx ssh.Context, pk ssh.PublicKey) {
23- ctx.SetValue(ctxPublicKey{}, pk)
24-}
25-
26-func GetUserCtx(ctx ssh.Context) (*db.User, error) {
27- pk, ok := ctx.Value(ctxUserKey{}).(*db.User)
28- if !ok {
29- return nil, fmt.Errorf("user not set on `ssh.Context()` for connection")
30- }
31- return pk, nil
32-}
33-
34-func SetUserCtx(ctx ssh.Context, user *db.User) {
35- ctx.SetValue(ctxUserKey{}, user)
36-}
+17,
-17
1@@ -47,7 +47,7 @@ func registerUser(apiConfig *shared.ApiConfig, ctx ssh.Context, pubkey ssh.Publi
2 }
3
4 picoApi := shared.NewUserApi(user, pubkey)
5- shared.SetUserCtx(ctx, user)
6+ shared.SetUser(ctx, user)
7 err = json.NewEncoder(w).Encode(picoApi)
8 if err != nil {
9 logger.Error("json encoding error", "err", err.Error())
10@@ -63,7 +63,7 @@ func getFeatures(apiConfig *shared.ApiConfig, ctx ssh.Context) http.HandlerFunc
11 logger := apiConfig.Cfg.Logger
12 return func(w http.ResponseWriter, r *http.Request) {
13 w.Header().Set("Content-Type", "application/json")
14- user, _ := shared.GetUserCtx(ctx)
15+ user, _ := shared.GetUser(ctx)
16 if !ensureUser(w, user) {
17 return
18 }
19@@ -93,7 +93,7 @@ func findOrCreateRssToken(apiConfig *shared.ApiConfig, ctx ssh.Context) http.Han
20 logger := apiConfig.Cfg.Logger
21 return func(w http.ResponseWriter, r *http.Request) {
22 w.Header().Set("Content-Type", "application/json")
23- user, _ := shared.GetUserCtx(ctx)
24+ user, _ := shared.GetUser(ctx)
25 if !ensureUser(w, user) {
26 return
27 }
28@@ -132,7 +132,7 @@ func getPublicKeys(httpCtx *shared.ApiConfig, ctx ssh.Context) http.HandlerFunc
29 logger := httpCtx.Cfg.Logger
30 return func(w http.ResponseWriter, r *http.Request) {
31 w.Header().Set("Content-Type", "application/json")
32- user, _ := shared.GetUserCtx(ctx)
33+ user, _ := shared.GetUser(ctx)
34 if !ensureUser(w, user) {
35 return
36 }
37@@ -169,7 +169,7 @@ func createPubkey(httpCtx *shared.ApiConfig, ctx ssh.Context) http.HandlerFunc {
38 logger := httpCtx.Cfg.Logger
39 return func(w http.ResponseWriter, r *http.Request) {
40 w.Header().Set("Content-Type", "application/json")
41- user, _ := shared.GetUserCtx(ctx)
42+ user, _ := shared.GetUser(ctx)
43 if !ensureUser(w, user) {
44 return
45 }
46@@ -207,7 +207,7 @@ func deletePubkey(httpCtx *shared.ApiConfig, ctx ssh.Context) http.HandlerFunc {
47 logger := httpCtx.Cfg.Logger
48 return func(w http.ResponseWriter, r *http.Request) {
49 w.Header().Set("Content-Type", "application/json")
50- user, _ := shared.GetUserCtx(ctx)
51+ user, _ := shared.GetUser(ctx)
52 if !ensureUser(w, user) {
53 return
54 }
55@@ -249,7 +249,7 @@ func patchPubkey(httpCtx *shared.ApiConfig, ctx ssh.Context) http.HandlerFunc {
56 logger := httpCtx.Cfg.Logger
57 return func(w http.ResponseWriter, r *http.Request) {
58 w.Header().Set("Content-Type", "application/json")
59- user, _ := shared.GetUserCtx(ctx)
60+ user, _ := shared.GetUser(ctx)
61 if !ensureUser(w, user) {
62 return
63 }
64@@ -303,7 +303,7 @@ func getTokens(httpCtx *shared.ApiConfig, ctx ssh.Context) http.HandlerFunc {
65 logger := httpCtx.Cfg.Logger
66 return func(w http.ResponseWriter, r *http.Request) {
67 w.Header().Set("Content-Type", "application/json")
68- user, _ := shared.GetUserCtx(ctx)
69+ user, _ := shared.GetUser(ctx)
70 if !ensureUser(w, user) {
71 return
72 }
73@@ -339,7 +339,7 @@ func createToken(httpCtx *shared.ApiConfig, ctx ssh.Context) http.HandlerFunc {
74 logger := httpCtx.Cfg.Logger
75 return func(w http.ResponseWriter, r *http.Request) {
76 w.Header().Set("Content-Type", "application/json")
77- user, _ := shared.GetUserCtx(ctx)
78+ user, _ := shared.GetUser(ctx)
79 if !ensureUser(w, user) {
80 return
81 }
82@@ -379,7 +379,7 @@ func deleteToken(httpCtx *shared.ApiConfig, ctx ssh.Context) http.HandlerFunc {
83 logger := httpCtx.Cfg.Logger
84 return func(w http.ResponseWriter, r *http.Request) {
85 w.Header().Set("Content-Type", "application/json")
86- user, _ := shared.GetUserCtx(ctx)
87+ user, _ := shared.GetUser(ctx)
88 if !ensureUser(w, user) {
89 return
90 }
91@@ -425,7 +425,7 @@ func getProjects(httpCtx *shared.ApiConfig, ctx ssh.Context) http.HandlerFunc {
92 logger := httpCtx.Cfg.Logger
93 return func(w http.ResponseWriter, r *http.Request) {
94 w.Header().Set("Content-Type", "application/json")
95- user, _ := shared.GetUserCtx(ctx)
96+ user, _ := shared.GetUser(ctx)
97 if !ensureUser(w, user) {
98 return
99 }
100@@ -456,7 +456,7 @@ func getPosts(httpCtx *shared.ApiConfig, ctx ssh.Context, space string) http.Han
101 logger := httpCtx.Cfg.Logger
102 return func(w http.ResponseWriter, r *http.Request) {
103 w.Header().Set("Content-Type", "application/json")
104- user, _ := shared.GetUserCtx(ctx)
105+ user, _ := shared.GetUser(ctx)
106 if !ensureUser(w, user) {
107 return
108 }
109@@ -502,7 +502,7 @@ func createFeature(httpCtx *shared.ApiConfig, ctx ssh.Context) http.HandlerFunc
110 logger := httpCtx.Cfg.Logger
111 return func(w http.ResponseWriter, r *http.Request) {
112 w.Header().Set("Content-Type", "application/json")
113- user, _ := shared.GetUserCtx(ctx)
114+ user, _ := shared.GetUser(ctx)
115 if !ensureUser(w, user) {
116 return
117 }
118@@ -542,7 +542,7 @@ func deleteFeature(httpCtx *shared.ApiConfig, ctx ssh.Context) http.HandlerFunc
119 logger := httpCtx.Cfg.Logger
120 return func(w http.ResponseWriter, r *http.Request) {
121 w.Header().Set("Content-Type", "application/json")
122- user, _ := shared.GetUserCtx(ctx)
123+ user, _ := shared.GetUser(ctx)
124 if !ensureUser(w, user) {
125 return
126 }
127@@ -574,7 +574,7 @@ func getProjectObjects(apiConfig *shared.ApiConfig, ctx ssh.Context) http.Handle
128 storage := apiConfig.Storage
129 return func(w http.ResponseWriter, r *http.Request) {
130 w.Header().Set("Content-Type", "application/json")
131- user, _ := shared.GetUserCtx(ctx)
132+ user, _ := shared.GetUser(ctx)
133 if !ensureUser(w, user) {
134 return
135 }
136@@ -616,7 +616,7 @@ func getAnalytics(apiConfig *shared.ApiConfig, ctx ssh.Context, sumtype, bytype,
137 dbpool := apiConfig.Dbpool
138 return func(w http.ResponseWriter, r *http.Request) {
139 w.Header().Set("Content-Type", "application/json")
140- user, _ := shared.GetUserCtx(ctx)
141+ user, _ := shared.GetUser(ctx)
142 if !ensureUser(w, user) {
143 return
144 }
145@@ -655,7 +655,7 @@ func getAnalytics(apiConfig *shared.ApiConfig, ctx ssh.Context, sumtype, bytype,
146
147 func CreateRoutes(apiConfig *shared.ApiConfig, ctx ssh.Context) []shared.Route {
148 logger := apiConfig.Cfg.Logger
149- pubkey, err := shared.GetPublicKeyCtx(ctx)
150+ pubkey, err := shared.GetPublicKey(ctx)
151 if err != nil {
152 logger.Error("could not get pubkey from ctx", "err", err.Error())
153 return []shared.Route{}