]> www.average.org Git - YkNeoCR.git/commitdiff
reorg, separate roles
authorEugene Crosser <crosser@average.org>
Thu, 9 May 2013 12:48:05 +0000 (16:48 +0400)
committerEugene Crosser <crosser@average.org>
Thu, 9 May 2013 12:48:05 +0000 (16:48 +0400)
AndroidManifest.xml
src/org/average/nfcauthcr/Autostart.java [deleted file]
src/org/average/nfcauthcr/Autostop.java [deleted file]
src/org/average/nfcauthcr/Enroll.java
src/org/average/nfcauthcr/NfcCRdispatch.java [new file with mode: 0644]
src/org/average/nfcauthcr/TagEvent.java
src/org/average/nfcauthcr/YkNeo.java

index 73d6683af9f33f0793296d2b3e6ff8b05be43ca4..e9e69e02bebd986c4cb7649a940f57bfd0b78159 100644 (file)
                        </intent-filter>
                </activity>
 
-               <receiver android:name=".Autostart">
-                       <intent-filter>
-                               <action android:name="android.intent.action.USER_PRESENT" />
-                       </intent-filter>
-               </receiver>
-               <receiver android:name=".Autostop">
-                       <intent-filter>
-                               <action android:name="android.intent.action.SCREEN_OFF" />
-                       </intent-filter>
-               </receiver>
-
-               <service android:name=".Check"
-                        android:label="@string/app_name">
-               </service>
+               <activity android:name=".TagEvent"
+                         android:label="@string/app_name">
+               </activity>
 
        </application>
 </manifest>
