@@ -273,7 +273,6 @@ object OpenGroupApi {
273
273
val queryParameters : Map <String , String > = mapOf(),
274
274
val parameters : Any? = null ,
275
275
val headers : Map <String , String > = mapOf(),
276
- val isAuthRequired : Boolean = true ,
277
276
val body : ByteArray? = null ,
278
277
/* *
279
278
* Always `true` under normal circumstances. You might want to disable
@@ -319,73 +318,72 @@ object OpenGroupApi {
319
318
? : return Promise .ofFail(Error .NoEd25519KeyPair )
320
319
val urlRequest = urlBuilder.toString()
321
320
val headers = request.headers.toMutableMap()
322
- if (request.isAuthRequired) {
323
- val nonce = sodium.nonce(16 )
324
- val timestamp = TimeUnit .MILLISECONDS .toSeconds(SnodeAPI .nowWithOffset)
325
- var pubKey = " "
326
- var signature = ByteArray (Sign .BYTES )
327
- var bodyHash = ByteArray (0 )
328
- if (request.parameters != null ) {
329
- val parameterBytes = JsonUtil .toJson(request.parameters).toByteArray()
330
- val parameterHash = ByteArray (GenericHash .BYTES_MAX )
331
- if (sodium.cryptoGenericHash(
332
- parameterHash,
333
- parameterHash.size,
334
- parameterBytes,
335
- parameterBytes.size.toLong()
336
- )
337
- ) {
338
- bodyHash = parameterHash
339
- }
340
- } else if (request.body != null ) {
341
- val byteHash = ByteArray (GenericHash .BYTES_MAX )
342
- if (sodium.cryptoGenericHash(
343
- byteHash,
344
- byteHash.size,
345
- request.body,
346
- request.body.size.toLong()
347
- )
348
- ) {
349
- bodyHash = byteHash
350
- }
321
+
322
+ val nonce = sodium.nonce(16 )
323
+ val timestamp = TimeUnit .MILLISECONDS .toSeconds(SnodeAPI .nowWithOffset)
324
+ var pubKey = " "
325
+ var signature = ByteArray (Sign .BYTES )
326
+ var bodyHash = ByteArray (0 )
327
+ if (request.parameters != null ) {
328
+ val parameterBytes = JsonUtil .toJson(request.parameters).toByteArray()
329
+ val parameterHash = ByteArray (GenericHash .BYTES_MAX )
330
+ if (sodium.cryptoGenericHash(
331
+ parameterHash,
332
+ parameterHash.size,
333
+ parameterBytes,
334
+ parameterBytes.size.toLong()
335
+ )
336
+ ) {
337
+ bodyHash = parameterHash
351
338
}
352
- val messageBytes = Hex .fromStringCondensed(publicKey)
353
- .plus(nonce)
354
- .plus(" $timestamp " .toByteArray(Charsets .US_ASCII ))
355
- .plus(request.verb.rawValue.toByteArray())
356
- .plus(" /${request.endpoint.value} " .toByteArray())
357
- .plus(bodyHash)
358
- if (serverCapabilities.isEmpty() || serverCapabilities.contains(Capability .BLIND .name.lowercase())) {
359
- SodiumUtilities .blindedKeyPair(publicKey, ed25519KeyPair)?.let { keyPair ->
360
- pubKey = SessionId (
361
- IdPrefix .BLINDED ,
362
- keyPair.publicKey.asBytes
363
- ).hexString
364
-
365
- signature = SodiumUtilities .sogsSignature(
366
- messageBytes,
367
- ed25519KeyPair.secretKey.asBytes,
368
- keyPair.secretKey.asBytes,
369
- keyPair.publicKey.asBytes
370
- ) ? : return Promise .ofFail(Error .SigningFailed )
371
- } ? : return Promise .ofFail(Error .SigningFailed )
372
- } else {
339
+ } else if (request.body != null ) {
340
+ val byteHash = ByteArray (GenericHash .BYTES_MAX )
341
+ if (sodium.cryptoGenericHash(
342
+ byteHash,
343
+ byteHash.size,
344
+ request.body,
345
+ request.body.size.toLong()
346
+ )
347
+ ) {
348
+ bodyHash = byteHash
349
+ }
350
+ }
351
+ val messageBytes = Hex .fromStringCondensed(publicKey)
352
+ .plus(nonce)
353
+ .plus(" $timestamp " .toByteArray(Charsets .US_ASCII ))
354
+ .plus(request.verb.rawValue.toByteArray())
355
+ .plus(" /${request.endpoint.value} " .toByteArray())
356
+ .plus(bodyHash)
357
+ if (serverCapabilities.isEmpty() || serverCapabilities.contains(Capability .BLIND .name.lowercase())) {
358
+ SodiumUtilities .blindedKeyPair(publicKey, ed25519KeyPair)?.let { keyPair ->
373
359
pubKey = SessionId (
374
- IdPrefix .UN_BLINDED ,
375
- ed25519KeyPair .publicKey.asBytes
360
+ IdPrefix .BLINDED ,
361
+ keyPair .publicKey.asBytes
376
362
).hexString
377
- sodium.cryptoSignDetached(
378
- signature,
363
+
364
+ signature = SodiumUtilities .sogsSignature(
379
365
messageBytes,
380
- messageBytes.size.toLong(),
381
- ed25519KeyPair.secretKey.asBytes
382
- )
383
- }
384
- headers[" X-SOGS-Nonce" ] = encodeBytes(nonce)
385
- headers[" X-SOGS-Timestamp" ] = " $timestamp "
386
- headers[" X-SOGS-Pubkey" ] = pubKey
387
- headers[" X-SOGS-Signature" ] = encodeBytes(signature)
366
+ ed25519KeyPair.secretKey.asBytes,
367
+ keyPair.secretKey.asBytes,
368
+ keyPair.publicKey.asBytes
369
+ ) ? : return Promise .ofFail(Error .SigningFailed )
370
+ } ? : return Promise .ofFail(Error .SigningFailed )
371
+ } else {
372
+ pubKey = SessionId (
373
+ IdPrefix .UN_BLINDED ,
374
+ ed25519KeyPair.publicKey.asBytes
375
+ ).hexString
376
+ sodium.cryptoSignDetached(
377
+ signature,
378
+ messageBytes,
379
+ messageBytes.size.toLong(),
380
+ ed25519KeyPair.secretKey.asBytes
381
+ )
388
382
}
383
+ headers[" X-SOGS-Nonce" ] = encodeBytes(nonce)
384
+ headers[" X-SOGS-Timestamp" ] = " $timestamp "
385
+ headers[" X-SOGS-Pubkey" ] = pubKey
386
+ headers[" X-SOGS-Signature" ] = encodeBytes(signature)
389
387
390
388
val requestBuilder = okhttp3.Request .Builder ()
391
389
.url(urlRequest)
@@ -927,7 +925,7 @@ object OpenGroupApi {
927
925
}
928
926
929
927
fun getCapabilities (server : String ): Promise <Capabilities , Exception > {
930
- val request = Request (verb = GET , room = null , server = server, endpoint = Endpoint .Capabilities , isAuthRequired = false )
928
+ val request = Request (verb = GET , room = null , server = server, endpoint = Endpoint .Capabilities )
931
929
return getResponseBody(request).map { response ->
932
930
JsonUtil .fromJson(response, Capabilities ::class .java)
933
931
}
0 commit comments