blob: 7d63c56843a70803e6590bb8a60d7202d491bf77 [file] [log] [blame]
margaretha139d0f72017-11-14 18:56:22 +01001package de.ids_mannheim.korap.authentication;
Michael Hanl87106d12015-09-14 18:13:51 +02002
margaretha398f4722019-01-09 19:07:20 +01003import java.util.Map;
4import java.util.concurrent.ScheduledThreadPoolExecutor;
5import java.util.concurrent.TimeUnit;
margaretha49cb6882018-07-04 04:19:54 +02006
7import org.apache.logging.log4j.LogManager;
8import org.apache.logging.log4j.Logger;
Michael Hanl87106d12015-09-14 18:13:51 +02009import org.joda.time.DateTime;
Michael Hanl87106d12015-09-14 18:13:51 +020010
margaretha398f4722019-01-09 19:07:20 +010011import de.ids_mannheim.korap.config.Attributes;
12import de.ids_mannheim.korap.config.KustvaktConfiguration;
13import de.ids_mannheim.korap.constant.TokenType;
14import de.ids_mannheim.korap.exceptions.KustvaktException;
15import de.ids_mannheim.korap.interfaces.EncryptionIface;
16import de.ids_mannheim.korap.security.context.TokenContext;
17import de.ids_mannheim.korap.user.User;
18import de.ids_mannheim.korap.utils.TimeUtils;
Michael Hanl87106d12015-09-14 18:13:51 +020019
20/**
Michael Hanl8abaf9e2016-05-23 16:46:35 +020021 * implementation of the AuthenticationIface to handle korap
22 * authentication
Michael Hanl87106d12015-09-14 18:13:51 +020023 * internals
Michael Hanl8abaf9e2016-05-23 16:46:35 +020024 *
Michael Hanl87106d12015-09-14 18:13:51 +020025 * @author hanl
26 */
margaretha8b682212023-06-12 11:04:18 +020027@Deprecated
Michael Hanl87106d12015-09-14 18:13:51 +020028public class SessionAuthentication implements AuthenticationIface {
29
margaretha49cb6882018-07-04 04:19:54 +020030 private static final Logger jlog = LogManager
Michael Hanlfdd9a012015-11-13 15:56:38 +010031 .getLogger(SessionAuthentication.class);
margarethadda4ef72018-12-06 14:20:51 +010032 public static boolean DEBUG = false;
margaretha35e1ca22023-11-16 22:00:01 +010033
margarethaf18298b2017-09-14 22:14:32 +020034 public static SessionFactory sessions;
Michael Hanl87106d12015-09-14 18:13:51 +020035 private ScheduledThreadPoolExecutor scheduled;
36 private EncryptionIface crypto;
37 private KustvaktConfiguration config;
38
Michael Hanl8abaf9e2016-05-23 16:46:35 +020039 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
52 @Override
margaretha35e1ca22023-11-16 22:00:01 +010053 public TokenContext getTokenContext (String authenticationToken)
Michael Hanl87106d12015-09-14 18:13:51 +020054 throws KustvaktException {
margarethadda4ef72018-12-06 14:20:51 +010055 if (DEBUG) {
56 jlog.debug(
57 "retrieving user session for user " + authenticationToken);
58 }
Michael Hanl87106d12015-09-14 18:13:51 +020059 return this.sessions.getSession(authenticationToken);
60 }
61
62 @Override
margaretha35e1ca22023-11-16 22:00:01 +010063 public TokenContext createTokenContext (User user, Map<String, Object> attr)
Michael Hanl87106d12015-09-14 18:13:51 +020064 throws KustvaktException {
65 DateTime now = TimeUtils.getNow();
Michael Hanl8abaf9e2016-05-23 16:46:35 +020066 DateTime ex = TimeUtils.getExpiration(now.getMillis(),
Michael Hanl2c3b0b12016-07-01 18:30:12 +020067 config.getShortTokenTTL());
Michael Hanl8abaf9e2016-05-23 16:46:35 +020068 String token = crypto.createToken(true, user.getUsername(),
69 now.getMillis());
Michael Hanle25dea22015-09-24 19:37:56 +020070 TokenContext ctx = new TokenContext();
Michael Hanl87106d12015-09-14 18:13:51 +020071 ctx.setUsername(user.getUsername());
margaretha2afb97d2017-12-07 19:18:44 +010072 ctx.setTokenType(TokenType.SESSION);
Michael Hanl87106d12015-09-14 18:13:51 +020073 ctx.setToken(token);
margaretha35e1ca22023-11-16 22:00:01 +010074 ctx.setExpirationTime(ex.getMillis() + (1000));
Michael Hanl87106d12015-09-14 18:13:51 +020075 ctx.setHostAddress(attr.get(Attributes.HOST).toString());
76 ctx.setUserAgent(attr.get(Attributes.USER_AGENT).toString());
77 this.sessions.putSession(token, ctx);
margarethadda4ef72018-12-06 14:20:51 +010078 if (DEBUG) {
79 jlog.debug(ctx.toJson());
margaretha35e1ca22023-11-16 22:00:01 +010080 jlog.debug("session " + sessions.getSession(token).toString());
margarethadda4ef72018-12-06 14:20:51 +010081 jlog.info("create session for user: " + user.getUsername());
82 }
Michael Hanl87106d12015-09-14 18:13:51 +020083 return ctx;
84 }
85
margaretha35e1ca22023-11-16 22:00:01 +010086 // @Override
87 // public void removeUserSession (String token) {
88 // this.sessions.removeSession(token);
89 // }
Michael Hanl8abaf9e2016-05-23 16:46:35 +020090
Michael Hanl87106d12015-09-14 18:13:51 +020091 @Override
margaretha2afb97d2017-12-07 19:18:44 +010092 public TokenType getTokenType () {
93 return TokenType.SESSION;
Michael Hanl87106d12015-09-14 18:13:51 +020094 }
95
96}