Skip to content

Commit

Permalink
OID4VCI Integration (#24)
Browse files Browse the repository at this point in the history
Signed-off-by: Tiago Nascimento <[email protected]>
  • Loading branch information
theosirian authored Sep 11, 2024
1 parent a80779f commit 3e5df43
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 322 deletions.
2 changes: 1 addition & 1 deletion MobileSdk/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ android {
}

dependencies {
api("com.spruceid.mobile.sdk.rs:mobilesdkrs:0.0.28")
api("com.spruceid.mobile.sdk.rs:mobilesdkrs:0.0.30")
//noinspection GradleCompatible
implementation("com.android.support:appcompat-v7:28.0.0")
/* Begin UI dependencies */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class BLESessionManager {
this.uuid,
"BLE",
"Central",
sessionData.bleIdent.toByteArray(),
sessionData.bleIdent,
::updateRequestData,
callback
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class CredentialsViewModel : ViewModel() {
_uuid.value,
"BLE",
"Central",
_session.value!!.bleIdent.toByteArray(),
_session.value!!.bleIdent,
::updateRequestData,
null
)
Expand Down Expand Up @@ -146,4 +146,4 @@ class CredentialsViewModel : ViewModel() {
throw e
}
}
}
}
81 changes: 81 additions & 0 deletions MobileSdk/src/main/java/com/spruceid/mobile/sdk/OID4VCI.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.spruceid.mobile.sdk

import com.spruceid.mobile.sdk.rs.AsyncHttpClient
import com.spruceid.mobile.sdk.rs.HttpRequest
import com.spruceid.mobile.sdk.rs.HttpResponse
import com.spruceid.mobile.sdk.rs.SyncHttpClient
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.BufferedInputStream
import java.io.DataOutputStream
import java.net.URL
import javax.net.ssl.HttpsURLConnection

class Oid4vciSyncHttpClient: SyncHttpClient {
override fun httpClient(request: HttpRequest): HttpResponse {
val connection: HttpsURLConnection = URL(request.url).openConnection() as HttpsURLConnection

connection.requestMethod = request.method
for ((k, v) in request.headers) {
connection.setRequestProperty(k, v)
}
connection.doOutput = true
connection.doInput = true

val wr = DataOutputStream(connection.outputStream)
wr.write(request.body)
wr.flush()
wr.close()

val statusCode = connection.responseCode
val stream = BufferedInputStream(connection.inputStream)
val body = stream.readBytes()
stream.close()

val headers = connection.headerFields.mapValues { it.value.joinToString(",") }

return HttpResponse(
statusCode = statusCode.toUShort(),
headers = headers,
body = body,
)
}
}

class Oid4vciAsyncHttpClient: AsyncHttpClient {
override suspend fun httpClient(request: HttpRequest): HttpResponse {
val connection: HttpsURLConnection =
withContext(Dispatchers.IO) {
URL(request.url).openConnection()
} as HttpsURLConnection

connection.requestMethod = request.method
for ((k, v) in request.headers) {
connection.setRequestProperty(k, v)
}
connection.doOutput = true
connection.doInput = true

val wr = DataOutputStream(connection.outputStream)
withContext(Dispatchers.IO) {
wr.write(request.body)
wr.flush()
wr.close()
}

val statusCode = connection.responseCode
val body: ByteArray
withContext(Dispatchers.IO) {
val stream = BufferedInputStream(connection.inputStream)
body = stream.readBytes()
stream.close()
}
val headers = connection.headerFields.mapValues { it.value.joinToString(",") }

return HttpResponse(
statusCode = statusCode.toUShort(),
headers = headers,
body = body,
)
}
}
Loading

0 comments on commit 3e5df43

Please sign in to comment.