Skip to content

Commit 1619277

Browse files
Merge pull request session-foundation#1026 from ceokot/sogs-auth
fix: Authenticate all Open Group API calls
2 parents ba9f729 + 0d0a868 commit 1619277

File tree

2 files changed

+63
-64
lines changed

2 files changed

+63
-64
lines changed

libsession/src/main/java/org/session/libsession/messaging/jobs/BackgroundGroupAddJob.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class BackgroundGroupAddJob(val joinUrl: String): Job {
3737
delegate?.handleJobFailed(this, dispatcherName, DuplicateGroupException())
3838
return
3939
}
40+
4041
storage.addOpenGroup(openGroup.joinUrl())
4142
storage.onOpenGroupAdded(openGroup.server, openGroup.room)
4243
} catch (e: Exception) {

libsession/src/main/java/org/session/libsession/messaging/open_groups/OpenGroupApi.kt

Lines changed: 62 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ object OpenGroupApi {
273273
val queryParameters: Map<String, String> = mapOf(),
274274
val parameters: Any? = null,
275275
val headers: Map<String, String> = mapOf(),
276-
val isAuthRequired: Boolean = true,
277276
val body: ByteArray? = null,
278277
/**
279278
* Always `true` under normal circumstances. You might want to disable
@@ -319,73 +318,72 @@ object OpenGroupApi {
319318
?: return Promise.ofFail(Error.NoEd25519KeyPair)
320319
val urlRequest = urlBuilder.toString()
321320
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
351338
}
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 ->
373359
pubKey = SessionId(
374-
IdPrefix.UN_BLINDED,
375-
ed25519KeyPair.publicKey.asBytes
360+
IdPrefix.BLINDED,
361+
keyPair.publicKey.asBytes
376362
).hexString
377-
sodium.cryptoSignDetached(
378-
signature,
363+
364+
signature = SodiumUtilities.sogsSignature(
379365
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+
)
388382
}
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)
389387

390388
val requestBuilder = okhttp3.Request.Builder()
391389
.url(urlRequest)
@@ -927,7 +925,7 @@ object OpenGroupApi {
927925
}
928926

929927
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)
931929
return getResponseBody(request).map { response ->
932930
JsonUtil.fromJson(response, Capabilities::class.java)
933931
}

0 commit comments

Comments
 (0)