Skip to content

Commit

Permalink
add twitter-screen-name (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanmomo authored Mar 18, 2024
1 parent a162655 commit 69d4b79
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 29 deletions.
22 changes: 17 additions & 5 deletions src/main/java/com/dl/officialsite/member/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,28 @@
import com.dl.officialsite.common.privacy.PrivacyEncrypt;
import com.dl.officialsite.common.privacy.PrivacyTypeEnum;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.DynamicUpdate;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;
import java.io.Serializable;

import static com.dl.officialsite.common.privacy.PrivacyTypeEnum.CUSTOMER;

@Getter
@Setter
@AllArgsConstructor
Expand Down Expand Up @@ -52,6 +61,9 @@ public class Member implements Serializable
private String githubId;
@Column(length = 20)
private String tweetId;

@Column(unique=true,length = 64)
private String tweetScreenName;
@Column(length = 20)
private String telegramId;
@Column(length = 20)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public Predicate toPredicate(Root<Member> root, CriteriaQuery<?> criteriaQuery,
public BaseResponse createMember(@Valid @RequestBody Member member, @RequestParam String address, HttpServletRequest request) {
member.setGithubId(HttpSessionUtils.getOAuthUserName(request.getSession(), OAuthSessionKey.GITHUB_USER_NAME));
member.setTweetId(HttpSessionUtils.getOAuthUserName(request.getSession(), OAuthSessionKey.TWITTER_USER_NAME));
member.setTweetScreenName(HttpSessionUtils.getOAuthUserName(request.getSession(), OAuthSessionKey.TWITTER_SCREEN_NAME));

this.setOAuthUserName(request.getSession(), member);

Expand All @@ -153,6 +154,8 @@ public BaseResponse updateMemberByAddress(@RequestParam String address, @Request
.ifPresent(githubUserName -> _member.setGithubId(githubUserName));
Optional.ofNullable(HttpSessionUtils.getOAuthUserName(request.getSession(), OAuthSessionKey.TWITTER_USER_NAME))
.ifPresent(twitterUserName -> _member.setTweetId(twitterUserName));
Optional.ofNullable(HttpSessionUtils.getOAuthUserName(request.getSession(), OAuthSessionKey.TWITTER_SCREEN_NAME))
.ifPresent(twitterScreenName -> _member.setTweetScreenName(twitterScreenName));

if (member.getWechatId() != null) {
_member.setWechatId(member.getWechatId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
@AllArgsConstructor
public enum OAuthSessionKey {
GITHUB_USER_NAME("GITHUB_USER_NAME"),
TWITTER_USER_NAME("TWITTER_USER_NAME")
TWITTER_USER_NAME("TWITTER_USER_NAME"),
TWITTER_SCREEN_NAME("TWITTER_SCREEN_NAME")
;

private String sessionKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.dl.officialsite.oauth2.config.OAuthConfig;
import com.dl.officialsite.oauth2.config.OAuthSessionKey;
import com.dl.officialsite.oauth2.config.RegistrationConfig;
import com.nimbusds.jose.util.Pair;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.oauth1.AuthorizedRequestToken;
Expand Down Expand Up @@ -50,10 +51,8 @@ public void setUpTwitter() {
}

@GetMapping("/oauth2/authorize/normal/twitter")
public void twitterOauthLogin(
@RequestParam(name = "test", defaultValue = "false") boolean test,
HttpServletResponse response
) throws IOException {
public void twitterOauthLogin(@RequestParam(name = "test", defaultValue = "false") boolean test, HttpServletResponse response)
throws IOException {
String authorizeUrl = this.generateAuthorizeUrl(test);
response.sendRedirect(authorizeUrl);
}
Expand All @@ -63,9 +62,9 @@ private String generateAuthorizeUrl(boolean test) {
OAuthToken requestToken = oauthOperations.fetchRequestToken(twitterConfig.getCallbackUrl(), null);

OAuth1Parameters oAuth1Parameters = new OAuth1Parameters(new HashMap<>());
if (test){
if (test) {
oAuth1Parameters.add("secret", requestToken.getSecret());
}else{
} else {
// Save user's oauth_token_secret for exchanging the profile
UserSecurityUtils.getUserLogin().setTwitterOauthTokenSecret(requestToken.getSecret());
}
Expand All @@ -74,34 +73,30 @@ private String generateAuthorizeUrl(boolean test) {
}

@GetMapping("/oauth2/callback/twitter")
public BaseResponse getTwitter(
@RequestParam("oauth_token") String oauthToken,
@RequestParam("oauth_verifier") String oauthVerifier,
@RequestParam(value = "secret", required = false) String secret,
HttpServletRequest request
) {
String twitterUserName = fetchProfile(oauthToken, oauthVerifier, secret);
HttpSessionUtils.setOAuthUserName(request.getSession(), OAuthSessionKey.TWITTER_USER_NAME, twitterUserName);
return BaseResponse.successWithData(twitterUserName);
public BaseResponse getTwitter(@RequestParam("oauth_token") String oauthToken, @RequestParam("oauth_verifier") String oauthVerifier,
@RequestParam(value = "secret", required = false) String secret, HttpServletRequest request) {
Pair<String, String> twitterUserNameAndScreenName = fetchProfile(oauthToken, oauthVerifier, secret);
HttpSessionUtils.setOAuthUserName(request.getSession(), OAuthSessionKey.TWITTER_USER_NAME, twitterUserNameAndScreenName.getLeft());
HttpSessionUtils.setOAuthUserName(request.getSession(), OAuthSessionKey.TWITTER_SCREEN_NAME,
twitterUserNameAndScreenName.getRight());
return BaseResponse.successWithData(twitterUserNameAndScreenName.getLeft());
}

private String fetchProfile(String oAuthToken, String verifier, String secret) {
private Pair<String, String> fetchProfile(String oAuthToken, String verifier, String secret) {
OAuth1Operations oauthOperations = connectionFactory.getOAuthOperations();


String twitterOauthTokenSecret =
Optional.ofNullable(secret).orElseGet(() -> UserSecurityUtils.getUserLogin().getTwitterOauthTokenSecret());

OAuthToken accessToken = oauthOperations.exchangeForAccessToken(new AuthorizedRequestToken(
new OAuthToken(oAuthToken, twitterOauthTokenSecret), verifier), OAuth1Parameters.NONE);
OAuthToken accessToken = oauthOperations.exchangeForAccessToken(
new AuthorizedRequestToken(new OAuthToken(oAuthToken, twitterOauthTokenSecret), verifier), OAuth1Parameters.NONE);

Twitter twitter = new TwitterTemplate(twitterConfig.getClientId(),
twitterConfig.getClientSecret(),
accessToken.getValue(),
Twitter twitter = new TwitterTemplate(twitterConfig.getClientId(), twitterConfig.getClientSecret(), accessToken.getValue(),
accessToken.getSecret());
TwitterProfile profile = twitter.userOperations().getUserProfile();
log.info("User's name:[{}]", profile.getName());
return profile.getName();
log.info("User's name:[{} : {}]", profile.getName(), profile.getScreenName());
return Pair.of(profile.getName(), profile.getScreenName());
}

// public static void main(String[] args) {
Expand Down

0 comments on commit 69d4b79

Please sign in to comment.