fierj
PublicTiny personal git forge
2845aac4d3963f1426873eeb2eca9044e765e678
diff --git a/auth.go b/auth.go
index d192443..f25446b 100644
--- a/auth.go
+++ b/auth.go
@@ -33,6 +33,9 @@ func LoadUsers(path string) (UserStore, error) {
}
func (u UserStore) Verify(username, password string) bool {
+ if u == nil || len(u) == 0 {
+ return false
+ }
hash, ok := u[username]
if !ok {
return false
@@ -41,6 +44,9 @@ func (u UserStore) Verify(username, password string) bool {
}
func (u UserStore) Save(path string) error {
+ if u == nil {
+ return fmt.Errorf("user store not configured")
+ }
data, err := json.MarshalIndent(u, "", " ")
if err != nil {
return err
@@ -49,6 +55,9 @@ func (u UserStore) Save(path string) error {
}
func (u UserStore) Add(username, password string) error {
+ if u == nil {
+ return fmt.Errorf("user store not configured; set users_path and cookie_secret in config")
+ }
if username == "" || password == "" {
return fmt.Errorf("username and password are required")
}
@@ -64,10 +73,15 @@ func (u UserStore) Add(username, password string) error {
}
func (u UserStore) Remove(username string) {
- delete(u, username)
+ if u != nil {
+ delete(u, username)
+ }
}
func (u UserStore) ChangePassword(username, newPassword string) error {
+ if u == nil {
+ return fmt.Errorf("user store not configured")
+ }
if newPassword == "" {
return fmt.Errorf("password is required")
}
diff --git a/config.go b/config.go
index 0c869bd..2b71bc1 100644
--- a/config.go
+++ b/config.go
@@ -22,10 +22,12 @@ type Config struct {
func LoadConfig(path string) Config {
// default config
cfg := Config{
- Addr: ":8080",
- Host: "localhost:8080",
- SSHAddr: ":2222",
- Dir: "repos",
+ Addr: ":8080",
+ Host: "localhost:8080",
+ SSHAddr: ":2222",
+ Dir: "repos",
+ UsersPath: "users.json",
+ CookieSecret: "change-me-in-production",
}
// read file, log read/unmarshal errors, if any