From 8a0fbbd35c13054a947ea0d27ca117542bc452b9 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 18 May 2018 15:33:38 +0300 Subject: JPA WIP --- .../java/com/juick/server/api/SocialLogin.java | 33 ++++++++++++---------- 1 file changed, 18 insertions(+), 15 deletions(-) (limited to 'juick-server/src/main/java/com/juick/server/api/SocialLogin.java') diff --git a/juick-server/src/main/java/com/juick/server/api/SocialLogin.java b/juick-server/src/main/java/com/juick/server/api/SocialLogin.java index dc7425e1..195d1217 100644 --- a/juick-server/src/main/java/com/juick/server/api/SocialLogin.java +++ b/juick-server/src/main/java/com/juick/server/api/SocialLogin.java @@ -25,6 +25,7 @@ import com.github.scribejava.core.model.OAuthRequest; import com.github.scribejava.core.model.Verb; import com.github.scribejava.core.oauth.OAuth20Service; import com.juick.facebook.User; +import com.juick.server.helpers.AnonymousUser; import com.juick.server.util.HttpBadRequestException; import com.juick.service.CrosspostService; import com.juick.service.EmailService; @@ -44,6 +45,8 @@ import org.springframework.web.util.UriComponentsBuilder; import javax.annotation.PostConstruct; import javax.inject.Inject; import java.io.IOException; +import java.util.Map; +import java.util.Optional; import java.util.UUID; import java.util.concurrent.ExecutionException; @@ -137,22 +140,22 @@ public class SocialLogin { throw new HttpBadRequestException(); } - int uid = crosspostService.getUIDbyFBID(fbID); - if (uid > 0) { + com.juick.User user = crosspostService.getUserByFBID(fbID).orElse(AnonymousUser.INSTANCE); + if (!user.isAnonymous()) { if (!crosspostService.updateFacebookUser(fbID, token.getAccessToken(), fb.getName(), fb.getLink())) { logger.error("error updating facebook user, id: {}, token: {}", fbID, token.getAccessToken()); throw new HttpBadRequestException(); } UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(redirectUrl); - uriComponentsBuilder.queryParam("hash", userService.getHashByUID(uid)); + uriComponentsBuilder.queryParam("hash", userService.getHashForUser(user)); return "redirect:" + uriComponentsBuilder.build().toUriString(); } else if (fb.getVerified()) { if (!crosspostService.createFacebookUser(fbID, state, token.getAccessToken(), fb.getName(), fb.getLink())) { if (StringUtils.isNotEmpty(fb.getEmail())) { logger.info("found {} for facebook user {}", fb.getEmail(), fb.getLink()); - Integer userId = crosspostService.getUIDbyFBID(fbID); - if (!emailService.getEmails(userId, false).contains(fb.getEmail())) { - emailService.addEmail(userId, fb.getEmail()); + user = crosspostService.getUserByFBID(fbID).orElse(AnonymousUser.INSTANCE); + if (!emailService.getEmails(user, false).contains(fb.getEmail())) { + emailService.addEmail(user, fb.getEmail()); } } logger.info("email not found for facebook user {}", fb.getLink()); @@ -254,10 +257,10 @@ public class SocialLogin { } Long vkID = NumberUtils.toLong(jsonUser.getId(), 0); - int uid = crosspostService.getUIDbyVKID(vkID); - if (uid > 0) { + com.juick.User vkUser = crosspostService.getUserByVKID(vkID).orElse(AnonymousUser.INSTANCE); + if (!vkUser.isAnonymous()) { UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(redirectUrl); - uriComponentsBuilder.queryParam("hash", userService.getHashByUID(uid)); + uriComponentsBuilder.queryParam("hash", userService.getHashForUser(vkUser)); return "redirect:" + uriComponentsBuilder.build().toUriString(); } else { String loginhash = UUID.randomUUID().toString(); @@ -282,17 +285,17 @@ public class SocialLogin { byte[] secretKey = DigestUtils.sha256(telegramToken); String resultString = new HmacUtils(HmacAlgorithms.HMAC_SHA_256, secretKey).hmacHex(dataCheckString); if (hash.equals(resultString)) { - Long tgUser = Long.valueOf(params.get("id")); - int uid = telegramService.getUser(tgUser); - if (uid > 0) { - Cookie c = new Cookie("hash", userService.getHashByUID(uid)); + Long tgUserId = Long.valueOf(params.get("id")); + com.juick.User tgUser = telegramService.getUser(tgUserId); + if (!tgUser.isAnonymous()) { + Cookie c = new Cookie("hash", userService.getHashForUser(tgUser)); c.setMaxAge(50 * 24 * 60 * 60); response.addCookie(c); return Utils.getPreviousPageByRequest(request).orElse("redirect:/"); } else { String username = StringUtils.defaultString(params.get("username"), params.get("first_name")); - telegramService.createTelegramUser(tgUser, username); - return "redirect:/signup?type=durov&hash=" + userService.getSignUpHashByTelegramID(tgUser, username); + telegramService.createTelegramUser(tgUserId, username); + return "redirect:/signup?type=durov&hash=" + userService.getSignUpHashByTelegramID(tgUserId, username); } } else { logger.warn("invalid tg hash {} for {}", resultString, hash); -- cgit v1.2.3