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