| Michael Hanl | 8ee3111 | 2016-07-21 14:10:24 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.filter; |
| 2 | |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 3 | import java.security.Principal; |
| Michael Hanl | 8ee3111 | 2016-07-21 14:10:24 +0200 | [diff] [blame] | 4 | |
| margaretha | bdde7f4 | 2023-02-10 08:24:03 +0100 | [diff] [blame] | 5 | import org.glassfish.jersey.server.ContainerRequest; |
| margaretha | ade7d4a | 2017-07-20 19:53:35 +0200 | [diff] [blame] | 6 | import org.springframework.beans.factory.annotation.Autowired; |
| 7 | import org.springframework.stereotype.Component; |
| 8 | |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 9 | import de.ids_mannheim.korap.config.KustvaktConfiguration; |
| 10 | import de.ids_mannheim.korap.constant.TokenType; |
| 11 | import de.ids_mannheim.korap.security.context.KustvaktContext; |
| 12 | import de.ids_mannheim.korap.security.context.TokenContext; |
| 13 | import de.ids_mannheim.korap.user.User; |
| 14 | import de.ids_mannheim.korap.utils.TimeUtils; |
| margaretha | db5da37 | 2023-09-01 11:02:52 +0200 | [diff] [blame] | 15 | import jakarta.annotation.Priority; |
| margaretha | 96c309d | 2023-08-16 12:24:12 +0200 | [diff] [blame] | 16 | import jakarta.ws.rs.Priorities; |
| 17 | import jakarta.ws.rs.container.ContainerRequestContext; |
| 18 | import jakarta.ws.rs.container.ContainerRequestFilter; |
| 19 | import jakarta.ws.rs.core.Context; |
| 20 | import jakarta.ws.rs.core.SecurityContext; |
| 21 | import jakarta.ws.rs.core.UriInfo; |
| Michael Hanl | 8ee3111 | 2016-07-21 14:10:24 +0200 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * Created by hanl on 7/15/14. |
| 25 | */ |
| margaretha | ade7d4a | 2017-07-20 19:53:35 +0200 | [diff] [blame] | 26 | @Component |
| abcpro1 | 2cb86c6 | 2022-11-07 18:46:36 +0000 | [diff] [blame] | 27 | @Priority(Priorities.AUTHENTICATION) |
| abcpro1 | 136ff59 | 2022-11-07 18:25:03 +0000 | [diff] [blame] | 28 | public class DemoUserFilter implements ContainerRequestFilter { |
| Michael Hanl | 8ee3111 | 2016-07-21 14:10:24 +0200 | [diff] [blame] | 29 | |
| 30 | @Context |
| 31 | UriInfo info; |
| margaretha | ade7d4a | 2017-07-20 19:53:35 +0200 | [diff] [blame] | 32 | @Autowired |
| 33 | private KustvaktConfiguration config; |
| Michael Hanl | 8ee3111 | 2016-07-21 14:10:24 +0200 | [diff] [blame] | 34 | |
| Michael Hanl | 8ee3111 | 2016-07-21 14:10:24 +0200 | [diff] [blame] | 35 | @Override |
| abcpro1 | 136ff59 | 2022-11-07 18:25:03 +0000 | [diff] [blame] | 36 | public void filter (ContainerRequestContext request) { |
| abcpro1 | a94a042 | 2022-11-07 20:07:23 +0000 | [diff] [blame] | 37 | String host = request.getHeaderString(ContainerRequest.HOST); |
| 38 | String ua = request.getHeaderString(ContainerRequest.USER_AGENT); |
| Michael Hanl | 8ee3111 | 2016-07-21 14:10:24 +0200 | [diff] [blame] | 39 | String authentication = request |
| abcpro1 | a94a042 | 2022-11-07 20:07:23 +0000 | [diff] [blame] | 40 | .getHeaderString(ContainerRequest.AUTHORIZATION); |
| Michael Hanl | 8ee3111 | 2016-07-21 14:10:24 +0200 | [diff] [blame] | 41 | |
| 42 | // means that this is the public service |
| 43 | if (authentication == null || authentication.isEmpty()) { |
| 44 | Principal pr = null; |
| abcpro1 | 0298407 | 2022-11-07 19:55:21 +0000 | [diff] [blame] | 45 | SecurityContext securityContext = request.getSecurityContext(); |
| 46 | if (securityContext != null) { |
| 47 | pr = securityContext.getUserPrincipal(); |
| Michael Hanl | 8ee3111 | 2016-07-21 14:10:24 +0200 | [diff] [blame] | 48 | } |
| 49 | if (pr == null) |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 50 | request.setSecurityContext( |
| 51 | new KustvaktContext(createShorterToken(host, ua))); |
| Michael Hanl | 8ee3111 | 2016-07-21 14:10:24 +0200 | [diff] [blame] | 52 | } |
| Michael Hanl | 8ee3111 | 2016-07-21 14:10:24 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| Michael Hanl | 8ee3111 | 2016-07-21 14:10:24 +0200 | [diff] [blame] | 55 | private TokenContext createShorterToken (String host, String agent) { |
| 56 | User demo = User.UserFactory.getDemoUser(); |
| 57 | TokenContext c = new TokenContext(); |
| 58 | c.setUsername(demo.getUsername()); |
| 59 | c.setHostAddress(host); |
| 60 | c.setUserAgent(agent); |
| margaretha | bdde7f4 | 2023-02-10 08:24:03 +0100 | [diff] [blame] | 61 | c.setExpirationTime( |
| 62 | TimeUtils.plusSeconds(config.getShortTokenTTL()).getMillis()); |
| margaretha | 20f3123 | 2018-07-09 17:49:39 +0200 | [diff] [blame] | 63 | c.setTokenType(TokenType.BASIC); |
| Michael Hanl | 8ee3111 | 2016-07-21 14:10:24 +0200 | [diff] [blame] | 64 | return c; |
| 65 | } |
| Michael Hanl | 8ee3111 | 2016-07-21 14:10:24 +0200 | [diff] [blame] | 66 | } |