diff options
author | Vitaly Takmazov | 2023-01-04 03:38:19 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2023-01-04 05:46:16 +0300 |
commit | c471503ede9aad91193ff6f93966196e6aff15d6 (patch) | |
tree | 8c70c8f58b140465be651cd019f26eadd476711f /src/main/java/com/juick/www/SiteAttributesHandler.java | |
parent | 086d9a7625bfc5a386f5b1028d364fb546c2fa9d (diff) |
OAuth authentication for Mastodon and ActivityPub C2S
Diffstat (limited to 'src/main/java/com/juick/www/SiteAttributesHandler.java')
-rw-r--r-- | src/main/java/com/juick/www/SiteAttributesHandler.java | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/main/java/com/juick/www/SiteAttributesHandler.java b/src/main/java/com/juick/www/SiteAttributesHandler.java index e06a2070..4ead9d1e 100644 --- a/src/main/java/com/juick/www/SiteAttributesHandler.java +++ b/src/main/java/com/juick/www/SiteAttributesHandler.java @@ -17,6 +17,14 @@ package com.juick.www; +import com.juick.model.AnonymousUser; +import com.juick.model.User; +import com.juick.service.UserService; +import com.juick.service.security.entities.JuickUser; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.security.oauth2.jwt.Jwt; +import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ModelAttribute; @@ -24,8 +32,12 @@ import org.springframework.web.servlet.support.ServletUriComponentsBuilder; import jakarta.servlet.http.HttpServletResponse; +import javax.inject.Inject; + @ControllerAdvice public class SiteAttributesHandler { + @Inject + private UserService userService; @ModelAttribute public void setVaryResponseHeader(HttpServletResponse response) { response.setHeader("Vary", "Accept-Language"); @@ -34,4 +46,17 @@ public class SiteAttributesHandler { public void setReturnPathAttribute(Model model) { model.addAttribute("retpath", ServletUriComponentsBuilder.fromCurrentRequestUri().toUriString()); } + @ModelAttribute + public User visitor(Model model, @AuthenticationPrincipal Object principal) { + if (principal != null) { + if (principal instanceof JuickUser) { + return ((JuickUser) principal).getUser(); + } + if (principal instanceof Jwt) { + var uname = (String) ((Jwt) principal).getClaims().get("sub"); + return userService.getUserByName(uname); + } + } + return AnonymousUser.INSTANCE; + } } |