diff options
author | Vitaly Takmazov | 2022-12-21 02:36:49 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2022-12-21 02:36:49 +0300 |
commit | 1ad1b73a7b85e7e907de1870991a8849487f9543 (patch) | |
tree | 4cda16090472d739a79ab57efbc94a6ad4e8ddc9 /src/main/java/com/juick | |
parent | 8a32b359eed27ab1d4e054ffed99b6602fa18acf (diff) |
Social logins: adopt records
Diffstat (limited to 'src/main/java/com/juick')
-rw-r--r-- | src/main/java/com/juick/model/ext/facebook/User.java | 31 | ||||
-rw-r--r-- | src/main/java/com/juick/model/ext/twitter/User.java | 10 | ||||
-rw-r--r-- | src/main/java/com/juick/model/ext/vk/Token.java | 23 | ||||
-rw-r--r-- | src/main/java/com/juick/model/ext/vk/User.java | 28 | ||||
-rw-r--r-- | src/main/java/com/juick/model/ext/vk/UsersResponse.java | 10 | ||||
-rw-r--r-- | src/main/java/com/juick/www/api/ApiSocialLogin.java | 29 | ||||
-rw-r--r-- | src/main/java/com/juick/www/controllers/SocialLogin.java | 28 |
7 files changed, 40 insertions, 119 deletions
diff --git a/src/main/java/com/juick/model/ext/facebook/User.java b/src/main/java/com/juick/model/ext/facebook/User.java index 44a3e73e..4c9433e9 100644 --- a/src/main/java/com/juick/model/ext/facebook/User.java +++ b/src/main/java/com/juick/model/ext/facebook/User.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008-2020, Juick + * Copyright (C) 2008-2022, Juick * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -24,32 +24,5 @@ import com.fasterxml.jackson.annotation.JsonProperty; * Created by vitalyster on 28.11.2016. */ @JsonIgnoreProperties(ignoreUnknown = true) -public class User { - private String id; - private String name; - private String firstName; - private String lastName; - private String email; - - public String getId() { - return id; - } - - public String getName() { - return name; - } - - @JsonProperty("first_name") - public String getFirstName() { - return firstName; - } - - @JsonProperty("last_name") - public String getLastName() { - return lastName; - } - - public String getEmail() { - return email; - } +public record User(String id, String name, @JsonProperty("first_name") String firstName, @JsonProperty("last_name") String lastName, String email) { } diff --git a/src/main/java/com/juick/model/ext/twitter/User.java b/src/main/java/com/juick/model/ext/twitter/User.java index ea89f675..5a09556d 100644 --- a/src/main/java/com/juick/model/ext/twitter/User.java +++ b/src/main/java/com/juick/model/ext/twitter/User.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008-2020, Juick + * Copyright (C) 2008-2022, Juick * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -24,11 +24,5 @@ import com.fasterxml.jackson.annotation.JsonProperty; * Created by vitalyster on 28.11.2016. */ @JsonIgnoreProperties(ignoreUnknown = true) -public class User { - private String screenName; - - @JsonProperty("screen_name") - public String getScreenName() { - return screenName; - } +public record User( @JsonProperty("screen_name") String screenName) { } diff --git a/src/main/java/com/juick/model/ext/vk/Token.java b/src/main/java/com/juick/model/ext/vk/Token.java index 42a3195d..7623a3e9 100644 --- a/src/main/java/com/juick/model/ext/vk/Token.java +++ b/src/main/java/com/juick/model/ext/vk/Token.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008-2020, Juick + * Copyright (C) 2008-2022, Juick * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -24,23 +24,6 @@ import com.fasterxml.jackson.annotation.JsonProperty; * Created by vitalyster on 28.11.2016. */ @JsonIgnoreProperties(ignoreUnknown = true) -public class Token { - private Long userId; - private String accessToken; - private String expiresIn; - - @JsonProperty("user_id") - public Long getUserId() { - return userId; - } - - @JsonProperty("access_token") - public String getAccessToken() { - return accessToken; - } - - @JsonProperty("expires_in") - public String getExpiresIn() { - return expiresIn; - } +public record Token(@JsonProperty("user_id") Long userId, @JsonProperty("access_token") String accessToken, + @JsonProperty("expires_in") String expiresIn) { } diff --git a/src/main/java/com/juick/model/ext/vk/User.java b/src/main/java/com/juick/model/ext/vk/User.java index a39ca6dc..3404a9e6 100644 --- a/src/main/java/com/juick/model/ext/vk/User.java +++ b/src/main/java/com/juick/model/ext/vk/User.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008-2020, Juick + * Copyright (C) 2008-2022, Juick * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -24,28 +24,6 @@ import com.fasterxml.jackson.annotation.JsonProperty; * Created by vitalyster on 28.11.2016. */ @JsonIgnoreProperties(ignoreUnknown = true) -public class User { - private String id; - private String firstName; - private String lastName; - private String screenName; - - @JsonProperty("first_name") - public String getFirstName() { - return firstName; - } - - @JsonProperty("last_name") - public String getLastName() { - return lastName; - } - - @JsonProperty("screen_name") - public String getScreenName() { - return screenName; - } - - public String getId() { - return id; - } +public record User(String id, @JsonProperty("first_name") String firstName, + @JsonProperty("last_name") String lastName, @JsonProperty("screen_name") String screenName) { } diff --git a/src/main/java/com/juick/model/ext/vk/UsersResponse.java b/src/main/java/com/juick/model/ext/vk/UsersResponse.java index 60f6c06e..0ece27fc 100644 --- a/src/main/java/com/juick/model/ext/vk/UsersResponse.java +++ b/src/main/java/com/juick/model/ext/vk/UsersResponse.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008-2020, Juick + * Copyright (C) 2008-2022, Juick * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -24,11 +24,5 @@ import java.util.List; /** * Created by vitalyster on 28.11.2016. */ -public class UsersResponse { - private List<User> users; - - @JsonProperty("response") - public List<User> getUsers() { - return users; - } +public record UsersResponse(@JsonProperty("response") List<User> users) { } diff --git a/src/main/java/com/juick/www/api/ApiSocialLogin.java b/src/main/java/com/juick/www/api/ApiSocialLogin.java index 38884cd6..c1e31b68 100644 --- a/src/main/java/com/juick/www/api/ApiSocialLogin.java +++ b/src/main/java/com/juick/www/api/ApiSocialLogin.java @@ -149,23 +149,23 @@ public class ApiSocialLogin { throw new HttpBadRequestException(); } User fb = jsonMapper.readValue(graph, User.class); - long fbID = NumberUtils.toLong(fb.getId(), 0); - if (fbID == 0 || StringUtils.isBlank(fb.getName())) { - logger.error("Missing required fields, id: {}, name: {}", fbID, fb.getName()); + long fbID = NumberUtils.toLong(fb.id(), 0); + if (fbID == 0 || StringUtils.isBlank(fb.name())) { + logger.error("Missing required fields, id: {}, name: {}", fbID, fb.name()); throw new HttpBadRequestException(); } Optional<com.juick.model.User> existingFacebookUser = userService.getUserByFacebookId(fbID); if (existingFacebookUser.isPresent()) { - if (!userService.updateFacebookUser(fbID, token.getAccessToken(), fb.getName())) { + if (!userService.updateFacebookUser(fbID, token.getAccessToken(), fb.name())) { logger.error("error updating facebook user, id: {}, token: {}", fbID, token.getAccessToken()); throw new HttpBadRequestException(); } - if (StringUtils.isNotEmpty(fb.getEmail())) { - logger.info("found {} for facebook user {}", fb.getEmail(), fb.getName()); + if (StringUtils.isNotEmpty(fb.email())) { + logger.info("found {} for facebook user {}", fb.email(), fb.name()); Integer userId = existingFacebookUser.get().getUid(); - if (!emailService.getEmails(userId, false).contains(fb.getEmail())) { - emailService.addEmail(userId, fb.getEmail()); + if (!emailService.getEmails(userId, false).contains(fb.email())) { + emailService.addEmail(userId, fb.email()); } } UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(redirectUrl); @@ -173,7 +173,7 @@ public class ApiSocialLogin { uriComponentsBuilder.queryParam("retpath", redirectUrl); return "redirect:" + uriComponentsBuilder.build().toUriString(); } else { - if (!userService.createFacebookUser(fbID, state, token.getAccessToken(), fb.getName())) { + if (!userService.createFacebookUser(fbID, state, token.getAccessToken(), fb.name())) { throw new HttpBadRequestException(); } return "redirect:/signup?type=fb&hash=" + state; @@ -199,16 +199,16 @@ public class ApiSocialLogin { vkAuthService.signRequest(token, meRequest); String graph = vkAuthService.execute(meRequest).getBody(); - com.juick.model.ext.vk.User jsonUser = jsonMapper.readValue(graph, UsersResponse.class).getUsers().get(0); - String vkName = jsonUser.getFirstName() + " " + jsonUser.getLastName(); - String vkLink = jsonUser.getScreenName(); + com.juick.model.ext.vk.User jsonUser = jsonMapper.readValue(graph, UsersResponse.class).users().get(0); + String vkName = jsonUser.firstName() + " " + jsonUser.lastName(); + String vkLink = jsonUser.screenName(); if (vkName.length() == 1 || StringUtils.isBlank(vkLink)) { logger.error("vk user error"); throw new HttpBadRequestException(); } - long vkID = NumberUtils.toLong(jsonUser.getId(), 0); + long vkID = NumberUtils.toLong(jsonUser.id(), 0); int uid = userService.getUIDbyVKID(vkID); if (uid > 0) { UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(redirectUrl); @@ -226,8 +226,7 @@ public class ApiSocialLogin { } @ResponseBody @PostMapping("/api/_google") - public AuthResponse googleSignIn(@RequestParam(name = "idToken") String idTokenString) - throws GeneralSecurityException, IOException { + public AuthResponse googleSignIn(@RequestParam(name = "idToken") String idTokenString) { logger.info("Token: {}", idTokenString); logger.info("Client: {}", googleClientId); Optional<String> verifiedEmail = GoogleTokenVerifier.validateToken(googleClientId, idTokenString); diff --git a/src/main/java/com/juick/www/controllers/SocialLogin.java b/src/main/java/com/juick/www/controllers/SocialLogin.java index dad3d969..c9611543 100644 --- a/src/main/java/com/juick/www/controllers/SocialLogin.java +++ b/src/main/java/com/juick/www/controllers/SocialLogin.java @@ -154,23 +154,23 @@ public class SocialLogin { throw new HttpBadRequestException(); } User fb = jsonMapper.readValue(graph, User.class); - long fbID = NumberUtils.toLong(fb.getId(), 0); - if (fbID == 0 || StringUtils.isBlank(fb.getName())) { - logger.error("Missing required fields, id: {}, name: {}", fbID, fb.getName()); + long fbID = NumberUtils.toLong(fb.id(), 0); + if (fbID == 0 || StringUtils.isBlank(fb.name())) { + logger.error("Missing required fields, id: {}, name: {}", fbID, fb.name()); throw new HttpBadRequestException(); } Optional<com.juick.model.User> existingFacebookUser = userService.getUserByFacebookId(fbID); if (existingFacebookUser.isPresent()) { - if (!userService.updateFacebookUser(fbID, token.getAccessToken(), fb.getName())) { + if (!userService.updateFacebookUser(fbID, token.getAccessToken(), fb.name())) { logger.error("error updating facebook user, id: {}, token: {}", fbID, token.getAccessToken()); throw new HttpBadRequestException(); } - if (StringUtils.isNotEmpty(fb.getEmail())) { - logger.info("found {} for facebook user {}", fb.getEmail(), fb.getName()); + if (StringUtils.isNotEmpty(fb.email())) { + logger.info("found {} for facebook user {}", fb.email(), fb.name()); Optional<com.juick.model.User> newFacebookUser = userService.getUserByFacebookId(fbID); - if (!emailService.getEmails(newFacebookUser.get().getUid(), false).contains(fb.getEmail())) { - emailService.addEmail(newFacebookUser.get().getUid(), fb.getEmail()); + if (!emailService.getEmails(newFacebookUser.get().getUid(), false).contains(fb.email())) { + emailService.addEmail(newFacebookUser.get().getUid(), fb.email()); } } if (!existingFacebookUser.get().isBanned()) { @@ -183,7 +183,7 @@ public class SocialLogin { return "redirect:/login"; } } else { - if (!userService.createFacebookUser(fbID, state, token.getAccessToken(), fb.getName())) { + if (!userService.createFacebookUser(fbID, state, token.getAccessToken(), fb.name())) { throw new HttpBadRequestException(); } return "redirect:/signup?type=fb&hash=" + state; @@ -226,7 +226,7 @@ public class SocialLogin { com.juick.model.ext.twitter.User twitterUser = jsonMapper.readValue( oAuthService.execute(oAuthRequest).getBody(), com.juick.model.ext.twitter.User.class); if (userService.linkTwitterAccount(user, accessToken.getToken(), accessToken.getTokenSecret(), - twitterUser.getScreenName())) { + twitterUser.screenName())) { response.setStatus(HttpServletResponse.SC_FOUND); response.setHeader("Location", "http://juick.com/settings"); } else { @@ -264,17 +264,17 @@ public class SocialLogin { Response vkResponse = vkAuthService.execute(meRequest); if (vkResponse.isSuccessful()) { String graph = vkResponse.getBody(); - com.juick.model.ext.vk.User jsonUser = jsonMapper.readValue(graph, UsersResponse.class).getUsers().stream() + com.juick.model.ext.vk.User jsonUser = jsonMapper.readValue(graph, UsersResponse.class).users().stream() .findFirst().orElseThrow(HttpBadRequestException::new); - String vkName = jsonUser.getFirstName() + " " + jsonUser.getLastName(); - String vkLink = jsonUser.getScreenName(); + String vkName = jsonUser.firstName() + " " + jsonUser.lastName(); + String vkLink = jsonUser.screenName(); if (vkName.length() == 1 || StringUtils.isBlank(vkLink)) { logger.error("vk user error"); throw new HttpBadRequestException(); } - long vkID = NumberUtils.toLong(jsonUser.getId(), 0); + long vkID = NumberUtils.toLong(jsonUser.id(), 0); int uid = userService.getUIDbyVKID(vkID); if (uid > 0) { Cookie c = new Cookie("hash", userService.getHashByUID(uid)); |