databag/net/server/internal/api_setNodeAccount.go

50 lines
1.0 KiB
Go
Raw Normal View History

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