Skip to content

Commit

Permalink
Added some missing error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
h0ker committed Jan 25, 2024
1 parent d049eb3 commit a4aad50
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ class IsodepControllerImpl @Inject constructor(
val isoDep = IsoDep.get(tag)
isoDep.connect()

val ndefResult = isoDep.transceive(NDEF_SEL)
//TODO: Add error check on this result
isoDep.transceive(NDEF_SEL)

// send part 1 command
val part1Result = isoDep.transceive(command)
Expand Down Expand Up @@ -194,7 +195,7 @@ class IsodepControllerImpl @Inject constructor(
OperationResult.Failure()
}
} catch (e: Exception) {
Log.i("getVivokeyJwt", e.message.toString())
Log.i(this@IsodepControllerImpl::class.java.name, e.message.toString())
OperationResult.Failure(e)
}
}
Expand Down
117 changes: 62 additions & 55 deletions intra/src/main/java/com/carbidecowboy/intra/data/NfcVControllerImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -150,66 +150,73 @@ class NfcVControllerImpl @Inject constructor(
}

override suspend fun getVivokeyJwt(tag: Tag): OperationResult<String> {
return withContext(Dispatchers.IO) {
return try {

//get challenge from
val challengeRequest = ChallengeRequest(1)
val challengeResponse = async {
authApiService.postChallenge(challengeRequest).body()
}.await()
withContext(Dispatchers.IO) {

if (challengeResponse == null) {
OperationResult.Failure()
}
//get challenge from
val challengeRequest = ChallengeRequest(1)
val challengeResponse = async {
authApiService.postChallenge(challengeRequest).body()
}.await()

val nfcV = NfcV.get(tag)
nfcV.connect()

// truncate challenge to 10 bytes
// challenge string into hex
val challengeBytes: ByteArray = Hex.decodeHex(challengeResponse!!.payload.substring(0, 20))
val command = ByteArray(15 + UID_BYTE_LENGTH)
// Spark 1 flag mode (addressed command)
command[0] = 0x20
// authentication command code
command[1] = 0x35
// copy into command byte array
tag.id.copyInto(command, 2, 0)
// CSI (AES = 0x00)
command[UID_BYTE_LENGTH + 2] = 0x00
// RFU
command[UID_BYTE_LENGTH + 3] = 0x00
// Key slot as byte (only supporting slot 2 for now)
command[UID_BYTE_LENGTH + 4] = 0x02
// copy the challenge
challengeBytes.copyInto(command, UID_BYTE_LENGTH + 5, 0)
// connect and send command
Log.i("Command", Hex.encodeHexString(command))
val response = nfcV.transceive(command)
nfcV.close()
Log.i("Response", Hex.encodeHexString(response))

val sessionRequest = SessionRequest(
uid = Hex.encodeHexString(tag.id!!.reversedArray()),
response = Hex.encodeHexString(response),
token = challengeResponse.token
)

val sessionResponse = async {
authApiService.postSession(sessionRequest)
}.await()

if (!sessionResponse.isSuccessful) {
OperationResult.Failure()
}
if (challengeResponse == null) {
OperationResult.Failure()
}

val result = sessionResponse.body()
result?.token?.let { jwt ->
println(jwt)
return@withContext OperationResult.Success(jwt)
}
val nfcV = NfcV.get(tag)
nfcV.connect()

// truncate challenge to 10 bytes
// challenge string into hex
val challengeBytes: ByteArray =
Hex.decodeHex(challengeResponse!!.payload.substring(0, 20))
val command = ByteArray(15 + UID_BYTE_LENGTH)
// Spark 1 flag mode (addressed command)
command[0] = 0x20
// authentication command code
command[1] = 0x35
// copy into command byte array
tag.id.copyInto(command, 2, 0)
// CSI (AES = 0x00)
command[UID_BYTE_LENGTH + 2] = 0x00
// RFU
command[UID_BYTE_LENGTH + 3] = 0x00
// Key slot as byte (only supporting slot 2 for now)
command[UID_BYTE_LENGTH + 4] = 0x02
// copy the challenge
challengeBytes.copyInto(command, UID_BYTE_LENGTH + 5, 0)
// connect and send command
Log.i("Command", Hex.encodeHexString(command))
val response = nfcV.transceive(command)
nfcV.close()
Log.i("Response", Hex.encodeHexString(response))

val sessionRequest = SessionRequest(
uid = Hex.encodeHexString(tag.id!!.reversedArray()),
response = Hex.encodeHexString(response),
token = challengeResponse.token
)

val sessionResponse = async {
authApiService.postSession(sessionRequest)
}.await()

if (!sessionResponse.isSuccessful) {
OperationResult.Failure()
}

val result = sessionResponse.body()
result?.token?.let { jwt ->
println(jwt)
return@withContext OperationResult.Success(jwt)
}

OperationResult.Failure()
OperationResult.Failure()
}
} catch (e: Exception) {
Log.i(this@NfcVControllerImpl::class.java.name, e.message.toString())
OperationResult.Failure(e)
}
}

Expand Down

0 comments on commit a4aad50

Please sign in to comment.