Skip to content

Commit

Permalink
Add a random admin password in config.toml generated with --new.
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Dec 18, 2021
1 parent 232be45 commit fe9e284
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
17 changes: 17 additions & 0 deletions cmd/dictpress/init.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package main

import (
"bytes"
"crypto/rand"
"errors"
"fmt"
"html/template"
"io/ioutil"
mrand "math/rand"
"net/http"
"os"
"path/filepath"
"unicode"

"github.com/jmoiron/sqlx"
"github.com/knadh/dictpress/internal/data"
Expand Down Expand Up @@ -208,6 +212,19 @@ func generateNewFiles() error {
return fmt.Errorf("error reading sample config (is binary stuffed?): %v", err)
}

// Inject a random password.
p := make([]byte, 12)
rand.Read(p)
pwd := []byte(fmt.Sprintf("%x", p))

for i, c := range pwd {
if mrand.Intn(4) == 1 {
pwd[i] = byte(unicode.ToUpper(rune(c)))
}
}

b = bytes.Replace(b, []byte("dictpress_admin_password"), pwd, -1)

if err := ioutil.WriteFile("config.toml", b, 0644); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions config.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ root_url = "http://localhost:9000"
enable_pages = false

# Admin dashboard and API credentials.
admin_username = "your-user-name-min-6-chars"
admin_password = "use-a-long-password-min-8-chars"
admin_username = "dictpress"
admin_password = "dictpress_admin_password"


[results]
Expand Down

0 comments on commit fe9e284

Please sign in to comment.