Skip to content

Commit e9b3141

Browse files
committed
Implement 'useradd' CLI command for basic auth
1 parent fcf33b4 commit e9b3141

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed

command.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"time"
99

1010
"dyn-https/blockchain/dynamic"
11+
"dyn-https/configs"
12+
"dyn-https/configs/settings"
1113
"dyn-https/https/rest"
1214
"dyn-https/util"
1315

@@ -29,7 +31,7 @@ func unlockWallet(d *dynamic.Dynamicd) bool {
2931

3032
func appCommandLoop(acc *[]dynamic.Account, al *dynamic.ActiveLinks,
3133
shutdown *rest.AppShutdown, d *dynamic.Dynamicd,
32-
status *dynamic.SyncStatus, sync bool) {
34+
status *dynamic.SyncStatus, sync bool, c *settings.Configuration) {
3335
go func() {
3436
var err error
3537
var unlocked = false
@@ -74,6 +76,13 @@ func appCommandLoop(acc *[]dynamic.Account, al *dynamic.ActiveLinks,
7476
}
7577
} else if strings.HasPrefix(cmdText, "restart") {
7678
rest.RestartWebServiceRouter()
79+
} else if strings.HasPrefix(cmdText, "useradd") {
80+
user, err := configs.AdminUserCommand(c, cmdText)
81+
if err != nil {
82+
util.Error.Println("User add command failed. %v\n", err)
83+
} else {
84+
util.Info.Printf("User %v successfully added or updated\n", user)
85+
}
7786
} else {
7887
util.Warning.Println("Invalid command", cmdText)
7988
status, err = d.GetSyncStatus()

configs/adminuser.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package configs
2+
3+
import (
4+
"crypto/sha256"
5+
"encoding/base64"
6+
"errors"
7+
"fmt"
8+
"strings"
9+
10+
"dyn-https/configs/settings"
11+
"dyn-https/models"
12+
)
13+
14+
func AdminUserCommand(c *settings.Configuration, cmd string) (string, error) {
15+
cmd = strings.Replace(cmd, "useradd ", "", -1)
16+
spaceindex := strings.Index(cmd, " ")
17+
if spaceindex <= 0 {
18+
return "", errors.New("Invalid paramaters")
19+
}
20+
params := strings.Split(cmd, " ")
21+
if len(params) != 2 {
22+
return "", fmt.Errorf("Invalid number of paramaters %d", len(params))
23+
}
24+
username := params[0]
25+
password := params[1]
26+
if len(password) < 8 {
27+
return username, fmt.Errorf("Invalid password length %d. Minimum password must be at least 8 charactors.", len(password))
28+
}
29+
// todo: check password complexity
30+
hash := sha256.Sum256([]byte(username + password))
31+
expectedHash := base64.StdEncoding.EncodeToString(hash[:])
32+
admin := models.Admin{UserName: username, ExpectedHash: expectedHash}
33+
if len(admin.ExpectedHash) == 0 || len(admin.UserName) == 0 {
34+
return username, fmt.Errorf("Invalid number of paramaters %d", len(params))
35+
}
36+
c.UpdateAdmins(admin)
37+
return username, nil
38+
}

configs/settings/configuration.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,9 @@ func (c *Configuration) Load(dir, seperator string) error {
118118
util.Info.Println("Configuration loaded successfully.")
119119
}
120120
if c.configFile.Admins == nil {
121-
admin := models.Admin{UserName: "admin", ExpectedHash: "u9cYLNDulUiPGh5vP+DY+U7Q0U5NsdznE/6CoyMcUj0="}
122-
admins := []models.Admin{admin}
121+
// admin with password test
122+
//admin := models.Admin{UserName: "admin", ExpectedHash: "u9cYLNDulUiPGh5vP+DY+U7Q0U5NsdznE/6CoyMcUj0="}
123+
admins := []models.Admin{}
123124
c.configFile.Admins = admins
124125
c.updateFile()
125126
}
@@ -141,3 +142,23 @@ func (c *Configuration) AdminArrayToMap() map[string]string {
141142
}
142143
return admins
143144
}
145+
146+
func (c *Configuration) UpdateAdmins(admin models.Admin) {
147+
c.mut.Lock()
148+
defer c.mut.Unlock()
149+
index := -1
150+
for i, value := range c.configFile.Admins {
151+
if value.UserName == admin.UserName {
152+
index = i
153+
break
154+
}
155+
}
156+
if index != -1 {
157+
c.configFile.Admins[index] = admin
158+
} else {
159+
c.configFile.Admins = append(c.configFile.Admins, admin)
160+
}
161+
// update config file
162+
c.updateFile()
163+
return
164+
}

init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func Init(version, githash string) error {
148148
util.Info.Printf("Found %v links\n", len(al.Links))
149149
}
150150

151-
appCommandLoop(acc, al, &shutdown, dynamicd, status, sync)
151+
appCommandLoop(acc, al, &shutdown, dynamicd, status, sync, &config)
152152

153153
for {
154154
select {

0 commit comments

Comments
 (0)