Skip to content

Commit

Permalink
Add specific version for lombok and improved logging of Java Springbo…
Browse files Browse the repository at this point in the history
…ot Example (#371)

* Add specific version for lombok

* Added better logging
  • Loading branch information
CharlesShuller committed Oct 20, 2023
1 parent 82be405 commit 08aeafe
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
Cargo.lock
__pycache__/
*~
15 changes: 15 additions & 0 deletions examples/java-springboot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.20</version>
</dependency>

<dependency>
Expand All @@ -89,6 +90,20 @@
<scope>system</scope>
<systemPath>${basedir}/didkit.jar</systemPath>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.21.0</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.21.0</version>
</dependency>


</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.slf4j.Logger;

@Configuration
public class WebConfig {

@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@
import java.util.Map;
import java.util.UUID;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


@RestController
@AllArgsConstructor
public class CredentialOfferController {
private final UserService userService;
private final StringRedisTemplate redisTemplate;
private static Logger logger = LogManager.getLogger();

private static final DateTimeFormatter dateFormat = DateTimeFormatter.ISO_INSTANT.withZone(ZoneId.from(ZoneOffset.UTC));

Expand All @@ -40,6 +45,7 @@ public CredentialOffer credentialOfferGet(
final Resource keyFile;
final String key;
final String issuer;
logger.info("GET /credential-offer/" + token);

try {
keyFile = new FileSystemResource(Resources.key);
Expand Down Expand Up @@ -93,6 +99,7 @@ public Map<String, Object> credentialOfferPost(
@PathVariable("token") String token,
@RequestParam("subject_id") String did
) throws Exception {
logger.info("/credential-offer/" + token);
final String username = redisTemplate.opsForValue().get(token);
final User user = (User) userService.loadUserByUsername(username);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


@RestController
@AllArgsConstructor
public class VerifiablePresentationRequestController {
private final UserService userService;
private final StringRedisTemplate redisTemplate;
private static Logger logger = LogManager.getLogger();

@Autowired
private final ConcurrentHashMap<String, WebSocketSession> sessionMap;
Expand All @@ -43,6 +47,7 @@ public class VerifiablePresentationRequestController {
public VerifiablePresentationRequest vpRequestGet(
@PathVariable("challenge") String challenge
) {
logger.info("GET /verifiable-presentation-request/" + challenge);
return new VerifiablePresentationRequest(
"VerifiablePresentationRequest",
Collections.singletonList(new VerifiablePresentationRequestQuery(
Expand All @@ -66,35 +71,46 @@ public void vpRequestPost(
@PathVariable("challenge") String challenge,
@RequestParam("presentation") String presentation
) throws Exception {
logger.info("POST /verifiable-presentation-request/" + challenge);
final Resource keyFile;
final String key;

logger.info("Attempting to load key");
try {
keyFile = new FileSystemResource(Resources.key);
key = Files.readString(keyFile.getFile().toPath());
} catch (Exception e) {
logger.error("POST verifiable-presentation-request failed to load key");
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to load key");
}

logger.info("VerifiablePresentation.verifyPresentation");
final Map<String, Object> vc = VerifiablePresentation.verifyPresentation(key, presentation, challenge);
final Map<String, Object> credentialSubject = (Map<String, Object>) vc.get("credentialSubject");

final String username = credentialSubject.get("alumniOf").toString();
logger.info("userService.loadUserByUsername");
final User user = (User) userService.loadUserByUsername(username);

final String uuid = UUID.randomUUID().toString();
redisTemplate.opsForValue().set(uuid, user.getUsername());
redisTemplate.expire(uuid, Duration.ofSeconds(90));

if (sessionMap.containsKey(challenge)) {
logger.info("SessionMap has a challenge");
try {
logger.info("Trying to send message");
sessionMap.get(challenge).sendMessage(new TextMessage(uuid));
} catch (Exception e) {
logger.error("POST Failed to return sign in token");
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to return sign in token");
}
sessionMap.remove(challenge);
} else {
logger.info("SessionMap does not have a challenge");
logger.error("POST invalid or expired token");
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid or expired token");
}
logger.info("Success");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


@RestController
public class VersionController {
private static Logger logger = LogManager.getLogger();

@RequestMapping("/version")
public String version() {
logger.info("GET /version");
return DIDKit.getVersion();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.time.Duration;
import java.util.UUID;

Expand All @@ -22,20 +25,24 @@
public class UserController {
private final UserService userService;
private final StringRedisTemplate redisTemplate;
private static Logger logger = LogManager.getLogger();

@GetMapping("/sign-up")
String signUpGet(User user) {
logger.info("GET /sign-up");
return "sign-up";
}

@PostMapping("/sign-up")
String signUpPost(User user) {
logger.info("POST /sign-up");
userService.signUp(user);
return "redirect:/sign-in";
}

@GetMapping("/sign-in")
ModelAndView signIn() throws Exception {
logger.info("GET /sign-in");
final String uuid = UUID.randomUUID().toString();
final String url = "https://" + Resources.baseUrl + "/verifiable-presentation-request/" + uuid;
final ModelAndView model = QRCode.getModelAndView("sign-in", url);
Expand All @@ -47,6 +54,7 @@ ModelAndView signIn() throws Exception {
ModelAndView credential(
@AuthenticationPrincipal User user
) throws Exception {
logger.info("GET /credential");
final String uuid = UUID.randomUUID().toString();
redisTemplate.opsForValue().set(uuid, user.getUsername());
redisTemplate.expire(uuid, Duration.ofSeconds(90));
Expand All @@ -60,6 +68,7 @@ ModelAndView credentialOffer(
@RequestParam("did") String did,
ModelAndView model
) throws Exception {
logger.info("POST /credential");
final String credential = userService.issueCredential(did, user);
model.addObject("credential", credential);
return model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,40 @@
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;

import java.util.AbstractList;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


public class VerifiablePresentation {
private static Logger logger = LogManager.getLogger();

public static Map<String, Object> verifyPresentation(
final String key,
final String presentation,
final String challenge
) throws Exception {
logger.info("Converting String VP into Map<String, Object>");
logger.info("VP: " + presentation);
final ObjectMapper mapper = new ObjectMapper();

final Map<String, Object> presentationMap = mapper.readValue(presentation, new TypeReference<>() {
});
final Map<String, Object> presentationMap =
mapper.readValue(presentation, new TypeReference<>() {});

return VerifiablePresentation.verifyPresentation(key, presentationMap, challenge);
return VerifiablePresentation
.verifyPresentation(key, presentationMap, challenge);
}

public static Map<String, Object> verifyPresentation(
final String key,
final Map<String, Object> presentation,
final String challenge
) {
logger.info("Attempting to verify Map presentation");

final ObjectMapper mapper = new ObjectMapper();

try {
Expand All @@ -40,20 +52,29 @@ public static Map<String, Object> verifyPresentation(
final String vpStr = mapper.writeValueAsString(presentation);
final String optionsStr = mapper.writeValueAsString(options);

logger.info("vpStr: " + vpStr);
logger.info("optionsStr: " + optionsStr);


final String result = DIDKit.verifyPresentation(vpStr, optionsStr);
final Map<String, Object> resultMap = mapper.readValue(result, new TypeReference<>() {
});
logger.info("DIDKit.verifyPresentation result: " + result);
final Map<String, Object> resultMap =
mapper.readValue(result, new TypeReference<>() { });

if (((List<String>) resultMap.get("errors")).size() > 0) {
System.out.println("[ERROR] VP: " + resultMap.get("errors"));
logger.error("VP: " + resultMap.get("errors"));
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid presentation");
}
} catch (Exception e) {
logger.error("Failed to verify presentation: " + e.toString());
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to verify presentation");
}

//Select the first vc if we have multiple in the presentation
final Object vcs = presentation.get("verifiableCredential");
final Map<String, Object> vc = (Map<String, Object>) (vcs instanceof Object[] ? ((Object[]) vcs)[0] : vcs);
logger.info("vcs type: " + vcs.getClass());
//final Map<String, Object> vc = (Map<String, Object>) (vcs instanceof Object[] ? ((Object[]) vcs)[0] : vcs);
final Map<String, Object> vc = getFirstVc(vcs);

try {
final DIDKitOptions options = new DIDKitOptions(
Expand All @@ -79,4 +100,22 @@ public static Map<String, Object> verifyPresentation(

return vc;
}

private static Map<String, Object> getFirstVc(Object vcs) {

if(vcs instanceof Object[]) {
Object r = ((Object[]) vcs)[0];
logger.info("r type: " + r.getClass());
return (Map<String, Object>) r;
}
else if(vcs instanceof AbstractList) {
Object r = ((AbstractList) vcs).get(0);
logger.info("r type: " + r.getClass());
return (Map<String, Object>) r;
}
else {
logger.info("vc type: " + vcs.getClass());
return (Map<String, Object>) vcs;
}
}
}

0 comments on commit 08aeafe

Please sign in to comment.