-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOAuth2ServiceImpl.java
118 lines (80 loc) · 4.51 KB
/
OAuth2ServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.ivansan.blogfinalproject.service;
import com.ivansan.blogfinalproject.dto.LoginRequestDTO;
import com.ivansan.blogfinalproject.dto.LoginResponseDTO;
import com.ivansan.blogfinalproject.dto.UserRequestDTO;
import com.ivansan.blogfinalproject.entity.User;
import com.ivansan.blogfinalproject.enums.AuthProvider;
import com.ivansan.blogfinalproject.repository.RoleRepository;
import com.ivansan.blogfinalproject.repository.UserRepository;
import com.ivansan.blogfinalproject.security.OAuthAttributes;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.Set;
@Service
@RequiredArgsConstructor
public class OAuth2ServiceImpl implements OAuth2Service {
private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;
private final RoleRepository roleRepository;
private final JWTService jwtService;
private final PasswordGeneratorService passwordGeneratorService;
private final OAuth2AuthorizedClientService authorizedClientService;
@Value("${oauth2.fixed-password}")
private String oauth2FixedPassword;
@Override
public LoginResponseDTO registerAndLogin(OAuth2AuthenticationToken authentication) {
String githubEmail = "";
if ("github".equals(authentication.getAuthorizedClientRegistrationId())) {
OAuth2AuthorizedClient authorizedClient = this.authorizedClientService.loadAuthorizedClient(
authentication.getAuthorizedClientRegistrationId(),
authentication.getName());
String accessToken = authorizedClient.getAccessToken().getTokenValue();
HttpHeaders headers = new HttpHeaders();
headers.setBearerAuth(accessToken);
HttpEntity<String> entity = new HttpEntity<>(headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.exchange("https://api.github.com/user/emails", HttpMethod.GET, entity, String.class);
githubEmail = response.getBody().split("\"email\":")[1].split("\"")[1];
}
OAuthAttributes oAuthAttributes = OAuthAttributes.of(authentication, githubEmail);
var password = passwordGeneratorService.generateRandomPassword();
UserRequestDTO userRequestDTO = new UserRequestDTO(
oAuthAttributes.getName() + oAuthAttributes.getProviderId().substring(oAuthAttributes.getProviderId().length() - 3),
passwordEncoder.encode(oauth2FixedPassword),
oAuthAttributes.getEmail(),
oAuthAttributes.getPicture(),
oAuthAttributes.getProvider().name(),
oAuthAttributes.getProviderId()
);
LoginRequestDTO loginRequestDTO = new LoginRequestDTO(
oAuthAttributes.getEmail(),
oauth2FixedPassword
);
if (userRepository.existsByEmailIgnoreCase(userRequestDTO.getEmail())) {
return new LoginResponseDTO(jwtService.jwtToken(new UsernamePasswordAuthenticationToken(loginRequestDTO.username(), loginRequestDTO.password())));
}
var user = User.builder()
.username(userRequestDTO.getUsername())
.email(userRequestDTO.getEmail())
.image(userRequestDTO.getImage())
.provider(AuthProvider.valueOf(userRequestDTO.getProvider()))
.providerId(userRequestDTO.getProviderId())
.password(passwordEncoder.encode(oauth2FixedPassword))
.build();
var role = roleRepository.findByNameIgnoreCase("ROLE_USER").orElseThrow();
user.setRoles(Set.of(role));
userRepository.save(user);
return new LoginResponseDTO(jwtService.jwtToken(new UsernamePasswordAuthenticationToken(loginRequestDTO.username(), loginRequestDTO.password())));
}
}