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