Skip to content

Commit d6918cd

Browse files
committed
more error events
1 parent 7f04fcf commit d6918cd

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

core/src/main/java/io/snabble/sdk/Events.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,5 +294,21 @@ public EventType getEventType() {
294294
}
295295
}
296296

297+
public static void logErrorEvent(String projectId, String format, Object... args) {
298+
Logger.d(format, args);
299+
300+
// since we have no error logging without a project, we try to find the project by id
301+
// and if no project is found we just use the first project to at least log it to something
302+
Project project = Snabble.getInstance().getProjectById(projectId);
303+
if (project == null) {
304+
List<Project> projects = Snabble.getInstance().getProjects();
305+
if (projects != null && projects.size() > 0) {
306+
project = projects.get(0);
307+
}
308+
}
297309

310+
if (project != null) {
311+
project.logErrorEvent(format, args);
312+
}
313+
}
298314
}

core/src/main/java/io/snabble/sdk/payment/PaymentCredentials.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import javax.crypto.spec.OAEPParameterSpec;
2323
import javax.crypto.spec.PSource;
2424

25+
import io.snabble.sdk.Events;
2526
import io.snabble.sdk.PaymentMethod;
2627
import io.snabble.sdk.R;
2728
import io.snabble.sdk.Snabble;
@@ -567,11 +568,13 @@ public boolean validate() {
567568
if (type == Type.CREDIT_CARD_PSD2) {
568569
Date date = new Date(validTo);
569570
if (date.getTime() < System.currentTimeMillis()) {
571+
Events.logErrorEvent(null, "removing payment credentials: expired");
570572
return false;
571573
}
572574
}
573575

574576
if (type == Type.CREDIT_CARD) {
577+
Events.logErrorEvent(null, "removing payment credentials: old credit card type");
575578
return false;
576579
}
577580

@@ -582,6 +585,7 @@ public boolean validate() {
582585
}
583586
}
584587

588+
Events.logErrorEvent(null, "removing payment credentials: different gateway certificate signature");
585589
return false;
586590
}
587591

core/src/main/java/io/snabble/sdk/payment/PaymentCredentialsStore.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.concurrent.CopyOnWriteArrayList;
1616

1717
import io.snabble.sdk.Environment;
18+
import io.snabble.sdk.Events;
1819
import io.snabble.sdk.PaymentMethod;
1920
import io.snabble.sdk.Project;
2021
import io.snabble.sdk.Snabble;
@@ -180,8 +181,7 @@ public void removeInvalidCredentials() {
180181
Snabble snabble = Snabble.getInstance();
181182
if (snabble.getProjects().size() > 0) {
182183
if (removals.size() > 0) {
183-
snabble.getProjects().get(0).getEvents()
184-
.log("Deleted payment credentials because device is not secure anymore. Lost access to %d payment credentials", removals.size());
184+
Events.logErrorEvent(null, "Deleted payment credentials because device is not secure anymore. Lost access to %d payment credentials", removals.size());
185185
}
186186
}
187187
}

utils/src/main/java/io/snabble/sdk/utils/security/KeyStoreCipherMarshmallow.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import androidx.annotation.RequiresApi;
1010

11+
import java.security.Key;
1112
import java.security.KeyStore;
1213
import java.security.KeyStoreException;
1314
import java.util.Date;
@@ -48,8 +49,6 @@ public class KeyStoreCipherMarshmallow extends KeyStoreCipher {
4849
private boolean createKeys() {
4950
try {
5051
if (!isKeyAccessible()) {
51-
keyStore.deleteEntry(alias);
52-
5352
KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, ANDROID_KEY_STORE);
5453
KeyGenParameterSpec.Builder spec = new KeyGenParameterSpec.Builder(alias,
5554
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT);
@@ -87,13 +86,16 @@ private boolean isKeyAccessible() {
8786
try {
8887
Cipher c = Cipher.getInstance(AES_MODE);
8988
IvParameterSpec ivParameterSpec = new IvParameterSpec(FIXED_IV);
90-
c.init(Cipher.ENCRYPT_MODE, keyStore.getKey(alias, null), ivParameterSpec);
89+
Key key = keyStore.getKey(alias, null);
90+
c.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec);
9191
return true;
9292
} catch (KeyPermanentlyInvalidatedException e) {
93+
Logger.d("KeyPermanentlyInvalidatedException: " + e.getMessage());
9394
return false;
9495
} catch (UserNotAuthenticatedException e) {
9596
return true;
9697
} catch (Exception e) {
98+
Logger.d(e.getClass().getName() + ": " + e.getMessage());
9799
return false;
98100
}
99101
}

0 commit comments

Comments
 (0)