lab3
This commit is contained in:
@@ -3,6 +3,7 @@ package rest
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
@@ -231,10 +232,19 @@ func (h *Handler) CreateMessage(
|
||||
) (oas.CreateMessageRes, error) {
|
||||
callerID := caller(ctx)
|
||||
|
||||
fingerprint, err := messageFingerprint(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
message, err := h.messages.Send(ctx, domain.MessageDraft{
|
||||
ChatID: params.ChatID,
|
||||
SenderID: callerID,
|
||||
Text: req.Text,
|
||||
Idempotency: domain.MessageIdempotency{
|
||||
Key: params.IdempotencyKey,
|
||||
RequestHash: fingerprint,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -246,6 +256,20 @@ func (h *Handler) CreateMessage(
|
||||
}, nil
|
||||
}
|
||||
|
||||
// messageFingerprint canonicalises the create-message payload so that the key
|
||||
// order of the client JSON cannot change the fingerprint, while a different
|
||||
// payload with the same Idempotency-Key can (contract 1.3.0).
|
||||
func messageFingerprint(req *oas.CreateMessage) ([sha256.Size]byte, error) {
|
||||
payload, err := json.Marshal(struct {
|
||||
Text string `json:"text"`
|
||||
}{Text: req.Text})
|
||||
if err != nil {
|
||||
return [sha256.Size]byte{}, fmt.Errorf("encode message payload: %w", err)
|
||||
}
|
||||
|
||||
return sha256.Sum256(payload), nil
|
||||
}
|
||||
|
||||
func (h *Handler) ListMessages(ctx context.Context, params oas.ListMessagesParams) (oas.ListMessagesRes, error) {
|
||||
callerID := caller(ctx)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user