repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

commit
4483b9c
parent
b7e5168
author
Eric Bower
date
2024-09-08 03:32:44 +0000 UTC
fix(pubsub): scope `ls` to ssh user
1 files changed,  +26, -3
M pubsub/cli.go
+26, -3
 1@@ -40,6 +40,24 @@ func getUser(s ssh.Session, dbpool db.DB) (*db.User, error) {
 2 	return user, nil
 3 }
 4 
 5+// scope channel to user by prefixing name.
 6+func toChannel(userName, name string) string {
 7+	return fmt.Sprintf("%s@%s", userName, name)
 8+}
 9+
10+// extract user and scoped channel from channel.
11+func fromChannel(channel string) (string, string) {
12+	sp := strings.SplitN(channel, "@", 2)
13+	ln := len(sp)
14+	if ln == 0 {
15+		return "", ""
16+	}
17+	if ln == 1 {
18+		return "", ""
19+	}
20+	return sp[0], sp[1]
21+}
22+
23 var helpStr = "Commands: [pub, sub, ls]\n"
24 
25 type CliHandler struct {
26@@ -84,10 +102,15 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
27 						writer := NewTabWriter(sesh)
28 						fmt.Fprintln(writer, "Channel\tID")
29 						for _, sub := range subs {
30+							userName, channel := fromChannel(sub.Name)
31+							if userName != user.Name {
32+								continue
33+							}
34+
35 							fmt.Fprintf(
36 								writer,
37 								"%s\t%s\n",
38-								sub.Name, sub.ID,
39+								channel, sub.ID,
40 							)
41 						}
42 						writer.Flush()
43@@ -110,7 +133,7 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
44 			if cmd == "pub" {
45 				wish.Println(sesh, "sending msg ...")
46 				msg := &psub.Msg{
47-					Name:   fmt.Sprintf("%s@%s", user.Name, repoName),
48+					Name:   toChannel(user.Name, repoName),
49 					Reader: sesh,
50 				}
51 
52@@ -136,7 +159,7 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
53 			} else if cmd == "sub" {
54 				err = pubsub.PubSub.Sub(&psub.Subscriber{
55 					ID:     uuid.NewString(),
56-					Name:   fmt.Sprintf("%s@%s", user.Name, repoName),
57+					Name:   toChannel(user.Name, repoName),
58 					Writer: sesh,
59 					Chan:   make(chan error),
60 				})