Skip to content

Commit cf81346

Browse files
committed
feat: replace base64url with percent encoding
1 parent 0936eb6 commit cf81346

File tree

2 files changed

+26
-30
lines changed

2 files changed

+26
-30
lines changed

README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,29 +104,30 @@ The following values are used in the examples:
104104
```
105105

106106
#### Send a text message to a group
107-
* Topic: `<TOPIC_PREFIX>/<MQTT_SUBSCRIBE_TOPIC>/group/<BASE64URL_ENCODED_GROUP_ID>`
108-
* Note: The group id (which is `base64` encoded) must be converted to `base64url` encoding
109-
by applying the following replacements:
110-
- `+` (plus) becomes `-` (minus)
111-
- `/` (slash) becomes `_` (underscore)
107+
* Topic: `<TOPIC_PREFIX>/<MQTT_SUBSCRIBE_TOPIC>/group/<PERCENT_ENCODED_GROUP_ID>`
108+
* Note: The group-id must be percent-encoded.
109+
A group-id (which is base64 encoded) may be converted to percent encoding by applying the following replacements:
110+
- `+` (plus) becomes `%2B`
111+
- `/` (slash) becomes `%2F`
112+
- `=` (equals) becomes `%3D`
112113
* Example:
113114
```sh
114-
$ mosquitto_pub -h broker -t signal/send/group/LS0-YWRtaW5zPz8_Cg== -m 'Outgoing message'
115+
$ mosquitto_pub -h broker -t signal/send/group/LS0%2BYWRtaW5zPz8%2FCg%3D%3D -m 'Outgoing message'
115116
```
116117
The text _Outgoing message_ is sent to the group _Admins_.
117118

118119
#### Receive a text message from a group
119-
* Topic: `<TOPIC_PREFIX>/<MQTT_PUBLISH_TOPIC>/<PHONE_NUMBER_WITHOUT_LEADING_PLUS>/timestamp/<TIMESTAMP>/group/<BASE64URL_ENCODED_GROUP_ID>`
120-
* Note: The last segment of the topic is `base64url` encoded.
121-
To retrieve the group id, it must be converted to `base64` encoding
122-
by applying the following replacements:
123-
- `-` (minus) becomes `+` (plus)
124-
- `_` (underscore) becomes `/` (slash)
120+
* Topic: `<TOPIC_PREFIX>/<MQTT_PUBLISH_TOPIC>/<PHONE_NUMBER_WITHOUT_LEADING_PLUS>/timestamp/<TIMESTAMP>/group/<PERCENT_ENCODED_GROUP_ID>`
121+
* Note: The last segment of the topic is percent-encoded.
122+
It may be converted into a (base64 encoded) group-id by applying the following replacements:
123+
- `%2B` becomes `+` (plus)
124+
- `%2F` becomes `/` (slash)
125+
- `%3D` becomes `=` (equals)
125126
* Example:
126127
The text _Incoming message_ is sent from the phone to the group _Admins_.
127128
```sh
128129
$ mosquitto_sub -v -h broker -t signal/#
129-
signal/receive/491713920000/timestamp/1577882096000/group/LS0-YWRtaW5zPz8_Cg== Incoming message
130+
signal/receive/491713920000/timestamp/1577882096000/group/LS0%2BYWRtaW5zPz8%2FCg%3D%3D Incoming message
130131
```
131132

132133
#### Receive a quotation message

image/signal-mqtt

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,31 +45,26 @@ _build_json_send() {
4545
'{jsonrpc:"2.0", method:"send", params:{($name):$value, message:$message}}'
4646
}
4747

48-
#
49-
# base64url encoding is required when a base64-encoded string
50-
# (e.g. group-id) is used as part of an mqtt topic.
51-
#
52-
5348
#######################################
54-
# Convert a text from base64 to base64url.
49+
# Encode a text to percent-encoding.
5550
# Arguments:
56-
# 1: Text in base64 encoding
51+
# 1: Text
5752
# Outputs:
58-
# Text in base64url encoding
53+
# Text in percent-encoding
5954
#######################################
60-
_to_base64url() {
61-
printf '%s' "${1}" | tr '/+' '_-'
55+
percent_encode() {
56+
printf '%s' "${1}" | jq -sRr @uri
6257
}
6358

6459
#######################################
65-
# Convert a text from base64url to base64.
60+
# Decode a text from percent-encoding.
6661
# Arguments:
67-
# 1: Text in base64url encoding
62+
# 1: Text in percent-encoding
6863
# Outputs:
69-
# Text in base64 encoding
64+
# Decoded Text
7065
#######################################
71-
_from_base64url() {
72-
printf '%s' "${1}" | tr '_-' '/+'
66+
percent_decode() {
67+
printf '%s' "${1}" | printf '%b\n' "$(sed -E -e 's/\+/ /g' -e 's/%([0-9a-fA-F]{2})/\\x\1/g')"
7368
}
7469

7570
#######################################
@@ -121,7 +116,7 @@ mqtt_publish() {
121116
topic="${topic}/timestamp/${timestamp}"
122117
fi
123118
if [ "${group_id}" ]; then
124-
topic="${topic}/group/$(_to_base64url "${group_id}")"
119+
topic="${topic}/group/$(percent_encode "${group_id}")"
125120
fi
126121
if [ "${quote_timestamp}" ]; then
127122
topic="${topic}/quote/${quote_timestamp}"
@@ -150,7 +145,7 @@ mqtt_subscribe() {
150145
case "${topic}" in
151146
"${MQTT_SUBSCRIBE_TOPIC}") printf '%s\n' "${payload}" ;;
152147
"${MQTT_SUBSCRIBE_TOPIC}"/group/*)
153-
_build_json_send "${payload}" groupId "$(_from_base64url "${topic##*/}")"
148+
_build_json_send "${payload}" groupId "$(percent_decode "${topic##*/}")"
154149
;;
155150
"${MQTT_SUBSCRIBE_TOPIC}"/*)
156151
_build_json_send "${payload}" recipient "+${topic##*/}"

0 commit comments

Comments
 (0)