-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathEjson.java
More file actions
269 lines (230 loc) · 9.11 KB
/
Copy pathEjson.java
File metadata and controls
269 lines (230 loc) · 9.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package chat.rocket.reactnative.notification;
import android.util.Log;
import com.tencent.mmkv.MMKV;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.io.UnsupportedEncodingException;
import chat.rocket.reactnative.BuildConfig;
import chat.rocket.reactnative.storage.MMKVKeyManager;
class Utils {
static public String toHex(String arg) {
try {
return String.format("%x", new BigInteger(1, arg.getBytes("UTF-8")));
} catch (Exception e) {
return "";
}
}
}
public class Ejson {
private static final String TAG = "RocketChat.Ejson";
private static final String TOKEN_KEY = "reactnativemeteor_usertoken-";
public String host;
String rid;
String type;
Sender sender;
Caller caller; // For video conf notifications
String messageId;
String callId; // For video conf notifications
String notificationType;
String messageType;
String senderName;
String name; // Room name for groups/channels
String msg;
Integer status; // For video conf: 0=incoming, 4=cancelled
String tmid;
Content content;
private MMKV getMMKV() {
String encryptionKey = MMKVKeyManager.getEncryptionKey();
if (encryptionKey != null && !encryptionKey.isEmpty()) {
return MMKV.mmkvWithID("default", MMKV.SINGLE_PROCESS_MODE, encryptionKey);
}
// Fallback to no encryption if key is not available
// This can happen if Keystore is unavailable (e.g., device locked/Direct Boot)
Log.w(TAG, "MMKV encryption key not available, opening without encryption");
return MMKV.mmkvWithID("default", MMKV.SINGLE_PROCESS_MODE);
}
private String buildAvatarUri(String avatarPath, int sizePx) {
String server = serverURL();
if (server == null || server.isEmpty()) {
Log.w(TAG, "Cannot generate avatar URI: serverURL is null");
return null;
}
return server + avatarPath + "?format=png&size=" + sizePx;
}
public String getAvatarUri() {
String avatarPath;
if ("d".equals(type)) {
if (sender == null || sender.username == null || sender.username.isEmpty()) {
Log.w(TAG, "Cannot generate avatar URI: sender or username is null");
return null;
}
try {
avatarPath = "/avatar/" + URLEncoder.encode(sender.username, "UTF-8");
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Failed to encode username", e);
return null;
}
} else {
if (rid == null || rid.isEmpty()) {
Log.w(TAG, "Cannot generate avatar URI: rid is null for non-DM");
return null;
}
try {
avatarPath = "/avatar/room/" + URLEncoder.encode(rid, "UTF-8");
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Failed to encode rid", e);
return null;
}
}
return buildAvatarUri(avatarPath, 100);
}
/**
* Factory for building caller avatar URIs from host + username (e.g. VoIP payload).
* Caller is package-private, so this is the only way to get avatar URI from outside the package.
*/
public static Ejson forCallerAvatar(String host, String username) {
if (host == null || host.isEmpty() || username == null || username.isEmpty()) {
return null;
}
Ejson ejson = new Ejson();
ejson.host = host;
ejson.caller = new Caller();
ejson.caller.username = username;
return ejson;
}
/**
* Generates avatar URI for video conference caller (default size 100).
* Returns null if caller username is not available (username is required for avatar endpoint).
*/
public String getCallerAvatarUri() {
return getCallerAvatarUri(100);
}
/**
* Generates avatar URI for video conference caller with custom size.
* Returns null if caller username is not available.
*/
public String getCallerAvatarUri(int sizePx) {
if (caller == null || caller.username == null || caller.username.isEmpty()) {
Log.w(TAG, "Cannot generate caller avatar URI: caller or username is null");
return null;
}
try {
String avatarPath = "/avatar/" + URLEncoder.encode(caller.username, "UTF-8");
return buildAvatarUri(avatarPath, sizePx);
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Failed to encode caller username", e);
return null;
}
}
public String token() {
String serverURL = serverURL();
String userId = userId();
MMKV mmkv = getMMKV();
if (mmkv == null) {
Log.e(TAG, "token() called but MMKV is null");
return "";
}
if (userId == null || userId.isEmpty()) {
Log.w(TAG, "token() called but userId is null or empty");
return "";
}
// Server-scoped key: reactnativemeteor_usertoken-{server}-{userId}.
// Keep this format in sync with getUserTokenKey() on the JS side. The token used to be
// stored under reactnativemeteor_usertoken-{userId} (no server component), which let a
// lookup resolve a token belonging to a different server when two servers shared a userId
// (token confusion). Read the server-scoped slot first and only fall back to the legacy
// slot for the window between an app update and the JS migration running on next launch.
String token = null;
if (serverURL != null && !serverURL.isEmpty()) {
String key = TOKEN_KEY.concat(serverURL).concat("-").concat(userId);
if (BuildConfig.DEBUG) {
Log.d(TAG, "Looking up token with server-scoped key");
}
token = mmkv.decodeString(key);
}
if (token == null || token.isEmpty()) {
// Legacy fallback (pre-migration). Safe because the exported-receiver vector that
// abused this ambiguity is closed (ReplyBroadcast/DismissNotification are not exported).
token = mmkv.decodeString(TOKEN_KEY.concat(userId));
}
if (token == null || token.isEmpty()) {
Log.w(TAG, "No token found in MMKV for userId");
} else if (BuildConfig.DEBUG) {
Log.d(TAG, "Successfully retrieved token from MMKV");
}
return token != null ? token : "";
}
public String userId() {
String serverURL = serverURL();
if (serverURL == null) {
Log.e(TAG, "userId() called but serverURL is null");
return "";
}
MMKV mmkv = getMMKV();
if (mmkv == null) {
Log.e(TAG, "userId() called but MMKV is null");
return "";
}
String key = TOKEN_KEY.concat(serverURL);
if (BuildConfig.DEBUG) {
Log.d(TAG, "Looking up userId with key: " + key);
}
String userId = mmkv.decodeString(key);
if (userId == null || userId.isEmpty()) {
Log.w(TAG, "No userId found in MMKV for server: " + NotificationHelper.sanitizeUrl(serverURL));
// Only list keys in debug builds for diagnostics
if (BuildConfig.DEBUG) {
try {
String[] allKeys = mmkv.allKeys();
if (allKeys != null && allKeys.length > 0) {
Log.d(TAG, "Available MMKV keys count: " + allKeys.length);
// Log only keys that match the TOKEN_KEY pattern for security
for (String k : allKeys) {
if (k != null && k.startsWith("reactnativemeteor_usertoken")) {
Log.d(TAG, "Found auth key: " + k);
}
}
} else {
Log.w(TAG, "MMKV has no keys stored");
}
} catch (Exception e) {
Log.e(TAG, "Error listing MMKV keys", e);
}
}
} else if (BuildConfig.DEBUG) {
Log.d(TAG, "Successfully retrieved userId from MMKV");
}
return userId != null ? userId : "";
}
public String privateKey() {
String serverURL = serverURL();
MMKV mmkv = getMMKV();
if (mmkv != null && serverURL != null) {
return mmkv.decodeString(serverURL.concat("-RC_E2E_PRIVATE_KEY"));
}
return null;
}
public String serverURL() {
String url = this.host;
if (url != null && url.endsWith("/")) {
url = url.substring(0, url.length() - 1);
}
return url;
}
static class Sender {
String _id;
String username;
String name;
}
static class Caller {
String _id;
String name;
String username;
}
static class Content {
String algorithm;
String ciphertext;
String kid;
String iv;
}
}