databag/net/server/internal/api_setNodeAccount.go

49 lines
1006 B
Go
Raw Normal View History

2022-03-09 23:12:05 +00:00
package databag
import (
2022-07-22 19:28:14 +00:00
"databag/internal/store"
"encoding/hex"
"github.com/gorilla/mux"
2022-03-09 23:12:05 +00:00
"github.com/theckman/go-securerandom"
2022-07-22 19:28:14 +00:00
"net/http"
"strconv"
"time"
2022-03-09 23:12:05 +00:00
)
func SetNodeAccount(w http.ResponseWriter, r *http.Request) {
2022-07-22 19:28:14 +00:00
// get referenced account id
params := mux.Vars(r)
accountID, res := strconv.ParseUint(params["accountID"], 10, 32)
if res != nil {
ErrResponse(w, http.StatusBadRequest, res)
return
}
if code, res := ParamAdminToken(r); res != nil {
ErrResponse(w, code, res)
return
}
data, err := securerandom.Bytes(APPResetSize)
if err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
token := hex.EncodeToString(data)
accountToken := store.AccountToken{
TokenType: APPTokenReset,
Token: token,
AccountID: uint(accountID),
Expires: time.Now().Unix() + APPCreateExpire,
}
if err := store.DB.Create(&accountToken).Error; err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
WriteResponse(w, token)
2022-03-09 23:12:05 +00:00
}