Code now builds

android-scanner
Tal Moran 2017-06-25 18:05:52 +03:00
parent 324a079a90
commit 6b47387920
4 changed files with 53 additions and 83 deletions

View File

@ -18,23 +18,26 @@ import android.widget.Toast;
import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.InvalidProtocolBufferException;
import com.google.zxing.integration.android.IntentIntegrator; import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult; import com.google.zxing.integration.android.IntentResult;
import meerkat.crypto.DigitalSignatureGenerator;
import meerkat.crypto.concrete.ECDSADeterministicSignature;
import meerkat.pollingstation.PollingStationScanner;
import meerkat.pollingstation.ScannerClientAPI; import meerkat.pollingstation.ScannerClientAPI;
import meerkat.protobuf.PollingStation; import meerkat.protobuf.PollingStation;
import java.net.MalformedURLException; import java.math.BigInteger;
import java.net.URI; import java.util.Date;
import java.net.URL;
import java.net.URLDecoder;
import static com.meerkat.laura.fakescannerapp.R.xml.preferences; import static com.meerkat.laura.fakescannerapp.R.xml.preferences;
public class MainActivity extends AppCompatActivity implements View.OnClickListener { public class MainActivity extends AppCompatActivity implements View.OnClickListener {
final public static String SCANNER_NAME = "AndroidScanner";
SharedPreferences sharedPref; SharedPreferences sharedPref;
Button scanBtn; Button scanBtn;
TextView formatTxt, contentTxt, responseTxt; TextView formatTxt, contentTxt, responseTxt;
ScannerClientAPI scannerClientAPI; PollingStationScanner.ScannerClient scannerClient;
DigitalSignatureGenerator signer;
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
@ -58,7 +61,8 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
void setNewScannerClientAPI(SharedPreferences sharedPref) { void setNewScannerClientAPI(SharedPreferences sharedPref) {
String pscUrlString = sharedPref.getString(SettingsActivity.PSC_URL, ""); String pscUrlString = sharedPref.getString(SettingsActivity.PSC_URL, "");
scannerClientAPI = new ScannerClientAPI(pscUrlString);
scannerClient = new ScannerClientAPI(signer);
} }
@Override @Override
@ -80,6 +84,11 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
contentTxt = (TextView)findViewById(R.id.scan_content); contentTxt = (TextView)findViewById(R.id.scan_content);
responseTxt = (TextView)findViewById(R.id.server_response); responseTxt = (TextView)findViewById(R.id.server_response);
signer = new ECDSADeterministicSignature();
signer.generateSigningCertificate(BigInteger.ONE, new Date(System.currentTimeMillis()),
new Date(System.currentTimeMillis() + 1000*60*60*24*365), SCANNER_NAME);
setNewScannerClientAPI(sharedPref); setNewScannerClientAPI(sharedPref);
scanBtn.setOnClickListener(this); scanBtn.setOnClickListener(this);
} }
@ -113,13 +122,13 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
public void sendPost(String scanContent) { public void sendPost(String scanContent) {
try { try {
PollingStation.ScannedData scannedData = PollingStation.ScannedData.parseFrom(Base64.decode(scanContent.getBytes(), Base64.DEFAULT)); PollingStation.ScannedBallot scannedBallot = PollingStation.ScannedBallot.parseFrom(Base64.decode(scanContent.getBytes(), Base64.DEFAULT));
// //
// PollingStation.ScannedData scannedData = PollingStation.ScannedData.newBuilder() // PollingStation.ScannedData scannedData = PollingStation.ScannedData.newBuilder()
// .setChannel(ByteString.copyFrom(body)) // .setChannel(ByteString.copyFrom(body))
// .build(); // .build();
if (scannerClientAPI.sendScan(scannedData)) { if (scannerClient.newScan(scannedBallot)) {
Log.i("MainActivity", "post submitted to API."); Log.i("MainActivity", "post submitted to API.");
} else { } else {
Log.e("MainActivity", "Unable to submit post to API."); Log.e("MainActivity", "Unable to submit post to API.");

View File

@ -27,15 +27,10 @@ ext { isSnapshot = false }
ext { ext {
groupId = 'org.factcenter.meerkat' groupId = 'org.factcenter.meerkat'
nexusRepository = "https://cs.idc.ac.il/nexus/content/groups/${isSnapshot ? 'unstable' : 'public'}/" // Credentials for publishing repositories
publishRepository = "https://cs.idc.ac.il/nexus/content/repositories/${project.isSnapshot ? 'snapshots' : 'releases'}"
// Credentials for IDC nexus repositories (needed only for using unstable repositories and publishing) publishUser = project.hasProperty('publishUser') ? project.property('publishUser') : ""
// Should be set in ${HOME}/.gradle/gradle.properties publishPassword = project.hasProperty('publishPassword') ? project.property('publishPassword') : ""
// Credentials for publishing repositories
publishRepository = "https://cs.idc.ac.il/nexus/content/repositories/${project.isSnapshot ? 'snapshots' : 'releases'}"
publishUser = project.hasProperty('publishUser') ? project.property('publishUser') : ""
publishPassword = project.hasProperty('publishPassword') ? project.property('publishPassword') : ""
} }
description = "Common files for polling-station scanner client API" description = "Common files for polling-station scanner client API"
@ -218,8 +213,8 @@ publishing {
repositories { repositories {
maven { maven {
url publishRepository url publishRepository
credentials { username credentials { username
password password
username publishUser username publishUser
password publishPassword password publishPassword
@ -230,3 +225,4 @@ publishing {

View File

@ -1,84 +1,49 @@
package meerkat.voting.gui; package meerkat.voting.gui;
import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.FutureCallback;
import meerkat.crypto.DigitalSignature;
import meerkat.crypto.concrete.ECDSADeterministicSignature;
import meerkat.pollingstation.PollingStationScanner; import meerkat.pollingstation.PollingStationScanner;
import meerkat.pollingstation.PollingStationWebScanner; import meerkat.pollingstation.PollingStationWebScanner;
import meerkat.protobuf.PollingStation; import meerkat.protobuf.PollingStation;
import org.slf4j.Logger;
import java.util.concurrent.Semaphore; import org.slf4j.LoggerFactory;
/** /**
* Created by Laura on 2/1/2017. * Created by Laura on 2/1/2017.
*/ */
public class PollingStationServerToyRun { public class PollingStationServerToyRun {
final Logger logger = LoggerFactory.getLogger(getClass());
private static final String SCAN_CONTEXT_PATH = "/scan";
private PollingStationScanner.PollingStationServer scanner; private PollingStationScanner.PollingStationServer scannerServer;
private static final String ADDRESS = "http://localhost"; PollingStation.ConnectionServerData serverData;
private static final String SUB_ADDRESS = ""; DigitalSignature verifier;
private static final int PORT = 8080;
private Semaphore semaphore;
private Throwable thrown;
private boolean dataIsAsExpected;
private class ScanHandler implements FutureCallback<PollingStation.ScannedData> {
private final PollingStation.ScannedData expectedData;
public ScanHandler(PollingStation.ScannedData expectedData) {
this.expectedData = expectedData;
}
@Override
public void onSuccess(PollingStation.ScannedData result) {
dataIsAsExpected = result.getChannel().equals(expectedData.getChannel());
semaphore.release();
}
@Override
public void onFailure(Throwable t) {
dataIsAsExpected = false;
thrown = t;
semaphore.release();
}
}
private class ErrorHandler implements FutureCallback<PollingStation.ScannedData> {
private final String expectedErrorMessage;
public ErrorHandler(String expectedErrorMessage) {
this.expectedErrorMessage = expectedErrorMessage;
}
@Override
public void onSuccess(PollingStation.ScannedData result) {
dataIsAsExpected = false;
semaphore.release();
}
@Override
public void onFailure(Throwable t) {
dataIsAsExpected = t.getMessage().equals(expectedErrorMessage);
semaphore.release();
}
}
public void init() { public void init() {
System.err.println("Setting up Scanner WebApp!"); verifier = new ECDSADeterministicSignature();
scanner = new PollingStationWebScanner(PORT, SUB_ADDRESS); logger.debug("Setting up Scanner WebApp!");
semaphore = new Semaphore(0); scannerServer = new PollingStationWebScanner(0, SCAN_CONTEXT_PATH);
thrown = null;
scannerServer.subscribe(new FutureCallback<PollingStation.ScannedData>() {
@Override
public void onSuccess(PollingStation.ScannedData result) {
logger.info("Received scan #{}", result.getSerial());
}
@Override
public void onFailure(Throwable t) { }
});
try { try {
scanner.start(); serverData = scannerServer.start(false);
logger.info("Scanner server started at {}", serverData.getServerUrl());
} catch (Exception e) { } catch (Exception e) {
System.out.println("Could not start server: " + e.getMessage()); logger.error("Could not start server: " + e.getMessage());
} }
} }
public static void main(String [] args) { public static void main(String [] args) {

View File

@ -120,16 +120,16 @@ public class FXWindowOutputDevice extends AsyncRunnableOutputDevice {
plaintextSerialNumber + ", ciphertext# = " + encryptedSerialNumber); plaintextSerialNumber + ", ciphertext# = " + encryptedSerialNumber);
} }
ScannedData scannedData = ScannedData.newBuilder() PollingStation.ScannedBallot scannedBallot = PollingStation.ScannedBallot.newBuilder()
.setChannel(command.getChannelIdentifierByteString()) .setChannel(command.getChannelIdentifierByteString())
.setSignedEncryptedBallot(command.getSignedEncryptedBallot()) .setSignedEncryptedBallot(command.getSignedEncryptedBallot())
.build(); .build();
System.out.println(scannedData.getSerializedSize()); System.out.println(scannedBallot.getSerializedSize());
encryptedText = Base64.getEncoder().encodeToString(scannedData.toByteArray()); encryptedText = Base64.getEncoder().encodeToString(scannedBallot.toByteArray());
try { try {
ScannedData result = ScannedData.parseFrom(Base64.getDecoder().decode(encryptedText.getBytes())); PollingStation.ScannedBallot result = PollingStation.ScannedBallot.parseFrom(Base64.getDecoder().decode(encryptedText.getBytes()));
System.out.println(result.getChannel()); System.out.println(result.getChannel());
System.out.println(command.getChannelIdentifierByteString()); System.out.println(command.getChannelIdentifierByteString());
} catch (InvalidProtocolBufferException e) { } catch (InvalidProtocolBufferException e) {