-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apple-appattest: add AppleAppAttestStatement
- Loading branch information
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
...core/src/main/java/com/webauthn4j/data/attestation/statement/AppleAppAttestStatement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
.../src/test/java/com/webauthn4j/data/attestation/statement/AppleAppAttestStatementTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
} |