Skip to content

Commit

Permalink
update user github info when GitHub redirect to callback url
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanmomo committed May 26, 2024
1 parent 387733f commit eda6273
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public interface MemberRepository extends JpaRepository<Member, Long>, JpaSpecif

@Modifying
@javax.transaction.Transactional
@Query(value = "update member set github_id=NULL, discord_id=NULL, telegram_user_id=NULL where address = :address", nativeQuery =
true)
@Query(value = "update member set github_id=NULL, github_status=0, discord_id=NULL, telegram_user_id=NULL where address = :address",
nativeQuery = true)
void removeGitHubTgAndDiscordId(@Param("address") String address);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

import com.dl.officialsite.common.base.BaseResponse;
import com.dl.officialsite.common.utils.HttpSessionUtils;
import com.dl.officialsite.login.model.SessionUserInfo;
import com.dl.officialsite.member.Member;
import com.dl.officialsite.member.MemberRepository;
import com.dl.officialsite.oauth2.AccessTokenCacheService;
import com.dl.officialsite.oauth2.config.GitHubOAuthConfig;
import com.dl.officialsite.oauth2.config.OAuthSessionKey;
Expand All @@ -26,7 +29,9 @@
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Base64;
import java.util.Optional;

/**
* Process oauth requests.
Expand All @@ -46,6 +51,8 @@ public class GitHubController {

@Autowired
private RestTemplate restTemplate;
@Autowired
private MemberRepository memberRepository;

/**
* Accept oauth authorization request like:
Expand All @@ -56,7 +63,7 @@ public class GitHubController {
* the browser then jump to github page to allow user to authenticate himself.
*/
@RequestMapping("authorization/github")
public void handleAuthorization(HttpServletResponse response ) throws Exception {
public void handleAuthorization(HttpServletResponse response) throws Exception {

/**
* 1. Create auth url parameters, forming:
Expand Down Expand Up @@ -95,7 +102,8 @@ public BaseResponse<String> fetchAccessTokenAndUsername(
@RequestParam("code") String code,
@RequestParam("state") String state,
HttpServletRequest request,
HttpServletResponse response
HttpServletResponse response,
HttpSession session
) throws Exception {
/**
* 1. Combine access_token request and get access token
Expand Down Expand Up @@ -124,6 +132,17 @@ public BaseResponse<String> fetchAccessTokenAndUsername(
// add access token to cache
AccessTokenCacheService.addGitHubAccessToken(username, accessToken);
HttpSessionUtils.setOAuthUserName(request.getSession(), OAuthSessionKey.GITHUB_USER_NAME, username);

SessionUserInfo sessionUserInfo = HttpSessionUtils.getMember(session);
if (sessionUserInfo != null) {
// update user GitHub info if user exists
Optional<Member> member = this.memberRepository.findByAddress(sessionUserInfo.getAddress());
member.ifPresent(m -> {
m.setGithubId(username);
m.setGithubStatus(1);
memberRepository.save(m);
});
}
/**
* 3. Bind userInfo
*/
Expand All @@ -134,16 +153,17 @@ public BaseResponse<String> fetchAccessTokenAndUsername(
/**
* Refer:
* https://docs.github.com/en/free-pro-team@latest/rest/users/users?apiVersion=2022-11-28#get-the-authenticated-user
*
* @param userInfoUri
* @param accessToken
* @return
*/
private GithubUserInfo retrieve(String userInfoUri, String accessToken) {
RequestEntity requestEntity = RequestEntity.get(userInfoUri).
header("Authorization","Bearer "+accessToken)
header("Authorization", "Bearer " + accessToken)
.build();

ResponseEntity<GithubUserInfo> responseEntity = restTemplate.exchange(requestEntity,GithubUserInfo.class);
ResponseEntity<GithubUserInfo> responseEntity = restTemplate.exchange(requestEntity, GithubUserInfo.class);

System.out.println(responseEntity.getStatusCode());
System.out.println(responseEntity.getBody());
Expand Down

0 comments on commit eda6273

Please sign in to comment.