Skip to content

Commit

Permalink
return challenge tokens in send endpoint as explicit key
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard B committed Mar 25, 2024
1 parent 8811842 commit de631ed
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 10 deletions.
17 changes: 13 additions & 4 deletions src/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ type Error struct {
Msg string `json:"error"`
}

type SendMessageError struct {
Msg string `json:"error"`
ChallengeTokens []string `json:"challenge_tokens,omitempty"`
}

type CreateGroupResponse struct {
Id string `json:"id"`
}
Expand All @@ -138,7 +143,7 @@ type TrustIdentityRequest struct {
}

type SendMessageResponse struct {
Timestamp string `json:"timestamp"`
Timestamp string `json:"timestamp"`
}

type TrustModeRequest struct {
Expand Down Expand Up @@ -367,7 +372,7 @@ func (a *Api) Send(c *gin.Context) {
// @Accept json
// @Produce json
// @Success 201 {object} SendMessageResponse
// @Failure 400 {object} Error
// @Failure 400 {object} SendMessageError
// @Param data body SendMessageV2 true "Input Data"
// @Router /v2/send [post]
func (a *Api) SendV2(c *gin.Context) {
Expand Down Expand Up @@ -401,15 +406,19 @@ func (a *Api) SendV2(c *gin.Context) {
return
}

timestamps, err := a.signalClient.SendV2(
data, err := a.signalClient.SendV2(
req.Number, req.Message, req.Recipients, req.Base64Attachments, req.Sticker,
req.Mentions, req.QuoteTimestamp, req.QuoteAuthor, req.QuoteMessage, req.QuoteMentions, req.TextMode, req.EditTimestamp)
if err != nil {
if data != nil {
c.JSON(400, SendMessageError{Msg: err.Error(), ChallengeTokens: (*data)[0].ChallengeTokens})
return
}
c.JSON(400, Error{Msg: err.Error()})
return
}

c.JSON(201, SendMessageResponse{Timestamp: strconv.FormatInt((*timestamps)[0].Timestamp, 10)})
c.JSON(201, SendMessageResponse{Timestamp: strconv.FormatInt((*data)[0].Timestamp, 10)})
}

func (a *Api) handleSignalReceive(ws *websocket.Conn, number string, stop chan struct{}) {
Expand Down
12 changes: 11 additions & 1 deletion src/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ type SignalCliIdentityEntry struct {
}

type SendResponse struct {
Timestamp int64 `json:"timestamp"`
Timestamp int64 `json:"timestamp"`
ChallengeTokens []string `json:"challenge_tokens"`
}

type About struct {
Expand Down Expand Up @@ -459,6 +460,15 @@ func (s *SignalClient) send(number string, message string,
if strings.Contains(err.Error(), signalCliV2GroupError) {
return nil, errors.New("Cannot send message to group - please first update your profile.")
}

switch errorType := err.(type) {
case *RateLimitErrorType:
rateLimitError := errors.New(err.Error() + ". Use the attached challenge tokens to lift the rate limit restrictions via the '/v1/accounts/{number}/rate-limit-challenge' endpoint.")
resp.ChallengeTokens = errorType.ChallengeTokens
return &resp, rateLimitError
default:
return nil, err
}
return nil, err
}
} else {
Expand Down
15 changes: 13 additions & 2 deletions src/client/jsonrpc2.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net"
"time"
"sync"
"strings"

"github.com/bbernhard/signal-cli-rest-api/utils"
uuid "github.com/gofrs/uuid"
Expand Down Expand Up @@ -45,6 +44,15 @@ type RateLimitResult struct {
Token string `json:"token"`
}

type RateLimitErrorType struct {
ChallengeTokens []string
Err error
}

func (r *RateLimitErrorType) Error() string {
return r.Err.Error()
}

type JsonRpc2Client struct {
conn net.Conn
receivedResponsesById map[string]chan JsonRpc2MessageResponse
Expand Down Expand Up @@ -151,7 +159,10 @@ func (r *JsonRpc2Client) getRaw(command string, account *string, args interface{
challengeTokens = append(challengeTokens, rateLimitResult.Token)
}

return "", errors.New(resp.Err.Message + " Challenge Tokens: " + strings.Join(challengeTokens, ","))
return "", &RateLimitErrorType{
ChallengeTokens: challengeTokens,
Err : errors.New(resp.Err.Message),
}
}
return "", errors.New(resp.Err.Message)
}
Expand Down
16 changes: 15 additions & 1 deletion src/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1905,7 +1905,7 @@ var doc = `{
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/api.Error"
"$ref": "#/definitions/api.SendMessageError"
}
}
}
Expand Down Expand Up @@ -2089,6 +2089,20 @@ var doc = `{
}
}
},
"api.SendMessageError": {
"type": "object",
"properties": {
"challenge_tokens": {
"type": "array",
"items": {
"type": "string"
}
},
"error": {
"type": "string"
}
}
},
"api.SendMessageResponse": {
"type": "object",
"properties": {
Expand Down
16 changes: 15 additions & 1 deletion src/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1889,7 +1889,7 @@
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/api.Error"
"$ref": "#/definitions/api.SendMessageError"
}
}
}
Expand Down Expand Up @@ -2073,6 +2073,20 @@
}
}
},
"api.SendMessageError": {
"type": "object",
"properties": {
"challenge_tokens": {
"type": "array",
"items": {
"type": "string"
}
},
"error": {
"type": "string"
}
}
},
"api.SendMessageResponse": {
"type": "object",
"properties": {
Expand Down
11 changes: 10 additions & 1 deletion src/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ definitions:
registered:
type: boolean
type: object
api.SendMessageError:
properties:
challenge_tokens:
items:
type: string
type: array
error:
type: string
type: object
api.SendMessageResponse:
properties:
timestamp:
Expand Down Expand Up @@ -1598,7 +1607,7 @@ paths:
"400":
description: Bad Request
schema:
$ref: '#/definitions/api.Error'
$ref: '#/definitions/api.SendMessageError'
summary: Send a signal message.
tags:
- Messages
Expand Down

0 comments on commit de631ed

Please sign in to comment.