Skip to content

Commit

Permalink
Google Java Format
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Sep 4, 2023
1 parent 9226009 commit 05448b7
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
Expand Down Expand Up @@ -62,7 +61,7 @@ protected void doFilterInternal(
} catch (final Exception e) {
// use Spring Security logger here instead of SLF4J
logger.info("JWT not accepted: %s".formatted(e.getMessage()));

ErrorResponse errorResponse = new ErrorResponse(e.getMessage());
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.getWriter().print(JsonUtils.convertToString(errorResponse));
Expand All @@ -75,7 +74,7 @@ protected void doFilterInternal(

// use Spring Security logger here instead of SLF4J
logger.info("JWT email=%s role=%s".formatted(email, role));

User user = new User();
user.setEmail(email);
user.setAccessToken(token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ public boolean validate(String accessToken) {
try {
parseToken(accessToken);
return true;
}catch(JwtException e) {
} catch (JwtException e) {
return false;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public class WebSecurity {
@Autowired private JwtFilter jwtFilter;

@Bean
AuthenticationManager authenticationManager(AuthenticationConfiguration config)
throws Exception {
AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception {
return config.getAuthenticationManager();
}

Expand All @@ -65,47 +64,63 @@ SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http.build();
}

private AuthorizeHttpRequestsConfigurer<HttpSecurity>.AuthorizationManagerRequestMatcherRegistry buildAuth(
AuthorizeHttpRequestsConfigurer<HttpSecurity>.AuthorizationManagerRequestMatcherRegistry auth) {
AuthorizeHttpRequestsConfigurer<HttpSecurity>.AuthorizationManagerRequestMatcherRegistry reg = auth
.requestMatchers("" + WebConfiguration.CONTEXT_PATH + "/**")
.permitAll();

reg = applyAuth(
reg.requestMatchers(HttpMethod.POST, safeRequests(authFilter.getRequestPost().getRequests(), "POST")),
authFilter.getRequestPost().getAuthorities());
reg = applyAuth(
reg.requestMatchers(HttpMethod.GET, safeRequests(authFilter.getRequestGet().getRequests(), "GET")),
authFilter.getRequestGet().getAuthorities());
reg = applyAuth(
reg.requestMatchers(HttpMethod.DELETE, safeRequests(authFilter.getRequestDelete().getRequests(), "DELETE")),
authFilter.getRequestDelete().getAuthorities());
reg = applyAuth(
reg.requestMatchers(HttpMethod.PUT, safeRequests(authFilter.getRequestPut().getRequests(), "PUT")),
authFilter.getRequestPut().getAuthorities());
reg = applyAuth(
reg.requestMatchers(HttpMethod.PATCH, safeRequests(authFilter.getRequestPatch().getRequests(), "PATCH")),
authFilter.getRequestPatch().getAuthorities());

reg = reg
.anyRequest()
.permitAll();
private AuthorizeHttpRequestsConfigurer<HttpSecurity>.AuthorizationManagerRequestMatcherRegistry
buildAuth(
AuthorizeHttpRequestsConfigurer<HttpSecurity>.AuthorizationManagerRequestMatcherRegistry
auth) {
AuthorizeHttpRequestsConfigurer<HttpSecurity>.AuthorizationManagerRequestMatcherRegistry reg =
auth.requestMatchers("" + WebConfiguration.CONTEXT_PATH + "/**").permitAll();

reg =
applyAuth(
reg.requestMatchers(
HttpMethod.POST, safeRequests(authFilter.getRequestPost().getRequests(), "POST")),
authFilter.getRequestPost().getAuthorities());
reg =
applyAuth(
reg.requestMatchers(
HttpMethod.GET, safeRequests(authFilter.getRequestGet().getRequests(), "GET")),
authFilter.getRequestGet().getAuthorities());
reg =
applyAuth(
reg.requestMatchers(
HttpMethod.DELETE,
safeRequests(authFilter.getRequestDelete().getRequests(), "DELETE")),
authFilter.getRequestDelete().getAuthorities());
reg =
applyAuth(
reg.requestMatchers(
HttpMethod.PUT, safeRequests(authFilter.getRequestPut().getRequests(), "PUT")),
authFilter.getRequestPut().getAuthorities());
reg =
applyAuth(
reg.requestMatchers(
HttpMethod.PATCH,
safeRequests(authFilter.getRequestPatch().getRequests(), "PATCH")),
authFilter.getRequestPatch().getAuthorities());

reg = reg.anyRequest().permitAll();
return reg;
}

private String[] safeRequests(String[] src, String method) {
if (src == null || src.length == 0 || (src.length == 1 && src[0].isEmpty())) {
LoggerFactory.getLogger(getClass()).warn("Http {} security request patterns outdated. Fixed to a list with one String \"**\" - please update your configuration", method);
LoggerFactory.getLogger(getClass())
.warn(
"Http {} security request patterns outdated. Fixed to a list with one String \"**\" -"
+ " please update your configuration",
method);
return new String[] {"**"};
}else {
} else {
return src;
}
}

private AuthorizeHttpRequestsConfigurer<HttpSecurity>.AuthorizationManagerRequestMatcherRegistry applyAuth(AuthorizeHttpRequestsConfigurer<HttpSecurity>.AuthorizedUrl url, String[] auths) {

private AuthorizeHttpRequestsConfigurer<HttpSecurity>.AuthorizationManagerRequestMatcherRegistry
applyAuth(AuthorizeHttpRequestsConfigurer<HttpSecurity>.AuthorizedUrl url, String[] auths) {
if (auths == null || auths.length == 0 || (auths.length == 1 && auths[0].isEmpty())) {
return url.permitAll();
}else {
} else {
return url.hasAnyAuthority(auths);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@ AuthFilter authFilter() {
}
}

@Autowired
private MockMvc mvc;
@Autowired private MockMvc mvc;

@Autowired
private JwtHelper jwtHelper;
@Autowired private JwtHelper jwtHelper;

@Test
void validateJwt() {
Expand All @@ -70,53 +68,52 @@ void validateJwt() {
void getEndpoint() throws Exception {
String jwt = TestJwtCreator.generate("ROLE_IGNORED");
mvc.perform(
get(FULL_NONCONTEXT_PATH)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", "Bearer " + jwt))
get(FULL_NONCONTEXT_PATH)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", "Bearer " + jwt))
.andExpect(status().isNotFound());
}

@Test
void postEndpoint() throws Exception {
String jwt = TestJwtCreator.generate("ROLE_IGNORED");
mvc.perform(
post(FULL_NONCONTEXT_PATH)
.content("{}")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", "Bearer " + jwt))
post(FULL_NONCONTEXT_PATH)
.content("{}")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", "Bearer " + jwt))
.andExpect(status().isNotFound());
}

@Test
void deleteEndpoint() throws Exception {
String jwt = TestJwtCreator.generate("ROLE_IGNORED");
mvc.perform(
delete(FULL_NONCONTEXT_PATH)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", "Bearer " + jwt))
delete(FULL_NONCONTEXT_PATH)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", "Bearer " + jwt))
.andExpect(status().isNotFound());
}

@Test
void patchEndpoint() throws Exception {
String jwt = TestJwtCreator.generate("ROLE_IGNORED");
mvc.perform(
patch(FULL_NONCONTEXT_PATH)
.content("{}")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", "Bearer " + jwt))
patch(FULL_NONCONTEXT_PATH)
.content("{}")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", "Bearer " + jwt))
.andExpect(status().isNotFound());
}

@Test
void putEndpoint() throws Exception {
String jwt = TestJwtCreator.generate("ROLE_IGNORED");
mvc.perform(
put(FULL_NONCONTEXT_PATH)
.content("{}")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", "Bearer " + jwt))
put(FULL_NONCONTEXT_PATH)
.content("{}")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", "Bearer " + jwt))
.andExpect(status().isNotFound());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,61 +22,58 @@
class WebSecurityContextPathTest {

// ====== TEST CONTEXT-BASED SECURITY (uuid must be in header) ======

private static final String FULL_CONTEXT_PATH = WebConfiguration.CONTEXT_PATH + "/";

@Autowired
private MockMvc mvc;
@Autowired private MockMvc mvc;

@Autowired
private SecurityUUID securityUUID;
@Autowired private SecurityUUID securityUUID;

@Test
void getContextEndpoint() throws Exception {
mvc.perform(
get(FULL_CONTEXT_PATH)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", securityUUID.getAuthKey()))
get(FULL_CONTEXT_PATH)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", securityUUID.getAuthKey()))
.andExpect(status().isNotFound());
}

@Test
void postContextEndpoint() throws Exception {
mvc.perform(
post(FULL_CONTEXT_PATH)
.content("{}")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", securityUUID.getAuthKey()))
post(FULL_CONTEXT_PATH)
.content("{}")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", securityUUID.getAuthKey()))
.andExpect(status().isNotFound());
}

@Test
void deleteContextEndpoint() throws Exception {
mvc.perform(
delete(FULL_CONTEXT_PATH)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", securityUUID.getAuthKey()))
delete(FULL_CONTEXT_PATH)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", securityUUID.getAuthKey()))
.andExpect(status().isNotFound());
}

@Test
void patchContextEndpoint() throws Exception {
mvc.perform(
patch(FULL_CONTEXT_PATH)
.content("{}")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", securityUUID.getAuthKey()))
patch(FULL_CONTEXT_PATH)
.content("{}")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", securityUUID.getAuthKey()))
.andExpect(status().isNotFound());
}

@Test
void putContextEndpoint() throws Exception {
mvc.perform(
put(FULL_CONTEXT_PATH)
.content("{}")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", securityUUID.getAuthKey()))
put(FULL_CONTEXT_PATH)
.content("{}")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", securityUUID.getAuthKey()))
.andExpect(status().isNotFound());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ static void setupAll() {
System.setProperty("jwt.secret", "edge-chain-unit-test-jwt-secret");
}

@Autowired
private MockMvc mvc;
@Autowired private MockMvc mvc;

@Autowired
private JwtHelper jwtHelper;
@Autowired private JwtHelper jwtHelper;

@Test
void validateJwt() {
Expand All @@ -47,53 +45,52 @@ void validateJwt() {
void getEndpoint() throws Exception {
String jwt = TestJwtCreator.generate("ROLE_IGNORED");
mvc.perform(
get(FULL_NONCONTEXT_PATH)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", "Bearer " + jwt))
get(FULL_NONCONTEXT_PATH)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", "Bearer " + jwt))
.andExpect(status().isNotFound());
}

@Test
void postEndpoint() throws Exception {
String jwt = TestJwtCreator.generate("ROLE_IGNORED");
mvc.perform(
post(FULL_NONCONTEXT_PATH)
.content("{}")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", "Bearer " + jwt))
post(FULL_NONCONTEXT_PATH)
.content("{}")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", "Bearer " + jwt))
.andExpect(status().isNotFound());
}

@Test
void deleteEndpoint() throws Exception {
String jwt = TestJwtCreator.generate("ROLE_IGNORED");
mvc.perform(
delete(FULL_NONCONTEXT_PATH)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", "Bearer " + jwt))
delete(FULL_NONCONTEXT_PATH)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", "Bearer " + jwt))
.andExpect(status().isNotFound());
}

@Test
void patchEndpoint() throws Exception {
String jwt = TestJwtCreator.generate("ROLE_IGNORED");
mvc.perform(
patch(FULL_NONCONTEXT_PATH)
.content("{}")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", "Bearer " + jwt))
patch(FULL_NONCONTEXT_PATH)
.content("{}")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", "Bearer " + jwt))
.andExpect(status().isNotFound());
}

@Test
void putEndpoint() throws Exception {
String jwt = TestJwtCreator.generate("ROLE_IGNORED");
mvc.perform(
put(FULL_NONCONTEXT_PATH)
.content("{}")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", "Bearer " + jwt))
put(FULL_NONCONTEXT_PATH)
.content("{}")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", "Bearer " + jwt))
.andExpect(status().isNotFound());
}

}
Loading

0 comments on commit 05448b7

Please sign in to comment.