repos / pico

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

commit
71a4682
parent
f767ab9
author
Eric Bower
date
2024-03-11 19:06:35 +0000 UTC
chore: cleanup
3 files changed,  +30, -10
M db/db.go
+2, -0
 1@@ -17,6 +17,7 @@ var ErrPublicKeyTaken = errors.New("public key is already associated with anothe
 2 type PublicKey struct {
 3 	ID        string     `json:"id"`
 4 	UserID    string     `json:"user_id"`
 5+	Name      string     `json:"name"`
 6 	Key       string     `json:"key"`
 7 	CreatedAt *time.Time `json:"created_at"`
 8 }
 9@@ -280,6 +281,7 @@ type DB interface {
10 	RegisterUser(name, pubkey string) (*User, error)
11 	RemoveUsers(userIDs []string) error
12 	LinkUserKey(userID string, pubkey string, tx *sql.Tx) error
13+	InsertPublicKey(userID, pubkey, name string, tx *sql.Tx) (*PublicKey, error)
14 	FindPublicKeyForKey(pubkey string) (*PublicKey, error)
15 	FindKeysForUser(user *User) ([]*PublicKey, error)
16 	RemoveKeys(pubkeyIDs []string) error
M db/postgres/storage.go
+27, -4
 1@@ -136,8 +136,8 @@ var (
 2 )
 3 
 4 const (
 5-	sqlSelectPublicKey         = `SELECT id, user_id, public_key, created_at FROM public_keys WHERE public_key = $1`
 6-	sqlSelectPublicKeys        = `SELECT id, user_id, public_key, created_at FROM public_keys WHERE user_id = $1`
 7+	sqlSelectPublicKey         = `SELECT id, user_id, name, public_key, created_at FROM public_keys WHERE public_key = $1`
 8+	sqlSelectPublicKeys        = `SELECT id, user_id, name, public_key, created_at FROM public_keys WHERE user_id = $1`
 9 	sqlSelectUser              = `SELECT id, name, created_at FROM app_users WHERE id = $1`
10 	sqlSelectUserForName       = `SELECT id, name, created_at FROM app_users WHERE name = $1`
11 	sqlSelectUserForNameAndKey = `SELECT app_users.id, app_users.name, app_users.created_at, public_keys.id as pk_id, public_keys.public_key, public_keys.created_at as pk_created_at FROM app_users LEFT JOIN public_keys ON public_keys.user_id = app_users.id WHERE app_users.name = $1 AND public_keys.public_key = $2`
12@@ -419,6 +419,29 @@ func (me *PsqlDB) LinkUserKey(userID string, key string, tx *sql.Tx) error {
13 	return err
14 }
15 
16+func (me *PsqlDB) InsertPublicKey(userID, key, name string, tx *sql.Tx) (*db.PublicKey, error) {
17+	pk, _ := me.FindPublicKeyForKey(key)
18+	if pk != nil {
19+		return nil, db.ErrPublicKeyTaken
20+	}
21+	query := `INSERT INTO public_keys (user_id, public_key, name) VALUES ($1, $2, $3)`
22+	var err error
23+	if tx != nil {
24+		_, err = tx.Exec(query, userID, key, name)
25+	} else {
26+		_, err = me.Db.Exec(query, userID, key, name)
27+	}
28+	if err != nil {
29+		return nil, err
30+	}
31+
32+	pk, err = me.FindPublicKeyForKey(key)
33+	if err != nil {
34+		return nil, err
35+	}
36+	return pk, nil
37+}
38+
39 func (me *PsqlDB) FindPublicKeyForKey(key string) (*db.PublicKey, error) {
40 	var keys []*db.PublicKey
41 	rs, err := me.Db.Query(sqlSelectPublicKey, key)
42@@ -428,7 +451,7 @@ func (me *PsqlDB) FindPublicKeyForKey(key string) (*db.PublicKey, error) {
43 
44 	for rs.Next() {
45 		pk := &db.PublicKey{}
46-		err := rs.Scan(&pk.ID, &pk.UserID, &pk.Key, &pk.CreatedAt)
47+		err := rs.Scan(&pk.ID, &pk.UserID, &pk.Name, &pk.Key, &pk.CreatedAt)
48 		if err != nil {
49 			return nil, err
50 		}
51@@ -463,7 +486,7 @@ func (me *PsqlDB) FindKeysForUser(user *db.User) ([]*db.PublicKey, error) {
52 	}
53 	for rs.Next() {
54 		pk := &db.PublicKey{}
55-		err := rs.Scan(&pk.ID, &pk.UserID, &pk.Key, &pk.CreatedAt)
56+		err := rs.Scan(&pk.ID, &pk.UserID, &pk.Name, &pk.Key, &pk.CreatedAt)
57 		if err != nil {
58 			return keys, err
59 		}
M ui/api.go
+1, -6
 1@@ -159,16 +159,11 @@ func createPubkey(httpCtx *shared.HttpCtx, ctx ssh.Context, user *db.User) http.
 2 		var payload createPubkeyPayload
 3 		body, _ := io.ReadAll(r.Body)
 4 		_ = json.Unmarshal(body, &payload)
 5-		err := dbpool.LinkUserKey(user.ID, payload.Pubkey, nil)
 6+		pubkey, err := dbpool.InsertPublicKey(user.ID, payload.Pubkey, payload.Name, nil)
 7 		if err != nil {
 8 			shared.JSONError(w, err.Error(), http.StatusUnprocessableEntity)
 9 			return
10 		}
11-		pubkey, err := dbpool.FindPublicKeyForKey(payload.Pubkey)
12-		if err != nil {
13-			shared.JSONError(w, err.Error(), http.StatusNotFound)
14-			return
15-		}
16 
17 		err = json.NewEncoder(w).Encode(pubkey)
18 		if err != nil {