Skip to content

Commit 3af6519

Browse files
author
Ariel Guelfi
committed
Add JSON login back; remove submodule cloning being necessary, as we now use the git reference to material-table
1 parent be9a395 commit 3af6519

6 files changed

Lines changed: 63 additions & 53 deletions

File tree

README.md

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,6 @@ Installed below dependencies:
4949
- Java 17
5050
- NodeJS 18
5151

52-
# Cloning
53-
54-
This project uses the [material-table](https://github.com/databucket/material-table.git) project as a Git Submodule.
55-
In order to be able to build the frontend, the submodule is also built.
56-
Clone the project with the extra `--recurse-submodules` argument in order to also have the material-table code.
57-
58-
```shell
59-
git clone --recurse-submodules https://github.com/databucket/databucket-server.git
60-
```
61-
62-
If you need to update the material-table submodule, you can run:
63-
```shell
64-
git submodule update --recursive --remote
65-
```
66-
6752
## Useful commands
6853

6954
| command | what it does |
Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
package pl.databucket.server.controller;
22

33
import io.swagger.annotations.Api;
4-
import java.util.Arrays;
5-
import java.util.Collections;
64
import javax.mail.MessagingException;
75
import lombok.AccessLevel;
86
import lombok.RequiredArgsConstructor;
97
import lombok.experimental.FieldDefaults;
10-
import org.slf4j.Logger;
11-
import org.slf4j.LoggerFactory;
12-
import org.springframework.http.HttpEntity;
13-
import org.springframework.http.HttpHeaders;
14-
import org.springframework.http.HttpMethod;
158
import org.springframework.http.HttpStatus;
169
import org.springframework.http.MediaType;
1710
import org.springframework.http.ResponseEntity;
@@ -21,9 +14,6 @@
2114
import org.springframework.web.bind.annotation.PathVariable;
2215
import org.springframework.web.bind.annotation.RequestMapping;
2316
import org.springframework.web.bind.annotation.RestController;
24-
import org.springframework.web.client.RestTemplate;
25-
import pl.databucket.server.configuration.AppProperties;
26-
import pl.databucket.server.dto.ReCaptchaSiteVerifyResponseDTO;
2717
import pl.databucket.server.exception.ExceptionFormatter;
2818
import pl.databucket.server.exception.ForbiddenRepetitionException;
2919
import pl.databucket.server.service.ManageUserService;
@@ -36,9 +26,7 @@
3626
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
3727
public class PublicController {
3828

39-
Logger logger = LoggerFactory.getLogger(PublicController.class);
4029
ManageUserService manageUserService;
41-
AppProperties appProperties;
4230
private final ExceptionFormatter exceptionFormatter = new ExceptionFormatter(PublicController.class);
4331

4432

@@ -70,28 +58,5 @@ public ResponseEntity<?> signUpConfirmation(@PathVariable String jwts) {
7058
}
7159
}
7260

73-
private boolean checkReCaptcha(String token) {
74-
RestTemplate restTemplate = new RestTemplate();
75-
76-
String uri = "https://www.google.com/recaptcha/api/siteverify" +
77-
"?secret=" + appProperties.getRecaptchaSecretKey() +
78-
"&response=" + token;
79-
80-
HttpHeaders headers = new HttpHeaders();
81-
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
82-
83-
HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
84-
ResponseEntity<?> result = restTemplate.exchange(uri, HttpMethod.GET, entity,
85-
ReCaptchaSiteVerifyResponseDTO.class);
86-
ReCaptchaSiteVerifyResponseDTO reCaptchaSiteVerifyResponse = (ReCaptchaSiteVerifyResponseDTO) result.getBody();
87-
assert reCaptchaSiteVerifyResponse != null;
88-
89-
if (!reCaptchaSiteVerifyResponse.isSuccess()) {
90-
logger.error("ReCaptcha failed: " + Arrays.toString(reCaptchaSiteVerifyResponse.getErrorCodes()));
91-
}
92-
93-
return reCaptchaSiteVerifyResponse.isSuccess() && reCaptchaSiteVerifyResponse.getScore() > 0.5;
94-
}
95-
9661

9762
}

src/main/java/pl/databucket/server/security/BasicAuthSecurityConfig.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.springframework.security.web.SecurityFilterChain;
1313
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
1414
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
15+
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
1516
import pl.databucket.server.dto.AuthRespDTO;
1617

