Skip to content

Commit

Permalink
feat: add proper password check
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Jan 31, 2024
1 parent 6f84eb5 commit 6320e36
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ func (api *API) Start(startRequest *models.StartRequest) error {
}

func (api *API) Setup(setupRequest *models.SetupRequest) error {
api.svc.cfg.SavePasswordCheck(setupRequest.UnlockPassword)

// only update non-empty values
if setupRequest.LNBackendType != "" {
api.svc.cfg.SetUpdate("LNBackendType", setupRequest.LNBackendType, "")
Expand Down
11 changes: 11 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
BreezBackendType = "BREEZ"
SessionCookieName = "session"
SessionCookieAuthKey = "authenticated"
UnlockPasswordCheck = "THIS STRING SHOULD MATCH IF PASSWORD IS CORRECT"
)

type AppConfig struct {
Expand Down Expand Up @@ -128,6 +129,16 @@ func (cfg *Config) SetUpdate(key string, value string, encryptionKey string) {
cfg.set(key, value, clauses, encryptionKey)
}

func (cfg *Config) CheckUnlockPassword(encryptionKey string) bool {
decryptedValue, err := cfg.Get("UnlockPasswordCheck", encryptionKey)

return err == nil && decryptedValue == UnlockPasswordCheck
}

func (cfg *Config) SavePasswordCheck(encryptionKey string) {
cfg.SetUpdate("UnlockPasswordCheck", UnlockPasswordCheck, encryptionKey)
}

func randomHex(n int) (string, error) {
bytes := make([]byte, n)
if _, err := rand.Read(bytes); err != nil {
Expand Down
7 changes: 7 additions & 0 deletions start.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"errors"

"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip19"
)
Expand Down Expand Up @@ -78,6 +80,11 @@ func (svc *Service) StartNostr(encryptionKey string) error {
}

func (svc *Service) StartApp(encryptionKey string) error {
if !svc.cfg.CheckUnlockPassword(encryptionKey) {
svc.Logger.Errorf("Invalid password")
return errors.New("Invalid password")
}

err := svc.launchLNBackend(encryptionKey)
if err != nil {
svc.Logger.Errorf("Failed to launch LN backend: %v", err)
Expand Down

0 comments on commit 6320e36

Please sign in to comment.