package it.inaf.ia2.gms.controller; import it.inaf.ia2.gms.authn.RapPrincipal; import it.inaf.ia2.gms.exception.BadRequestException; import it.inaf.ia2.gms.persistence.GroupsDAO; import it.inaf.ia2.gms.persistence.MembershipsDAO; import it.inaf.ia2.gms.persistence.model.GroupEntity; import it.inaf.ia2.gms.service.JoinService; import java.io.IOException; import java.io.PrintWriter; import java.security.Principal; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Web service called by other web applications using JWT (delegation). */ @RestController @RequestMapping("/ws/jwt") public class JWTWebServiceController { @Autowired private MembershipsDAO membershipsDAO; @Autowired private JoinService joinService; @Autowired private GroupsDAO groupsDAO; /** * This endpoint is compliant with the IVOA GMS standard. */ @GetMapping(value = "/search", produces = MediaType.TEXT_PLAIN_VALUE) public void getGroups(Principal principal, HttpServletResponse response) throws IOException { List memberships = membershipsDAO.getUserMemberships(principal.getName()); // We need to return the complete group name, so it is necessary to load // all the parents too. Map idNameMap = new HashMap<>(); Set allIdentifiers = getAllIdentifiers(memberships); for (GroupEntity group : groupsDAO.findGroupsByIds(allIdentifiers)) { idNameMap.put(group.getId(), group.getName()); } try (PrintWriter pw = new PrintWriter(response.getOutputStream())) { for (GroupEntity group : memberships) { pw.println(getGroupCompleteName(group, idNameMap)); } } } /** * This endpoint is compliant with the IVOA GMS standard. Warning: for * supporting the groups of groups (with dots inside) the path variable must * be defined adding ".+", otherwise Spring will think it is a file * extension (thanks https://stackoverflow.com/a/16333149/771431) */ @GetMapping(value = "/search/{group:.+}", produces = MediaType.TEXT_PLAIN_VALUE) public void isMemberOf(@PathVariable("group") String group, Principal principal, HttpServletResponse response) throws IOException { String userId = principal.getName(); List groupNames = extractGroupNames(group); boolean isMemeber = false; String parentPath = ""; // starting from ROOT for (String groupName : groupNames) { Optional optionalGroup = groupsDAO.findGroupByParentAndName(parentPath, groupName); if (optionalGroup.isPresent()) { GroupEntity groupEntity = optionalGroup.get(); parentPath = groupEntity.getPath(); isMemeber = membershipsDAO.isMemberOf(userId, groupEntity.getId()); if (isMemeber) { break; } } else { break; } } if (isMemeber) { try (PrintWriter pw = new PrintWriter(response.getOutputStream())) { pw.println(group); } } // else: empty response (as defined by GMS standard) } private List extractGroupNames(String groupStr) { List names = new ArrayList<>(); String currentName = ""; for (int i = 0; i < groupStr.length(); i++) { char c = groupStr.charAt(i); // dot is the group separator and it must be escaped if used inside // group names if (c == '.' && groupStr.charAt(i - 1) != '\\') { names.add(currentName.replace("\\.", ".")); currentName = ""; } else { currentName += c; } } names.add(currentName); return names; } private Set getAllIdentifiers(List groups) { Set allIdentifiers = new HashSet<>(); for (GroupEntity group : groups) { if (!"".equals(group.getPath())) { String[] ids = group.getPath().split("\\."); for (String id : ids) { allIdentifiers.add(id); } } } return allIdentifiers; } private String getGroupCompleteName(GroupEntity group, Map idNameMap) { if ("ROOT".equals(group.getId())) { return group.getName(); } List names = new ArrayList<>(); for (String groupId : group.getPath().split("\\.")) { String groupName = idNameMap.get(groupId); // Dot inside names is considered a special character (because it is // used to separate the group from its parents), so we use a // backslash to escape it (client apps need to be aware of this). groupName = groupName.replace("\\.", "\\\\."); names.add(groupName); } return String.join(".", names); } @PostMapping(value = "/join", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity join(RapPrincipal principal) { String fromUser = principal.getName(); String toUser = principal.getAlternativeName(); if (toUser == null) { throw new BadRequestException("Missing alternative subject"); } joinService.join(fromUser, toUser); Map responseBody = new HashMap<>(); responseBody.put("mergedId", fromUser); return ResponseEntity.ok(responseBody); } }