-
Notifications
You must be signed in to change notification settings - Fork 25
/
update_password.go
60 lines (49 loc) · 1.69 KB
/
update_password.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package user
import (
"encoding/json"
"log"
"net/http"
"strconv"
"github.com/kentonh/gPanel/pkg/database"
"github.com/kentonh/gPanel/pkg/encryption"
)
func UpdatePassword(res http.ResponseWriter, req *http.Request, logger *log.Logger, dir string) bool {
if req.Method != "UPDATE" {
logger.Println(req.URL.Path + "::" + req.Method + "::" + strconv.Itoa(http.StatusMethodNotAllowed) + "::" + http.StatusText(http.StatusMethodNotAllowed))
http.Error(res, req.Method+" HTTP method is unsupported for this API.", http.StatusMethodNotAllowed)
return false
}
var updatePasswordRequestData struct {
User string `json:"user"`
Pass string `json:"pass"`
}
err := json.NewDecoder(req.Body).Decode(&updatePasswordRequestData)
if err != nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusBadRequest)
return false
}
ds, err := database.Open(dir + database.DB_MAIN)
if err != nil || ds == nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusInternalServerError)
return false
}
defer ds.Close()
var userDatabaseData database.Struct_Users
userDatabaseData.Pass, err = encryption.HashPassword(updatePasswordRequestData.Pass)
if err != nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusInternalServerError)
return false
}
userDatabaseData.Secret = ""
err = ds.Put(database.BUCKET_USERS, []byte(updatePasswordRequestData.User), userDatabaseData)
if err != nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusInternalServerError)
return false
}
res.WriteHeader(http.StatusNoContent)
return true
}