-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Eoin/plt 796 move evervault multiplatform into evervault android (#27)
* port evervault-multiplatform to evervault-android * Minor fixes * Allow running of e2e tests in debug mode
- Loading branch information
Showing
79 changed files
with
2,357 additions
and
508 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
plugins { | ||
id("com.android.library") | ||
id("org.jetbrains.kotlin.android") | ||
kotlin("plugin.serialization") version "1.8.21" | ||
} | ||
|
||
java { | ||
sourceCompatibility = JavaVersion.VERSION_11 | ||
targetCompatibility = JavaVersion.VERSION_11 | ||
} | ||
|
||
android { | ||
namespace = "com.evervault.sdk.e2e" | ||
compileSdk = 33 | ||
|
||
defaultConfig { | ||
minSdk = 33 | ||
|
||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" | ||
consumerProguardFiles("consumer-rules.pro") | ||
} | ||
|
||
buildTypes { | ||
release { | ||
isMinifyEnabled = false | ||
proguardFiles( | ||
getDefaultProguardFile("proguard-android-optimize.txt"), | ||
"proguard-rules.pro" | ||
) | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility = JavaVersion.VERSION_11 | ||
targetCompatibility = JavaVersion.VERSION_11 | ||
} | ||
kotlinOptions { | ||
jvmTarget = "11" | ||
} | ||
} | ||
|
||
val ktorVersion = "2.3.1" | ||
|
||
dependencies { | ||
implementation(project(":evervault-common")) | ||
implementation(kotlin("stdlib-common")) | ||
implementation("org.jetbrains.kotlin:kotlin-stdlib-common") | ||
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1") | ||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2") | ||
implementation("io.ktor:ktor-client-core:$ktorVersion") | ||
implementation("io.ktor:ktor-client-okhttp:$ktorVersion") | ||
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1") | ||
implementation("com.google.code.gson:gson:2.8.7") | ||
testImplementation("junit:junit:4.13.2") | ||
} |
197 changes: 197 additions & 0 deletions
197
evervault-common-e2e/src/test/java/com/evervault/sdk/e2e/EvervaultTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
package com.evervault.sdk.e2e | ||
|
||
import com.evervault.sdk.common.ConfigUrls | ||
import com.evervault.sdk.common.CustomConfig | ||
import org.junit.Test | ||
import com.evervault.sdk.common.Evervault | ||
import com.google.gson.Gson | ||
import io.ktor.client.HttpClient | ||
import io.ktor.client.plugins.defaultRequest | ||
import io.ktor.client.request.header | ||
import io.ktor.client.request.headers | ||
import io.ktor.client.request.post | ||
import io.ktor.client.request.setBody | ||
import io.ktor.client.statement.HttpResponse | ||
import io.ktor.client.statement.bodyAsText | ||
import io.ktor.http.HttpHeaders | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNotNull | ||
import org.junit.Before | ||
import java.util.Base64 | ||
|
||
class EvervaultTest { | ||
private val encryptedStringRegex = Regex("((ev(:|%3A))(debug(:|%3A))?(([A-z0-9+/=%]+)(:|%3A))?((number|boolean|string)(:|%3A))?(([A-z0-9+/=%]+)(:|%3A)){3}(\\$|%24))|(((eyJ[A-z0-9+=.]+){2})([\\w]{8}(-[\\w]{4}){3}-[\\w]{12}))") | ||
|
||
private lateinit var apiKey: String | ||
private lateinit var appUuid: String | ||
private lateinit var teamUuid: String | ||
private val isDebugMode = setDebugMode() | ||
|
||
private var httpClient = HttpClient { | ||
defaultRequest { | ||
header(HttpHeaders.ContentType, "application/json") | ||
} | ||
} | ||
|
||
private val json = Json { ignoreUnknownKeys = true } | ||
|
||
@Before | ||
fun setup() { | ||
Evervault.shared.configure( | ||
teamId = teamUuid, | ||
appId = appUuid, | ||
customConfig = CustomConfig( | ||
isDebugMode = isDebugMode | ||
) | ||
) | ||
} | ||
|
||
@Test | ||
fun testEncryptString() = runBlocking { | ||
val encryptedString = Evervault.shared.encrypt("dummy string to encrypt") | ||
println(encryptedString) | ||
assertNotNull(encryptedString) | ||
val matches = encryptedStringRegex.findAll(encryptedString as String).count() | ||
assertEquals(1, matches) | ||
} | ||
|
||
@Test | ||
fun testDecryptData() = runBlocking { | ||
// Test requires an Evervault API key so don't run if its not set | ||
if (!isDebugMode) { | ||
val data = RawData( | ||
stringData = "Bob", | ||
numberData = 1, | ||
floatData = 1.5, | ||
booleanData = true, | ||
arrayData = arrayListOf("hello", "world"), | ||
) | ||
// Encrypt some data | ||
val encrypted = encryptData(ConfigUrls().apiUrl, data) | ||
val clientToken = createClientSideToken(ConfigUrls().apiUrl, encrypted) | ||
assertNotNull(clientToken) | ||
val decrypted = Evervault.shared.decrypt(clientToken.token, encrypted) as Map<*, *> | ||
println(decrypted) | ||
assertEquals( | ||
decrypted["stringData"], | ||
"Bob" | ||
) | ||
|
||
assertEquals( | ||
decrypted["numberData"], | ||
1.0 | ||
) | ||
|
||
assertEquals( | ||
decrypted["floatData"], | ||
1.5 | ||
) | ||
|
||
assertEquals( | ||
decrypted["booleanData"], | ||
true | ||
) | ||
|
||
assertEquals( | ||
decrypted["arrayData"], | ||
arrayListOf<String>("hello", "world") | ||
) | ||
} | ||
} | ||
|
||
@Serializable | ||
data class TokenData( | ||
val token: String, | ||
) | ||
|
||
@Serializable | ||
data class RawData( | ||
var stringData: String?, | ||
var numberData: Int?, | ||
var floatData: Double?, | ||
var booleanData: Boolean?, | ||
var arrayData: ArrayList<String>?, | ||
) | ||
@Serializable | ||
data class EncryptedTestData( | ||
var stringData: String?, | ||
var numberData: String?, | ||
var floatData: String?, | ||
var booleanData: String?, | ||
var arrayData: ArrayList<String>?, | ||
) | ||
|
||
private suspend fun createClientSideToken(url: Any, data: Any): TokenData { | ||
val task = coroutineScope { | ||
async { | ||
try { | ||
val headerValue = | ||
"${appUuid}:${apiKey}" | ||
val b64HeaderValue = | ||
Base64.getEncoder().encodeToString(headerValue.toByteArray()); | ||
val body = Gson().toJson(mapOf("action" to "api:decrypt", "payload" to data)) | ||
val response: HttpResponse = | ||
httpClient.post("${url}/client-side-tokens") { | ||
setBody(body) | ||
headers { | ||
append("Authorization", "Basic $b64HeaderValue") | ||
} | ||
} | ||
|
||
val responseBody = response.bodyAsText() | ||
return@async json.decodeFromString<TokenData>(responseBody) | ||
} catch (error: Error) { | ||
throw error | ||
} | ||
} | ||
} | ||
|
||
return task.await() | ||
} | ||
|
||
private suspend fun encryptData(url: Any, data: RawData): EncryptedTestData { | ||
val task = coroutineScope { | ||
async { | ||
try { | ||
val headerValue = "${appUuid}:${apiKey}" | ||
val b64HeaderValue = | ||
Base64.getEncoder().encodeToString(headerValue.toByteArray()); | ||
val body = json.encodeToString(data) | ||
val response: HttpResponse = | ||
httpClient.post("${url}/encrypt") { | ||
setBody(body) | ||
headers{ | ||
append("Authorization", "Basic $b64HeaderValue") | ||
} | ||
} | ||
val responseBody = response.bodyAsText() | ||
return@async json.decodeFromString<EncryptedTestData>(responseBody) | ||
} catch (error: Error) { | ||
throw error | ||
} | ||
} | ||
} | ||
|
||
return task.await() | ||
} | ||
|
||
private fun setDebugMode(): Boolean { | ||
try { | ||
apiKey = System.getenv("EV_API_KEY") | ||
appUuid = System.getenv("EV_APP_UUID") | ||
teamUuid = System.getenv("EV_TEAM_UUID") | ||
} catch (e: RuntimeException) { | ||
apiKey = "test-api-key" | ||
appUuid = "test-app-uuid" | ||
teamUuid = "test-team-uuid" | ||
return true | ||
} | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
plugins { | ||
id("com.android.library") | ||
id("org.jetbrains.kotlin.android") | ||
kotlin("plugin.serialization") version "1.8.21" | ||
} | ||
|
||
android { | ||
namespace = "com.evervault.sdk.common" | ||
compileSdk = 33 | ||
|
||
defaultConfig { | ||
minSdk = 26 | ||
|
||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" | ||
consumerProguardFiles("consumer-rules.pro") | ||
} | ||
|
||
buildTypes { | ||
release { | ||
isMinifyEnabled = false | ||
proguardFiles( | ||
getDefaultProguardFile("proguard-android-optimize.txt"), | ||
"proguard-rules.pro" | ||
) | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility = JavaVersion.VERSION_11 | ||
targetCompatibility = JavaVersion.VERSION_11 | ||
} | ||
kotlinOptions { | ||
jvmTarget = "11" | ||
} | ||
} | ||
|
||
val ktorVersion = "2.3.1" | ||
|
||
dependencies { | ||
implementation("androidx.core:core-ktx:1.9.0") | ||
implementation("androidx.appcompat:appcompat:1.6.1") | ||
implementation("com.google.android.material:material:1.8.0") | ||
implementation("io.ktor:ktor-client-core:$ktorVersion") | ||
implementation("io.ktor:ktor-client-okhttp:$ktorVersion") | ||
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1") | ||
implementation("com.google.code.gson:gson:2.8.9") | ||
implementation("org.bouncycastle:bcprov-jdk15on:1.70") | ||
testImplementation("junit:junit:4.13.2") | ||
implementation("org.mockito.kotlin:mockito-kotlin:5.0.0") | ||
androidTestImplementation("androidx.test.ext:junit:1.1.5") | ||
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
Oops, something went wrong.