aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/www/controllers/SocialLogin.java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2023-04-10 22:53:40 +0300
committerGravatar Vitaly Takmazov2023-04-13 19:49:55 +0300
commit379f1a8661da46040332923a5ccfb555656b0a8b (patch)
tree12b3d6121d749c4dc942b10c733f77d7d6dbba94 /src/main/java/com/juick/www/controllers/SocialLogin.java
parent3a1e45d3015df62eb5768a8e6929cb41dcaf9913 (diff)
Premium users
Diffstat (limited to 'src/main/java/com/juick/www/controllers/SocialLogin.java')
-rw-r--r--src/main/java/com/juick/www/controllers/SocialLogin.java46
1 files changed, 40 insertions, 6 deletions
diff --git a/src/main/java/com/juick/www/controllers/SocialLogin.java b/src/main/java/com/juick/www/controllers/SocialLogin.java
index 1ab0a139..b0738fea 100644
--- a/src/main/java/com/juick/www/controllers/SocialLogin.java
+++ b/src/main/java/com/juick/www/controllers/SocialLogin.java
@@ -45,6 +45,7 @@ import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
+import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.authentication.RememberMeAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.RememberMeServices;
@@ -119,7 +120,7 @@ public class SocialLogin {
String facebookRedirectUri = redirectBuilder.replacePath("/_fblogin").build().toUriString();
facebookAuthService = facebookBuilder.apiSecret(FACEBOOK_SECRET).callback(facebookRedirectUri)
.defaultScope("email").build(FacebookApi.instance());
- vkAuthService = vkBuilder.apiSecret(VK_SECRET).defaultScope("friends,wall,offline").callback(VK_REDIRECT)
+ vkAuthService = vkBuilder.apiSecret(VK_SECRET).defaultScope("friends,wall,offline,groups").callback(VK_REDIRECT)
.build(VkontakteApi.instance());
ServiceBuilder appleSignInBuilder = new ServiceBuilder(appleApplicationId);
String appleSignInRedirectUri = redirectBuilder.replacePath("/_apple").build().toUriString();
@@ -194,7 +195,7 @@ public class SocialLogin {
@GetMapping("/_twitter")
protected void doTwitterLogin(com.juick.model.User user, HttpServletRequest request,
- HttpServletResponse response) throws IOException, ExecutionException, InterruptedException {
+ HttpServletResponse response) throws IOException, ExecutionException, InterruptedException {
String hash = StringUtils.EMPTY, request_token = StringUtils.EMPTY, request_token_secret = StringUtils.EMPTY;
String verifier = request.getParameter("oauth_verifier");
Cookie[] cookies = request.getCookies();
@@ -279,6 +280,7 @@ public class SocialLogin {
long vkID = NumberUtils.toLong(jsonUser.id(), 0);
int uid = userService.getUIDbyVKID(vkID);
if (uid > 0) {
+ userService.updateVkUser(vkID, token.getAccessToken(), vkName, vkLink);
Cookie c = new Cookie("hash", userService.getHashByUID(uid));
c.setMaxAge(50 * 24 * 60 * 60);
response.addCookie(c);
@@ -299,9 +301,9 @@ public class SocialLogin {
@GetMapping("/_tglogin")
public String doDurovLogin(@RequestParam Map<String, String> params,
- @RequestParam String hash,
- @RequestHeader(value = "referer", required = false) String referer,
- HttpServletRequest request, HttpServletResponse response) {
+ @RequestParam String hash,
+ @RequestHeader(value = "referer", required = false) String referer,
+ HttpServletRequest request, HttpServletResponse response) {
String dataCheckString = params.entrySet().stream().filter(p -> !p.getKey().equals("hash"))
.sorted(Map.Entry.comparingByKey()).map(p -> p.getKey() + "=" + p.getValue())
.collect(Collectors.joining("\n"));
@@ -345,7 +347,8 @@ public class SocialLogin {
@PostMapping("/_apple")
public String doVerifyAppleResponse(HttpServletRequest request, HttpServletResponse response,
- @RequestParam Map<String, String> body, HttpSession session) throws InterruptedException, ExecutionException, IOException {
+ @RequestParam Map<String, String> body, HttpSession session)
+ throws InterruptedException, ExecutionException, IOException {
OAuth2AccessToken token = appleSignInService.getAccessToken(body.get("code"));
var jsonNode = jsonMapper.readTree(token.getRawResponse());
var idToken = jsonNode.get("id_token").textValue();
@@ -372,4 +375,35 @@ public class SocialLogin {
}
throw new HttpBadRequestException();
}
+
+ @Scheduled(fixedRate = 3600000)
+ public void updatePremium() {
+ userService.getVkTokens(List.of())
+ .forEach(vkUser -> {
+ var userId = userService.getUIDbyVKID(Long.parseLong(vkUser.getLeft()));
+ if (userId > 0) {
+ OAuth2AccessToken token = new OAuth2AccessToken(vkUser.getRight());
+ OAuthRequest donRequest = new OAuthRequest(Verb.GET,
+ "https://api.vk.com/method/donut.isDon?owner_id=-67669480&v=5.131");
+ vkAuthService.signRequest(token, donRequest);
+ try {
+ Response vkResponse = vkAuthService.execute(donRequest);
+ if (vkResponse.isSuccessful()) {
+ logger.info(vkResponse.getBody());
+ var response = jsonMapper.readTree(vkResponse.getBody());
+ if (response.has("response")) {
+ var isDon = response.get("response").intValue() > 0;
+ logger.info("{} is Don: {}", vkUser.getLeft(), isDon);
+ userService.setPremium(userId, isDon);
+ } else {
+ // token is expired or does not have "groups" permissions
+ userService.updateVkToken(userId, "");
+ }
+ }
+ } catch (Exception e) {
+ logger.error("Don request error", e);
+ }
+ }
+ });
+ }
}