@Test public void should_include_header_csrf_header() throws Exception { String rootUrl = mWebMockRule.getRootUrl(); MockResponse cookieResponse = MockResponseFactory.create302() .addHeader("Location", rootUrl + LOCATION_SUCCESS) .addHeader("Set-Cookie", "cookie=12"); mWebMockRule.enqueue(cookieResponse); apiUnderTest.springAuth("joeuser", "joeuser", null, null); String xdm = rootUrl.substring(0, rootUrl.length() - 1); RecordedRequest response = mWebMockRule.get().takeRequest(); assertThat(response, containsHeader("x-jasper-xdm", xdm)); }
public void springAuth(@NotNull final String username, @NotNull final String password, final String organization, final Map<String, String> params) throws HttpException, IOException { FormEncodingBuilder formBody = new FormEncodingBuilder() .add("j_password", password) .add("j_username", username); if (organization != null) { formBody.add("orgId", organization); } if (params != null) { Set<Map.Entry<String, String>> entrySet = params.entrySet(); for (Map.Entry<String, String> entry : entrySet) { formBody.add(entry.getKey(), entry.getValue()); } } HttpUrl baseUrl = mNetworkClient.getBaseUrl(); HttpUrl url = baseUrl.resolve("j_spring_security_check"); String scheme = baseUrl.scheme(); String host = baseUrl.host(); int port = baseUrl.port(); String xdm = scheme + ": Request request = new Request.Builder() .url(url) .addHeader("x-jasper-xdm", xdm) .post(formBody.build()) .build(); Response response = mNetworkClient.makeCall(request); int statusCode = response.code(); if (statusCode >= 300 && statusCode < 400) { String location = response.headers().get("Location"); boolean error = location.contains("error"); if (error) { com.squareup.okhttp.Response response401 = new com.squareup.okhttp.Response.Builder() .protocol(response.protocol()) .request(response.request()) .headers(response.headers()) .body(response.body()) .code(401) .build(); throw HttpException.httpError(response401); } } else { throw HttpException.httpError(response); } }
AuthRestApi { public void springAuth(@NotNull final String username, @NotNull final String password, final String organization, final Map<String, String> params) throws HttpException, IOException { FormEncodingBuilder formBody = new FormEncodingBuilder() .add("j_password", password) .add("j_username", username); if (organization != null) { formBody.add("orgId", organization); } if (params != null) { Set<Map.Entry<String, String>> entrySet = params.entrySet(); for (Map.Entry<String, String> entry : entrySet) { formBody.add(entry.getKey(), entry.getValue()); } } HttpUrl baseUrl = mNetworkClient.getBaseUrl(); HttpUrl url = baseUrl.resolve("j_spring_security_check"); String scheme = baseUrl.scheme(); String host = baseUrl.host(); int port = baseUrl.port(); String xdm = scheme + ": Request request = new Request.Builder() .url(url) .addHeader("x-jasper-xdm", xdm) .post(formBody.build()) .build(); Response response = mNetworkClient.makeCall(request); int statusCode = response.code(); if (statusCode >= 300 && statusCode < 400) { String location = response.headers().get("Location"); boolean error = location.contains("error"); if (error) { com.squareup.okhttp.Response response401 = new com.squareup.okhttp.Response.Builder() .protocol(response.protocol()) .request(response.request()) .headers(response.headers()) .body(response.body()) .code(401) .build(); throw HttpException.httpError(response401); } } else { throw HttpException.httpError(response); } } }
AuthRestApi { public void springAuth(@NotNull final String username, @NotNull final String password, final String organization, final Map<String, String> params) throws HttpException, IOException { FormEncodingBuilder formBody = new FormEncodingBuilder() .add("j_password", password) .add("j_username", username); if (organization != null) { formBody.add("orgId", organization); } if (params != null) { Set<Map.Entry<String, String>> entrySet = params.entrySet(); for (Map.Entry<String, String> entry : entrySet) { formBody.add(entry.getKey(), entry.getValue()); } } HttpUrl baseUrl = mNetworkClient.getBaseUrl(); HttpUrl url = baseUrl.resolve("j_spring_security_check"); String scheme = baseUrl.scheme(); String host = baseUrl.host(); int port = baseUrl.port(); String xdm = scheme + ": Request request = new Request.Builder() .url(url) .addHeader("x-jasper-xdm", xdm) .post(formBody.build()) .build(); Response response = mNetworkClient.makeCall(request); int statusCode = response.code(); if (statusCode >= 300 && statusCode < 400) { String location = response.headers().get("Location"); boolean error = location.contains("error"); if (error) { com.squareup.okhttp.Response response401 = new com.squareup.okhttp.Response.Builder() .protocol(response.protocol()) .request(response.request()) .headers(response.headers()) .body(response.body()) .code(401) .build(); throw HttpException.httpError(response401); } } else { throw HttpException.httpError(response); } } AuthRestApi(@NotNull NetworkClient client); }
AuthRestApi { public void springAuth(@NotNull final String username, @NotNull final String password, final String organization, final Map<String, String> params) throws HttpException, IOException { FormEncodingBuilder formBody = new FormEncodingBuilder() .add("j_password", password) .add("j_username", username); if (organization != null) { formBody.add("orgId", organization); } if (params != null) { Set<Map.Entry<String, String>> entrySet = params.entrySet(); for (Map.Entry<String, String> entry : entrySet) { formBody.add(entry.getKey(), entry.getValue()); } } HttpUrl baseUrl = mNetworkClient.getBaseUrl(); HttpUrl url = baseUrl.resolve("j_spring_security_check"); String scheme = baseUrl.scheme(); String host = baseUrl.host(); int port = baseUrl.port(); String xdm = scheme + ": Request request = new Request.Builder() .url(url) .addHeader("x-jasper-xdm", xdm) .post(formBody.build()) .build(); Response response = mNetworkClient.makeCall(request); int statusCode = response.code(); if (statusCode >= 300 && statusCode < 400) { String location = response.headers().get("Location"); boolean error = location.contains("error"); if (error) { com.squareup.okhttp.Response response401 = new com.squareup.okhttp.Response.Builder() .protocol(response.protocol()) .request(response.request()) .headers(response.headers()) .body(response.body()) .code(401) .build(); throw HttpException.httpError(response401); } } else { throw HttpException.httpError(response); } } AuthRestApi(@NotNull NetworkClient client); void springAuth(@NotNull final String username,
                           @NotNull final String password,
                           final String organization,
                           final Map<String, String> params); @NotNull EncryptionKey requestEncryptionMetadata(); }
AuthRestApi { public void springAuth(@NotNull final String username, @NotNull final String password, final String organization, final Map<String, String> params) throws HttpException, IOException { FormEncodingBuilder formBody = new FormEncodingBuilder() .add("j_password", password) .add("j_username", username); if (organization != null) { formBody.add("orgId", organization); } if (params != null) { Set<Map.Entry<String, String>> entrySet = params.entrySet(); for (Map.Entry<String, String> entry : entrySet) { formBody.add(entry.getKey(), entry.getValue()); } } HttpUrl baseUrl = mNetworkClient.getBaseUrl(); HttpUrl url = baseUrl.resolve("j_spring_security_check"); String scheme = baseUrl.scheme(); String host = baseUrl.host(); int port = baseUrl.port(); String xdm = scheme + ": Request request = new Request.Builder() .url(url) .addHeader("x-jasper-xdm", xdm) .post(formBody.build()) .build(); Response response = mNetworkClient.makeCall(request); int statusCode = response.code(); if (statusCode >= 300 && statusCode < 400) { String location = response.headers().get("Location"); boolean error = location.contains("error"); if (error) { com.squareup.okhttp.Response response401 = new com.squareup.okhttp.Response.Builder() .protocol(response.protocol()) .request(response.request()) .headers(response.headers()) .body(response.body()) .code(401) .build(); throw HttpException.httpError(response401); } } else { throw HttpException.httpError(response); } } AuthRestApi(@NotNull NetworkClient client); void springAuth(@NotNull final String username,
                           @NotNull final String password,
                           final String organization,
                           final Map<String, String> params); @NotNull EncryptionKey requestEncryptionMetadata();  }