| margaretha | 139d0f7 | 2017-11-14 18:56:22 +0100 | [diff] [blame] | 1 | package de.ids_mannheim.korap.authentication; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 2 | |
| margaretha | 398f472 | 2019-01-09 19:07:20 +0100 | [diff] [blame] | 3 | import java.util.Map; |
| 4 | import java.util.concurrent.ScheduledThreadPoolExecutor; |
| 5 | import java.util.concurrent.TimeUnit; |
| margaretha | 49cb688 | 2018-07-04 04:19:54 +0200 | [diff] [blame] | 6 | |
| 7 | import org.apache.logging.log4j.LogManager; |
| 8 | import org.apache.logging.log4j.Logger; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 9 | import org.joda.time.DateTime; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 10 | |
| margaretha | 398f472 | 2019-01-09 19:07:20 +0100 | [diff] [blame] | 11 | import de.ids_mannheim.korap.config.Attributes; |
| 12 | import de.ids_mannheim.korap.config.KustvaktConfiguration; |
| 13 | import de.ids_mannheim.korap.constant.TokenType; |
| 14 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 15 | import de.ids_mannheim.korap.interfaces.EncryptionIface; |
| 16 | import de.ids_mannheim.korap.security.context.TokenContext; |
| 17 | import de.ids_mannheim.korap.user.User; |
| 18 | import de.ids_mannheim.korap.utils.TimeUtils; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 19 | |
| 20 | /** |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 21 | * implementation of the AuthenticationIface to handle korap |
| 22 | * authentication |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 23 | * internals |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 24 | * |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 25 | * @author hanl |
| 26 | */ |
| margaretha | 8b68221 | 2023-06-12 11:04:18 +0200 | [diff] [blame] | 27 | @Deprecated |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 28 | public class SessionAuthentication implements AuthenticationIface { |
| 29 | |
| margaretha | 49cb688 | 2018-07-04 04:19:54 +0200 | [diff] [blame] | 30 | private static final Logger jlog = LogManager |
| Michael Hanl | fdd9a01 | 2015-11-13 15:56:38 +0100 | [diff] [blame] | 31 | .getLogger(SessionAuthentication.class); |
| margaretha | dda4ef7 | 2018-12-06 14:20:51 +0100 | [diff] [blame] | 32 | public static boolean DEBUG = false; |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 33 | |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 34 | public static SessionFactory sessions; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 35 | private ScheduledThreadPoolExecutor scheduled; |
| 36 | private EncryptionIface crypto; |
| 37 | private KustvaktConfiguration config; |
| 38 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 39 | public SessionAuthentication (KustvaktConfiguration config, |
| 40 | EncryptionIface crypto) { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 41 | jlog.info("initialize session authentication handler"); |
| 42 | this.crypto = crypto; |
| 43 | this.config = config; |
| 44 | this.scheduled = new ScheduledThreadPoolExecutor(1); |
| 45 | this.sessions = new SessionFactory(this.config.isAllowMultiLogIn(), |
| 46 | this.config.getInactiveTime()); |
| 47 | this.scheduled.scheduleAtFixedRate(this.sessions, |
| 48 | this.config.getInactiveTime() / 2, |
| 49 | this.config.getInactiveTime(), TimeUnit.SECONDS); |
| 50 | } |
| 51 | |
| 52 | @Override |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 53 | public TokenContext getTokenContext (String authenticationToken) |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 54 | throws KustvaktException { |
| margaretha | dda4ef7 | 2018-12-06 14:20:51 +0100 | [diff] [blame] | 55 | if (DEBUG) { |
| 56 | jlog.debug( |
| 57 | "retrieving user session for user " + authenticationToken); |
| 58 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 59 | return this.sessions.getSession(authenticationToken); |
| 60 | } |
| 61 | |
| 62 | @Override |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 63 | public TokenContext createTokenContext (User user, Map<String, Object> attr) |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 64 | throws KustvaktException { |
| 65 | DateTime now = TimeUtils.getNow(); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 66 | DateTime ex = TimeUtils.getExpiration(now.getMillis(), |
| Michael Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 67 | config.getShortTokenTTL()); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 68 | String token = crypto.createToken(true, user.getUsername(), |
| 69 | now.getMillis()); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 70 | TokenContext ctx = new TokenContext(); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 71 | ctx.setUsername(user.getUsername()); |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 72 | ctx.setTokenType(TokenType.SESSION); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 73 | ctx.setToken(token); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 74 | ctx.setExpirationTime(ex.getMillis() + (1000)); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 75 | ctx.setHostAddress(attr.get(Attributes.HOST).toString()); |
| 76 | ctx.setUserAgent(attr.get(Attributes.USER_AGENT).toString()); |
| 77 | this.sessions.putSession(token, ctx); |
| margaretha | dda4ef7 | 2018-12-06 14:20:51 +0100 | [diff] [blame] | 78 | if (DEBUG) { |
| 79 | jlog.debug(ctx.toJson()); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 80 | jlog.debug("session " + sessions.getSession(token).toString()); |
| margaretha | dda4ef7 | 2018-12-06 14:20:51 +0100 | [diff] [blame] | 81 | jlog.info("create session for user: " + user.getUsername()); |
| 82 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 83 | return ctx; |
| 84 | } |
| 85 | |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 86 | // @Override |
| 87 | // public void removeUserSession (String token) { |
| 88 | // this.sessions.removeSession(token); |
| 89 | // } |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 90 | |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 91 | @Override |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 92 | public TokenType getTokenType () { |
| 93 | return TokenType.SESSION; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | } |