2022-01-18 05:48:42 +00:00
|
|
|
package databag
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
"encoding/hex"
|
|
|
|
"databag/internal/store"
|
|
|
|
"github.com/theckman/go-securerandom"
|
|
|
|
)
|
|
|
|
|
|
|
|
func AddAccountApp(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
id, err := AccountLogin(r)
|
|
|
|
if err != nil {
|
|
|
|
LogMsg("failed to login")
|
|
|
|
w.WriteHeader(http.StatusUnauthorized);
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, res := securerandom.Bytes(4)
|
|
|
|
if res != nil {
|
|
|
|
LogMsg("failed to generate token")
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2022-01-18 06:56:00 +00:00
|
|
|
token := hex.EncodeToString(data)
|
2022-01-18 05:48:42 +00:00
|
|
|
|
2022-01-18 06:56:00 +00:00
|
|
|
accountToken := store.AccountToken{
|
2022-01-18 05:48:42 +00:00
|
|
|
AccountID: id,
|
|
|
|
TokenType: "attach",
|
2022-01-18 06:56:00 +00:00
|
|
|
Token: token,
|
2022-01-18 05:48:42 +00:00
|
|
|
Expires: time.Now().Unix() + APP_ATTACHEXPIRE,
|
|
|
|
};
|
2022-01-18 06:56:00 +00:00
|
|
|
if store.DB.Create(&accountToken).Error != nil {
|
2022-01-18 05:48:42 +00:00
|
|
|
LogMsg("failed to store token")
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-18 06:56:00 +00:00
|
|
|
WriteResponse(w, token);
|
2022-01-18 05:48:42 +00:00
|
|
|
}
|
|
|
|
|