@Test void testBuildJwt() throws Exception { String keyId = TestKeys.HOLDER_KEY_ID; AlgorithmProvider.Type type = AlgorithmProvider.Type.ES256; Algorithm algorithm = AlgorithmProvider.create(type); PrivateKey priKey = algorithm.byteToPrivateKey(EncodeType.HEX.decode(TestKeys.PRIVATE_KEY_ES256)); String rawCredential = "eyJhbGciOiJFUzI1NiIsImtpZCI6ImRpZDppY29uOjAwMDA5NjFiNmNkNjQyNTNmYjI4YzliMGQzZDIyNGJlNWY5YjE4ZDQ5ZjAxZGEzOTBmMDgjaXNzdWVyIn0.eyJjbGFpbSI6eyJFbWFpbCI6ImFhYUBpY29uLmZvdW5kYXRpb24ifSwiZXhwIjoxNTQ3ODIwMzg2LCJpYXQiOjE1NDc3MzM5ODYsImlzcyI6ImRpZDppY29uOjAwMDA5NjFiNmNkNjQyNTNmYjI4YzliMGQzZDIyNGJlNWY5YjE4ZDQ5ZjAxZGEzOTBmMDgiLCJub25jZSI6IjAzMDcxZjJkLThiZTAtNDAxNS04Y2I4LTg4YTI3M2FiZWY2NiIsInN1YiI6ImRpZDppY29uOjAwMDA5NjFiNmNkNjQyNTNmYjI4YzliMGQzZDIyNGJlNWY5YjE4ZDQ5ZjAxZGEzOTBmMDgiLCJ0eXBlIjpbIkVtYWlsIl19"; String did = "did:icon:0000961b6cd64253fb28c9b0d3d224be5f9b18d49f01da390f08"; String version = "1.0"; DidKeyHolder keyHolder = new DidKeyHolder.Builder() .did(did) .keyId(keyId) .type(type) .privateKey(priKey) .build(); Presentation presentation = new Presentation.Builder() .didKeyHolder(keyHolder) .build(); presentation.addCredential(rawCredential); presentation.setNonce("6a41b8a4-d18d-4e31-b5f8-f39b15db5827"); presentation.setVersion(version); long issuedTimestamp = 1545208842000L; Date issued = new Date(issuedTimestamp); long duration = presentation.getIssuerDid().getDuration() * 1000L; Date expiration = new Date(issuedTimestamp + duration); Jwt didJwt = presentation.buildJwt(issued, expiration); String jwt = keyHolder.sign(didJwt); String expectedJwt = "eyJhbGciOiJFUz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wt.split("\\.")).stream().limit(2).collect(Collectors.toList())); Assertions.assertEquals(expectedJwt, actualJwt); }
@Override public Jwt buildJwt(Date issued, Date expiration) { String kid = issuerDid.getDid() + "#" + issuerDid.getKeyId(); return new Jwt.Builder() .alg(issuerDid.getAlgorithm()) .kid(kid) .iss(issuerDid.getDid()) .iat(issued) .exp(expiration) .type(getTypes()) .credential(credentials) .nonce(nonce) .jti(jti) .version(version) .build(); }
Presentation implements ConvertJwt { @Override public Jwt buildJwt(Date issued, Date expiration) { String kid = issuerDid.getDid() + "#" + issuerDid.getKeyId(); return new Jwt.Builder() .alg(issuerDid.getAlgorithm()) .kid(kid) .iss(issuerDid.getDid()) .iat(issued) .exp(expiration) .type(getTypes()) .credential(credentials) .nonce(nonce) .jti(jti) .version(version) .build(); } }
Presentation implements ConvertJwt { @Override public Jwt buildJwt(Date issued, Date expiration) { String kid = issuerDid.getDid() + "#" + issuerDid.getKeyId(); return new Jwt.Builder() .alg(issuerDid.getAlgorithm()) .kid(kid) .iss(issuerDid.getDid()) .iat(issued) .exp(expiration) .type(getTypes()) .credential(credentials) .nonce(nonce) .jti(jti) .version(version) .build(); } Presentation(IssuerDid issuerDid); }
Presentation implements ConvertJwt { @Override public Jwt buildJwt(Date issued, Date expiration) { String kid = issuerDid.getDid() + "#" + issuerDid.getKeyId(); return new Jwt.Builder() .alg(issuerDid.getAlgorithm()) .kid(kid) .iss(issuerDid.getDid()) .iat(issued) .exp(expiration) .type(getTypes()) .credential(credentials) .nonce(nonce) .jti(jti) .version(version) .build(); } Presentation(IssuerDid issuerDid); void addCredential(String claim); void setCredentials(List<String> credentials); void setNonce(String nonce); void setJti(String jti); void setVersion(String version); String getDid(); String getKeyId(); String getAlgorithm(); List<String> getTypes(); String getNonce(); String getJti(); String getVersion(); @Override int getDuration(); @Override Jwt buildJwt(Date issued, Date expiration); static Presentation valueOf(String encodedJwt); static Presentation valueOf(Jwt jwt); IssuerDid getIssuerDid(); List<String> getCredentials(); @Override String toString(); }
Presentation implements ConvertJwt { @Override public Jwt buildJwt(Date issued, Date expiration) { String kid = issuerDid.getDid() + "#" + issuerDid.getKeyId(); return new Jwt.Builder() .alg(issuerDid.getAlgorithm()) .kid(kid) .iss(issuerDid.getDid()) .iat(issued) .exp(expiration) .type(getTypes()) .credential(credentials) .nonce(nonce) .jti(jti) .version(version) .build(); } Presentation(IssuerDid issuerDid); void addCredential(String claim); void setCredentials(List<String> credentials); void setNonce(String nonce); void setJti(String jti); void setVersion(String version); String getDid(); String getKeyId(); String getAlgorithm(); List<String> getTypes(); String getNonce(); String getJti(); String getVersion(); @Override int getDuration(); @Override Jwt buildJwt(Date issued, Date expiration); static Presentation valueOf(String encodedJwt); static Presentation valueOf(Jwt jwt); IssuerDid getIssuerDid(); List<String> getCredentials(); @Override String toString(); static final String DEFAULT_TYPE; }