diff options
author | Runxi Yu <me@runxiyu.org> | 2025-04-06 01:55:21 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-04-06 02:08:58 +0800 |
commit | faa5ca8fab23176d390e9522f1485d467851545b (patch) | |
tree | d3b1d081e0ea5e7f71a94dc1d301e2540a8abcc8 /internal/unsorted/users.go | |
parent | Slight refactor on NewServer (diff) | |
download | forge-0.1.28.tar.gz forge-0.1.28.tar.zst forge-0.1.28.zip |
Move stuff into internal/unsortedv0.1.28
Diffstat (limited to 'internal/unsorted/users.go')
-rw-r--r-- | internal/unsorted/users.go | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/internal/unsorted/users.go b/internal/unsorted/users.go new file mode 100644 index 0000000..0f72eed --- /dev/null +++ b/internal/unsorted/users.go @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org> + +package unsorted + +import ( + "context" + + "github.com/jackc/pgx/v5" +) + +// addUserSSH adds a new user solely based on their SSH public key. +// +// TODO: Audit all users of this function. +func (s *Server) addUserSSH(ctx context.Context, pubkey string) (userID int, err error) { + var txn pgx.Tx + + if txn, err = s.database.Begin(ctx); err != nil { + return + } + defer func() { + _ = txn.Rollback(ctx) + }() + + if err = txn.QueryRow(ctx, `INSERT INTO users (type) VALUES ('pubkey_only') RETURNING id`).Scan(&userID); err != nil { + return + } + + if _, err = txn.Exec(ctx, `INSERT INTO ssh_public_keys (key_string, user_id) VALUES ($1, $2)`, pubkey, userID); err != nil { + return + } + + err = txn.Commit(ctx) + return +} |