Skip to content

Commit

Permalink
SDK-590: Standardise Image functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
bucky-boy committed Oct 15, 2018
1 parent 7654293 commit c34e903
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 16 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,21 @@ try {
Optional<YourAppUserClass> user = yourUserSearchMethod(activityDetails.getRememberMeId());
if (user.isPresent()) {
String rememberMeId = activityDetails.getRememberMeId();
String base64Selfie = activityDetails.getBase64Selfie();

Attribute<Image> selfie = profile.getSelfie();
Image selfieValue = selfie.getValue();
if (selfie != null) {
Image selfieValue = selfie.getValue();
String base64Selfie = selfieValue.getBase64Content();
}
Attribute<String> fullName = profile.getFullName();
Attribute<String> givenNames = profile.getGivenNames();
Attribute<String> familyName = profile.getFamilyName();
Attribute<String> phoneNumber = profile.getPhoneNumber();
Attribute<String> emailAddress = profile.getEmailAddress();
Boolean isAgeVerified = profile.isAgeVerified();
AgeVerification over18Verification = profile.findAgeOverVerification(18);
if (over18Verification != null) {
boolean isAgedOver18 = over18Verification.getResult();
}
Attribute<Date> dateOfBirth = profile.getDateOfBirth();
Attribute<String> gender = profile.getGender();
Attribute<String> postalAddress = profile.getPostalAddress();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ public interface ActivityDetails {
String getReceiptId();

/**
* JPEG selfie in Base64 string
*
* @deprecated From v2.1 onwards you should use getUserProfile().getSelfie()
* JPEG selfie in Base64 string.
*
* @return JPEG selfie image in Base64 string format
*/
@Deprecated
String getBase64Selfie();

}
9 changes: 9 additions & 0 deletions yoti-sdk-api/src/main/java/com/yoti/api/client/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*
*/
public interface Image {

/**
* Get mime type of the content.
*
Expand All @@ -18,4 +19,12 @@ public interface Image {
* @return image as byte[]
*/
byte[] getContent();

/**
* Base64 encoded image
*
* @return image as Base64 encoded String
*/
String getBase64Content();

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.yoti.api.client.spi.remote;

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

import com.yoti.api.client.Image;

/**
* Image attribute values.
*
*/
abstract class ImageAttributeValue implements Image {

Expand All @@ -24,9 +25,9 @@ abstract class ImageAttributeValue implements Image {
@Override
public abstract byte[] getContent();

protected byte[] newArray(byte[] source) {
byte[] result = new byte[source.length];
System.arraycopy(source, 0, result, 0, source.length);
return result;
@Override
public String getBase64Content() {
return String.format("data:%s;base64,%s", getMimeType(), toBase64String(getContent()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class JpegAttributeValue extends ImageAttributeValue {
private final byte[] content;

public JpegAttributeValue(byte[] content) {
this.content = newArray(content);
this.content = content;
}

@Override
Expand All @@ -21,6 +21,7 @@ public String getMimeType() {

@Override
public byte[] getContent() {
return newArray(content);
return content.clone();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class PngAttributeValue extends ImageAttributeValue {
private final byte[] content;

public PngAttributeValue(byte[] content) {
this.content = newArray(content);
this.content = content;
}

@Override
Expand All @@ -21,6 +21,7 @@ public String getMimeType() {

@Override
public byte[] getContent() {
return newArray(content);
return content.clone();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public String getReceiptId() {
}

@Override
@Deprecated
public String getBase64Selfie() {
return base64Selfie;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.yoti.api.client.spi.remote;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;

import org.junit.Test;

public class JpegAttributeValueTest {

private static final byte[] IMAGE_DATA = new byte[] { 0, 1, 2, 3 };

JpegAttributeValue testObj = new JpegAttributeValue(IMAGE_DATA);

@Test
public void shouldReturnCopyOfTheData() {
byte[] result = testObj.getContent();

assertArrayEquals(IMAGE_DATA, result);
assertNotSame(IMAGE_DATA, result);
}

@Test
public void shouldReturnBase64Selfie() {
String result = testObj.getBase64Content();

String base64Data = Base64.getEncoder().encodeToString(IMAGE_DATA);
assertEquals("data:image/jpeg;base64," + base64Data, result);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.yoti.api.client.spi.remote;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;

import org.junit.Test;

public class PngAttributeValueTest {

private static final byte[] IMAGE_DATA = new byte[] { 0, 1, 2, 3 };

PngAttributeValue testObj = new PngAttributeValue(IMAGE_DATA);

@Test
public void shouldReturnCopyOfTheData() {
byte[] result = testObj.getContent();

assertArrayEquals(IMAGE_DATA, result);
assertNotSame(IMAGE_DATA, result);
}

@Test
public void shouldReturnBase64Selfie() {
String result = testObj.getBase64Content();

String base64Data = Base64.getEncoder().encodeToString(IMAGE_DATA);
assertEquals("data:image/png;base64," + base64Data, result);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Date;

import com.yoti.api.client.Attribute;
import com.yoti.api.client.Image;
import com.yoti.api.client.Profile;

import org.bouncycastle.util.encoders.Base64;
Expand Down Expand Up @@ -81,13 +82,14 @@ public void shouldReturnReceiptId() {

@Test
public void shouldReturnBase64SelfieIfSelfieSet() {
Attribute selfie = new SimpleAttribute("selfie", new JpegAttributeValue(SOME_SELFIE_BYTES));
Attribute<Image> selfie = new SimpleAttribute("selfie", new JpegAttributeValue(SOME_SELFIE_BYTES));
SimpleProfile profile = new SimpleProfile(Collections.<Attribute<?>>singletonList(selfie));

SimpleActivityDetails result = new SimpleActivityDetails(REMEMBER_ME_ID, profile, APP_PROFILE, TIMESTAMP, RECEIPT_ID);

String expected = "data:image/jpeg;base64," + Base64.toBase64String(SOME_SELFIE_BYTES);
assertEquals(expected, result.getBase64Selfie());
assertEquals(selfie.getValue().getBase64Content(), result.getBase64Selfie());
}

@Test
Expand All @@ -99,4 +101,5 @@ public void shouldReturnBlankBase64SelfieIfSelfieNotSet() {

assertEquals("", result.getBase64Selfie());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.yoti.api.client.Attribute;
import com.yoti.api.client.DateTime;
import com.yoti.api.client.HumanProfile;
import com.yoti.api.client.Image;
import com.yoti.api.client.ProfileException;
import com.yoti.api.client.YotiClient;
import com.yoti.api.spring.YotiClientProperties;
Expand Down Expand Up @@ -66,9 +67,11 @@ public String doLogin(@RequestParam("token") final String token, final Model mod

// load activityDetails into ui model
model.addAttribute("rememberMeId", activityDetails.getRememberMeId());
model.addAttribute("base64Selfie", activityDetails.getBase64Selfie());

// load humanProfile data into ui model
Attribute<Image> selfie = humanProfile.getSelfie();
model.addAttribute("base64Selfie", selfie == null ? "" : selfie.getValue().getBase64Content());

addAttributeToModel(model, humanProfile.getFamilyName());
addAttributeToModel(model, humanProfile.getGivenNames());
addAttributeToModel(model, humanProfile.getFullName());
Expand Down

0 comments on commit c34e903

Please sign in to comment.