- commit
- 0df20ce
- parent
- 2129823
- author
- Eric Bower
- date
- 2024-02-21 15:52:22 +0000 UTC
refactor: make public keys unique (#81) BREAKING CHANGE: we will no longer support multiple account with the same public key
3 files changed,
+19,
-0
M
db/db.go
+1,
-0
1@@ -11,6 +11,7 @@ import (
2 var ErrNameTaken = errors.New("username has already been claimed")
3 var ErrNameDenied = errors.New("username is on the denylist")
4 var ErrNameInvalid = errors.New("username has invalid characters in it")
5+var ErrPublicKeyTaken = errors.New("public key is already associated with another user")
6
7 type PublicKey struct {
8 ID string `json:"id"`
+4,
-0
1@@ -378,6 +378,10 @@ func (me *PsqlDB) RemoveUsers(userIDs []string) error {
2 }
3
4 func (me *PsqlDB) LinkUserKey(userID string, key string) error {
5+ pk, _ := me.FindPublicKeyForKey(key)
6+ if pk != nil {
7+ return db.ErrPublicKeyTaken
8+ }
9 _, err := me.Db.Exec(sqlInsertPublicKey, userID, key)
10 return err
11 }
+14,
-0
1@@ -1,6 +1,7 @@
2 package createkey
3
4 import (
5+ "errors"
6 "strings"
7
8 "github.com/charmbracelet/bubbles/spinner"
9@@ -30,6 +31,7 @@ const (
10 type KeySetMsg string
11
12 type KeyInvalidMsg struct{}
13+type KeyTakenMsg struct{}
14
15 type errMsg struct {
16 err error
17@@ -193,6 +195,15 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
18
19 return m, nil
20
21+ case KeyTakenMsg:
22+ m.state = ready
23+ head := m.styles.Error.Render("Invalid public key. ")
24+ helpMsg := "Public key is associated with another user"
25+ body := m.styles.Subtle.Render(helpMsg)
26+ m.errMsg = m.styles.Wrap.Render(head + body)
27+
28+ return m, nil
29+
30 case errMsg:
31 m.state = ready
32 head := m.styles.Error.Render("Oh, what? There was a curious error we were not expecting. ")
33@@ -270,6 +281,9 @@ func addPublicKey(m Model) tea.Cmd {
34 key := sanitizeKey(m.newKey)
35 err := m.dbpool.LinkUserKey(m.user.ID, key)
36 if err != nil {
37+ if errors.Is(err, db.ErrPublicKeyTaken) {
38+ return KeyTakenMsg{}
39+ }
40 return errMsg{err}
41 }
42