mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 03:09:04 +01:00
[headless] expose ContactManager methods for adding contacts remotely
This commit is contained in:
@@ -64,6 +64,16 @@ constructor(
|
||||
path("/v1") {
|
||||
path("/contacts") {
|
||||
get { ctx -> contactController.list(ctx) }
|
||||
path("add") {
|
||||
post { ctx -> contactController.addPendingContact(ctx) }
|
||||
path("link") {
|
||||
get { ctx -> contactController.link(ctx) }
|
||||
}
|
||||
path("pending") {
|
||||
get { ctx -> contactController.listPendingContacts(ctx) }
|
||||
delete { ctx -> contactController.removePendingContact(ctx) }
|
||||
}
|
||||
}
|
||||
path("/:contactId") {
|
||||
delete { ctx -> contactController.delete(ctx) }
|
||||
}
|
||||
|
||||
@@ -5,6 +5,10 @@ import io.javalin.Context
|
||||
interface ContactController {
|
||||
|
||||
fun list(ctx: Context): Context
|
||||
fun link(ctx: Context): Context
|
||||
fun addPendingContact(ctx: Context): Context
|
||||
fun listPendingContacts(ctx: Context): Context
|
||||
fun removePendingContact(ctx: Context): Context
|
||||
fun delete(ctx: Context): Context
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
package org.briarproject.briar.headless.contact
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import io.javalin.Context
|
||||
import io.javalin.NotFoundResponse
|
||||
import org.briarproject.bramble.api.contact.ContactManager
|
||||
import org.briarproject.bramble.api.contact.PendingContactId
|
||||
import org.briarproject.bramble.api.db.NoSuchContactException
|
||||
import org.briarproject.bramble.api.db.NoSuchPendingContactException
|
||||
import org.briarproject.briar.headless.getContactIdFromPathParam
|
||||
import org.briarproject.briar.headless.getFromJson
|
||||
import org.briarproject.briar.headless.json.JsonDict
|
||||
import org.spongycastle.util.encoders.Base64
|
||||
import org.spongycastle.util.encoders.DecoderException
|
||||
import javax.annotation.concurrent.Immutable
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
@@ -13,7 +20,8 @@ import javax.inject.Singleton
|
||||
@Singleton
|
||||
internal class ContactControllerImpl
|
||||
@Inject
|
||||
constructor(private val contactManager: ContactManager) : ContactController {
|
||||
constructor(private val contactManager: ContactManager, private val objectMapper: ObjectMapper) :
|
||||
ContactController {
|
||||
|
||||
override fun list(ctx: Context): Context {
|
||||
val contacts = contactManager.contacts.map { contact ->
|
||||
@@ -22,6 +30,44 @@ constructor(private val contactManager: ContactManager) : ContactController {
|
||||
return ctx.json(contacts)
|
||||
}
|
||||
|
||||
override fun link(ctx: Context): Context {
|
||||
val linkDict = JsonDict("link" to contactManager.handshakeLink)
|
||||
return ctx.json(linkDict)
|
||||
}
|
||||
|
||||
override fun addPendingContact(ctx: Context): Context {
|
||||
val link = ctx.getFromJson(objectMapper, "link")
|
||||
val alias = ctx.getFromJson(objectMapper, "alias")
|
||||
val pendingContact = contactManager.addPendingContact(link, alias)
|
||||
return ctx.json(pendingContact.output())
|
||||
}
|
||||
|
||||
override fun listPendingContacts(ctx: Context): Context {
|
||||
val pendingContacts = contactManager.pendingContacts.map { pendingContact ->
|
||||
pendingContact.output()
|
||||
}
|
||||
return ctx.json(pendingContacts)
|
||||
}
|
||||
|
||||
override fun removePendingContact(ctx: Context): Context {
|
||||
// construct and check PendingContactId
|
||||
val pendingContactString = ctx.getFromJson(objectMapper, "pendingContactId")
|
||||
val pendingContactBytes = try {
|
||||
Base64.decode(pendingContactString)
|
||||
} catch (e: DecoderException) {
|
||||
throw NotFoundResponse()
|
||||
}
|
||||
if (pendingContactBytes.size != PendingContactId.LENGTH) throw NotFoundResponse()
|
||||
val id = PendingContactId(pendingContactBytes)
|
||||
// remove
|
||||
try {
|
||||
contactManager.removePendingContact(id)
|
||||
} catch (e: NoSuchPendingContactException) {
|
||||
throw NotFoundResponse()
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
override fun delete(ctx: Context): Context {
|
||||
val contactId = ctx.getContactIdFromPathParam()
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package org.briarproject.briar.headless.contact
|
||||
|
||||
import org.briarproject.bramble.api.contact.PendingContact
|
||||
import org.briarproject.bramble.api.contact.PendingContactState.*
|
||||
import org.briarproject.briar.headless.json.JsonDict
|
||||
|
||||
internal fun PendingContact.output() = JsonDict(
|
||||
"pendingContactId" to id.bytes,
|
||||
"alias" to alias,
|
||||
"state" to when(state) {
|
||||
WAITING_FOR_CONNECTION -> "waiting_for_connection"
|
||||
CONNECTED -> "connected"
|
||||
ADDING_CONTACT -> "adding_contact"
|
||||
FAILED -> "failed"
|
||||
else -> throw AssertionError()
|
||||
},
|
||||
"timestamp" to timestamp
|
||||
)
|
||||
Reference in New Issue
Block a user