databag/net/server/internal/api_getCloseMessage.go

54 lines
1.3 KiB
Go
Raw Normal View History

2022-02-11 22:29:28 +00:00
package databag
import (
2022-07-22 19:28:14 +00:00
"databag/internal/store"
"errors"
"github.com/gorilla/mux"
"gorm.io/gorm"
"net/http"
2022-02-11 22:29:28 +00:00
)
2022-07-29 07:21:32 +00:00
//GetCloseMessage retrieves message to disconnect from contact
2022-02-11 22:29:28 +00:00
func GetCloseMessage(w http.ResponseWriter, r *http.Request) {
2022-07-22 19:28:14 +00:00
account, code, res := ParamAgentToken(r, true)
if res != nil {
ErrResponse(w, code, res)
return
}
detail := account.AccountDetail
cardID := mux.Vars(r)["cardID"]
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 {
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
}
if slot.Card.Status == APPCardConnecting || slot.Card.Status == APPCardConnected {
ErrResponse(w, http.StatusMethodNotAllowed, errors.New("invalid card state"))
return
}
disconnect := &Disconnect{
Contact: slot.Card.GUID,
}
msg, err := WriteDataMessage(detail.PrivateKey, detail.PublicKey, detail.KeyType,
APPSignPKCS1V15, account.GUID, APPMsgDisconnect, &disconnect)
if err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
WriteResponse(w, msg)
2022-02-11 22:29:28 +00:00
}