2022-06-08 08:25:41 +00:00
|
|
|
package databag
|
|
|
|
|
|
|
|
import (
|
2022-07-22 19:28:14 +00:00
|
|
|
"databag/internal/store"
|
|
|
|
"encoding/hex"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/theckman/go-securerandom"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
2022-06-08 08:25:41 +00:00
|
|
|
)
|
|
|
|
|
2022-07-25 18:44:33 +00:00
|
|
|
//AddNodeAccountAccess generate a new token to be use for account login reset
|
2022-06-08 08:25:41 +00:00
|
|
|
func AddNodeAccountAccess(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
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(APPResetSize)
|
|
|
|
if ret != nil {
|
|
|
|
ErrResponse(w, http.StatusInternalServerError, ret)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
token := hex.EncodeToString(data)
|
|
|
|
|
|
|
|
accountToken := store.AccountToken{
|
|
|
|
AccountID: uint(accountID),
|
|
|
|
TokenType: APPTokenReset,
|
|
|
|
Token: token,
|
|
|
|
Expires: time.Now().Unix() + APPResetExpire,
|
|
|
|
}
|
|
|
|
if err := store.DB.Create(&accountToken).Error; err != nil {
|
|
|
|
ErrResponse(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
WriteResponse(w, token)
|
2022-06-08 08:25:41 +00:00
|
|
|
}
|