databag/net/server/internal/api_addNodeAccountAccess.go

50 lines
1.0 KiB
Go
Raw Normal View History

2022-06-08 08:25:41 +00:00
package databag
import (
"net/http"
"time"
"strconv"
"encoding/hex"
"databag/internal/store"
"github.com/theckman/go-securerandom"
"github.com/gorilla/mux"
)
func AddNodeAccountAccess(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
accountId, res := strconv.ParseUint(params["accountId"], 10, 32)
if res != nil {
ErrResponse(w, http.StatusBadRequest, res)
return
}
if code, err := ParamAdminToken(r); err != nil {
ErrResponse(w, code, err)
return
}
data, ret := securerandom.Bytes(APP_RESETSIZE)
if ret != nil {
ErrResponse(w, http.StatusInternalServerError, ret)
return
}
token := hex.EncodeToString(data)
accountToken := store.AccountToken{
AccountID: uint(accountId),
TokenType: APP_TOKENRESET,
Token: token,
Expires: time.Now().Unix() + APP_RESETEXPIRE,
}
if err := store.DB.Create(&accountToken).Error; err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
WriteResponse(w, token)
}