Code now builds
							parent
							
								
									324a079a90
								
							
						
					
					
						commit
						6b47387920
					
				| 
						 | 
				
			
			@ -18,23 +18,26 @@ import android.widget.Toast;
 | 
			
		|||
import com.google.protobuf.InvalidProtocolBufferException;
 | 
			
		||||
import com.google.zxing.integration.android.IntentIntegrator;
 | 
			
		||||
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.protobuf.PollingStation;
 | 
			
		||||
 | 
			
		||||
import java.net.MalformedURLException;
 | 
			
		||||
import java.net.URI;
 | 
			
		||||
import java.net.URL;
 | 
			
		||||
import java.net.URLDecoder;
 | 
			
		||||
import java.math.BigInteger;
 | 
			
		||||
import java.util.Date;
 | 
			
		||||
 | 
			
		||||
import static com.meerkat.laura.fakescannerapp.R.xml.preferences;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 | 
			
		||||
    final public static String SCANNER_NAME = "AndroidScanner";
 | 
			
		||||
    SharedPreferences sharedPref;
 | 
			
		||||
 | 
			
		||||
    Button scanBtn;
 | 
			
		||||
    TextView formatTxt, contentTxt, responseTxt;
 | 
			
		||||
    ScannerClientAPI scannerClientAPI;
 | 
			
		||||
    PollingStationScanner.ScannerClient scannerClient;
 | 
			
		||||
    DigitalSignatureGenerator signer;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean onCreateOptionsMenu(Menu menu) {
 | 
			
		||||
| 
						 | 
				
			
			@ -58,7 +61,8 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
 | 
			
		|||
 | 
			
		||||
    void setNewScannerClientAPI(SharedPreferences sharedPref) {
 | 
			
		||||
        String pscUrlString = sharedPref.getString(SettingsActivity.PSC_URL, "");
 | 
			
		||||
        scannerClientAPI = new ScannerClientAPI(pscUrlString);
 | 
			
		||||
 | 
			
		||||
        scannerClient = new ScannerClientAPI(signer);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
| 
						 | 
				
			
			@ -80,6 +84,11 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
 | 
			
		|||
        contentTxt = (TextView)findViewById(R.id.scan_content);
 | 
			
		||||
        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);
 | 
			
		||||
        scanBtn.setOnClickListener(this);
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -113,13 +122,13 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
 | 
			
		|||
public void sendPost(String scanContent) {
 | 
			
		||||
 | 
			
		||||
        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()
 | 
			
		||||
    //                .setChannel(ByteString.copyFrom(body))
 | 
			
		||||
    //                .build();
 | 
			
		||||
 | 
			
		||||
            if (scannerClientAPI.sendScan(scannedData)) {
 | 
			
		||||
            if (scannerClient.newScan(scannedBallot)) {
 | 
			
		||||
                Log.i("MainActivity", "post submitted to API.");
 | 
			
		||||
            } else {
 | 
			
		||||
                Log.e("MainActivity", "Unable to submit post to API.");
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -27,11 +27,6 @@ ext { isSnapshot = false }
 | 
			
		|||
 | 
			
		||||
ext {
 | 
			
		||||
        groupId = 'org.factcenter.meerkat'
 | 
			
		||||
        nexusRepository = "https://cs.idc.ac.il/nexus/content/groups/${isSnapshot ? 'unstable' : 'public'}/"
 | 
			
		||||
        
 | 
			
		||||
        // Credentials for IDC nexus repositories (needed only for using unstable repositories and publishing)
 | 
			
		||||
        // Should be set in ${HOME}/.gradle/gradle.properties
 | 
			
		||||
 | 
			
		||||
    // Credentials for publishing repositories
 | 
			
		||||
    publishRepository = "https://cs.idc.ac.il/nexus/content/repositories/${project.isSnapshot ? 'snapshots' : 'releases'}"
 | 
			
		||||
    publishUser =  project.hasProperty('publishUser') ? project.property('publishUser') : ""
 | 
			
		||||
| 
						 | 
				
			
			@ -230,3 +225,4 @@ publishing {
 | 
			
		|||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,84 +1,49 @@
 | 
			
		|||
package meerkat.voting.gui;
 | 
			
		||||
 | 
			
		||||
import com.google.common.util.concurrent.FutureCallback;
 | 
			
		||||
import meerkat.crypto.DigitalSignature;
 | 
			
		||||
import meerkat.crypto.concrete.ECDSADeterministicSignature;
 | 
			
		||||
import meerkat.pollingstation.PollingStationScanner;
 | 
			
		||||
import meerkat.pollingstation.PollingStationWebScanner;
 | 
			
		||||
import meerkat.protobuf.PollingStation;
 | 
			
		||||
 | 
			
		||||
import java.util.concurrent.Semaphore;
 | 
			
		||||
import org.slf4j.Logger;
 | 
			
		||||
import org.slf4j.LoggerFactory;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Created by Laura on 2/1/2017.
 | 
			
		||||
 */
 | 
			
		||||
public class PollingStationServerToyRun {
 | 
			
		||||
    final Logger logger = LoggerFactory.getLogger(getClass());
 | 
			
		||||
    private static final String SCAN_CONTEXT_PATH = "/scan";
 | 
			
		||||
 | 
			
		||||
    private PollingStationScanner.PollingStationServer scanner;
 | 
			
		||||
    private static final String ADDRESS = "http://localhost";
 | 
			
		||||
    private static final String SUB_ADDRESS = "";
 | 
			
		||||
    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();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    private PollingStationScanner.PollingStationServer scannerServer;
 | 
			
		||||
    PollingStation.ConnectionServerData serverData;
 | 
			
		||||
    DigitalSignature verifier;
 | 
			
		||||
 | 
			
		||||
    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);
 | 
			
		||||
        thrown = null;
 | 
			
		||||
        scannerServer = new PollingStationWebScanner(0, SCAN_CONTEXT_PATH);
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            scanner.start();
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            System.out.println("Could not start server: " + e.getMessage());
 | 
			
		||||
        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 {
 | 
			
		||||
            serverData = scannerServer.start(false);
 | 
			
		||||
            logger.info("Scanner server started at {}", serverData.getServerUrl());
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            logger.error("Could not start server: " + e.getMessage());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void main(String [] args) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -120,16 +120,16 @@ public class FXWindowOutputDevice extends AsyncRunnableOutputDevice {
 | 
			
		|||
                    plaintextSerialNumber + ", ciphertext# = " + encryptedSerialNumber);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        ScannedData scannedData = ScannedData.newBuilder()
 | 
			
		||||
        PollingStation.ScannedBallot scannedBallot = PollingStation.ScannedBallot.newBuilder()
 | 
			
		||||
                .setChannel(command.getChannelIdentifierByteString())
 | 
			
		||||
                .setSignedEncryptedBallot(command.getSignedEncryptedBallot())
 | 
			
		||||
                .build();
 | 
			
		||||
 | 
			
		||||
        System.out.println(scannedData.getSerializedSize());
 | 
			
		||||
        encryptedText = Base64.getEncoder().encodeToString(scannedData.toByteArray());
 | 
			
		||||
        System.out.println(scannedBallot.getSerializedSize());
 | 
			
		||||
        encryptedText = Base64.getEncoder().encodeToString(scannedBallot.toByteArray());
 | 
			
		||||
 | 
			
		||||
        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(command.getChannelIdentifierByteString());
 | 
			
		||||
        } catch (InvalidProtocolBufferException e) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue