Commit d6ee290f authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Join implementation changes

parent 9cbdd322
Loading
Loading
Loading
Loading
+1 −9
Original line number Diff line number Diff line
@@ -93,13 +93,6 @@ public class CLI implements CommandLineRunner {
                client.removePermission(getNames(args, 1, args.length - 2), args[args.length - 1]);
                System.out.println("Permission removed");
                break;
            case "prepare-join":
                if (args.length != 3) {
                    displayUsage();
                }
                client.prepareToJoin(args[1], args[2]);
                System.out.println("Join prepared");
                break;
            default:
                displayUsage();
                break;
@@ -113,8 +106,7 @@ public class CLI implements CommandLineRunner {
                + "    add-member <name1 name2 name3> <user_id>\n"
                + "    remove-member <name1 name2 name3> <user_id>\n"
                + "    add-permission <name1 name2 name3> <user_id> <permission>\n"
                + "    delete-permission <name1 name2 name3> <user_id>\n"
                + "    prepare-join <from_user_id> <to_user_id>");
                + "    delete-permission <name1 name2 name3> <user_id>");
        System.exit(0);
    }

+0 −14
Original line number Diff line number Diff line
@@ -116,20 +116,6 @@ public class GmsClient {
        restTemplate.exchange(url, HttpMethod.DELETE, getEntity(), Void.class);
    }

    public void prepareToJoin(String fromUserId, String toUserId) {

        String url = UriComponentsBuilder.fromHttpUrl(baseUrl)
                .pathSegment("prepare-join")
                .toUriString();

        Map<String, Object> params = new HashMap<>();
        params.put("fromUserId", fromUserId);
        params.put("toUserId", toUserId);
        HttpEntity<Map<String, Object>> httpEntity = getEntity(params);

        restTemplate.exchange(url, HttpMethod.POST, httpEntity, Void.class);
    }

    private HttpEntity<?> getEntity() {
        return new HttpEntity<>(getHeaders());
    }
+0 −22
Original line number Diff line number Diff line
@@ -150,28 +150,6 @@ public class GmsClientTest {
        verifyAuthHeaders(entity);
    }

    @Test
    public void testPrepareToJoin() {

        String fromUserId = "from_user_id";
        String toUserId = "to_user_id";

        client.prepareToJoin(fromUserId, toUserId);

        ArgumentCaptor<HttpEntity> entityCaptor = ArgumentCaptor.forClass(HttpEntity.class);
        verify(restTemplate, times(1)).exchange(eq(BASE_URL + "/ws/prepare-join"),
                eq(HttpMethod.POST), entityCaptor.capture(), eq(Void.class));

        HttpEntity<?> entity = entityCaptor.getValue();
        verifyAuthHeaders(entity);

        Map<String, Object> expectedBody = new HashMap<>();
        expectedBody.put("fromUserId", fromUserId);
        expectedBody.put("toUserId", toUserId);

        verifyBody(entity, expectedBody);
    }

    private void verifyAuthHeaders(HttpEntity<?> entity) {//
        String authHeader = entity.getHeaders().getFirst("Authorization");
        assertEquals("Basic dGVzdDp0ZXN0", authHeader);
+4 −0
Original line number Diff line number Diff line
@@ -2,8 +2,12 @@ package it.inaf.ia2.gms;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
@Configuration
@EnableTransactionManagement
public class GmsApplication {

    public static void main(String[] args) {
+6 −8
Original line number Diff line number Diff line
@@ -11,7 +11,6 @@ import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.token.store.jwk.JwkTokenStore;

@@ -45,29 +44,28 @@ public class JWTFilter implements Filter {

        Map<String, Object> claims = accessToken.getAdditionalInformation();

        String principal = (String) claims.get("sub");
        if (principal == null) {
        if (claims.get("sub") == null) {
            response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid access token: missing sub claim");
            return;
        }

        ServletRequest wrappedRequest = new ServletRequestWithJWTPrincipal(request, principal);
        ServletRequest wrappedRequest = new ServletRequestWithJWTPrincipal(request, claims);

        fc.doFilter(wrappedRequest, res);
    }

    private static class ServletRequestWithJWTPrincipal extends HttpServletRequestWrapper {

        private final String principal;
        private final Principal principal;

        public ServletRequestWithJWTPrincipal(HttpServletRequest request, String principal) {
        public ServletRequestWithJWTPrincipal(HttpServletRequest request, Map<String, Object> jwtClaims) {
            super(request);
            this.principal = principal;
            this.principal = new RapPrincipal(jwtClaims);
        }

        @Override
        public Principal getUserPrincipal() {
            return new UsernamePasswordAuthenticationToken(principal, null);
            return principal;
        }
    }
}
Loading