| 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.exceptions.KustvaktException; |
| 4 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| margaretha | 0e8f4e7 | 2018-04-05 14:11:52 +0200 | [diff] [blame] | 5 | import de.ids_mannheim.korap.security.context.TokenContext; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 6 | import de.ids_mannheim.korap.user.DemoUser; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 7 | import de.ids_mannheim.korap.utils.ConcurrentMultiMap; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 8 | import de.ids_mannheim.korap.utils.TimeUtils; |
| margaretha | 49cb688 | 2018-07-04 04:19:54 +0200 | [diff] [blame] | 9 | |
| 10 | import org.apache.logging.log4j.LogManager; |
| 11 | import org.apache.logging.log4j.Logger; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 12 | import org.joda.time.DateTime; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 13 | import org.springframework.cache.annotation.CacheEvict; |
| 14 | import org.springframework.cache.annotation.Cacheable; |
| 15 | |
| 16 | import java.util.HashSet; |
| Michael Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 17 | import java.util.List; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 18 | import java.util.Map.Entry; |
| 19 | import java.util.Set; |
| 20 | import java.util.concurrent.ConcurrentHashMap; |
| 21 | import java.util.concurrent.ConcurrentMap; |
| 22 | |
| 23 | /** |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 24 | * session object to hold current user sessions and track inactive |
| 25 | * time to close |
| 26 | * unused sessions. Inactive sessions are not enforced until user |
| 27 | * makes a |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 28 | * request through thrift |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 29 | * |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 30 | * @author hanl |
| 31 | */ |
| Michael Hanl | 7368aa4 | 2016-02-05 18:15:47 +0100 | [diff] [blame] | 32 | //todo: use simple ehcache! |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 33 | public class SessionFactory implements Runnable { |
| 34 | |
| margaretha | 49cb688 | 2018-07-04 04:19:54 +0200 | [diff] [blame] | 35 | private static Logger jlog = LogManager.getLogger(SessionFactory.class); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 36 | |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 37 | public static ConcurrentMap<String, TokenContext> sessionsObject; |
| 38 | public static ConcurrentMap<String, DateTime> timeCheck; |
| 39 | public static ConcurrentMultiMap<String, String> loggedInRecord; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 40 | // private final ConcurrentMultiMap<String, Long> failedLogins; |
| 41 | private final boolean multipleEnabled; |
| 42 | private final int inactive; |
| 43 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 44 | |
| 45 | public SessionFactory (boolean multipleEnabled, int inactive) { |
| margaretha | 49cb688 | 2018-07-04 04:19:54 +0200 | [diff] [blame] | 46 | jlog.debug("allow multiple sessions per user: "+ multipleEnabled); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 47 | this.multipleEnabled = multipleEnabled; |
| 48 | this.inactive = inactive; |
| 49 | this.sessionsObject = new ConcurrentHashMap<>(); |
| 50 | this.timeCheck = new ConcurrentHashMap<>(); |
| 51 | this.loggedInRecord = new ConcurrentMultiMap<>(); |
| 52 | } |
| 53 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 54 | |
| 55 | public boolean hasSession (TokenContext context) { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 56 | if (context.getUsername().equalsIgnoreCase(DemoUser.DEMOUSER_NAME)) |
| 57 | return false; |
| Michael Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 58 | |
| 59 | List<String> value = loggedInRecord.get(context.getUsername()); |
| 60 | return value != null && !value.isEmpty(); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| Michael Hanl | 2c3b0b1 | 2016-07-01 18:30:12 +0200 | [diff] [blame] | 63 | // todo: remove this! |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 64 | @Cacheable("session") |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 65 | public TokenContext getSession (String token) throws KustvaktException { |
| margaretha | 49cb688 | 2018-07-04 04:19:54 +0200 | [diff] [blame] | 66 | jlog.debug("logged in users: "+ loggedInRecord); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 67 | TokenContext context = sessionsObject.get(token); |
| 68 | if (context != null) { |
| Michael Hanl | 99cb963 | 2016-06-29 16:24:40 +0200 | [diff] [blame] | 69 | // fixme: set context to respecitve expiratin interval and return context. handler checks expiration later! |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 70 | if (isUserSessionValid(token)) { |
| 71 | resetInterval(token); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 72 | } |
| 73 | else |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 74 | throw new KustvaktException(StatusCodes.EXPIRED); |
| 75 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 76 | } |
| Michael Hanl | 99cb963 | 2016-06-29 16:24:40 +0200 | [diff] [blame] | 77 | return context; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 80 | |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 81 | //todo: ?! |
| 82 | @CacheEvict(value = "session", key = "#session.token") |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 83 | public void putSession (final String token, final TokenContext activeUser) |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 84 | throws KustvaktException { |
| 85 | if (!hasSession(activeUser) | multipleEnabled) { |
| 86 | loggedInRecord.put(activeUser.getUsername(), token); |
| 87 | sessionsObject.put(token, activeUser); |
| 88 | timeCheck.put(token, TimeUtils.getNow()); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 89 | } |
| 90 | else { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 91 | removeAll(activeUser); |
| 92 | throw new KustvaktException(StatusCodes.ALREADY_LOGGED_IN); |
| 93 | } |
| 94 | } |
| 95 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 96 | |
| 97 | public void removeAll (final TokenContext activeUser) { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 98 | for (String existing : loggedInRecord.get(activeUser.getUsername())) { |
| 99 | timeCheck.remove(existing); |
| 100 | sessionsObject.remove(existing); |
| 101 | } |
| 102 | loggedInRecord.remove(activeUser.getUsername()); |
| 103 | } |
| 104 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 105 | |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 106 | @CacheEvict(value = "session", key = "#session.token") |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 107 | public void removeSession (String token) { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 108 | String username = sessionsObject.get(token).getUsername(); |
| 109 | loggedInRecord.remove(username, token); |
| 110 | if (loggedInRecord.get(username).isEmpty()) |
| 111 | loggedInRecord.remove(username); |
| 112 | timeCheck.remove(token); |
| 113 | sessionsObject.remove(token); |
| 114 | } |
| 115 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 116 | |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 117 | /** |
| 118 | * reset inactive time interval to 0 |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 119 | * |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 120 | * @param token |
| 121 | */ |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 122 | private void resetInterval (String token) { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 123 | timeCheck.put(token, TimeUtils.getNow()); |
| 124 | } |
| 125 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 126 | |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 127 | /** |
| 128 | * if user possesses a valid non-expired session token |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 129 | * |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 130 | * @param token |
| 131 | * @return validity of user to request a backend function |
| 132 | */ |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 133 | private boolean isUserSessionValid (String token) { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 134 | if (timeCheck.containsKey(token)) { |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 135 | if (TimeUtils.plusSeconds(timeCheck.get(token).getMillis(), |
| 136 | inactive).isAfterNow()) { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 137 | jlog.debug("user has session"); |
| 138 | return true; |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 139 | } |
| 140 | else |
| margaretha | 49cb688 | 2018-07-04 04:19:54 +0200 | [diff] [blame] | 141 | jlog.debug("user with token "+token+" has an invalid session"); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 142 | } |
| 143 | return false; |
| 144 | } |
| 145 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 146 | |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 147 | /** |
| 148 | * clean inactive sessions from session object |
| 149 | * TODO: persist userdata to database when session times out! |
| 150 | */ |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 151 | private void timeoutMaintenance () { |
| Michael Hanl | 3520dcd | 2016-02-08 19:11:37 +0100 | [diff] [blame] | 152 | jlog.trace("running session cleanup thread"); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 153 | Set<String> inactive = new HashSet<>(); |
| 154 | for (Entry<String, DateTime> entry : timeCheck.entrySet()) { |
| 155 | if (!isUserSessionValid(entry.getKey())) { |
| 156 | TokenContext user = sessionsObject.get(entry.getKey()); |
| margaretha | 49cb688 | 2018-07-04 04:19:54 +0200 | [diff] [blame] | 157 | jlog.trace("removing user session for user "+ |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 158 | user.getUsername()); |
| 159 | inactive.add(user.getUsername()); |
| 160 | removeSession(entry.getKey()); |
| 161 | } |
| 162 | } |
| Michael Hanl | 7368aa4 | 2016-02-05 18:15:47 +0100 | [diff] [blame] | 163 | // fixme: not doing anything! |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 164 | if (inactive.size() > 0) |
| margaretha | 49cb688 | 2018-07-04 04:19:54 +0200 | [diff] [blame] | 165 | jlog.trace("removing inactive user session for users "+ |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 166 | inactive); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 167 | } |
| 168 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 169 | |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 170 | /** |
| 171 | * run cleanup-thread |
| 172 | */ |
| 173 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 174 | public void run () { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 175 | timeoutMaintenance(); |
| Michael Hanl | 7368aa4 | 2016-02-05 18:15:47 +0100 | [diff] [blame] | 176 | if (loggedInRecord.size() > 0) |
| margaretha | 49cb688 | 2018-07-04 04:19:54 +0200 | [diff] [blame] | 177 | jlog.debug("logged users: "+ loggedInRecord.toString()); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 178 | } |
| 179 | } |