1718
@Log4j2
@@ -22,10 +23,12 @@ public class BasicAuthSecurityConfig {
2223
@Bean
2324
public SecurityFilterChain basicSecurityFilterChain(HttpSecurity http,
2425
AuthResponseBuilder authResponseBuilder,
26+
JsonAuthenticationFilter jsonLoginFilter,
2527
ObjectMapper mapper) throws Exception {
2628
AuthenticationSuccessHandler successHandler = getFormSuccessHandler(authResponseBuilder);
2729
AuthenticationFailureHandler failureHandler = getAuthenticationFailureHandler(mapper);
2830
http.cors().and().csrf().disable()
31+
.addFilterBefore(jsonLoginFilter, UsernamePasswordAuthenticationFilter.class)
2932
.authorizeRequests()
3033
.antMatchers(HttpMethod.GET,
3134
"/", "/login**", "/sign-up", "/forgot-password", "/change-password",
@@ -52,7 +55,8 @@ public SecurityFilterChain basicSecurityFilterChain(HttpSecurity http,
5255
return http.build();
5356
}
5457

55-
private static AuthenticationFailureHandler getAuthenticationFailureHandler(ObjectMapper mapper) {
58+
@Bean
59+
public AuthenticationFailureHandler getAuthenticationFailureHandler(ObjectMapper mapper) {
5660
return (request, response, exception) -> {
5761
log.error("Auth error", exception);
5862
AuthRespDTO authResponse = AuthRespDTO.builder().message(exception.getMessage()).build();
@@ -63,7 +67,8 @@ private static AuthenticationFailureHandler getAuthenticationFailureHandler(Obje
6367
};
6468
}
6569

66-
private AuthenticationSuccessHandler getFormSuccessHandler(AuthResponseBuilder authResponseBuilder) {
70+
@Bean
71+
public AuthenticationSuccessHandler getFormSuccessHandler(AuthResponseBuilder authResponseBuilder) {
6772
return new FormAuthSuccessHandler(authResponseBuilder);
6873
}
6974
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package pl.databucket.server.security;
2+
3+
import com.fasterxml.jackson.core.type.TypeReference;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import java.io.IOException;
6+
import java.util.Map;
7+
import javax.servlet.http.HttpServletRequest;
8+
import org.springframework.security.authentication.AuthenticationManager;
9+
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
10+
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
11+
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
12+
import org.springframework.stereotype.Component;
13+
14+
/**
15+
* Allows for a POST JSON sigin in instead of the x-www-form-urlencoded version when calling POST /login-form
16+
*/
17+
@Component
18+
public class JsonAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
19+
20+
private final ObjectMapper mapper;
21+
/**
22+
* We can't call request.getReader() twice, so we save the result of the first call in a ThreadLocal var as not to
23+
* mix different requests.
24+
*/
25+
private static final ThreadLocal<Map<String, String>> ongoingAuth = new ThreadLocal<>();
26+
27+
public JsonAuthenticationFilter(ObjectMapper mapper, AuthenticationManager authenticationManager,
28+
AuthenticationSuccessHandler successHandler) {
29+
super(authenticationManager);
30+
this.setAuthenticationSuccessHandler(successHandler);
31+
this.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher("/api/public/sign-in",
32+
"POST"));
33+
this.mapper = mapper;
34+
}
35+
36+
@Override
37+
protected String obtainUsername(HttpServletRequest request) {
38+
try {
39+
Map<String, String> map = mapper.readValue(request.getReader(), new TypeReference<>() {
40+
});
41+
ongoingAuth.set(map);
42+
return map.get(SPRING_SECURITY_FORM_USERNAME_KEY);
43+
} catch (IOException e) {
44+
throw new RuntimeException(e);
45+
}
46+
}
47+
48+
@Override
49+
protected String obtainPassword(HttpServletRequest request) {
50+
return ongoingAuth.get().get(SPRING_SECURITY_FORM_PASSWORD_KEY);
51+
}
52+
}

src/main/java/pl/databucket/server/security/OAuth2SecurityConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.springframework.security.web.SecurityFilterChain;
1313
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
1414
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
15+
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
1516
import pl.databucket.server.dto.AuthRespDTO;
1617
import pl.databucket.server.repository.RoleRepository;
1718
import pl.databucket.server.service.ManageUserService;
@@ -26,11 +27,13 @@ public class OAuth2SecurityConfig {
2627
public SecurityFilterChain oauth2SecurityFilterChain(HttpSecurity http,
2728
OAuth2LogoutHandler oauth2LogoutHandler,
2829
AuthResponseBuilder authResponseBuilder,
30+
JsonAuthenticationFilter jsonLoginFilter,
2931
ObjectMapper mapper,
3032
AuthenticationSuccessHandler oAuth2SuccessHandler) throws Exception {
3133
AuthenticationSuccessHandler successHandler = getFormSuccessHandler(authResponseBuilder);
3234
AuthenticationFailureHandler failureHandler = getAuthenticationFailureHandler(mapper);
3335
http.cors().and().csrf().disable()
36+
.addFilterBefore(jsonLoginFilter, UsernamePasswordAuthenticationFilter.class)
3437
.authorizeRequests()
3538
.antMatchers(HttpMethod.GET,
3639
"/", "/login**", "/sign-up", "/forgot-password", "/change-password",

0 commit comments

Comments
 (0)