Skip to content

Commit

Permalink
apple-appattest: add AppleAppAttestStatement
Browse files Browse the repository at this point in the history
  • Loading branch information
veehaitch committed Aug 17, 2020
1 parent 3220129 commit d9a9b14
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.webauthn4j.data.attestation.statement;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.webauthn4j.util.ArrayUtil;
import com.webauthn4j.validator.exception.ConstraintViolationException;

import java.util.Arrays;
import java.util.Objects;

@JsonIgnoreProperties(value = "format")
@JsonTypeName(AppleAppAttestStatement.FORMAT)
public class AppleAppAttestStatement implements CertificateBaseAttestationStatement {
public static final String FORMAT = "apple-appattest";

@JsonProperty
private final AttestationCertificatePath x5c;

@JsonProperty
private final byte[] receipt;

public AppleAppAttestStatement(
@JsonProperty("x5c") AttestationCertificatePath x5c,
@JsonProperty("receipt") byte[] receipt) {
this.x5c = x5c;
this.receipt = receipt;
}

public byte[] getReceipt() {
return ArrayUtil.clone(receipt);
}

@Override
public AttestationCertificatePath getX5c() {
return x5c;
}

@Override
public String getFormat() {
return FORMAT;
}

@Override
public void validate() {
if (x5c == null) {
throw new ConstraintViolationException("x5c must not be null");
}
if (receipt == null) {
throw new ConstraintViolationException("receipt must not be null");
}
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AppleAppAttestStatement that = (AppleAppAttestStatement) o;
return x5c.equals(that.x5c) &&
Arrays.equals(receipt, that.receipt);
}

@Override
public int hashCode() {
int result = Objects.hash(x5c);
result = 31 * result + Arrays.hashCode(receipt);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.webauthn4j.data.attestation.statement;

import com.webauthn4j.test.TestDataUtil;
import com.webauthn4j.validator.RegistrationObject;
import com.webauthn4j.validator.exception.ConstraintViolationException;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class AppleAppAttestStatementTest {

@Test
void validate_test() {
new AppleAppAttestStatement(new AttestationCertificatePath(), new byte[32]).validate();
assertAll(
() -> {
AppleAppAttestStatement appleAppAttestStatement = new AppleAppAttestStatement(null, new byte[32]);
assertThrows(ConstraintViolationException.class, appleAppAttestStatement::validate);
},
() -> {
AppleAppAttestStatement appleAppAttestStatement = new AppleAppAttestStatement(new AttestationCertificatePath(), null);
assertThrows(ConstraintViolationException.class, appleAppAttestStatement::validate);
}
);
}

@Test
void equals_hashCode_test() {
RegistrationObject registrationObjectA = TestDataUtil.createRegistrationObjectWithAppleAppAttestAttestation();
AppleAppAttestStatement instanceA = (AppleAppAttestStatement) registrationObjectA.getAttestationObject().getAttestationStatement();
RegistrationObject registrationObjectB = TestDataUtil.createRegistrationObjectWithAppleAppAttestAttestation();
AppleAppAttestStatement instanceB = (AppleAppAttestStatement) registrationObjectA.getAttestationObject().getAttestationStatement();

assertAll(
() -> assertThat(instanceA).isEqualTo(instanceB),
() -> assertThat(instanceA).hasSameHashCodeAs(instanceB)
);
}
}

0 comments on commit d9a9b14

Please sign in to comment.