From a0b7a0f2a5ad8637545fa6d68c46822e90e117e5 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 18 Jan 2023 13:03:48 +0300 Subject: Cleanup unused code --- src/main/java/com/juick/KeystoreManager.java | 14 +--- src/main/java/com/juick/config/SecurityConfig.java | 1 - .../security/BearerTokenAuthenticationFilter.java | 82 ---------------------- 3 files changed, 1 insertion(+), 96 deletions(-) delete mode 100644 src/main/java/com/juick/service/security/BearerTokenAuthenticationFilter.java (limited to 'src') diff --git a/src/main/java/com/juick/KeystoreManager.java b/src/main/java/com/juick/KeystoreManager.java index da52411f..e00d99e8 100644 --- a/src/main/java/com/juick/KeystoreManager.java +++ b/src/main/java/com/juick/KeystoreManager.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008-2020, Juick + * Copyright (C) 2008-2023, 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 @@ -17,9 +17,7 @@ package com.juick; -import com.juick.model.User; import com.juick.www.api.activity.model.objects.Actor; -import io.jsonwebtoken.Jwts; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.Resource; @@ -32,9 +30,6 @@ import java.io.InputStream; import java.security.*; import java.security.cert.Certificate; import java.security.cert.CertificateException; -import java.time.ZonedDateTime; -import java.util.Arrays; -import java.util.Date; public class KeystoreManager { private static final Logger logger = LoggerFactory.getLogger("ActivityPub"); @@ -82,11 +77,4 @@ public class KeystoreManager { String pubkeyPem = person.getPublicKey().getPublicKeyPem(); return Keys.decode(pubkeyPem.getBytes()).getKey(); } - public String generateToken(User user) { - return Jwts.builder() - .setSubject(user.getName()) - .setIssuedAt(Date.from(ZonedDateTime.now().toInstant())) - .signWith(getPrivateKey()) - .compact(); - } } diff --git a/src/main/java/com/juick/config/SecurityConfig.java b/src/main/java/com/juick/config/SecurityConfig.java index 0b9e85ff..e73aa65d 100644 --- a/src/main/java/com/juick/config/SecurityConfig.java +++ b/src/main/java/com/juick/config/SecurityConfig.java @@ -68,7 +68,6 @@ import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.util.Arrays; import java.util.Collections; -import java.util.UUID; /** * Created by aalexeev on 11/21/16. diff --git a/src/main/java/com/juick/service/security/BearerTokenAuthenticationFilter.java b/src/main/java/com/juick/service/security/BearerTokenAuthenticationFilter.java deleted file mode 100644 index e5fdd738..00000000 --- a/src/main/java/com/juick/service/security/BearerTokenAuthenticationFilter.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (C) 2008-2023, 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 - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package com.juick.service.security; - -import com.juick.service.UserService; -import com.juick.service.security.entities.JuickUser; -import io.jsonwebtoken.JwtParser; -import io.jsonwebtoken.Jwts; -import jakarta.servlet.FilterChain; -import jakarta.servlet.ServletException; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.http.HttpHeaders; -import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.context.SecurityContextHolder; - -import javax.annotation.Nonnull; -import java.io.IOException; -import java.security.KeyPair; -import java.util.Collections; -import java.util.stream.Collectors; - -public class BearerTokenAuthenticationFilter extends BaseAuthenticationFilter { - private static final Logger logger = LoggerFactory.getLogger("Auth"); - private final JwtParser jwtParser; - private final UserService userService; - - public BearerTokenAuthenticationFilter(UserService userService, KeyPair keys) { - this.userService = userService; - this.jwtParser = Jwts.parserBuilder() - .setSigningKey(keys.getPrivate()) - .build(); - } - - @Override - protected void doFilterInternal(HttpServletRequest request, - HttpServletResponse response, - FilterChain filterChain) throws ServletException, IOException { - if (authenticationIsRequired()) { - var headers = Collections.list(request.getHeaderNames()) - .stream() - .collect(Collectors.toMap(String::toLowerCase, request::getHeader)); - var authorizationHeaderValue = headers.get(HttpHeaders.AUTHORIZATION.toLowerCase()); - if (StringUtils.isNotEmpty(authorizationHeaderValue) && authorizationHeaderValue.startsWith("Bearer")) { - String token = authorizationHeaderValue.substring(7); - try { - var claims = jwtParser.parseClaimsJws(token).getBody(); - var user = userService.getUserByName(claims.getSubject()); - if (!user.isAnonymous()) { - Authentication authentication = new UsernamePasswordAuthenticationToken( - new JuickUser(user), - user.getCredentials(), - JuickUser.USER_AUTHORITY); - SecurityContextHolder.getContext().setAuthentication(authentication); - } - } catch (Exception e) { - logger.warn("Invalid Bearer token: {}", e.getMessage()); - } - } - } - filterChain.doFilter(request, response); - } -} -- cgit v1.2.3