diff --git a/src/org/average/nfcauthcr/Autostart.java b/src/org/average/nfcauthcr/Autostart.java
deleted file mode 100644 (file)
index ac24703..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-package org.average.nfcauthcr;
-
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.Intent;
-import android.util.Log;
-
-import org.average.nfcauthcr.TagEvent;
-
-public class Autostart extends BroadcastReceiver {
-
-       final String TAG = getClass().getName();
-
-       @Override
-       public void onReceive(Context context, Intent intent) {
-               Log.v(TAG, "Autostart called");
-               Intent i = new Intent(context, TagEvent.class);
-               i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-               context.startService(i);
-       }
-}
diff --git a/src/org/average/nfcauthcr/Autostop.java b/src/org/average/nfcauthcr/Autostop.java
deleted file mode 100644 (file)
index e845a42..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-package org.average.nfcauthcr;
-
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.Intent;
-import android.util.Log;
-
-import org.average.nfcauthcr.TagEvent;
-
-public class Autostop extends BroadcastReceiver {
-
-       final String TAG = getClass().getName();
-
-       @Override
-       public void onReceive(Context context, Intent intent) {
-               Log.v(TAG, "Autostop called");
-               Intent i = new Intent(context, TagEvent.class);
-               context.stopService(i);
-       }
-}
index 12533e7bf8c469fb9bd719696d5215fa898b3826..6c104c4ad9513ad22574924fccd89f824a17d033 100644 (file)
@@ -24,7 +24,6 @@ public class Enroll extends Activity {
        private static boolean waitingForResult = false;
        private static SharedPreferences prefs;
        private static int slot;
-       private AlertDialog swipeDialog;
 
        @Override
        protected void onCreate(Bundle savedInstanceState)
@@ -53,8 +52,6 @@ public class Enroll extends Activity {
        @Override
        protected void onPause() {
                super.onPause();
-               Log.v(TAG, "Going inactive, try to stop");
-               if (!waitingForResult) { finish(); }
        }
 
        @Override
@@ -89,10 +86,6 @@ public class Enroll extends Activity {
                                        Intent intent) {
                Log.v(TAG, "Got activity result");
                waitingForResult = false;
-               if(swipeDialog != null) {
-                       swipeDialog.dismiss();
-                       swipeDialog = null;
-               }
 
                if (resultCode != RESULT_OK) {
                        Log.v(TAG, "Error result code " + resultCode);
@@ -131,26 +124,10 @@ public class Enroll extends Activity {
                rng.nextBytes(challenge);
                Log.v(TAG, "Random challenge: " + hex(challenge));
                Intent crIntent = new Intent(this, TagEvent.class);
-               crIntent.putExtra("slot", slot);
+               crIntent.putExtra("yubikey_neo_slot", slot);
                crIntent.putExtra("challenge", challenge);
-               this.startActivityForResult(crIntent, 0);
                waitingForResult = true;
-               if (swipeDialog != null) swipeDialog.dismiss();
-               swipeDialog = makeDialog();
-               swipeDialog.show();
-       }
-
-       private AlertDialog makeDialog() {
-               AlertDialog.Builder builder = new AlertDialog.Builder(this);
-               builder.setTitle(R.string.challenging);
-               builder.setMessage(R.string.swipe);
-               builder.setOnCancelListener(
-                               new DialogInterface.OnCancelListener() {
-                       public void onCancel(DialogInterface dialog) {
-                               Log.v(TAG, "unbind service FIXME");
-                       }
-               });
-               return builder.create();
+               this.startActivityForResult(crIntent, 0);
        }
 
        private String hex(byte[] a) {
diff --git a/src/org/average/nfcauthcr/NfcCRdispatch.java b/src/org/average/nfcauthcr/NfcCRdispatch.java
new file mode 100644 (file)
index 0000000..bfacfa9
--- /dev/null
@@ -0,0 +1,81 @@
+package org.average.nfcauthcr;
+
+import android.app.Activity;
+import android.app.PendingIntent;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.nfc.NfcAdapter;
+import android.nfc.Tag;
+import android.nfc.TagLostException;
+import android.nfc.tech.IsoDep;
+import android.util.Log;
+import android.widget.Toast;
+
+import org.average.nfcauthcr.YkNeo;
+import org.average.nfcauthcr.CRException;
+
+public class NfcCRdispatch {
+
+       private final String TAG = getClass().getName();
+
+       private Activity activity = null;
+       private PendingIntent tagIntent = null;
+       private byte[] challenge;
+
+       NfcCRdispatch(Activity activity) {
+               Log.v(TAG, "new NfcCRdispatch, activity=" + activity);
+               this.activity = activity;
+       }
+
+       public byte[] onNewIntent(Intent intent) {
+               Log.v(TAG, "NFC Intent arrived");
+               Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
+               if (tag == null) return null;
+               IsoDep isoTag = IsoDep.get(tag);
+               try {
+                       int slot = intent.getIntExtra("yubikey_neo_slot", -1);
+                       return YkNeo.doChalResp(isoTag, slot, challenge);
+               } catch (CRException e) {
+                       Log.v(TAG, e.getMessage());
+                       Toast.makeText(activity, e.getMessage(),
+                                       Toast.LENGTH_LONG).show();
+                       return null;
+               }
+       }
+
+       public void onResume(byte[] challenge) {
+               this.challenge = challenge;
+               Intent intent = activity.getIntent();
+               intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
+               tagIntent = PendingIntent.getActivity(activity, 0, intent, 0);
+               IntentFilter iso =
+                       new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
+               String[] techs =
+                       new String[] {IsoDep.class.getName()};
+               NfcAdapter adapter = NfcAdapter.getDefaultAdapter(activity);
+               if (adapter == null) {
+                       Toast.makeText(activity, R.string.no_nfc,
+                                               Toast.LENGTH_LONG).show();
+                       return;
+               }
+               if (!adapter.isEnabled()) {
+                       Toast.makeText(activity, R.string.nfc_disabled,
+                                               Toast.LENGTH_LONG).show();
+                       return;
+               }
+               adapter.enableForegroundDispatch(activity, tagIntent,
+                                       new IntentFilter[] {iso},
+                                       new String[][] {techs});
+       }
+
+       public void onPause() {
+               if (tagIntent != null) {
+                       tagIntent.cancel();
+                       tagIntent = null;
+               }
+               NfcAdapter adapter = NfcAdapter.getDefaultAdapter(activity);
+               if (adapter != null) {
+                       adapter.disableForegroundDispatch(activity);
+               }
+       }
+}
index 7ef69433537bae7c64cd28196f5d2b4e0f54555b..febfa63fc9914e14089915e06130c3ecba4881c3 100644 (file)
@@ -13,26 +13,35 @@ import android.nfc.tech.IsoDep;
 import android.util.Log;
 import android.widget.Toast;
 
-import org.average.nfcauthcr.YkNeo;
-import org.average.nfcauthcr.CRException;
+import org.average.nfcauthcr.NfcCRdispatch;
 
 public class TagEvent extends Activity {
 
        private final String TAG = getClass().getName();
 
-       private PendingIntent tagIntent;
+       private NfcCRdispatch dispatch = new NfcCRdispatch((Activity)this);
 
        @Override
        protected void onResume() {
                super.onResume();
                Log.v(TAG, "Starting the work");
 
+               new AlertDialog.Builder(this)
+                       .setTitle(R.string.challenging)
+                       .setMessage(R.string.swipe)
+                       .setOnCancelListener(
+                               new DialogInterface.OnCancelListener() {
+                               public void onCancel(DialogInterface dialog) {
+                                       Log.v(TAG, "Cancel");
+                                       finish();
+                               }
+                       })
+                       .create().show();
+
                Intent intent = getIntent();
                setResult(RESULT_CANCELED);
-               int slot = intent.getIntExtra("slot", -1);
-               if (slot > 0) {
-                       enableDispatch(slot);
-               }
+               byte[] challenge = intent.getByteArrayExtra("challenge");
+               dispatch.onResume(challenge);
        }
 
        @Override
@@ -40,83 +49,18 @@ public class TagEvent extends Activity {
                super.onPause();
                Log.v(TAG, "Finished the work");
 
-               disableDispatch();
+               dispatch.onPause();
        }
 
-       public void onNewIntent(Intent intent) {
+       public void onNewIntent(Intent newintent) {
                Log.v(TAG, "NFC Intent arrived");
-               int slot = intent.getIntExtra("slot", -1);
-               byte[] challenge = intent.getByteArrayExtra("challenge");
-               if (slot <= 0) return;
-               Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
-               if (tag == null) return;
-               IsoDep isoTag = IsoDep.get(tag);
-               try {
-                       byte[] response = YkNeo.doChallengeYubiKey(
-                                               isoTag, slot, challenge);
-                       Intent data = getIntent();
-                       data.putExtra("response", response);
-                       setResult(RESULT_OK, data);
-               } catch (CRException e) {
-                       Log.v(TAG, e.getMessage());
-                       Toast.makeText(this, e.getMessage(),
-                                       Toast.LENGTH_LONG).show();
-               }
-               finish();
-       }
-
-/*
-<receiver android:name=".IsoDepReceiver"
-          android:label="IsoDepReceiver">
-     <intent-filter>
-         <action android:name="android.nfc.action.TECH_DISCOVERED" />
-     </intent-filter>
-
-     <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
-         android:resource="@xml/filter_nfc"
-     />
- </receiver>
-
-<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
-     <!-- capture anything using IsoDep -->
-     <tech-list>
-         <tech>android.nfc.tech.IsoDep</tech>
-     </tech-list>
- </resources>
 
-*/
-
-       private void enableDispatch(int slot) {
-               Intent intent = getIntent();
-               intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
-               intent.putExtra("slot", slot);
-               tagIntent = PendingIntent.getActivity(this, 0, intent, 0);
-               IntentFilter iso =
-                       new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
-               NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
-               if (adapter == null) {
-                       Toast.makeText(this, R.string.no_nfc,
-                                               Toast.LENGTH_LONG).show();
-                       finish();
-               }
-               if (! adapter.isEnabled()) {
-                       Toast.makeText(this, R.string.nfc_disabled,
-                                               Toast.LENGTH_LONG).show();
+               byte[] response = dispatch.onNewIntent(newintent);
+               if (response != null) {
+                       Intent masterintent = getIntent();
+                       masterintent.putExtra("response", response);
+                       setResult(RESULT_OK, masterintent);
                        finish();
                }
-               adapter.enableForegroundDispatch(
-                       this, tagIntent, new IntentFilter[] {iso},
-                       new String[][] {new String[] {IsoDep.class.getName()}});
-       }
-
-       private void disableDispatch() {
-               if (tagIntent != null) {
-                       tagIntent.cancel();
-                       tagIntent = null;
-               }
-               NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
-               if (adapter != null) {
-                       adapter.disableForegroundDispatch(this);
-               }
        }
 }
index 6dcaf640f4d84bdca90ecf8395421d0772da4790..39c760b795ad4d887ff48d2d3e0865444ee6c1c9 100644 (file)
@@ -24,9 +24,8 @@ public class YkNeo {
        private static final byte SLOT_CHAL_HMAC1 = 0x30;
        private static final byte SLOT_CHAL_HMAC2 = 0x38;
 
-       public static final byte[] doChallengeYubiKey(IsoDep isoTag, int slot,
-                                               byte[] challenge)
-                               throws CRException {
+       public static byte[] doChalResp(IsoDep isoTag, int slot,
+                                       byte[] challenge) throws CRException {
                try {
                        isoTag.connect();
                        byte[] resp = isoTag.transceive(selectApdu);