Create a user from command line
This commit is contained in:
parent
392aff5956
commit
aef8f1a572
17
main.go
17
main.go
@ -24,9 +24,10 @@ const LISTEN_HOST = "0.0.0.0"
|
||||
const DB_SCHEMA = 13
|
||||
|
||||
var (
|
||||
flagPort int
|
||||
flagVersion bool
|
||||
flagConfig string
|
||||
flagPort int
|
||||
flagVersion bool
|
||||
flagCreateUser bool
|
||||
flagConfig string
|
||||
|
||||
connectionManager ConnectionManager
|
||||
static http.Handler
|
||||
@ -44,6 +45,7 @@ func init() { // {{{
|
||||
configFilename := os.Getenv("HOME") + "/.config/notes.yaml"
|
||||
flag.IntVar(&flagPort, "port", 1371, "TCP port to listen on")
|
||||
flag.BoolVar(&flagVersion, "version", false, "Shows Notes version and exists")
|
||||
flag.BoolVar(&flagCreateUser, "createuser", false, "Create a user and exit")
|
||||
flag.StringVar(&flagConfig, "config", configFilename, "Filename of configuration file")
|
||||
flag.Parse()
|
||||
|
||||
@ -72,6 +74,15 @@ func main() { // {{{
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if flagCreateUser {
|
||||
err = createUser()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
connectionManager = NewConnectionManager()
|
||||
go connectionManager.BroadcastLoop()
|
||||
|
||||
|
42
user.go
42
user.go
@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
// Standard
|
||||
"database/sql"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func (session Session) UpdatePassword(currPass, newPass string) (ok bool, err error) {
|
||||
@ -24,9 +25,9 @@ func (session Session) UpdatePassword(currPass, newPass string) (ok bool, err er
|
||||
password=password_hash(SUBSTRING(password FROM 1 FOR 32), $3::bytea)
|
||||
RETURNING id
|
||||
`,
|
||||
newPass,
|
||||
session.UserID,
|
||||
currPass,
|
||||
newPass,
|
||||
session.UserID,
|
||||
currPass,
|
||||
)
|
||||
|
||||
if rowsAffected, err = result.RowsAffected(); err != nil {
|
||||
@ -35,3 +36,38 @@ func (session Session) UpdatePassword(currPass, newPass string) (ok bool, err er
|
||||
|
||||
return rowsAffected > 0, nil
|
||||
}
|
||||
|
||||
func createUser() (err error) {
|
||||
var username, password string
|
||||
fmt.Printf("Username: ")
|
||||
fmt.Scanln(&username)
|
||||
fmt.Printf("Password: ")
|
||||
fmt.Scanln(&password)
|
||||
|
||||
err = CreateDbUser(username, password)
|
||||
return
|
||||
}
|
||||
|
||||
func CreateDbUser(username string, password string) (err error) {
|
||||
var result sql.Result
|
||||
|
||||
result, err = db.Exec(`
|
||||
INSERT INTO public.user(username, password)
|
||||
VALUES(
|
||||
$1,
|
||||
password_hash(
|
||||
/* salt in hex */
|
||||
ENCODE(gen_random_bytes(16), 'hex'),
|
||||
|
||||
/* password */
|
||||
$2::bytea
|
||||
)
|
||||
)
|
||||
`,
|
||||
username,
|
||||
password,
|
||||
)
|
||||
|
||||
_, err = result.RowsAffected()
|
||||
return
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user