Compare commits
2 commits
2b29ef81b2
...
6f264dccf7
Author | SHA1 | Date | |
---|---|---|---|
6f264dccf7 |
|||
0e150e9ded |
4 changed files with 10 additions and 40 deletions
|
@ -25,8 +25,6 @@ import java.util.*;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class GitHubService {
|
public class GitHubService {
|
||||||
private static final Logger logger = LoggerFactory.getLogger(GitHubService.class);
|
|
||||||
|
|
||||||
@Value("${spring.security.oauth2.client.registration.github.client-id}")
|
@Value("${spring.security.oauth2.client.registration.github.client-id}")
|
||||||
private String clientId;
|
private String clientId;
|
||||||
|
|
||||||
|
@ -71,32 +69,27 @@ public class GitHubService {
|
||||||
);
|
);
|
||||||
|
|
||||||
Map<String, Object> responseBody = response.getBody();
|
Map<String, Object> responseBody = response.getBody();
|
||||||
logger.info("GitHub token response: {}", responseBody);
|
|
||||||
|
|
||||||
// Check if there's an error in the response
|
// Check if there's an error in the response
|
||||||
if (responseBody.containsKey("error")) {
|
if (responseBody.containsKey("error")) {
|
||||||
String error = (String) responseBody.get("error");
|
String error = (String) responseBody.get("error");
|
||||||
String errorDescription = (String) responseBody.get("error_description");
|
String errorDescription = (String) responseBody.get("error_description");
|
||||||
logger.error("GitHub OAuth error: {} - {}", error, errorDescription);
|
|
||||||
throw new RuntimeException("GitHub OAuth error: " + errorDescription);
|
throw new RuntimeException("GitHub OAuth error: " + errorDescription);
|
||||||
}
|
}
|
||||||
|
|
||||||
String accessToken = (String) responseBody.get("access_token");
|
String accessToken = (String) responseBody.get("access_token");
|
||||||
if (accessToken == null || accessToken.isEmpty()) {
|
if (accessToken == null || accessToken.isEmpty()) {
|
||||||
logger.error("No access token received from GitHub");
|
|
||||||
throw new RuntimeException("Failed to receive access token from GitHub");
|
throw new RuntimeException("Failed to receive access token from GitHub");
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("Received access token from GitHub");
|
|
||||||
|
|
||||||
// Get user info
|
// Get user info
|
||||||
HttpHeaders userInfoHeaders = new HttpHeaders();
|
HttpHeaders userInfoHeaders = new HttpHeaders();
|
||||||
userInfoHeaders.set("Authorization", "Bearer " + accessToken);
|
userInfoHeaders.set("Authorization", "Bearer " + accessToken);
|
||||||
|
|
||||||
HttpEntity<String> userInfoRequestEntity = new HttpEntity<>(null, userInfoHeaders);
|
HttpEntity<String> userInfoRequestEntity = new HttpEntity<>(null, userInfoHeaders);
|
||||||
|
|
||||||
logger.info("Making request to GitHub API with token: {}", accessToken.substring(0, 5) + "...");
|
|
||||||
|
|
||||||
ResponseEntity<Map> userResponse = restTemplate.exchange(
|
ResponseEntity<Map> userResponse = restTemplate.exchange(
|
||||||
"https://api.github.com/user",
|
"https://api.github.com/user",
|
||||||
HttpMethod.GET,
|
HttpMethod.GET,
|
||||||
|
@ -105,7 +98,6 @@ public class GitHubService {
|
||||||
);
|
);
|
||||||
|
|
||||||
Map<String, Object> userAttributes = userResponse.getBody();
|
Map<String, Object> userAttributes = userResponse.getBody();
|
||||||
logger.info("Retrieved user info from GitHub: {}", userAttributes.get("login"));
|
|
||||||
|
|
||||||
// Get user emails
|
// Get user emails
|
||||||
HttpHeaders emailsHeaders = new HttpHeaders();
|
HttpHeaders emailsHeaders = new HttpHeaders();
|
||||||
|
@ -137,8 +129,6 @@ public class GitHubService {
|
||||||
email = (String) emails.get(0).get("email");
|
email = (String) emails.get(0).get("email");
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("Using email: {}", email);
|
|
||||||
|
|
||||||
// Process user data
|
// Process user data
|
||||||
String githubId = userAttributes.get("id").toString();
|
String githubId = userAttributes.get("id").toString();
|
||||||
String username = (String) userAttributes.get("login");
|
String username = (String) userAttributes.get("login");
|
||||||
|
@ -150,7 +140,6 @@ public class GitHubService {
|
||||||
if (userOptional.isPresent()) {
|
if (userOptional.isPresent()) {
|
||||||
// Update existing user
|
// Update existing user
|
||||||
user = userOptional.get();
|
user = userOptional.get();
|
||||||
logger.info("Found existing user with providerId: {}", githubId);
|
|
||||||
} else {
|
} else {
|
||||||
// Check if email exists
|
// Check if email exists
|
||||||
userOptional = userRepository.findByEmail(email);
|
userOptional = userRepository.findByEmail(email);
|
||||||
|
@ -159,7 +148,6 @@ public class GitHubService {
|
||||||
user = userOptional.get();
|
user = userOptional.get();
|
||||||
user.setProvider(AuthProvider.GITHUB);
|
user.setProvider(AuthProvider.GITHUB);
|
||||||
user.setProviderId(githubId);
|
user.setProviderId(githubId);
|
||||||
logger.info("Updating existing user with email: {}", email);
|
|
||||||
} else {
|
} else {
|
||||||
// Create new user
|
// Create new user
|
||||||
user = new UserEntity();
|
user = new UserEntity();
|
||||||
|
@ -170,7 +158,6 @@ public class GitHubService {
|
||||||
user.setEmailVerified(true);
|
user.setEmailVerified(true);
|
||||||
|
|
||||||
user.setBalance(new BigDecimal("1000.00"));
|
user.setBalance(new BigDecimal("1000.00"));
|
||||||
logger.info("Creating new user for: {}", username);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -183,12 +170,10 @@ public class GitHubService {
|
||||||
|
|
||||||
// Generate JWT token
|
// Generate JWT token
|
||||||
String token = jwtUtils.generateToken(authentication);
|
String token = jwtUtils.generateToken(authentication);
|
||||||
logger.info("Generated JWT token");
|
|
||||||
|
|
||||||
return new AuthResponseDto(token);
|
return new AuthResponseDto(token);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("Error processing GitHub code", e);
|
|
||||||
throw new RuntimeException("Failed to process GitHub authentication", e);
|
throw new RuntimeException("Failed to process GitHub authentication", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,9 +22,4 @@ public class GitHubOAuth2UserInfo extends OAuth2UserInfo {
|
||||||
public String getEmail() {
|
public String getEmail() {
|
||||||
return (String) attributes.get("email");
|
return (String) attributes.get("email");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getImageUrl() {
|
|
||||||
return (String) attributes.get("avatar_url");
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -1,7 +1,10 @@
|
||||||
package de.szut.casino.security.oauth2;
|
package de.szut.casino.security.oauth2;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Getter
|
||||||
public abstract class OAuth2UserInfo {
|
public abstract class OAuth2UserInfo {
|
||||||
protected Map<String, Object> attributes;
|
protected Map<String, Object> attributes;
|
||||||
|
|
||||||
|
@ -9,15 +12,9 @@ public abstract class OAuth2UserInfo {
|
||||||
this.attributes = attributes;
|
this.attributes = attributes;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, Object> getAttributes() {
|
|
||||||
return attributes;
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract String getId();
|
public abstract String getId();
|
||||||
|
|
||||||
public abstract String getName();
|
public abstract String getName();
|
||||||
|
|
||||||
public abstract String getEmail();
|
public abstract String getEmail();
|
||||||
|
|
||||||
public abstract String getImageUrl();
|
|
||||||
}
|
}
|
|
@ -1,6 +1,8 @@
|
||||||
package de.szut.casino.security.oauth2;
|
package de.szut.casino.security.oauth2;
|
||||||
|
|
||||||
import de.szut.casino.user.UserEntity;
|
import de.szut.casino.user.UserEntity;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
import org.springframework.security.core.GrantedAuthority;
|
import org.springframework.security.core.GrantedAuthority;
|
||||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
@ -12,11 +14,14 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class UserPrincipal implements OAuth2User, UserDetails {
|
public class UserPrincipal implements OAuth2User, UserDetails {
|
||||||
|
@Getter
|
||||||
private Long id;
|
private Long id;
|
||||||
|
@Getter
|
||||||
private String email;
|
private String email;
|
||||||
private String username;
|
private String username;
|
||||||
private String password;
|
private String password;
|
||||||
private Collection<? extends GrantedAuthority> authorities;
|
private Collection<? extends GrantedAuthority> authorities;
|
||||||
|
@Setter
|
||||||
private Map<String, Object> attributes;
|
private Map<String, Object> attributes;
|
||||||
|
|
||||||
public UserPrincipal(Long id, String email, String username, String password, Collection<? extends GrantedAuthority> authorities) {
|
public UserPrincipal(Long id, String email, String username, String password, Collection<? extends GrantedAuthority> authorities) {
|
||||||
|
@ -46,14 +51,6 @@ public class UserPrincipal implements OAuth2User, UserDetails {
|
||||||
return userPrincipal;
|
return userPrincipal;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getEmail() {
|
|
||||||
return email;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getPassword() {
|
public String getPassword() {
|
||||||
return password;
|
return password;
|
||||||
|
@ -99,10 +96,6 @@ public class UserPrincipal implements OAuth2User, UserDetails {
|
||||||
return attributes;
|
return attributes;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAttributes(Map<String, Object> attributes) {
|
|
||||||
this.attributes = attributes;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return String.valueOf(id);
|
return String.valueOf(id);
|
||||||
|
|
Reference in a new issue