can create users with passwords
parent
5674955902
commit
01b6315b99
@ -0,0 +1,5 @@
|
||||
package db
|
||||
|
||||
type DB struct {
|
||||
Users
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type SQLite struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func OpenSQLite(path string) (*SQLite, error) {
|
||||
db, err := sql.Open("sqlite3", path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to open sqlite3 database at %s: %v", path, err)
|
||||
}
|
||||
|
||||
if _, err := db.Exec(`
|
||||
create table if not exists users (
|
||||
id integer primary key autoincrement,
|
||||
name text unique not null,
|
||||
phash text not null,
|
||||
psalt text not null
|
||||
);`); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to create users table: %v\n", err)
|
||||
}
|
||||
|
||||
return &SQLite{db: db}, nil
|
||||
}
|
||||
|
||||
func (db *SQLite) CreateUser(name, pass, salt string) error {
|
||||
combined := []byte(pass + salt)
|
||||
hashBytes, err := bcrypt.GenerateFromPassword(combined, 13)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to generate password hash: %v", err)
|
||||
}
|
||||
hash := string(hashBytes)
|
||||
if _, err := db.db.Exec(`
|
||||
insert into users (name, phash, psalt)
|
||||
values (?, ?, ?);
|
||||
`, name, hash, salt); err != nil {
|
||||
return fmt.Errorf("unable to insert user: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *SQLite) CheckPassword(name, pass string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *SQLite) Close() error { return db.db.Close() }
|
@ -0,0 +1,6 @@
|
||||
package db
|
||||
|
||||
type Users interface {
|
||||
CreateUser(name, pass string) error
|
||||
CheckPassword(name, pass string) error
|
||||
}
|
@ -1,48 +1,110 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/jordanorelli/blammo"
|
||||
"github.com/jordanorelli/kloam/db"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func cryptostring(n int) string {
|
||||
b := make([]byte, n)
|
||||
rand.Read(b)
|
||||
letters := `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%*-=+`
|
||||
r := make([]byte, 0, n)
|
||||
for _, n := range b {
|
||||
r = append(r, letters[int(n)%len(letters)])
|
||||
}
|
||||
return string(r)
|
||||
}
|
||||
|
||||
func runServer(cmd *cobra.Command, args []string) {
|
||||
stdout := blammo.NewLineWriter(os.Stdout)
|
||||
stderr := blammo.NewLineWriter(os.Stderr)
|
||||
log := blammo.NewLog("kloam", blammo.DebugWriter(stdout), blammo.InfoWriter(stdout), blammo.ErrorWriter(stderr))
|
||||
|
||||
conn, err := db.OpenSQLite(cmd.Flag("db").Value.String())
|
||||
if err != nil {
|
||||
log.Error("unable to open sqlite database: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
s := server{
|
||||
Log: log,
|
||||
db: conn,
|
||||
join: make(chan player),
|
||||
leave: make(chan *player),
|
||||
inbox: make(chan message),
|
||||
souls: make(map[string]soul),
|
||||
}
|
||||
s.init()
|
||||
go s.run()
|
||||
lis, err := net.Listen("tcp", cmd.Flag("listen").Value.String())
|
||||
if err != nil {
|
||||
log.Error("listen error: %v", err)
|
||||
return
|
||||
}
|
||||
log.Info("listening on %v", lis.Addr())
|
||||
http.Serve(lis, s.handler())
|
||||
}
|
||||
|
||||
func runUserCreate(cmd *cobra.Command, args []string) {
|
||||
conn, err := db.OpenSQLite(cmd.Flag("db").Value.String())
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "unable to open sqlite database: %v\n", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
user := args[0]
|
||||
var pass string
|
||||
if len(args) > 1 {
|
||||
pass = args[1]
|
||||
} else {
|
||||
pass = cryptostring(12)
|
||||
}
|
||||
salt := cryptostring(12)
|
||||
|
||||
if err := conn.CreateUser(user, pass, salt); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to create user: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("created:\n\tuser:\t%s\n\tpass:\t%s\n", user, pass)
|
||||
}
|
||||
|
||||
func main() {
|
||||
cmd := &cobra.Command{
|
||||
Use: "kloam",
|
||||
}
|
||||
cmd.PersistentFlags().String("db", "./kloam.sqlite3", "path to a sqlite3 file to use as a database")
|
||||
|
||||
server := &cobra.Command{
|
||||
Use: "server",
|
||||
Short: "the kloam multiplayer server",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
stdout := blammo.NewLineWriter(os.Stdout)
|
||||
stderr := blammo.NewLineWriter(os.Stderr)
|
||||
log := blammo.NewLog("kloam", blammo.DebugWriter(stdout), blammo.InfoWriter(stdout), blammo.ErrorWriter(stderr))
|
||||
|
||||
s := server{
|
||||
Log: log,
|
||||
join: make(chan player),
|
||||
leave: make(chan *player),
|
||||
inbox: make(chan message),
|
||||
souls: make(map[string]soul),
|
||||
}
|
||||
s.init()
|
||||
go s.run()
|
||||
lis, err := net.Listen("tcp", cmd.Flag("listen").Value.String())
|
||||
if err != nil {
|
||||
log.Error("listen error: %v", err)
|
||||
return
|
||||
}
|
||||
log.Info("listening on %v", lis.Addr())
|
||||
http.Serve(lis, s.handler())
|
||||
},
|
||||
Run: runServer,
|
||||
}
|
||||
server.Flags().StringP("listen", "l", "0.0.0.0:9001", "ip:port to listen on")
|
||||
|
||||
cmd.AddCommand(server)
|
||||
cmd.Execute()
|
||||
|
||||
user := &cobra.Command{
|
||||
Use: "user",
|
||||
Short: "user management stuff",
|
||||
}
|
||||
cmd.AddCommand(user)
|
||||
|
||||
userCreate := &cobra.Command{
|
||||
Use: "create",
|
||||
Short: "create a user",
|
||||
Args: cobra.RangeArgs(1, 2),
|
||||
Run: runUserCreate,
|
||||
}
|
||||
user.AddCommand(userCreate)
|
||||
|
||||
cmd.Execute()
|
||||
}
|
||||
|
Loading…
Reference in New Issue