|
| 1 | +package com.appliedrec.credentials.app; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.SharedPreferences; |
| 5 | +import android.net.Uri; |
| 6 | +import android.os.Build; |
| 7 | +import android.util.Base64; |
| 8 | + |
| 9 | +import androidx.core.util.Pair; |
| 10 | +import androidx.preference.PreferenceManager; |
| 11 | + |
| 12 | +import com.google.gson.Gson; |
| 13 | +import com.google.gson.stream.JsonReader; |
| 14 | + |
| 15 | +import java.io.ByteArrayInputStream; |
| 16 | +import java.io.ByteArrayOutputStream; |
| 17 | +import java.io.InputStream; |
| 18 | +import java.io.InputStreamReader; |
| 19 | +import java.io.OutputStream; |
| 20 | +import java.net.MalformedURLException; |
| 21 | +import java.net.URL; |
| 22 | +import java.nio.charset.StandardCharsets; |
| 23 | +import java.util.List; |
| 24 | +import java.util.UUID; |
| 25 | + |
| 26 | +import javax.net.ssl.HttpsURLConnection; |
| 27 | + |
| 28 | +import io.reactivex.Completable; |
| 29 | +import io.reactivex.Observable; |
| 30 | +import io.reactivex.schedulers.Schedulers; |
| 31 | + |
| 32 | +public class IntellicheckBarcodeVerifier { |
| 33 | + |
| 34 | + private String url; |
| 35 | + private SharedPreferences sharedPreferences; |
| 36 | + private String password; |
| 37 | + private String appId; |
| 38 | + |
| 39 | + public IntellicheckBarcodeVerifier(Context context, String password) { |
| 40 | + this.url = BuildConfig.INTELLICHECK_URL; |
| 41 | + this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); |
| 42 | + this.password = password; |
| 43 | + this.appId = context.getApplicationContext().getPackageName(); |
| 44 | + } |
| 45 | + |
| 46 | + private String getDeviceId() { |
| 47 | + String key = "device_id"; |
| 48 | + String deviceId = sharedPreferences.getString(key, UUID.randomUUID().toString()); |
| 49 | + if (!sharedPreferences.contains(key)) { |
| 50 | + sharedPreferences.edit().putString(key, deviceId).apply(); |
| 51 | + } |
| 52 | + return deviceId; |
| 53 | + } |
| 54 | + |
| 55 | + public Completable testPassword() { |
| 56 | + return Completable.create(emitter -> { |
| 57 | + try { |
| 58 | + Uri uri = Uri.parse(url); |
| 59 | + List<String> pathSegments = uri.getPathSegments(); |
| 60 | + Uri.Builder builder = new Uri.Builder() |
| 61 | + .scheme(uri.getScheme()) |
| 62 | + .authority(uri.getAuthority()); |
| 63 | + for (int i=0; i<pathSegments.size()-1; i++) { |
| 64 | + builder.appendPath(pathSegments.get(i)); |
| 65 | + } |
| 66 | + builder.appendPath("check-password"); |
| 67 | + uri = builder.build(); |
| 68 | + HttpsURLConnection connection = (HttpsURLConnection) new URL(uri.toString()).openConnection(); |
| 69 | + connection.setRequestMethod("POST"); |
| 70 | + connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); |
| 71 | + connection.setDoOutput(true); |
| 72 | + connection.setDoInput(true); |
| 73 | + byte[] body = new Uri.Builder() |
| 74 | + .appendQueryParameter("device_id", getDeviceId()) |
| 75 | + .appendQueryParameter("app_id", appId) |
| 76 | + .appendQueryParameter("password", password).build().getQuery().getBytes(StandardCharsets.UTF_8); |
| 77 | + OutputStream outputStream = connection.getOutputStream(); |
| 78 | + ByteArrayInputStream inputStream = new ByteArrayInputStream(body); |
| 79 | + int read; |
| 80 | + byte[] buffer = new byte[512]; |
| 81 | + while ((read = inputStream.read(buffer, 0, buffer.length)) > 0) { |
| 82 | + outputStream.write(buffer, 0, read); |
| 83 | + } |
| 84 | + outputStream.close(); |
| 85 | + if (connection.getResponseCode() == 200) { |
| 86 | + emitter.onComplete(); |
| 87 | + } else { |
| 88 | + StringBuilder stringBuilder = new StringBuilder(); |
| 89 | + while ((read = connection.getErrorStream().read(buffer, 0, buffer.length)) > 0) { |
| 90 | + stringBuilder.append(new String(buffer, 0, read, StandardCharsets.UTF_8)); |
| 91 | + } |
| 92 | + String response = stringBuilder.toString(); |
| 93 | + if (!response.isEmpty()) { |
| 94 | + throw new Exception(response); |
| 95 | + } else { |
| 96 | + throw new Exception("Unknown error"); |
| 97 | + } |
| 98 | + } |
| 99 | + } catch (Exception e) { |
| 100 | + emitter.onError(e); |
| 101 | + } |
| 102 | + }).subscribeOn(Schedulers.io()); |
| 103 | + } |
| 104 | + |
| 105 | + public Observable<Pair<String,String>> parseBarcode(String barcode) { |
| 106 | + Observable<Pair<String,String>> observable = Observable.create(emitter -> { |
| 107 | + try { |
| 108 | + URL url = new URL(this.url); |
| 109 | + byte[] barcodeData = barcode.getBytes(StandardCharsets.UTF_8); |
| 110 | + HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); |
| 111 | + connection.setRequestMethod("POST"); |
| 112 | + connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); |
| 113 | + connection.setDoInput(true); |
| 114 | + connection.setDoOutput(true); |
| 115 | + String body = new Uri.Builder() |
| 116 | + .appendQueryParameter("device_os", "Android") |
| 117 | + .appendQueryParameter("device_id", getDeviceId()) |
| 118 | + .appendQueryParameter("device_name", Build.MODEL) |
| 119 | + .appendQueryParameter("password", password) |
| 120 | + .appendQueryParameter("app_id", appId) |
| 121 | + .appendQueryParameter("data", Base64.encodeToString(barcodeData, Base64.NO_WRAP)) |
| 122 | + .build() |
| 123 | + .getQuery(); |
| 124 | + ByteArrayInputStream inputStream = new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8)); |
| 125 | + OutputStream outputStream = connection.getOutputStream(); |
| 126 | + int read; |
| 127 | + byte[] buffer = new byte[512]; |
| 128 | + while ((read = inputStream.read(buffer, 0, buffer.length)) > 0) { |
| 129 | + outputStream.write(buffer, 0, read); |
| 130 | + } |
| 131 | + outputStream.close(); |
| 132 | + if (connection.getResponseCode() >= 400) { |
| 133 | + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| 134 | + while ((read = connection.getErrorStream().read(buffer, 0, buffer.length)) > 0) { |
| 135 | + byteArrayOutputStream.write(buffer, 0, read); |
| 136 | + } |
| 137 | + String response = new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8); |
| 138 | + throw new Exception(response); |
| 139 | + } |
| 140 | + // TODO: Finish this |
| 141 | + JsonReader jsonReader = new JsonReader(new InputStreamReader(connection.getInputStream())); |
| 142 | + jsonReader.setLenient(true); |
| 143 | + jsonReader.beginObject(); |
| 144 | + while (jsonReader.hasNext()) { |
| 145 | + if ("document_t".equals(jsonReader.nextName())) { |
| 146 | + jsonReader.beginObject(); |
| 147 | + while (jsonReader.hasNext()) { |
| 148 | + String name = jsonReader.nextName(); |
| 149 | + if ("$".equals(name)) { |
| 150 | + jsonReader.skipValue(); |
| 151 | + } else if ("testCard".equals(name)) { |
| 152 | + emitter.onNext(new Pair<>(name, jsonReader.nextBoolean() ? "Yes" : "No")); |
| 153 | + } else { |
| 154 | + try { |
| 155 | + String value = jsonReader.nextString(); |
| 156 | + if (value != null && !value.trim().isEmpty()) { |
| 157 | + emitter.onNext(new Pair<>(name, value)); |
| 158 | + } |
| 159 | + } catch (Exception e) { |
| 160 | + e.printStackTrace(); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + jsonReader.endObject(); |
| 165 | + } |
| 166 | + } |
| 167 | + jsonReader.endObject(); |
| 168 | + emitter.onComplete(); |
| 169 | + } catch (Exception e) { |
| 170 | + emitter.onError(e); |
| 171 | + } |
| 172 | + }); |
| 173 | + return observable.subscribeOn(Schedulers.io()); |
| 174 | + } |
| 175 | +} |
0 commit comments