✨ Add commands to manage users

This commit is contained in:
2020-10-10 16:30:35 +02:00
parent 7901eb8eb4
commit 9cef5c63a2
5 changed files with 213 additions and 78 deletions
+77 -72
View File
@@ -1,4 +1,4 @@
// Copyright (c) 2018, Andreas Schneider
// Copyright (c) 2020, Andreas Schneider
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
@@ -26,84 +26,89 @@
package main
import (
"context"
"flag"
"fmt"
"github.com/go-chi/chi"
pwgen "github.com/sethvargo/go-password/password"
"golang.org/x/crypto/bcrypt"
"golang.org/x/net/webdav"
"math/rand"
"net/http"
"time"
"github.com/alecthomas/kong"
)
var configFile = flag.String("config", "sharedav.yaml", "Config file to be used.")
var genPassword = flag.Bool("genpass", false, "If set, a password will be generated and hashed.")
var hashPassword = flag.String("hashpass", "", "If set, the given password will be hashed.")
type app struct {
config
CmdUser CmdUser `cmd:"" name:"user" help:"Manage users."`
userStore UserStore
shareStore ShareStore
}
func main() {
flag.Parse()
var app app
ctx := kong.Parse(&app)
if *genPassword {
// math.rand is not secure. For determining the number of digits in a password
// it should suffice, though.
rand.Seed(time.Now().UnixNano())
pwLen := 32
pw, err := pwgen.Generate(pwLen, rand.Intn(pwLen/2), 0, false, true)
if err != nil {
panic(err)
}
hash, err := bcrypt.GenerateFromPassword([]byte(pw), 0)
if err != nil {
panic(err)
}
store, err := NewDBStore(app.config.Db)
ctx.FatalIfErrorf(err)
defer store.Close()
fmt.Printf("Password: %s\n", pw)
fmt.Printf(" Hash: %s\n", hash)
return
} else if *hashPassword != "" {
hash, err := bcrypt.GenerateFromPassword([]byte(*hashPassword), 0)
if err != nil {
panic(err)
}
fmt.Println(string(hash))
return
}
app.userStore = store
app.shareStore = store
c := LoadConfig(*configFile)
ctx.FatalIfErrorf(ctx.Run(&app))
h := &webdav.Handler{}
h.Prefix = "/share/"
h.LockSystem = webdav.NewMemLS()
h.FileSystem = BaseDir(c.BaseDirectory)
//if *genPassword {
// // math.rand is not secure. For determining the number of digits in a password
// // it should suffice, though.
// rand.Seed(time.Now().UnixNano())
// pwLen := 32
// pw, err := pwgen.Generate(pwLen, rand.Intn(pwLen/2), 0, false, true)
// if err != nil {
// panic(err)
// }
// hash, err := bcrypt.GenerateFromPassword([]byte(pw), 0)
// if err != nil {
// panic(err)
// }
//
// fmt.Printf("Password: %s\n", pw)
// fmt.Printf(" Hash: %s\n", hash)
// return
//} else if *hashPassword != "" {
// hash, err := bcrypt.GenerateFromPassword([]byte(*hashPassword), 0)
// if err != nil {
// panic(err)
// }
// fmt.Println(string(hash))
// return
//}
authenticatedWebdavHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
username, password, ok := r.BasicAuth()
directory := ""
if ok {
ok, directory = c.ValidateDAVUser(username, password)
}
if !ok {
w.Header().Set("WWW-Authenticate", `Basic realm="ShareDAV"`)
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
shareName := chi.URLParam(r, "share")
directoryMapping := DirectoryMapping{
VirtualName: shareName,
RealName: directory,
}
// Use the WebDAV handler to actually serve the request. Also enhance the context
// to contain the subdirectory (of the base directory) which contains the data for
// the authenticated user.
h.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), "mapping", &directoryMapping)))
})
r := chi.NewRouter()
r.Handle("/share/{share}/*", authenticatedWebdavHandler)
http.ListenAndServe(c.ListenAddress, r)
//c := LoadConfig(*configFile)
//
//h := &webdav.Handler{}
//h.Prefix = "/share/"
//h.LockSystem = webdav.NewMemLS()
//h.FileSystem = BaseDir(c.BaseDirectory)
//
//authenticatedWebdavHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// username, password, ok := r.BasicAuth()
// directory := ""
// if ok {
// ok, directory = c.ValidateDAVUser(username, password)
// }
// if !ok {
// w.Header().Set("WWW-Authenticate", `Basic realm="ShareDAV"`)
// http.Error(w, "unauthorized", http.StatusUnauthorized)
// return
// }
//
// shareName := chi.URLParam(r, "share")
// directoryMapping := DirectoryMapping{
// VirtualName: shareName,
// RealName: directory,
// }
//
// // Use the WebDAV handler to actually serve the request. Also enhance the context
// // to contain the subdirectory (of the base directory) which contains the data for
// // the authenticated user.
// h.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), "mapping", &directoryMapping)))
//})
//
//r := chi.NewRouter()
//r.Handle("/share/{share}/*", authenticatedWebdavHandler)
//
//http.ListenAndServe(c.ListenAddress, r)
}