databag/net/server/internal/api_addNodeAccount.go

42 lines
841 B
Go
Raw Normal View History

2022-01-15 21:28:28 +00:00
package databag
import (
"net/http"
2022-01-18 05:48:42 +00:00
"encoding/hex"
"time"
2022-01-15 21:28:28 +00:00
"databag/internal/store"
"github.com/theckman/go-securerandom"
)
func AddNodeAccount(w http.ResponseWriter, r *http.Request) {
2022-01-17 21:42:17 +00:00
if !AdminLogin(r) {
2022-01-16 07:25:43 +00:00
LogMsg("invalid admin credentials");
2022-01-15 21:28:28 +00:00
w.WriteHeader(http.StatusUnauthorized);
return
}
2022-01-18 05:48:42 +00:00
data, err := securerandom.Bytes(16)
2022-01-15 21:28:28 +00:00
if err != nil {
2022-01-16 07:25:43 +00:00
LogMsg("failed to generate token");
2022-01-15 21:28:28 +00:00
w.WriteHeader(http.StatusInternalServerError);
return
}
2022-01-18 05:48:42 +00:00
token := hex.EncodeToString(data)
2022-01-15 21:28:28 +00:00
2022-01-18 05:48:42 +00:00
accountToken := store.AccountToken{
TokenType: "create",
Token: token,
Expires: time.Now().Unix() + APP_CREATEEXPIRE,
};
if store.DB.Create(&accountToken).Error != nil {
2022-01-16 07:25:43 +00:00
LogMsg("failed to store token");
2022-01-15 22:54:49 +00:00
w.WriteHeader(http.StatusInternalServerError);
2022-01-15 21:28:28 +00:00
return
}
2022-01-18 05:48:42 +00:00
WriteResponse(w, token);
2022-01-15 21:28:28 +00:00
}