-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0937184
commit 2afef92
Showing
22 changed files
with
349 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
version: '3.8' | ||
services: | ||
db: | ||
image: postgres | ||
container_name: falcon_pg | ||
environment: | ||
POSTGRES_USER: admin | ||
POSTGRES_PASSWORD: 123 | ||
POSTGRES_DB: falcon | ||
ports: | ||
- "5432:5432" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/main/java/com/vodacom/falcon/config/security/CustomFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.vodacom.falcon.config.security; | ||
|
||
import com.vodacom.falcon.model.User; | ||
import com.vodacom.falcon.repository.UserRepository; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
import java.util.Base64; | ||
import java.util.Objects; | ||
|
||
import static com.vodacom.falcon.util.FalconDefaults.AUTHORIZATION; | ||
import static com.vodacom.falcon.util.FalconDefaults.BASIC; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class CustomFilter extends OncePerRequestFilter { | ||
private final UserRepository userRepository; | ||
private final PasswordEncoderProvider encoderProvider; | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { | ||
log.info("Internal filter {}", request.getPathInfo()); | ||
if (isBasicAuth(request)) { | ||
String base64 = this.getHeader(request) | ||
.replace(BASIC, ""); | ||
String[] credentials = new String(Base64.getDecoder() | ||
.decode(base64)) | ||
.split(":"); | ||
|
||
String username = credentials[0]; | ||
String pass = credentials[1]; | ||
|
||
User user = userRepository.findByUsername(username); | ||
|
||
if (Objects.isNull(user)) { | ||
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); | ||
response.getWriter().write("User not found!"); | ||
return; | ||
} | ||
|
||
if (encoderProvider.passwordEncoder().matches(user.getPassword(), pass)) { | ||
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); | ||
response.getWriter().write("Wrong Password"); | ||
return; | ||
} | ||
|
||
Authentication authentication = new UsernamePasswordAuthenticationToken(user, null, null); | ||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
} | ||
|
||
filterChain.doFilter(request, response); | ||
} | ||
|
||
private boolean isBasicAuth(HttpServletRequest request) { | ||
String header = getHeader(request); | ||
return header != null && header.startsWith(BASIC); | ||
} | ||
|
||
private String getHeader(HttpServletRequest request) { | ||
return request.getHeader(AUTHORIZATION); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/vodacom/falcon/config/security/PasswordEncoderProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.vodacom.falcon.config.security; | ||
|
||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class PasswordEncoderProvider { | ||
public PasswordEncoder passwordEncoder(){ | ||
return new BCryptPasswordEncoder(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/vodacom/falcon/config/security/SecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.vodacom.falcon.config.security; | ||
|
||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
|
||
import static jakarta.servlet.DispatcherType.ERROR; | ||
import static jakarta.servlet.DispatcherType.FORWARD; | ||
|
||
@Configuration | ||
@EnableMethodSecurity | ||
@EnableWebSecurity | ||
public class SecurityConfig { | ||
@Autowired | ||
private CustomFilter filter; | ||
|
||
@Bean | ||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { | ||
return http | ||
.csrf(AbstractHttpConfigurer::disable) | ||
.authorizeHttpRequests(authorize -> authorize | ||
.dispatcherTypeMatchers(FORWARD, ERROR) | ||
.permitAll() | ||
.requestMatchers("falcon/auth/**", "falcon/insight/**") | ||
.permitAll() | ||
.anyRequest().denyAll() | ||
) | ||
.addFilterBefore(filter, UsernamePasswordAuthenticationFilter.class) | ||
.build(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/vodacom/falcon/config/security/UserDetails.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.vodacom.falcon.config.security; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class UserDetails { | ||
private String username; | ||
private String password; | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/vodacom/falcon/controller/AuthController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.vodacom.falcon.controller; | ||
|
||
|
||
import com.vodacom.falcon.model.request.UserRegistrationRequest; | ||
import com.vodacom.falcon.service.AuthService; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.CrossOrigin; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@CrossOrigin(value = "*") | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
@RequestMapping("falcon/auth") | ||
public class AuthController { | ||
private final AuthService service; | ||
|
||
@PostMapping("/signup") | ||
public ResponseEntity<Void> signup(@RequestBody UserRegistrationRequest userRegistrationRequest) { | ||
service.createUser(userRegistrationRequest); | ||
return new ResponseEntity<>(HttpStatus.ACCEPTED); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.vodacom.falcon.model; | ||
|
||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.UUID; | ||
|
||
@Entity | ||
@Setter | ||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
@Table(name = "falcon_user") | ||
public class User { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
private UUID id; | ||
private String username; | ||
private String password; | ||
private LocalDateTime createdAt; | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/vodacom/falcon/model/request/UserRegistrationRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.vodacom.falcon.model.request; | ||
|
||
public record UserRegistrationRequest(String username, String password) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/java/com/vodacom/falcon/model/response/MetadataResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.vodacom.falcon.model.response; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Setter | ||
@Getter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class MetadataResponse { | ||
private boolean isAuthenticatedUser; | ||
private String message; | ||
} |
5 changes: 4 additions & 1 deletion
5
src/main/java/com/vodacom/falcon/model/response/openweathermap/OpenDailyWhetherResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
package com.vodacom.falcon.model.response.openweathermap; | ||
|
||
import java.util.List; | ||
|
||
public record OpenDailyWhetherResponse(String dt, String sunrise, String sunset, | ||
String summary, OpenDailyTemperatureWeatherResponse temp) { | ||
String summary, List<OpenCurrentWeatherDescResponse> weather, | ||
OpenDailyTemperatureWeatherResponse temp) { | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/vodacom/falcon/repository/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.vodacom.falcon.repository; | ||
|
||
|
||
import com.vodacom.falcon.model.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.UUID; | ||
|
||
@Repository | ||
public interface UserRepository extends JpaRepository<User, UUID> { | ||
User findByUsername(String username); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.vodacom.falcon.service; | ||
|
||
|
||
import com.vodacom.falcon.config.security.PasswordEncoderProvider; | ||
import com.vodacom.falcon.model.User; | ||
import com.vodacom.falcon.model.request.UserRegistrationRequest; | ||
import com.vodacom.falcon.repository.UserRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class AuthService { | ||
private final PasswordEncoderProvider encoderProvider; | ||
private final UserRepository userRepository; | ||
|
||
public void createUser(UserRegistrationRequest auth) { | ||
User existingUser = userRepository.findByUsername(auth.username()); | ||
if (existingUser != null) { | ||
throw new Error("User already exists! Please login"); | ||
} | ||
|
||
User user = User.builder() | ||
.username(auth.username()) | ||
.password(encoderProvider.passwordEncoder().encode(auth.password())) | ||
.createdAt(LocalDateTime.now()) | ||
.build(); | ||
userRepository.save(user); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.