Skip to content

Commit

Permalink
Merge pull request #437 from getyoti/SDK-2431_null-check-optinal-rece…
Browse files Browse the repository at this point in the history
…ipt-properties

SDK-2431: Drop null check for optional receipt properties
  • Loading branch information
irotech authored Mar 14, 2024
2 parents d7ed454 + 7263b4b commit 5356075
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 52 deletions.
22 changes: 11 additions & 11 deletions yoti-sdk-api/src/main/java/com/yoti/api/client/ActivityDetails.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.yoti.api.client;

import static com.yoti.api.client.spi.remote.util.Validation.notNull;
import static com.yoti.validation.Validation.notNull;

import static org.bouncycastle.util.encoders.Base64.toBase64String;

Expand All @@ -13,26 +13,26 @@ public final class ActivityDetails {

private final String rememberMeId;
private final String parentRememberMeId;
private final ApplicationProfile applicationProfile;
private final HumanProfile userProfile;
private final ApplicationProfile thisPartyProfile;
private final HumanProfile otherPartyProfile;
private final Date timestamp;
private final String receiptId;
private final ExtraData extraData;

public ActivityDetails(String rememberMeId,
String parentRememberMeId,
HumanProfile userProfile,
ApplicationProfile applicationProfile,
HumanProfile otherPartyProfile,
ApplicationProfile thisPartyProfile,
ExtraData extraData,
Date timestamp,
byte[] receiptId) {
this.rememberMeId = notNull(rememberMeId, "Remember Me id");
this.rememberMeId = rememberMeId;
this.parentRememberMeId = parentRememberMeId;
this.userProfile = notNull(userProfile, "User profile");
this.applicationProfile = notNull(applicationProfile, "Application profile");
this.otherPartyProfile = notNull(otherPartyProfile, "User profile");
this.thisPartyProfile = notNull(thisPartyProfile, "Application profile");
this.timestamp = notNull(timestamp, "Timestamp");
this.receiptId = toBase64String(notNull(receiptId, "Receipt id"));
this.extraData = notNull(extraData, "extraData");
this.extraData = extraData;
}

/**
Expand All @@ -41,7 +41,7 @@ public ActivityDetails(String rememberMeId,
* @return profile containing attributes for the user
*/
public HumanProfile getUserProfile() {
return userProfile;
return otherPartyProfile;
}

/**
Expand All @@ -50,7 +50,7 @@ public HumanProfile getUserProfile() {
* @return profile containing attributes for the application
*/
public ApplicationProfile getApplicationProfile() {
return applicationProfile;
return thisPartyProfile;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static com.yoti.api.client.spi.remote.call.YotiConstants.DEFAULT_CHARSET;
import static com.yoti.api.client.spi.remote.call.YotiConstants.RFC3339_PATTERN;
import static com.yoti.api.client.spi.remote.call.YotiConstants.SYMMETRIC_CIPHER;
import static com.yoti.api.client.spi.remote.util.Validation.notNull;
import static com.yoti.validation.Validation.notNull;

import java.io.UnsupportedEncodingException;
import java.security.Key;
Expand Down Expand Up @@ -53,19 +53,27 @@ public ActivityDetails create(Receipt receipt, PrivateKey privateKey) throws Pro
byte[] decryptedKey = DecryptionHelper.decryptAsymmetric(receipt.getWrappedReceiptKey(), privateKey);
Key secretKey = new SecretKeySpec(decryptedKey, SYMMETRIC_CIPHER);

List<Attribute<?>> userProfileAttr = attributeListReader.read(receipt.getOtherPartyProfile(), secretKey);
List<Attribute<?>> applicationProfileAttr = attributeListReader.read(receipt.getProfile(), secretKey);
List<Attribute<?>> otherPartyAttr = attributeListReader.read(receipt.getOtherPartyProfile(), secretKey);
List<Attribute<?>> thisPartyAttr = attributeListReader.read(receipt.getProfile(), secretKey);

HumanProfile userProfile = new HumanProfile(userProfileAttr);
ApplicationProfile applicationProfile = new ApplicationProfile(applicationProfileAttr);
HumanProfile otherPartyProfile = new HumanProfile(otherPartyAttr);
ApplicationProfile thisPartyProfile = new ApplicationProfile(thisPartyAttr);

ExtraData extraData = parseExtraData(receipt.getExtraData(), secretKey);

String rememberMeId = parseRememberMeId(receipt.getRememberMeId());
String parentRememberMeId = parseRememberMeId(receipt.getParentRememberMeId());
Date timestamp = parseTimestamp(receipt.getTimestamp());

return new ActivityDetails(rememberMeId, parentRememberMeId, userProfile, applicationProfile, extraData, timestamp, receipt.getReceiptId());
return new ActivityDetails(
rememberMeId,
parentRememberMeId,
otherPartyProfile,
thisPartyProfile,
extraData,
timestamp,
receipt.getReceiptId()
);
}

private ExtraData parseExtraData(byte[] extraDataBytes, Key secretKey) throws ProfileException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import java.util.Date;

import org.bouncycastle.util.encoders.Base64;
import org.junit.Test;
import org.mockito.Mockito;
import org.junit.*;
import org.mockito.*;

public class ActivityDetailsTest {

Expand All @@ -18,22 +18,6 @@ public class ActivityDetailsTest {
private static final byte[] RECEIPT_ID = { 1, 2, 3, 4, 5, 6, 7, 8 };
private static final String RECEIPT_ID_STRING = Base64.toBase64String(RECEIPT_ID);
private static final Date TIMESTAMP = new Date();
private static final byte[] SOME_SELFIE_BYTES = "selfieTestVal".getBytes();

@Test(expected = IllegalArgumentException.class)
public void shouldFailConstructionForNullRememberMeId() {
new ActivityDetails(null, null, USER_PROFILE, APP_PROFILE, EXTRA_DATA, TIMESTAMP, RECEIPT_ID);
}

@Test(expected = IllegalArgumentException.class)
public void shouldFailConstructionForNullUserProfile() {
new ActivityDetails(REMEMBER_ME, null, null, APP_PROFILE, EXTRA_DATA, TIMESTAMP, RECEIPT_ID);
}

@Test(expected = IllegalArgumentException.class)
public void shouldFailConstructionForNullAppProfile() {
new ActivityDetails(REMEMBER_ME, null, USER_PROFILE, null, EXTRA_DATA, TIMESTAMP, RECEIPT_ID);
}

@Test(expected = IllegalArgumentException.class)
public void shouldFailConstructionForNullTimestamp() {
Expand All @@ -45,11 +29,6 @@ public void shouldFailConstructionForNullReceiptId() {
new ActivityDetails(REMEMBER_ME, null, USER_PROFILE, APP_PROFILE, EXTRA_DATA, TIMESTAMP, null);
}

@Test(expected = IllegalArgumentException.class)
public void shouldFailConstructionForNullProfile() {
new ActivityDetails(REMEMBER_ME, null, null, APP_PROFILE, EXTRA_DATA, TIMESTAMP, RECEIPT_ID);
}

@Test
public void shouldReturnUserId() {
ActivityDetails s = new ActivityDetails(REMEMBER_ME, PARENT_REMEMBER_ME, USER_PROFILE, APP_PROFILE, EXTRA_DATA, TIMESTAMP, RECEIPT_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

import java.security.GeneralSecurityException;
import java.security.Key;
Expand All @@ -31,17 +30,14 @@

import com.yoti.api.client.ActivityDetails;
import com.yoti.api.client.ExtraData;
import com.yoti.api.client.Profile;
import com.yoti.api.client.ProfileException;
import com.yoti.api.client.spi.remote.call.Receipt;
import com.yoti.api.client.spi.remote.util.CryptoUtil;

import org.junit.Before;
import org.junit.Test;
import org.junit.*;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.*;
import org.mockito.junit.*;

@RunWith(MockitoJUnitRunner.class)
public class ActivityDetailsFactoryTest {
Expand All @@ -68,8 +64,6 @@ public class ActivityDetailsFactoryTest {

KeyPair keyPair;
byte[] validReceiptKey;
@Mock Profile profileMock;
@Mock Profile otherProfileMock;
@Mock ExtraData extraDataMock;

@Before
Expand Down Expand Up @@ -235,4 +229,4 @@ public void shouldReThrowProfileExceptionFromExtraDataConverter() throws Excepti

fail("Expected an exception");
}
}
}

0 comments on commit 5356075

Please sign in to comment.