Skip to content

Commit

Permalink
Improve Exception handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
dainnilsson committed May 5, 2017
1 parent 00c3ebb commit 4237e9a
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 92 deletions.
36 changes: 19 additions & 17 deletions app/src/main/kotlin/com/yubico/yubioath/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.annotation.SuppressLint
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.hardware.usb.UsbDevice
import android.hardware.usb.UsbManager
import android.nfc.NfcAdapter
Expand All @@ -17,7 +16,6 @@ import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.WindowManager
import android.widget.Toast
import com.google.zxing.integration.android.IntentIntegrator
import com.yubico.yubioath.exc.AppletMissingException
import com.yubico.yubioath.exc.AppletSelectException
Expand All @@ -37,18 +35,17 @@ import org.jetbrains.anko.toast
import java.io.IOException

class MainActivity : AppCompatActivity(), OnDiscoveredTagListener {
private val ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION"
private val ACTION_USB_PERMISSION = "com.yubico.yubioath.USB_PERMISSION"

private lateinit var state: StateFragment

private lateinit var usbManager: UsbManager
private var tagDispatcher: TagDispatcher? = null
private lateinit var tagDispatcher: TagDispatcher
private var totpListener: OnYubiKeyListener? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

//This causes rotation animation to look like crap.
window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)

setContentView(R.layout.main_activity)
Expand All @@ -64,9 +61,10 @@ class MainActivity : AppCompatActivity(), OnDiscoveredTagListener {
else -> Unit
}

if (packageManager.hasSystemFeature(PackageManager.FEATURE_NFC)) {
tagDispatcher = TagDispatcherBuilder(this, this).enableReaderMode(false).build()
}
tagDispatcher = TagDispatcherBuilder(this, this)
.enableReaderMode(false)
.enableUnavailableNfcUserPrompt(false)
.build()
}

fun checkForUsbDevice(): Boolean {
Expand Down Expand Up @@ -126,7 +124,7 @@ class MainActivity : AppCompatActivity(), OnDiscoveredTagListener {
@SuppressLint("NewApi")
public override fun onPause() {
super.onPause()
tagDispatcher?.disableExclusiveNfc()
tagDispatcher.disableExclusiveNfc()
}

@SuppressLint("NewApi")
Expand All @@ -135,20 +133,21 @@ class MainActivity : AppCompatActivity(), OnDiscoveredTagListener {

if (intent.action == NfcAdapter.ACTION_NDEF_DISCOVERED && !state.ndefConsumed) {
state.ndefConsumed = true
tagDispatcher?.interceptIntent(intent)
tagDispatcher.interceptIntent(intent)
}

when (tagDispatcher?.enableExclusiveNfc()) {
when (tagDispatcher.enableExclusiveNfc()) {
TagDispatcher.NfcStatus.AVAILABLE_DISABLED -> {
with(supportFragmentManager) {
val transaction = beginTransaction()
findFragmentByTag("dialog")?.let { transaction.remove(it) }
EnableNfcDialog().show(transaction, "dialog")
if(!state.nfcWarned) {
toast(R.string.nfc_off)
state.nfcWarned = true
}
}
TagDispatcher.NfcStatus.NOT_AVAILABLE -> {
Toast.makeText(this, R.string.no_nfc, Toast.LENGTH_LONG).show()
finish()
if(!state.nfcWarned) {
toast(R.string.no_nfc)
state.nfcWarned = true
}
}
else -> Unit
}
Expand Down Expand Up @@ -195,6 +194,9 @@ class MainActivity : AppCompatActivity(), OnDiscoveredTagListener {
} catch (e: IOException) {
toast(R.string.tag_error)
Log.e("yubioath", "IOException in handler", e)
} catch (e: AppletMissingException) {
toast(R.string.applet_missing)
Log.e("yubioath", "AppletMissingException in handler", e)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ class AddAccountFragment : Fragment(), MainActivity.OnYubiKeyListener {
val type = if (credential_type.selectedItemId == 0.toLong()) OathType.TOTP else OathType.HOTP

if (name.isEmpty() || secret.isEmpty()) {
activity.longToast(R.string.credential_manual_error)
activity.longToast(R.string.credential_incomplete)
} else {
SetPasswordFragment.closeKeyboard(activity)

val base32 = Base32()
val key = when {
base32.isInAlphabet(secret) -> base32.decode(secret)
else -> throw IllegalArgumentException("Secret must be base32 encoded")
else -> { activity.longToast(R.string.credential_invalid_secret); return }
}
data = CredentialData(key, name, type)

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class StateFragment : Fragment() {

lateinit var keyManager: KeyManager
var ndefConsumed = false
var nfcWarned = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/kotlin/com/yubico/yubioath/model/YubiKeyOath.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import java.security.SecureRandom
import java.util.*
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
import kotlin.experimental.or

/**
* Created with IntelliJ IDEA.
Expand All @@ -51,6 +50,9 @@ import kotlin.experimental.or
* To change this template use File | Settings | File Templates.
*/

//Here because the current kotlin plugin doesn't seem to pick up kotlin.experimental.or
infix fun Byte.or(other:Byte):Byte = (toInt() or other.toInt()).toByte()

class YubiKeyOath @Throws(IOException::class, AppletSelectException::class)
constructor(private val keyManager: KeyManager, private val backend: Backend) : Closeable {
val persistent = backend.persistent
Expand Down Expand Up @@ -222,7 +224,7 @@ constructor(private val keyManager: KeyManager, private val backend: Backend) :
return ByteBuffer.allocate(4096).apply {
var resp = splitApduResponse(backend.sendApdu(apdu))
while (resp.status != APDU_OK) {
if (resp.status.shr(8).toByte() == APDU_DATA_REMAINING_SW1) {
if ((resp.status shr 8).toByte() == APDU_DATA_REMAINING_SW1) {
put(resp.data)
resp = splitApduResponse(backend.sendApdu(byteArrayOf(0, SEND_REMAINING_INS, 0, 0)))
} else {
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@
<item>Time based (TOTP)</item>
<item>Counter based (HOTP)</item>
</string-array>
<string name="credential_invalid_name">Name is invalid!</string>
<string name="credential_manual_error">Incomplete credentials</string>
<string name="credential_invalid_name">Invalid name</string>
<string name="credential_invalid_secret">Invalid secret key, must be a base32 encoded value</string>
<string name="credential_incomplete">Incomplete credentials</string>
<string name="add">Add</string>
<string name="save">Save</string>
<string name="cancel">Cancel</string>
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.1.1'
ext.kotlin_version = '1.1.2-3'
repositories {
jcenter()
}
Expand Down

0 comments on commit 4237e9a

Please sign in to comment.