-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from neuefische/Feat/add-login
Feat/add-login
- Loading branch information
Showing
19 changed files
with
300 additions
and
56 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
22 changes: 22 additions & 0 deletions
22
backend/src/main/java/com/github/esgoet/backend/security/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,22 @@ | ||
package com.github.esgoet.backend.security; | ||
|
||
import com.github.esgoet.backend.user.models.User; | ||
import com.github.esgoet.backend.user.services.UserService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/api/auth") | ||
@RequiredArgsConstructor | ||
public class AuthController { | ||
private final UserService userService; | ||
|
||
@GetMapping("/me") | ||
public User getUser(@AuthenticationPrincipal OAuth2User user) { | ||
return userService.getUserByGitHubId(user.getName()); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
backend/src/main/java/com/github/esgoet/backend/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,75 @@ | ||
package com.github.esgoet.backend.security; | ||
|
||
import com.github.esgoet.backend.user.dto.UserDto; | ||
import com.github.esgoet.backend.user.models.User; | ||
import com.github.esgoet.backend.user.models.UserNotFoundException; | ||
import com.github.esgoet.backend.user.services.UserService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.HttpStatus; | ||
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.config.http.SessionCreationPolicy; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService; | ||
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest; | ||
import org.springframework.security.oauth2.client.userinfo.OAuth2UserService; | ||
import org.springframework.security.oauth2.core.user.DefaultOAuth2User; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.security.web.authentication.HttpStatusEntryPoint; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
@RequiredArgsConstructor | ||
public class SecurityConfig { | ||
private final UserService userService; | ||
|
||
@Value("${app.url}") | ||
private String appUrl; | ||
|
||
@Bean | ||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { | ||
http | ||
.csrf(AbstractHttpConfigurer::disable) | ||
.authorizeHttpRequests(a -> a | ||
// .requestMatchers("/api/books").authenticated() | ||
// .requestMatchers("/api/books/**").authenticated() | ||
.anyRequest().permitAll() | ||
) | ||
.sessionManagement(s -> s.sessionCreationPolicy(SessionCreationPolicy.ALWAYS)) | ||
.exceptionHandling(e -> e | ||
.authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))) | ||
.oauth2Login(o -> o.defaultSuccessUrl(appUrl)) | ||
.logout(l -> l.logoutSuccessUrl(appUrl)); | ||
return http.build(); | ||
} | ||
|
||
@Bean | ||
public OAuth2UserService<OAuth2UserRequest, OAuth2User> oauth2UserService() { | ||
DefaultOAuth2UserService delegate = new DefaultOAuth2UserService(); | ||
|
||
return request -> { | ||
OAuth2User user = delegate.loadUser(request); | ||
User githubUser; | ||
try { | ||
githubUser = userService.getUserByGitHubId(user.getName()); | ||
} catch (UserNotFoundException e) { | ||
githubUser = userService.saveUser(new UserDto(user.getAttributes().get("login").toString(), | ||
0, | ||
LocalDate.now(), | ||
0, | ||
user.getName(), | ||
"USER")); | ||
} | ||
|
||
return new DefaultOAuth2User(List.of(new SimpleGrantedAuthority(githubUser.role())), user.getAttributes(), "id"); | ||
}; | ||
} | ||
} |
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
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
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,2 +1,6 @@ | ||
spring.application.name=backend | ||
spring.data.mongodb.uri=${MONGO_DB_URI} | ||
spring.security.oauth2.client.registration.github.client-id=${GITHUB_ID} | ||
spring.security.oauth2.client.registration.github.client-secret=${GITHUB_SECRET} | ||
spring.security.oauth2.client.registration.github.scope=none | ||
app.url=${APP_URL} |
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
47 changes: 47 additions & 0 deletions
47
backend/src/test/java/com/github/esgoet/backend/security/AuthControllerTest.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,47 @@ | ||
package com.github.esgoet.backend.security; | ||
|
||
import com.github.esgoet.backend.user.models.User; | ||
import com.github.esgoet.backend.user.repositories.UserRepository; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import java.time.LocalDate; | ||
|
||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.oidcLogin; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
class AuthControllerTest { | ||
@Autowired | ||
MockMvc mockMvc; | ||
@Autowired | ||
UserRepository userRepository; | ||
|
||
@Test | ||
@DirtiesContext | ||
void getLoggedInUserTest() throws Exception { | ||
userRepository.save(new User("1","esgoet", 0, LocalDate.now(), 0, "123", "USER")); | ||
|
||
mockMvc.perform(get("/api/auth/me") | ||
.with(oidcLogin().idToken(token -> token.subject("123")) | ||
.userInfoToken(token -> token.claim("login", "esgoet")))) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().json(""" | ||
{ | ||
"userName": "esgoet", | ||
"readingGoal": 0, | ||
"readBooks": 0, | ||
"gitHubId": "123", | ||
"role": "USER" | ||
} | ||
""")) | ||
.andExpect(jsonPath("$.id").exists()) | ||
.andExpect(jsonPath("$.goalDate").exists()); | ||
} | ||
} |
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.