databag/net/server/internal/api_getCloseMessage.go

55 lines
1.3 KiB
Go
Raw Normal View History

2022-02-11 22:29:28 +00:00
package databag
import (
"errors"
"net/http"
"gorm.io/gorm"
"github.com/gorilla/mux"
"databag/internal/store"
)
func GetCloseMessage(w http.ResponseWriter, r *http.Request) {
2022-04-05 23:52:39 +00:00
account, code, res := ParamAgentToken(r, true);
2022-02-11 22:29:28 +00:00
if res != nil {
ErrResponse(w, code, res)
return
}
detail := account.AccountDetail
cardID := mux.Vars(r)["cardID"]
2022-02-11 22:29:28 +00:00
var slot store.CardSlot
if err := store.DB.Preload("Card").Where("account_id = ? AND card_slot_id = ?", account.ID, cardID).First(&slot).Error; err != nil {
2022-02-11 22:29:28 +00:00
if !errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusInternalServerError, err)
} else {
ErrResponse(w, http.StatusNotFound, err)
}
return
}
if slot.Card == nil {
ErrResponse(w, http.StatusNotFound, errors.New("card has been deleted"))
return
}
2022-07-22 17:52:13 +00:00
if slot.Card.Status == APPCardConnecting || slot.Card.Status == APPCardConnected {
2022-02-11 22:29:28 +00:00
ErrResponse(w, http.StatusMethodNotAllowed, errors.New("invalid card state"))
return
}
disconnect := &Disconnect{
Contact: slot.Card.GUID,
2022-02-11 22:29:28 +00:00
}
msg, err := WriteDataMessage(detail.PrivateKey, detail.PublicKey, detail.KeyType,
2022-07-22 17:52:13 +00:00
APPSignPKCS1V15, account.GUID, APPMsgDisconnect, &disconnect)
2022-02-11 22:29:28 +00:00
if err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
WriteResponse(w, msg)
}