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