forked from Hexlet/hexlet-correction
-
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
Showing
11 changed files
with
155 additions
and
10 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
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
50 changes: 50 additions & 0 deletions
50
src/main/java/io/hexlet/typoreporter/domain/account/CustomOAuth2User.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,50 @@ | ||
package io.hexlet.typoreporter.domain.account; | ||
|
||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class CustomOAuth2User implements OAuth2User { | ||
private final OAuth2User oAuth2User; | ||
|
||
public CustomOAuth2User(OAuth2User oAuth2User) { | ||
this.oAuth2User = oAuth2User; | ||
} | ||
|
||
@Override | ||
public Map<String, Object> getAttributes() { | ||
return oAuth2User.getAttributes(); | ||
} | ||
|
||
@Override | ||
public Collection<? extends GrantedAuthority> getAuthorities() { | ||
return oAuth2User.getAuthorities(); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return oAuth2User.getAttribute("email"); | ||
} | ||
|
||
public String getEmail() { | ||
return oAuth2User.getAttribute("email"); | ||
} | ||
public String getLogin() { | ||
return oAuth2User.getAttribute("login"); | ||
} | ||
public String getFirstName() { | ||
String[] fullName = Objects.requireNonNull(oAuth2User.<String>getAttribute("name")).split(" "); | ||
return fullName[1]; | ||
} | ||
public String getLastName() { | ||
String[] fullName = Objects.requireNonNull(oAuth2User.<String>getAttribute("name")).split(" "); | ||
return fullName[0]; | ||
} | ||
public String getPassword() { | ||
Integer password = oAuth2User.getAttribute("id"); | ||
return Objects.requireNonNull(password).toString(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/io/hexlet/typoreporter/handler/OAuth2SuccessHandler.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,28 @@ | ||
package io.hexlet.typoreporter.handler; | ||
|
||
import io.hexlet.typoreporter.domain.account.CustomOAuth2User; | ||
import io.hexlet.typoreporter.service.AccountService; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
|
||
@Component | ||
public class OAuth2SuccessHandler implements AuthenticationSuccessHandler { | ||
@Autowired | ||
private AccountService accountService; | ||
|
||
@Override | ||
public void onAuthenticationSuccess(HttpServletRequest request, | ||
HttpServletResponse response, | ||
Authentication authentication) throws IOException, ServletException { | ||
CustomOAuth2User oAuth2User = (CustomOAuth2User) authentication.getPrincipal(); | ||
accountService.processOAuthPostLogin(oAuth2User); | ||
response.sendRedirect("/workspaces"); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/io/hexlet/typoreporter/security/service/CustomOAuth2UserService.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 io.hexlet.typoreporter.security.service; | ||
|
||
import io.hexlet.typoreporter.domain.account.CustomOAuth2User; | ||
import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService; | ||
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest; | ||
import org.springframework.security.oauth2.core.OAuth2AuthenticationException; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class CustomOAuth2UserService extends DefaultOAuth2UserService { | ||
@Override | ||
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException { | ||
OAuth2User user = super.loadUser(userRequest); | ||
return new CustomOAuth2User(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
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 |
---|---|---|
|
@@ -52,7 +52,7 @@ static void datasourceProperties(DynamicPropertyRegistry registry) { | |
private static final String EMAIL_UPPER_CASE = "[email protected]"; | ||
private static final String EMAIL_LOWER_CASE = EMAIL_UPPER_CASE.toLowerCase(); | ||
|
||
private SignupAccountModel model = new SignupAccountModel( | ||
private final SignupAccountModel model = new SignupAccountModel( | ||
"model_upper_case", | ||
EMAIL_UPPER_CASE, | ||
"password", "password", | ||
|