Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Don't expose core lib in Public API #65

Merged
merged 3 commits into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinCoroutinesVersion"

// Awala
api 'tech.relaycorp:relaynet:[1.48.0,2.0.0)'
implementation 'tech.relaycorp:relaynet:1.48.0'
implementation 'tech.relaycorp:poweb:1.5.15'

// Security
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,16 @@ public class PrivateThirdPartyEndpoint internal constructor(
InvalidAuthorizationException::class
)
public suspend fun import(
identityCertificate: Certificate,
identityCertificate: ByteArray,
authBundle: AuthorizationBundle
): PrivateThirdPartyEndpoint {

val identityCertificateDeserialized = try {
Certificate.deserialize(identityCertificate)
} catch (exp: CertificateException) {
throw InvalidAuthorizationException("Invalid identity certificate", exp)
}

val pda = Certificate.deserialize(authBundle.pdaSerialized)
val pdaChain = authBundle.pdaChainSerialized.map { Certificate.deserialize(it) }

Expand All @@ -104,12 +111,16 @@ public class PrivateThirdPartyEndpoint internal constructor(
throw InvalidAuthorizationException("PDA was not issued by third-party endpoint", e)
}

val endpoint =
PrivateThirdPartyEndpoint(firstPartyAddress, identityCertificate, pda, pdaChain)
val endpoint = PrivateThirdPartyEndpoint(
firstPartyAddress,
identityCertificateDeserialized,
pda,
pdaChain
)

Storage.privateThirdParty.set(
endpoint.storageKey,
PrivateThirdPartyEndpointData(identityCertificate, authBundle)
PrivateThirdPartyEndpointData(identityCertificateDeserialized, authBundle)
)

return endpoint
Expand Down Expand Up @@ -183,5 +194,6 @@ public class UnknownThirdPartyEndpointException(message: String) : AwaladroidExc
public class UnknownFirstPartyEndpointException(message: String) : AwaladroidException(message)
public class InvalidThirdPartyEndpoint(message: String, cause: Throwable? = null) :
AwaladroidException(message, cause)

public class InvalidAuthorizationException(message: String, cause: Throwable) :
AwaladroidException(message, cause)
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ internal class PrivateThirdPartyEndpointTest {
authorization.serialize(),
listOf(PDACertPath.PRIVATE_ENDPOINT.serialize())
)
val endpoint = PrivateThirdPartyEndpoint.import(PDACertPath.PRIVATE_ENDPOINT, authBundle)
val endpoint = PrivateThirdPartyEndpoint.import(
PDACertPath.PRIVATE_ENDPOINT.serialize(),
authBundle
)

assertEquals(
firstPartyAddress,
Expand Down Expand Up @@ -127,7 +130,25 @@ internal class PrivateThirdPartyEndpointTest {
)
}

@Test(expected = UnknownFirstPartyEndpointException::class)
@Test
fun import_invalidIdentityCertificate() = runBlockingTest {
val firstPartyEndpoint = FirstPartyEndpointFactory.build()
val authorization = issueDeliveryAuthorization(
subjectPublicKey = firstPartyEndpoint.keyPair.public,
issuerPrivateKey = KeyPairSet.PRIVATE_ENDPOINT.private,
validityEndDate = ZonedDateTime.now().plusDays(1),
issuerCertificate = PDACertPath.PRIVATE_ENDPOINT
)

expectedException.expect(InvalidAuthorizationException::class.java)
expectedException.expectMessage("Invalid identity certificate")
PrivateThirdPartyEndpoint.import(
"123456".toByteArray(),
AuthorizationBundle(authorization.serialize(), emptyList())
)
}

@Test
fun import_invalidFirstParty() = runBlockingTest {
val firstPartyEndpoint = FirstPartyEndpointFactory.build()
val authorization = issueDeliveryAuthorization(
Expand All @@ -137,8 +158,12 @@ internal class PrivateThirdPartyEndpointTest {
issuerCertificate = PDACertPath.PRIVATE_ENDPOINT
)

expectedException.expect(UnknownFirstPartyEndpointException::class.java)
expectedException.expectMessage(
"First party endpoint ${authorization.subjectPrivateAddress} not registered"
)
PrivateThirdPartyEndpoint.import(
PDACertPath.PRIVATE_ENDPOINT,
PDACertPath.PRIVATE_ENDPOINT.serialize(),
AuthorizationBundle(authorization.serialize(), emptyList())
)
}
Expand Down Expand Up @@ -166,7 +191,7 @@ internal class PrivateThirdPartyEndpointTest {
expectedException.expect(InvalidAuthorizationException::class.java)
expectedException.expectMessage("PDA was not issued by third-party endpoint")
PrivateThirdPartyEndpoint.import(
PDACertPath.PRIVATE_ENDPOINT,
PDACertPath.PRIVATE_ENDPOINT.serialize(),
AuthorizationBundle(
authorization.serialize(),
listOf(unrelatedCertificate.serialize())
Expand All @@ -191,12 +216,12 @@ internal class PrivateThirdPartyEndpointTest {
expectedException.expect(InvalidAuthorizationException::class.java)
expectedException.expectMessage("PDA is invalid")
PrivateThirdPartyEndpoint.import(
PDACertPath.PRIVATE_ENDPOINT,
PDACertPath.PRIVATE_ENDPOINT.serialize(),
AuthorizationBundle(authorization.serialize(), emptyList())
)
}

@Test(expected = PersistenceException::class)
@Test
fun import_persistenceException() = runBlockingTest {
val firstPartyEndpoint = FirstPartyEndpointFactory.build()
whenever(storage.identityCertificate.get(any())).thenThrow(PersistenceException(""))
Expand All @@ -208,8 +233,9 @@ internal class PrivateThirdPartyEndpointTest {
issuerCertificate = PDACertPath.PRIVATE_ENDPOINT
)

expectedException.expect(PersistenceException::class.java)
PrivateThirdPartyEndpoint.import(
PDACertPath.PRIVATE_ENDPOINT,
PDACertPath.PRIVATE_ENDPOINT.serialize(),
AuthorizationBundle(authorization.serialize(), emptyList())
)
}
Expand Down