blob: 1bb4c6db6de231018ee73387dce5ab8401cb6df1 [file] [log] [blame]
margaretha139d0f72017-11-14 18:56:22 +01001package de.ids_mannheim.korap.authentication;
Michael Hanl87106d12015-09-14 18:13:51 +02002
3import de.ids_mannheim.korap.config.KustvaktConfiguration;
margaretha0e8f4e72018-04-05 14:11:52 +02004import de.ids_mannheim.korap.constant.TokenType;
Michael Hanl87106d12015-09-14 18:13:51 +02005import de.ids_mannheim.korap.exceptions.KustvaktException;
6import de.ids_mannheim.korap.exceptions.StatusCodes;
7import de.ids_mannheim.korap.interfaces.AuthenticationIface;
8import de.ids_mannheim.korap.interfaces.EncryptionIface;
margaretha0e8f4e72018-04-05 14:11:52 +02009import de.ids_mannheim.korap.security.context.TokenContext;
Michael Hanl00b64e02016-05-24 20:24:27 +020010import de.ids_mannheim.korap.config.Attributes;
Michael Hanl87106d12015-09-14 18:13:51 +020011import de.ids_mannheim.korap.user.User;
Michael Hanl87106d12015-09-14 18:13:51 +020012import de.ids_mannheim.korap.utils.TimeUtils;
margaretha49cb6882018-07-04 04:19:54 +020013
14import org.apache.logging.log4j.LogManager;
15import org.apache.logging.log4j.Logger;
Michael Hanl87106d12015-09-14 18:13:51 +020016import org.joda.time.DateTime;
Michael Hanl87106d12015-09-14 18:13:51 +020017
18import java.util.Map;
19import java.util.concurrent.ScheduledThreadPoolExecutor;
20import java.util.concurrent.TimeUnit;
21
22/**
Michael Hanl8abaf9e2016-05-23 16:46:35 +020023 * implementation of the AuthenticationIface to handle korap
24 * authentication
Michael Hanl87106d12015-09-14 18:13:51 +020025 * internals
Michael Hanl8abaf9e2016-05-23 16:46:35 +020026 *
Michael Hanl87106d12015-09-14 18:13:51 +020027 * @author hanl
28 */
29public class SessionAuthentication implements AuthenticationIface {
30
margaretha49cb6882018-07-04 04:19:54 +020031 private static final Logger jlog = LogManager
Michael Hanlfdd9a012015-11-13 15:56:38 +010032 .getLogger(SessionAuthentication.class);
margarethaf18298b2017-09-14 22:14:32 +020033 public static SessionFactory sessions;
Michael Hanl87106d12015-09-14 18:13:51 +020034 private ScheduledThreadPoolExecutor scheduled;
35 private EncryptionIface crypto;
36 private KustvaktConfiguration config;
37
Michael Hanl8abaf9e2016-05-23 16:46:35 +020038
39 public SessionAuthentication (KustvaktConfiguration config,
40 EncryptionIface crypto) {
Michael Hanl87106d12015-09-14 18:13:51 +020041 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 Hanl8abaf9e2016-05-23 16:46:35 +020052
Michael Hanl87106d12015-09-14 18:13:51 +020053 @Override
Michael Hanlc0ed00f2016-06-23 14:33:10 +020054 public TokenContext getTokenContext(String authenticationToken)
Michael Hanl87106d12015-09-14 18:13:51 +020055 throws KustvaktException {
margaretha49cb6882018-07-04 04:19:54 +020056 jlog.debug("retrieving user session for user "+ authenticationToken);
Michael Hanl87106d12015-09-14 18:13:51 +020057 return this.sessions.getSession(authenticationToken);
58 }
59
Michael Hanl8abaf9e2016-05-23 16:46:35 +020060
Michael Hanl87106d12015-09-14 18:13:51 +020061 @Override
Michael Hanlc0ed00f2016-06-23 14:33:10 +020062 public TokenContext createTokenContext(User user, Map<String, Object> attr)
Michael Hanl87106d12015-09-14 18:13:51 +020063 throws KustvaktException {
64 DateTime now = TimeUtils.getNow();
Michael Hanl8abaf9e2016-05-23 16:46:35 +020065 DateTime ex = TimeUtils.getExpiration(now.getMillis(),
Michael Hanl2c3b0b12016-07-01 18:30:12 +020066 config.getShortTokenTTL());
Michael Hanl8abaf9e2016-05-23 16:46:35 +020067 String token = crypto.createToken(true, user.getUsername(),
68 now.getMillis());
Michael Hanle25dea22015-09-24 19:37:56 +020069 TokenContext ctx = new TokenContext();
Michael Hanl87106d12015-09-14 18:13:51 +020070 ctx.setUsername(user.getUsername());
margaretha2afb97d2017-12-07 19:18:44 +010071 ctx.setTokenType(TokenType.SESSION);
Michael Hanl87106d12015-09-14 18:13:51 +020072 ctx.setToken(token);
margarethaf18298b2017-09-14 22:14:32 +020073 ctx.setExpirationTime(ex.getMillis()+(1000));
Michael Hanl87106d12015-09-14 18:13:51 +020074 ctx.setHostAddress(attr.get(Attributes.HOST).toString());
75 ctx.setUserAgent(attr.get(Attributes.USER_AGENT).toString());
margarethaf18298b2017-09-14 22:14:32 +020076 jlog.debug(ctx.toJson());
Michael Hanl87106d12015-09-14 18:13:51 +020077 this.sessions.putSession(token, ctx);
margarethaf18298b2017-09-14 22:14:32 +020078 jlog.debug("session " +sessions.getSession(token).toString());
Michael Hanl87106d12015-09-14 18:13:51 +020079 jlog.info("create session for user: " + user.getUsername());
80 return ctx;
81 }
82
Michael Hanl8abaf9e2016-05-23 16:46:35 +020083
Michael Hanl87106d12015-09-14 18:13:51 +020084 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +020085 public void removeUserSession (String token) {
Michael Hanl87106d12015-09-14 18:13:51 +020086 this.sessions.removeSession(token);
87 }
88
Michael Hanl8abaf9e2016-05-23 16:46:35 +020089
Michael Hanl87106d12015-09-14 18:13:51 +020090 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +020091 public TokenContext refresh (TokenContext context) throws KustvaktException {
Michael Hanl87106d12015-09-14 18:13:51 +020092 throw new UnsupportedOperationException("method not supported");
93 }
94
Michael Hanl8abaf9e2016-05-23 16:46:35 +020095
Michael Hanl87106d12015-09-14 18:13:51 +020096 @Override
margaretha2afb97d2017-12-07 19:18:44 +010097 public TokenType getTokenType () {
98 return TokenType.SESSION;
Michael Hanl87106d12015-09-14 18:13:51 +020099 }
100
101}