2021-02-09 11:53:33 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2021-03-18 16:49:52 +00:00
|
|
|
// insert-user is a utility to create a new member and password
|
2021-02-08 16:47:42 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-02-09 16:29:42 +00:00
|
|
|
"context"
|
2021-03-18 16:49:52 +00:00
|
|
|
"flag"
|
2021-02-08 16:47:42 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-03-18 16:49:52 +00:00
|
|
|
"os/user"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2021-02-08 16:47:42 +00:00
|
|
|
"syscall"
|
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
|
|
|
|
2021-04-14 09:39:44 +00:00
|
|
|
"github.com/ssb-ngi-pointer/go-ssb-room/internal/repo"
|
|
|
|
"github.com/ssb-ngi-pointer/go-ssb-room/roomdb"
|
2021-03-16 14:11:13 +00:00
|
|
|
"github.com/ssb-ngi-pointer/go-ssb-room/roomdb/sqlite"
|
2021-04-14 09:39:44 +00:00
|
|
|
refs "go.mindeco.de/ssb-refs"
|
2021-02-08 16:47:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-03-18 16:49:52 +00:00
|
|
|
u, err := user.Current()
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
var (
|
2021-03-29 08:23:03 +00:00
|
|
|
login string
|
2021-03-23 17:00:08 +00:00
|
|
|
pubKey *refs.FeedRef
|
2021-03-19 13:02:19 +00:00
|
|
|
role roomdb.Role = roomdb.RoleAdmin
|
2021-03-23 17:00:08 +00:00
|
|
|
repoPath string
|
2021-03-18 16:49:52 +00:00
|
|
|
)
|
|
|
|
|
2021-03-29 08:23:03 +00:00
|
|
|
flag.StringVar(&login, "login", "", "username (used when logging into the room's web ui)")
|
2021-03-23 17:00:08 +00:00
|
|
|
flag.Func("key", "the public key of the user, format: @<base64-encoded public-key>.ed25519", func(val string) error {
|
|
|
|
if len(val) == 0 {
|
|
|
|
return fmt.Errorf("the public key is required. if you are just testing things out, generate one by running 'cmd/insert-user/generate-fake-id.sh'\n")
|
|
|
|
}
|
|
|
|
key, err := refs.ParseFeedRef(val)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s\n", err)
|
|
|
|
}
|
|
|
|
pubKey = key
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
flag.StringVar(&repoPath, "repo", filepath.Join(u.HomeDir, ".ssb-go-room"), "[optional] where the locally stored files of the room is located")
|
|
|
|
flag.Func("role", "[optional] which role the new member should have (values: mod[erator], admin, or member. default is admin)", func(val string) error {
|
2021-03-18 16:49:52 +00:00
|
|
|
switch strings.ToLower(val) {
|
|
|
|
case "admin":
|
|
|
|
role = roomdb.RoleAdmin
|
2021-03-23 17:00:08 +00:00
|
|
|
case "mod":
|
|
|
|
fallthrough
|
2021-03-18 16:49:52 +00:00
|
|
|
case "moderator":
|
2021-03-23 17:00:08 +00:00
|
|
|
role = roomdb.RoleModerator
|
2021-03-18 16:49:52 +00:00
|
|
|
case "member":
|
|
|
|
role = roomdb.RoleMember
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown member role: %q", val)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
flag.Parse()
|
|
|
|
|
2021-04-14 09:39:44 +00:00
|
|
|
/* we require at least 5 arguments: <executable> + -login <val> + -key <val> */
|
2021-03-24 09:09:35 +00:00
|
|
|
/* 1 2 3 4 5 */
|
2021-03-23 17:00:08 +00:00
|
|
|
if len(os.Args) < 5 {
|
2021-04-14 09:39:44 +00:00
|
|
|
cliMissingArguments("please provide the default arguments -login and -key")
|
2021-03-23 17:00:08 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 08:23:03 +00:00
|
|
|
if login == "" {
|
|
|
|
cliMissingArguments("please provide a username with -login <username>")
|
2021-03-23 17:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if pubKey == nil {
|
|
|
|
cliMissingArguments("please provide a public key with -key")
|
2021-02-08 16:47:42 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 16:49:52 +00:00
|
|
|
r := repo.New(repoPath)
|
2021-02-09 16:29:42 +00:00
|
|
|
db, err := sqlite.Open(r)
|
2021-02-08 16:47:42 +00:00
|
|
|
check(err)
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
fmt.Fprintln(os.Stderr, "Enter Password: ")
|
|
|
|
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
fmt.Fprintln(os.Stderr, "Repeat Password: ")
|
|
|
|
bytePasswordRepeat, err := terminal.ReadPassword(int(syscall.Stdin))
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
if !bytes.Equal(bytePassword, bytePasswordRepeat) {
|
2021-03-24 09:09:35 +00:00
|
|
|
fmt.Fprintln(os.Stderr, "Passwords didn't match")
|
2021-02-08 16:47:42 +00:00
|
|
|
os.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
2021-03-02 16:14:02 +00:00
|
|
|
|
2021-02-09 16:29:42 +00:00
|
|
|
ctx := context.Background()
|
2021-03-29 08:23:03 +00:00
|
|
|
mid, err := db.Members.Add(ctx, *pubKey, role)
|
2021-03-18 16:49:52 +00:00
|
|
|
check(err)
|
|
|
|
|
2021-03-29 08:23:03 +00:00
|
|
|
err = db.AuthFallback.Create(ctx, mid, login, bytePassword)
|
2021-02-08 16:47:42 +00:00
|
|
|
check(err)
|
2021-03-02 16:14:02 +00:00
|
|
|
|
2021-03-29 08:23:03 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "Created member %s (%s) with ID %d\n", login, role, mid)
|
2021-02-08 16:47:42 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 17:00:08 +00:00
|
|
|
func cliMissingArguments(message string) {
|
|
|
|
executable := strings.TrimPrefix(os.Args[0], "./")
|
2021-04-14 09:39:44 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "%s: %s\nusage:%s -login <login-name> -key <@<base64-encoded public key>.ed25519> <optional flags>\n", executable, message, executable)
|
2021-03-23 17:00:08 +00:00
|
|
|
flag.Usage()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2021-02-08 16:47:42 +00:00
|
|
|
func check(err error) {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error: %s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|