From c471503ede9aad91193ff6f93966196e6aff15d6 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 4 Jan 2023 03:38:19 +0300 Subject: OAuth authentication for Mastodon and ActivityPub C2S --- src/main/java/com/juick/www/controllers/Site.java | 32 +++++++++-------------- 1 file changed, 13 insertions(+), 19 deletions(-) (limited to 'src/main/java/com/juick/www/controllers/Site.java') diff --git a/src/main/java/com/juick/www/controllers/Site.java b/src/main/java/com/juick/www/controllers/Site.java index f45fe8f2..e8acc650 100644 --- a/src/main/java/com/juick/www/controllers/Site.java +++ b/src/main/java/com/juick/www/controllers/Site.java @@ -30,7 +30,6 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpSession; import com.juick.service.*; -import com.juick.service.security.annotation.Visitor; import com.juick.util.MessageUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.Pair; @@ -43,12 +42,7 @@ import org.springframework.security.core.AuthenticationException; import org.springframework.security.web.WebAttributes; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; -import org.springframework.web.bind.annotation.CookieValue; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestHeader; -import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.*; import javax.inject.Inject; import java.net.URLEncoder; @@ -99,7 +93,7 @@ public class Site { } @GetMapping("/login") - public String getloginForm(@Visitor User visitor, + public String getloginForm(@ModelAttribute User visitor, @RequestParam(name = "retpath", required = false, defaultValue = "/") String retPath, HttpSession session, ModelMap model) { @@ -125,7 +119,7 @@ public class Site { } @GetMapping("/") - protected String doGet(@Visitor User visitor, Locale locale, @RequestParam(required = false) String tag, + protected String doGet(@ModelAttribute User visitor, Locale locale, @RequestParam(required = false) String tag, @RequestParam(name = "show", required = false) String paramShow, @RequestParam(name = "search", required = false) String paramSearch, @RequestParam(name = "before", required = false, defaultValue = "0") Integer paramBefore, @@ -217,7 +211,7 @@ public class Site { } @GetMapping(path = "/{uname}/", headers = "Connection!=Upgrade") - protected String doGetBlog(@Visitor User visitor, @RequestParam(required = false, name = "show") String paramShow, + protected String doGetBlog(@ModelAttribute User visitor, @RequestParam(required = false, name = "show") String paramShow, @RequestParam(required = false, name = "tag") String paramTagStr, @RequestParam(required = false, name = "search") String paramSearch, @RequestParam(required = false, name = "page", defaultValue = "0") Integer page, @PathVariable String uname, @@ -324,7 +318,7 @@ public class Site { } @GetMapping("/{uname}/tags") - protected String doGetTags(@Visitor User visitor, @PathVariable String uname, ModelMap model) { + protected String doGetTags(@ModelAttribute User visitor, @PathVariable String uname, ModelMap model) { User user = userService.getUserByName(uname); if (visitor.isBanned()) { throw new HttpNotFoundException(); @@ -344,7 +338,7 @@ public class Site { } @GetMapping("/{uname}/friends") - protected String doGetFriends(@Visitor User visitor, @PathVariable String uname, ModelMap model) { + protected String doGetFriends(@ModelAttribute User visitor, @PathVariable String uname, ModelMap model) { User user = userService.getUserByName(uname); if (visitor.isBanned()) { throw new HttpNotFoundException(); @@ -360,7 +354,7 @@ public class Site { } @GetMapping("/{uname}/readers") - protected String doGetReaders(@Visitor User visitor, @PathVariable String uname, ModelMap model) { + protected String doGetReaders(@ModelAttribute User visitor, @PathVariable String uname, ModelMap model) { User user = userService.getUserByName(uname); visitor.setAvatar(webApp.getAvatarWebPath(visitor)); model.addAttribute("title", "Читатели " + user.getName()); @@ -373,7 +367,7 @@ public class Site { } @GetMapping("/{uname}/bl") - protected String doGetBL(@Visitor User visitor, @PathVariable String uname, ModelMap model) { + protected String doGetBL(@ModelAttribute User visitor, @PathVariable String uname, ModelMap model) { User user = userService.getUserByName(uname); if (visitor.getUid() != user.getUid()) { throw new HttpForbiddenException(); @@ -389,7 +383,7 @@ public class Site { } @GetMapping("/tag/{tagName}") - protected String tagAction(@Visitor User visitor, HttpServletRequest request, @PathVariable String tagName, + protected String tagAction(@ModelAttribute User visitor, HttpServletRequest request, @PathVariable String tagName, @RequestParam(required = false, defaultValue = "0") int before, ModelMap model) { visitor.setAvatar(webApp.getAvatarWebPath(visitor)); String paramTagStr = StringEscapeUtils.unescapeHtml4(tagName); @@ -452,7 +446,7 @@ public class Site { } @GetMapping("/pm/inbox") - protected String doGetInbox(@Visitor User visitor, ModelMap model) { + protected String doGetInbox(@ModelAttribute User visitor, ModelMap model) { visitor.setAvatar(webApp.getAvatarWebPath(visitor)); String title = "PM: Inbox"; List msgs = chatService.getInbox(visitor.getUid()); @@ -466,7 +460,7 @@ public class Site { } @GetMapping("/pm/sent") - protected String doGetSent(@Visitor User visitor, @RequestParam(required = false) String uname, ModelMap model) { + protected String doGetSent(@ModelAttribute User visitor, @RequestParam(required = false) String uname, ModelMap model) { visitor.setAvatar(webApp.getAvatarWebPath(visitor)); String title = "PM: Sent"; List msgs = chatService.getOutbox(visitor.getUid()); @@ -485,7 +479,7 @@ public class Site { @GetMapping(value = "/{uname}/{mid}", produces = { MediaType.TEXT_HTML_VALUE, Context.ACTIVITY_MEDIA_TYPE, Context.LD_JSON_MEDIA_TYPE }) - protected String threadAction(@Visitor User visitor, ModelMap model, @PathVariable String uname, + protected String threadAction(@ModelAttribute User visitor, ModelMap model, @PathVariable String uname, @PathVariable int mid, @RequestHeader(name = HttpHeaders.ACCEPT, required = false) String acceptHeader, @CookieValue(name = "sape_cookie", required = false, defaultValue = StringUtils.EMPTY) String sapeCookie) { @@ -589,7 +583,7 @@ public class Site { } @GetMapping("/post") - protected String postAction(@Visitor User visitor, @RequestParam(required = false) String body, ModelMap model) { + protected String postAction(@ModelAttribute User visitor, @RequestParam(required = false) String body, ModelMap model) { fillUserModel(model, visitor, visitor); visitor.setAvatar(webApp.getAvatarWebPath(visitor)); model.addAttribute("title", "Написать"); -- cgit v1.2.3