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.
[Hexlet#159] add authentication with private email
- Loading branch information
Showing
12 changed files
with
116 additions
and
66 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
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
33 changes: 0 additions & 33 deletions
33
src/main/java/io/hexlet/typoreporter/handler/OAuth2SuccessHandler.java
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
src/main/java/io/hexlet/typoreporter/handler/exception/OAuth2Exception.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 io.hexlet.typoreporter.handler.exception; | ||
|
||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.http.ProblemDetail; | ||
import org.springframework.web.ErrorResponseException; | ||
|
||
public class OAuth2Exception extends ErrorResponseException { | ||
public OAuth2Exception(HttpStatusCode status, ProblemDetail body, Throwable cause) { | ||
super(status, body, cause); | ||
} | ||
} |
20 changes: 18 additions & 2 deletions
20
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 |
---|---|---|
@@ -1,17 +1,33 @@ | ||
package io.hexlet.typoreporter.security.service; | ||
|
||
import io.hexlet.typoreporter.domain.account.CustomOAuth2User; | ||
import io.hexlet.typoreporter.service.AccountService; | ||
import io.hexlet.typoreporter.service.GithubService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
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 | ||
@RequiredArgsConstructor | ||
public class CustomOAuth2UserService extends DefaultOAuth2UserService { | ||
@Override | ||
private final GithubService githubService; | ||
private final AccountService accountService; | ||
|
||
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException { | ||
OAuth2User user = super.loadUser(userRequest); | ||
return new CustomOAuth2User(user); | ||
var email = githubService.getPrivateEmail(userRequest.getAccessToken().getTokenValue()); | ||
var customUser = new CustomOAuth2User(user, email); | ||
Authentication authentication = new UsernamePasswordAuthenticationToken( | ||
customUser, customUser.getPassword(), customUser.getAuthorities()); | ||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
accountService.processOAuthPostLogin(customUser); | ||
|
||
return customUser; | ||
} | ||
} |
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
41 changes: 41 additions & 0 deletions
41
src/main/java/io/hexlet/typoreporter/service/GithubService.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,41 @@ | ||
package io.hexlet.typoreporter.service; | ||
|
||
import io.hexlet.typoreporter.handler.exception.OAuth2Exception; | ||
import io.hexlet.typoreporter.service.dto.oauth2.PrivateEmail; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ProblemDetail; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.Arrays; | ||
|
||
@Service | ||
public class GithubService { | ||
@Autowired | ||
private RestTemplate restTemplate; | ||
private static final String GITHUB_API_USER_PRIVATE_EMAILS = "https://api.github.com/user/emails"; | ||
|
||
public PrivateEmail getPrivateEmail(String accessToken) { | ||
if (accessToken.isBlank()) { | ||
throw new OAuth2Exception(HttpStatus.FORBIDDEN, ProblemDetail.forStatusAndDetail(HttpStatus.FORBIDDEN, | ||
"Access token is not valid. Token is: " + accessToken), null); | ||
} | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setBearerAuth(accessToken); | ||
HttpEntity<String> requestEntity = new HttpEntity<>(headers); | ||
var response = restTemplate.exchange( | ||
GITHUB_API_USER_PRIVATE_EMAILS, HttpMethod.GET, requestEntity, PrivateEmail[].class); | ||
if (response.getStatusCode() != HttpStatus.OK) { | ||
throw new OAuth2Exception(HttpStatus.UNAUTHORIZED, ProblemDetail.forStatusAndDetail(HttpStatus.UNAUTHORIZED, | ||
"HTTP code response is not 200. Code: " + response.getStatusCode()), null); | ||
} | ||
return Arrays.stream(response.getBody()) | ||
.filter(PrivateEmail::isPrimary) | ||
.findFirst() | ||
.orElseThrow(() -> new RuntimeException("no available email")); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/io/hexlet/typoreporter/service/dto/oauth2/PrivateEmail.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,15 @@ | ||
package io.hexlet.typoreporter.service.dto.oauth2; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
|
||
@Getter | ||
@Setter | ||
@ToString | ||
public class PrivateEmail { | ||
private String email; | ||
private boolean verified; | ||
private boolean primary; | ||
private String visibility; | ||
} |
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