databag/net/server/internal/api_addAccountApp.go

40 lines
811 B
Go
Raw Normal View History

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) {
2022-03-08 21:31:04 +00:00
account, err := AccountLogin(r)
2022-01-18 05:48:42 +00:00
if err != nil {
2022-01-19 20:07:57 +00:00
ErrResponse(w, http.StatusUnauthorized, err)
2022-01-18 05:48:42 +00:00
return
}
data, res := securerandom.Bytes(4)
if res != nil {
2022-01-19 20:07:57 +00:00
ErrResponse(w, http.StatusInternalServerError, res)
2022-01-18 05:48:42 +00:00
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-03-08 21:31:04 +00:00
AccountID: account.ID,
TokenType: APP_TOKENATTACH,
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-03-08 18:18:31 +00:00
}
2022-01-19 20:07:57 +00:00
if err := store.DB.Create(&accountToken).Error; err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
2022-01-18 05:48:42 +00:00
return
}
2022-03-08 18:18:31 +00:00
WriteResponse(w, token)
2022-01-18 05:48:42 +00:00
}