| 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 | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 3 | import java.io.IOException; |
| 4 | import java.io.UnsupportedEncodingException; |
| 5 | import java.net.InetAddress; |
| 6 | import java.net.UnknownHostException; |
| 7 | import java.security.NoSuchAlgorithmException; |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 8 | import java.util.Arrays; |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 9 | import java.util.Collection; |
| 10 | import java.util.Map; |
| 11 | |
| 12 | import javax.ws.rs.core.HttpHeaders; |
| 13 | import javax.ws.rs.core.MultivaluedMap; |
| 14 | |
| 15 | import org.slf4j.Logger; |
| 16 | import org.slf4j.LoggerFactory; |
| 17 | |
| 18 | // import com.novell.ldap.*; search() funktioniert nicht korrekt, ausgewechselt gegen unboundID's Bibliothek 20.04.17/FB |
| 19 | //Using JAR from unboundID: |
| 20 | import com.unboundid.ldap.sdk.LDAPException; |
| Bodmo | ca3dcfb | 2017-05-24 16:36:00 +0200 | [diff] [blame] | 21 | |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 22 | import de.ids_mannheim.korap.auditing.AuditRecord; |
| Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 23 | import de.ids_mannheim.korap.config.Attributes; |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 24 | import de.ids_mannheim.korap.config.AuthenticationMethod; |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 25 | import de.ids_mannheim.korap.config.BeansFactory; |
| margaretha | 56e8e55 | 2017-12-05 16:31:21 +0100 | [diff] [blame] | 26 | import de.ids_mannheim.korap.config.FullConfiguration; |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 27 | import de.ids_mannheim.korap.config.TokenType; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 28 | import de.ids_mannheim.korap.config.URIParam; |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 29 | import de.ids_mannheim.korap.exceptions.EmptyResultException; |
| 30 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 31 | import de.ids_mannheim.korap.exceptions.NotAuthorizedException; |
| 32 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| 33 | import de.ids_mannheim.korap.exceptions.WrappedException; |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 34 | import de.ids_mannheim.korap.interfaces.AuthenticationIface; |
| 35 | import de.ids_mannheim.korap.interfaces.AuthenticationManagerIface; |
| 36 | import de.ids_mannheim.korap.interfaces.EncryptionIface; |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 37 | import de.ids_mannheim.korap.interfaces.ValidatorIface; |
| margaretha | 62055f7 | 2017-04-11 19:17:43 +0200 | [diff] [blame] | 38 | import de.ids_mannheim.korap.interfaces.db.AdminHandlerIface; |
| Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 39 | import de.ids_mannheim.korap.interfaces.db.AuditingIface; |
| 40 | import de.ids_mannheim.korap.interfaces.db.EntityHandlerIface; |
| Michael Hanl | 415276b | 2016-01-29 16:39:37 +0100 | [diff] [blame] | 41 | import de.ids_mannheim.korap.interfaces.db.UserDataDbIface; |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 42 | import de.ids_mannheim.korap.interfaces.defaults.ApacheValidator; |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 43 | import de.ids_mannheim.korap.user.DemoUser; |
| 44 | import de.ids_mannheim.korap.user.KorAPUser; |
| 45 | import de.ids_mannheim.korap.user.ShibUser; |
| 46 | import de.ids_mannheim.korap.user.TokenContext; |
| 47 | import de.ids_mannheim.korap.user.User; |
| Bodmo | ca3dcfb | 2017-05-24 16:36:00 +0200 | [diff] [blame] | 48 | import de.ids_mannheim.korap.user.User.CorpusAccess; |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 49 | import de.ids_mannheim.korap.user.User.Location; |
| 50 | import de.ids_mannheim.korap.user.UserDetails; |
| 51 | import de.ids_mannheim.korap.user.UserSettings; |
| 52 | import de.ids_mannheim.korap.user.Userdata; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 53 | import de.ids_mannheim.korap.utils.TimeUtils; |
| Bodmo | ca3dcfb | 2017-05-24 16:36:00 +0200 | [diff] [blame] | 54 | |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 55 | /** |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 56 | * contains the logic to authentication and registration processes. Uses |
| 57 | * interface implementations (AuthenticationIface) for different databases and |
| 58 | * handlers |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 59 | * |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 60 | * @author hanl |
| 61 | */ |
| 62 | public class KustvaktAuthenticationManager extends AuthenticationManagerIface { |
| 63 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 64 | private static Logger jlog = LoggerFactory.getLogger(KustvaktAuthenticationManager.class); |
| 65 | private EncryptionIface crypto; |
| 66 | private EntityHandlerIface entHandler; |
| 67 | private AdminHandlerIface adminHandler; |
| 68 | private AuditingIface auditing; |
| margaretha | 56e8e55 | 2017-12-05 16:31:21 +0100 | [diff] [blame] | 69 | private FullConfiguration config; |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 70 | private Collection userdatadaos; |
| 71 | private LoginCounter counter; |
| 72 | private ValidatorIface validator; |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 73 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 74 | public KustvaktAuthenticationManager(EntityHandlerIface userdb, AdminHandlerIface admindb, EncryptionIface crypto, |
| margaretha | 56e8e55 | 2017-12-05 16:31:21 +0100 | [diff] [blame] | 75 | FullConfiguration config, AuditingIface auditer, Collection<UserDataDbIface> userdatadaos) { |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 76 | this.entHandler = userdb; |
| 77 | this.adminHandler = admindb; |
| 78 | this.config = config; |
| 79 | this.crypto = crypto; |
| 80 | this.auditing = auditer; |
| 81 | this.counter = new LoginCounter(config); |
| 82 | this.userdatadaos = userdatadaos; |
| 83 | // todo: load via beancontext |
| 84 | try { |
| 85 | this.validator = new ApacheValidator(); |
| 86 | } catch (IOException e) { |
| 87 | e.printStackTrace(); |
| 88 | } |
| 89 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 90 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 91 | /** |
| 92 | * get session object if token was a session token |
| 93 | * |
| 94 | * @param token |
| 95 | * @param host |
| 96 | * @param useragent |
| 97 | * @return |
| 98 | * @throws KustvaktException |
| 99 | */ |
| 100 | @Override |
| margaretha | dfecb4b | 2017-12-12 19:32:30 +0100 | [diff] [blame] | 101 | public TokenContext getTokenContext(TokenType type, String token, |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 102 | String host, String useragent) throws KustvaktException { |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 103 | |
| margaretha | 4de4119 | 2017-11-15 11:47:11 +0100 | [diff] [blame] | 104 | AuthenticationIface provider = getProvider(type , null); |
| Michael Hanl | 99cb963 | 2016-06-29 16:24:40 +0200 | [diff] [blame] | 105 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 106 | if (provider == null) |
| 107 | // throw exception for missing type parameter |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 108 | throw new KustvaktException(StatusCodes.ILLEGAL_ARGUMENT, |
| 109 | "token type not defined or found", "token_type"); |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 110 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 111 | TokenContext context = provider.getTokenContext(token); |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 112 | // if (!matchStatus(host, useragent, context)) |
| 113 | // provider.removeUserSession(token); |
| 114 | return context; |
| 115 | } |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 116 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 117 | @Override |
| 118 | public User getUser(String username) throws KustvaktException { |
| 119 | // User user; |
| 120 | // Object value = this.getCacheValue(username); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 121 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 122 | if (User.UserFactory.isDemo(username)) |
| 123 | return User.UserFactory.getDemoUser(); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 124 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 125 | // if (value != null) { |
| 126 | // Map map = (Map) value; |
| 127 | // user = User.UserFactory.toUser(map); |
| 128 | // } |
| 129 | // else { |
| 130 | // user = entHandler.getAccount(username); |
| 131 | // this.storeInCache(username, user.toCache()); |
| 132 | // todo: not valid. for the duration of the session, the host should not |
| 133 | // change! |
| 134 | // } |
| 135 | // todo: |
| 136 | // user.addField(Attributes.HOST, context.getHostAddress()); |
| 137 | // user.addField(Attributes.USER_AGENT, context.getUserAgent()); |
| margaretha | 0b63de4 | 2017-12-20 18:48:09 +0100 | [diff] [blame] | 138 | |
| 139 | //EM:copied from EntityDao |
| 140 | KorAPUser user = new KorAPUser(); // oder eigentlich new DemoUser oder new DefaultUser. |
| 141 | user.setUsername(username); |
| 142 | return user; |
| 143 | // return entHandler.getAccount(username); |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 144 | } |
| margaretha | 7d0165c | 2018-02-26 15:31:37 +0100 | [diff] [blame^] | 145 | |
| 146 | @Override |
| 147 | public User getUser (String username, String method) |
| 148 | throws KustvaktException { |
| 149 | KorAPUser user = new KorAPUser(); |
| 150 | user.setUsername(username); |
| 151 | String email = null; |
| 152 | switch (method.toLowerCase()) { |
| 153 | case "ldap": |
| 154 | email = config.getTestEmail(); |
| 155 | break; |
| 156 | default: |
| 157 | email = config.getTestEmail(); |
| 158 | break; |
| 159 | } |
| 160 | user.setEmail(email); |
| 161 | return user; |
| 162 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 163 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 164 | public TokenContext refresh(TokenContext context) throws KustvaktException { |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 165 | AuthenticationIface provider = getProvider(context.getTokenType(), null); |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 166 | if (provider == null) { |
| 167 | // todo: |
| 168 | } |
| Michael Hanl | c444602 | 2016-02-12 18:03:17 +0100 | [diff] [blame] | 169 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 170 | try { |
| 171 | provider.removeUserSession(context.getToken()); |
| 172 | User user = getUser(context.getUsername()); |
| 173 | return provider.createTokenContext(user, context.params()); |
| 174 | } catch (KustvaktException e) { |
| 175 | throw new WrappedException(e, StatusCodes.LOGIN_FAILED); |
| 176 | } |
| 177 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 178 | |
| margaretha | fde771a | 2017-11-14 15:02:10 +0100 | [diff] [blame] | 179 | /** EM: fix type is not flexible |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 180 | * @param type |
| 181 | * @param attributes |
| 182 | * contains username and password to authenticate the user. |
| 183 | * Depending of the authentication schema, may contain other |
| 184 | * values as well |
| 185 | * @return User |
| 186 | * @throws KustvaktException |
| 187 | */ |
| 188 | @Override |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 189 | public User authenticate(AuthenticationMethod method, String username, String password, Map<String, Object> attributes) |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 190 | throws KustvaktException { |
| 191 | User user; |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 192 | switch (method) { |
| margaretha | 139d0f7 | 2017-11-14 18:56:22 +0100 | [diff] [blame] | 193 | case SHIBBOLETH: |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 194 | // todo: |
| 195 | user = authenticateShib(attributes); |
| 196 | break; |
| margaretha | 139d0f7 | 2017-11-14 18:56:22 +0100 | [diff] [blame] | 197 | case LDAP: |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 198 | // IdM/LDAP: (09.02.17/FB) |
| 199 | user = authenticateIdM(username, password, attributes); |
| 200 | break; |
| 201 | default: |
| 202 | user = authenticate(username, password, attributes); |
| 203 | break; |
| 204 | } |
| 205 | auditing.audit(AuditRecord.serviceRecord(user.getId(), StatusCodes.LOGIN_SUCCESSFUL, user.toString())); |
| 206 | return user; |
| 207 | } |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 208 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 209 | // a. set location depending on X-Forwarded-For. |
| 210 | // X-Forwarded-For: clientIP, ProxyID, ProxyID... |
| 211 | // the following private address spaces may be used to define intranet |
| 212 | // spaces: |
| 213 | // 10.0.0.0 - 10.255.255.255 (10/8 prefix) |
| 214 | // 172.16.0.0 - 172.31.255.255 (172.16/12 prefix) |
| 215 | // 192.168.0.0 - 192.168.255.255 (192.168/16 prefix) |
| 216 | // b. set corpusAccess depending on location: |
| Bodmo | 11fb6f8 | 2017-06-01 11:39:15 +0200 | [diff] [blame] | 217 | // c. DemoUser only gets corpusAccess=FREE. |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 218 | // 16.05.17/FB |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 219 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 220 | @Override |
| 221 | public void setAccessAndLocation(User user, HttpHeaders headers) { |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 222 | MultivaluedMap<String, String> headerMap = headers.getRequestHeaders(); |
| 223 | Location location = Location.EXTERN; |
| 224 | CorpusAccess corpusAccess = CorpusAccess.FREE; |
| margaretha | a89c3f9 | 2017-05-30 19:02:08 +0200 | [diff] [blame] | 225 | |
| Bodmo | 11fb6f8 | 2017-06-01 11:39:15 +0200 | [diff] [blame] | 226 | if( user instanceof DemoUser ) |
| 227 | { |
| 228 | // to be absolutely sure: |
| 229 | user.setCorpusAccess(User.CorpusAccess.FREE); |
| margaretha | a86b141 | 2018-02-21 20:40:35 +0100 | [diff] [blame] | 230 | jlog.debug("setAccessAndLocation: DemoUser: location=%s, access=%s.\n", user.locationtoString(), user.accesstoString()); |
| Bodmo | 11fb6f8 | 2017-06-01 11:39:15 +0200 | [diff] [blame] | 231 | return; |
| 232 | } |
| margaretha | a89c3f9 | 2017-05-30 19:02:08 +0200 | [diff] [blame] | 233 | |
| margaretha | 58e1863 | 2018-02-15 13:04:42 +0100 | [diff] [blame] | 234 | if (headerMap != null && headerMap.containsKey(com.google.common.net.HttpHeaders.X_FORWARDED_FOR)) { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 235 | |
| margaretha | 58e1863 | 2018-02-15 13:04:42 +0100 | [diff] [blame] | 236 | String[] vals = headerMap.getFirst(com.google.common.net.HttpHeaders.X_FORWARDED_FOR).split(","); |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 237 | String clientAddress = vals[0]; |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 238 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 239 | try { |
| 240 | InetAddress ip = InetAddress.getByName(clientAddress); |
| 241 | if (ip.isSiteLocalAddress()){ |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 242 | location = Location.INTERN; |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 243 | corpusAccess = CorpusAccess.ALL; |
| 244 | } |
| margaretha | a89c3f9 | 2017-05-30 19:02:08 +0200 | [diff] [blame] | 245 | else{ |
| 246 | corpusAccess = CorpusAccess.PUB; |
| 247 | } |
| 248 | |
| margaretha | bb48630 | 2017-11-21 13:47:22 +0100 | [diff] [blame] | 249 | jlog.debug(String.format("X-Forwarded-For : '%s' (%d values) -> %s\n", |
| 250 | Arrays.toString(vals), vals.length, vals[0])); |
| 251 | jlog.debug(String.format("X-Forwarded-For : location = %s corpusAccess = %s\n", |
| 252 | location == Location.INTERN ? "INTERN" : "EXTERN", corpusAccess == CorpusAccess.ALL ? "ALL" |
| 253 | : corpusAccess == CorpusAccess.PUB ? "PUB" : "FREE")); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 254 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 255 | } catch (UnknownHostException e) { |
| 256 | // TODO Auto-generated catch block |
| 257 | e.printStackTrace(); |
| 258 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 259 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 260 | user.setLocation(location); |
| 261 | user.setCorpusAccess(corpusAccess); |
| margaretha | a86b141 | 2018-02-21 20:40:35 +0100 | [diff] [blame] | 262 | |
| 263 | jlog.debug("setAccessAndLocation: KorAPUser: location=%s, access=%s.\n", user.locationtoString(), user.accesstoString()); |
| Bodmo | c125bf1 | 2017-06-01 16:23:59 +0200 | [diff] [blame] | 264 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 265 | } |
| 266 | } // getAccess |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 267 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 268 | @Override |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 269 | public TokenContext createTokenContext(User user, Map<String, Object> attr, TokenType type) |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 270 | throws KustvaktException { |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 271 | // use api token |
| 272 | AuthenticationIface provider = getProvider(type, TokenType.API); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 273 | |
| margaretha | 38d530e | 2017-07-11 19:06:50 +0200 | [diff] [blame] | 274 | // EM: not in the new DB |
| 275 | // if (attr.get(Attributes.SCOPES) != null) |
| 276 | // this.getUserData(user, UserDetails.class); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 277 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 278 | TokenContext context = provider.createTokenContext(user, attr); |
| 279 | if (context == null) |
| 280 | throw new KustvaktException(StatusCodes.NOT_SUPPORTED); |
| 281 | context.setUserAgent((String) attr.get(Attributes.USER_AGENT)); |
| 282 | context.setHostAddress(Attributes.HOST); |
| 283 | return context; |
| 284 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 285 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 286 | // todo: test |
| 287 | @Deprecated |
| 288 | private boolean matchStatus(String host, String useragent, TokenContext context) { |
| 289 | if (host.equals(context.getHostAddress())) { |
| 290 | if (useragent.equals(context.getUserAgent())) |
| 291 | return true; |
| 292 | } |
| 293 | return false; |
| 294 | } |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 295 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 296 | private User authenticateShib(Map<String, Object> attributes) throws KustvaktException { |
| 297 | // todo use persistent id, since eppn is not unique |
| 298 | String eppn = (String) attributes.get(Attributes.EPPN); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 299 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 300 | if (eppn == null || eppn.isEmpty()) |
| 301 | throw new KustvaktException(StatusCodes.REQUEST_INVALID); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 302 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 303 | if (!attributes.containsKey(Attributes.EMAIL) && validator.isValid(eppn, Attributes.EMAIL)) |
| 304 | attributes.put(Attributes.EMAIL, eppn); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 305 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 306 | User user = null; |
| 307 | if (isRegistered(eppn)) |
| 308 | user = createShibbUserAccount(attributes); |
| 309 | return user; |
| 310 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 311 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 312 | // todo: what if attributes null? |
| 313 | private User authenticate(String username, String password, Map<String, Object> attr) throws KustvaktException { |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 314 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 315 | Map<String, Object> attributes = validator.validateMap(attr); |
| 316 | User unknown; |
| 317 | // just to make sure that the plain password does not appear anywhere in |
| 318 | // the logs! |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 319 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 320 | try { |
| 321 | validator.validateEntry(username, Attributes.USERNAME); |
| 322 | } catch (KustvaktException e) { |
| 323 | throw new WrappedException(e, StatusCodes.LOGIN_FAILED, username); |
| 324 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 325 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 326 | if (username == null || username.isEmpty()) |
| 327 | throw new WrappedException(new KustvaktException(username, StatusCodes.BAD_CREDENTIALS), |
| 328 | StatusCodes.LOGIN_FAILED); |
| 329 | else { |
| 330 | try { |
| 331 | unknown = entHandler.getAccount(username); |
| 332 | } catch (EmptyResultException e) { |
| 333 | // mask exception to disable user guessing in possible attacks |
| 334 | throw new WrappedException(new KustvaktException(username, StatusCodes.BAD_CREDENTIALS), |
| 335 | StatusCodes.LOGIN_FAILED, username); |
| 336 | } catch (KustvaktException e) { |
| 337 | jlog.error("Error: {}", e); |
| 338 | throw new WrappedException(e, StatusCodes.LOGIN_FAILED, attributes.toString()); |
| 339 | } |
| 340 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 341 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 342 | boolean isAdmin = adminHandler.isAdmin(unknown.getId()); |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 343 | unknown.setSystemAdmin(isAdmin); |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 344 | jlog.debug("Authentication: found username " + unknown.getUsername()); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 345 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 346 | if (unknown instanceof KorAPUser) { |
| 347 | if (password == null || password.isEmpty()) |
| 348 | throw new WrappedException(new KustvaktException(unknown.getId(), StatusCodes.BAD_CREDENTIALS), |
| 349 | StatusCodes.LOGIN_FAILED, username); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 350 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 351 | KorAPUser user = (KorAPUser) unknown; |
| 352 | boolean check = crypto.checkHash(password, user.getPassword()); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 353 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 354 | if (!check) { |
| 355 | // the fail counter only applies for wrong password |
| 356 | jlog.warn("Wrong Password!"); |
| 357 | processLoginFail(unknown); |
| 358 | throw new WrappedException(new KustvaktException(user.getId(), StatusCodes.BAD_CREDENTIALS), |
| 359 | StatusCodes.LOGIN_FAILED, username); |
| 360 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 361 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 362 | // bad credentials error has precedence over account locked or |
| 363 | // unconfirmed codes |
| 364 | // since latter can lead to account guessing of third parties |
| 365 | if (user.isAccountLocked()) { |
| 366 | URIParam param = (URIParam) user.getField(URIParam.class); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 367 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 368 | if (param.hasValues()) { |
| 369 | jlog.debug("Account is not yet activated for user '{}'", user.getUsername()); |
| 370 | if (TimeUtils.getNow().isAfter(param.getUriExpiration())) { |
| 371 | jlog.error("URI token is expired. Deleting account for user {}", user.getUsername()); |
| 372 | deleteAccount(user); |
| 373 | throw new WrappedException( |
| 374 | new KustvaktException(unknown.getId(), StatusCodes.EXPIRED, |
| 375 | "account confirmation uri has expired!", param.getUriFragment()), |
| 376 | StatusCodes.LOGIN_FAILED, username); |
| 377 | } |
| 378 | throw new WrappedException( |
| 379 | new KustvaktException(unknown.getId(), StatusCodes.ACCOUNT_NOT_CONFIRMED), |
| 380 | StatusCodes.LOGIN_FAILED, username); |
| 381 | } |
| 382 | jlog.error("ACCESS DENIED: account not active for '{}'", unknown.getUsername()); |
| 383 | throw new WrappedException(new KustvaktException(unknown.getId(), StatusCodes.ACCOUNT_DEACTIVATED), |
| 384 | StatusCodes.LOGIN_FAILED, username); |
| 385 | } |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 386 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 387 | } else if (unknown instanceof ShibUser) { |
| 388 | // todo |
| 389 | } |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 390 | jlog.debug("Authentication done: "+unknown); |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 391 | return unknown; |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * authenticate using IdM (Identitätsmanagement) accessed by LDAP. |
| 396 | * |
| 397 | * @param username |
| 398 | * @param password |
| 399 | * @param attr |
| 400 | * @return |
| 401 | * @throws KustvaktException |
| 402 | * @date 09.02.17/FB |
| 403 | */ |
| 404 | // todo: what if attributes null? |
| Bodmo | 3d6bd35 | 2017-04-25 11:31:39 +0200 | [diff] [blame] | 405 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 406 | private User authenticateIdM(String username, String password, Map<String, Object> attr) throws KustvaktException { |
| Bodmo | 3d6bd35 | 2017-04-25 11:31:39 +0200 | [diff] [blame] | 407 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 408 | Map<String, Object> attributes = validator.validateMap(attr); |
| 409 | User unknown = null; |
| 410 | // just to make sure that the plain password does not appear anywhere in |
| 411 | // the logs! |
| Bodmo | 3d6bd35 | 2017-04-25 11:31:39 +0200 | [diff] [blame] | 412 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 413 | System.out.printf("Debug: authenticateIdM: entering for '%s'...\n", username); |
| Bodmo | 3d6bd35 | 2017-04-25 11:31:39 +0200 | [diff] [blame] | 414 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 415 | /** |
| 416 | * wozu Apache Validatoren für User/Passwort für IdM/LDAP? siehe |
| 417 | * validation.properties. Abgeschaltet 21.04.17/FB try { |
| 418 | * validator.validateEntry(username, Attributes.USERNAME); } catch |
| 419 | * (KustvaktException e) { throw new WrappedException(e, |
| 420 | * StatusCodes.LOGIN_FAILED, username); } |
| 421 | */ |
| 422 | if (username == null || username.isEmpty() || password == null || password.isEmpty()) |
| 423 | throw new WrappedException(new KustvaktException(username, StatusCodes.BAD_CREDENTIALS), |
| 424 | StatusCodes.LOGIN_FAILED); |
| Bodmo | 3d6bd35 | 2017-04-25 11:31:39 +0200 | [diff] [blame] | 425 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 426 | // LDAP Access: |
| 427 | try { |
| 428 | // todo: unknown = ... |
| margaretha | 65b6714 | 2017-05-29 16:23:16 +0200 | [diff] [blame] | 429 | int ret = LdapAuth3.login(username, password, config.getLdapConfig()); |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 430 | System.out.printf("Debug: autenticationIdM: Ldap.login(%s) returns: %d.\n", username, ret); |
| 431 | if (ret != LdapAuth3.LDAP_AUTH_ROK) { |
| 432 | jlog.error("LdapAuth3.login(username='{}') returns '{}'='{}'!", username, ret, |
| 433 | LdapAuth3.getErrMessage(ret)); |
| Bodmo | 3d6bd35 | 2017-04-25 11:31:39 +0200 | [diff] [blame] | 434 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 435 | // mask exception to disable user guessing in possible attacks |
| 436 | /* |
| 437 | * by Hanl throw new WrappedException(new |
| 438 | * KustvaktException(username, StatusCodes.BAD_CREDENTIALS), |
| 439 | * StatusCodes.LOGIN_FAILED, username); |
| 440 | */ |
| 441 | throw new WrappedException(new KustvaktException(username, StatusCodes.LDAP_BASE_ERRCODE + ret, |
| 442 | LdapAuth3.getErrMessage(ret), null), StatusCodes.LOGIN_FAILED, username); |
| 443 | } |
| 444 | } catch (LDAPException e) { |
| Bodmo | 3d6bd35 | 2017-04-25 11:31:39 +0200 | [diff] [blame] | 445 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 446 | jlog.error("Error: username='{}' -> '{}'!", username, e); |
| 447 | // mask exception to disable user guessing in possible attacks |
| 448 | /* |
| 449 | * by Hanl: throw new WrappedException(new |
| 450 | * KustvaktException(username, StatusCodes.BAD_CREDENTIALS), |
| 451 | * StatusCodes.LOGIN_FAILED, username); |
| 452 | */ |
| 453 | throw new WrappedException( |
| 454 | new KustvaktException(username, StatusCodes.LDAP_BASE_ERRCODE + LdapAuth3.LDAP_AUTH_RINTERR, |
| 455 | LdapAuth3.getErrMessage(LdapAuth3.LDAP_AUTH_RINTERR), null), |
| 456 | StatusCodes.LOGIN_FAILED, username); |
| 457 | } |
| Bodmo | 3d6bd35 | 2017-04-25 11:31:39 +0200 | [diff] [blame] | 458 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 459 | // Create a User |
| 460 | // TODO: KorAPUser für solche mit einem bestehenden Account |
| 461 | // DefaultUser sonst. |
| 462 | User user = new KorAPUser(); |
| 463 | user.setUsername(username); |
| 464 | /* |
| 465 | * folgender Code funktioniert hier noch nicht, da die Headers noch |
| 466 | * nicht ausgewertet worden sind - 23.05.17/FB Object o = |
| 467 | * attr.get(Attributes.LOCATION); String loc = (String)o.toString(); int |
| 468 | * location = Integer.parseInt(loc); user.setLocation(location); |
| 469 | * user.setCorpusAccess(Integer.parseInt(attr.get(Attributes. |
| 470 | * CORPUS_ACCESS).toString())); |
| 471 | */ |
| 472 | unknown = user; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 473 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 474 | jlog.trace("Authentication: found username " + unknown.getUsername()); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 475 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 476 | if (unknown instanceof KorAPUser) { |
| 477 | /* |
| 478 | * password already checked using LDAP: if (password == null || |
| 479 | * password.isEmpty()) throw new WrappedException(new |
| 480 | * KustvaktException( unknown.getId(), StatusCodes.BAD_CREDENTIALS), |
| 481 | * StatusCodes.LOGIN_FAILED, username); |
| 482 | * |
| 483 | * KorAPUser user = (KorAPUser) unknown; boolean check = |
| 484 | * crypto.checkHash(password, user.getPassword()); |
| 485 | * |
| 486 | * if (!check) { // the fail counter only applies for wrong password |
| 487 | * jlog.warn("Wrong Password!"); processLoginFail(unknown); throw |
| 488 | * new WrappedException(new KustvaktException(user.getId(), |
| 489 | * StatusCodes.BAD_CREDENTIALS), StatusCodes.LOGIN_FAILED, |
| 490 | * username); } |
| 491 | */ |
| 492 | // bad credentials error has precedence over account locked or |
| 493 | // unconfirmed codes |
| 494 | // since latter can lead to account guessing of third parties |
| 495 | /* |
| 496 | * if (user.isAccountLocked()) { |
| 497 | * |
| 498 | * URIParam param = (URIParam) user.getField(URIParam.class); |
| 499 | * |
| 500 | * if (param.hasValues()) { |
| 501 | * jlog.debug("Account is not yet activated for user '{}'", |
| 502 | * user.getUsername()); if |
| 503 | * (TimeUtils.getNow().isAfter(param.getUriExpiration())) { |
| 504 | * jlog.error( "URI token is expired. Deleting account for user {}", |
| 505 | * user.getUsername()); deleteAccount(user); throw new |
| 506 | * WrappedException(new KustvaktException( unknown.getId(), |
| 507 | * StatusCodes.EXPIRED, "account confirmation uri has expired!", |
| 508 | * param.getUriFragment()), StatusCodes.LOGIN_FAILED, username); } |
| 509 | * throw new WrappedException(new KustvaktException( |
| 510 | * unknown.getId(), StatusCodes.ACCOUNT_NOT_CONFIRMED), |
| 511 | * StatusCodes.LOGIN_FAILED, username); } |
| 512 | * jlog.error("ACCESS DENIED: account not active for '{}'", |
| 513 | * unknown.getUsername()); throw new WrappedException(new |
| 514 | * KustvaktException( unknown.getId(), |
| 515 | * StatusCodes.ACCOUNT_DEACTIVATED), StatusCodes.LOGIN_FAILED, |
| 516 | * username); } |
| 517 | */ |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 518 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 519 | } else if (unknown instanceof ShibUser) { |
| 520 | // todo |
| 521 | } |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 522 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 523 | jlog.debug("Authentication done: " + username); |
| 524 | return unknown; |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 525 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 526 | } // authenticateIdM |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 527 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 528 | public boolean isRegistered(String username) { |
| 529 | User user; |
| 530 | if (username == null || username.isEmpty()) |
| 531 | return false; |
| 532 | // throw new KustvaktException(username, StatusCodes.ILLEGAL_ARGUMENT, |
| 533 | // "username must be set", username); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 534 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 535 | try { |
| 536 | user = entHandler.getAccount(username); |
| 537 | } catch (EmptyResultException e) { |
| 538 | jlog.debug("user does not exist ({})", username); |
| 539 | return false; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 540 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 541 | } catch (KustvaktException e) { |
| 542 | jlog.error("KorAPException", e.string()); |
| 543 | return false; |
| 544 | // throw new KustvaktException(username, |
| 545 | // StatusCodes.ILLEGAL_ARGUMENT, |
| 546 | // "username invalid", username); |
| 547 | } |
| 548 | return user != null; |
| 549 | } |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 550 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 551 | public void logout(TokenContext context) throws KustvaktException { |
| 552 | try { |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 553 | AuthenticationIface provider = getProvider(context.getTokenType(), null); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 554 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 555 | if (provider == null) { |
| margaretha | 4de4119 | 2017-11-15 11:47:11 +0100 | [diff] [blame] | 556 | throw new KustvaktException(StatusCodes.ILLEGAL_ARGUMENT, "Authentication " |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 557 | + "provider not supported!", context.getTokenType().displayName()); |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 558 | } |
| 559 | provider.removeUserSession(context.getToken()); |
| 560 | } catch (KustvaktException e) { |
| 561 | throw new WrappedException(e, StatusCodes.LOGOUT_FAILED, context.toString()); |
| 562 | } |
| 563 | auditing.audit( |
| 564 | AuditRecord.serviceRecord(context.getUsername(), StatusCodes.LOGOUT_SUCCESSFUL, context.toString())); |
| 565 | this.removeCacheEntry(context.getToken()); |
| 566 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 567 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 568 | private void processLoginFail(User user) throws KustvaktException { |
| 569 | counter.registerFail(user.getUsername()); |
| 570 | if (!counter.validate(user.getUsername())) { |
| 571 | try { |
| 572 | this.lockAccount(user); |
| 573 | } catch (KustvaktException e) { |
| 574 | jlog.error("user account could not be locked", e); |
| 575 | throw new WrappedException(e, StatusCodes.UPDATE_ACCOUNT_FAILED); |
| 576 | } |
| 577 | throw new WrappedException(new KustvaktException(user.getId(), StatusCodes.ACCOUNT_DEACTIVATED), |
| 578 | StatusCodes.LOGIN_FAILED); |
| 579 | } |
| 580 | } |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 581 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 582 | public void lockAccount(User user) throws KustvaktException { |
| 583 | if (!(user instanceof KorAPUser)) |
| 584 | throw new KustvaktException(StatusCodes.REQUEST_INVALID); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 585 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 586 | KorAPUser u = (KorAPUser) user; |
| 587 | u.setAccountLocked(true); |
| 588 | jlog.info("locking account for user: {}", user.getUsername()); |
| 589 | entHandler.updateAccount(u); |
| 590 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 591 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 592 | public KorAPUser checkPasswordAllowance(KorAPUser user, String oldPassword, String newPassword) |
| 593 | throws KustvaktException { |
| 594 | String dbPassword = user.getPassword(); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 595 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 596 | if (oldPassword.trim().equals(newPassword.trim())) { |
| 597 | // TODO: special error StatusCodes for this? |
| 598 | throw new WrappedException(new KustvaktException(user.getId(), StatusCodes.ILLEGAL_ARGUMENT), |
| 599 | StatusCodes.PASSWORD_RESET_FAILED, newPassword); |
| 600 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 601 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 602 | boolean check = crypto.checkHash(oldPassword, dbPassword); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 603 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 604 | if (!check) |
| 605 | throw new WrappedException(new KustvaktException(user.getId(), StatusCodes.BAD_CREDENTIALS), |
| 606 | StatusCodes.PASSWORD_RESET_FAILED); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 607 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 608 | try { |
| 609 | user.setPassword(crypto.secureHash(newPassword)); |
| 610 | } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { |
| 611 | throw new WrappedException( |
| 612 | new KustvaktException(user.getId(), StatusCodes.ILLEGAL_ARGUMENT, "password invalid", newPassword), |
| 613 | StatusCodes.PASSWORD_RESET_FAILED, user.toString(), newPassword); |
| 614 | } |
| 615 | return user; |
| 616 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 617 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 618 | // fixme: use clientinfo for logging/auditing?! = from where did he access |
| 619 | // the reset function? |
| 620 | @Override |
| 621 | public void resetPassword(String uriFragment, String username, String newPassphrase) throws KustvaktException { |
| 622 | try { |
| 623 | validator.validateEntry(username, Attributes.USERNAME); |
| 624 | validator.validateEntry(newPassphrase, Attributes.PASSWORD); |
| 625 | } catch (KustvaktException e) { |
| 626 | jlog.error("Error: {}", e.string()); |
| 627 | throw new WrappedException( |
| 628 | new KustvaktException(username, StatusCodes.ILLEGAL_ARGUMENT, "password invalid", newPassphrase), |
| 629 | StatusCodes.PASSWORD_RESET_FAILED, username, newPassphrase); |
| 630 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 631 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 632 | try { |
| 633 | newPassphrase = crypto.secureHash(newPassphrase); |
| 634 | } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { |
| 635 | jlog.error("Encoding/Algorithm Error", e); |
| 636 | throw new WrappedException( |
| 637 | new KustvaktException(username, StatusCodes.ILLEGAL_ARGUMENT, "password invalid", newPassphrase), |
| 638 | StatusCodes.PASSWORD_RESET_FAILED, username, uriFragment, newPassphrase); |
| 639 | } |
| 640 | int result = entHandler.resetPassphrase(username, uriFragment, newPassphrase); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 641 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 642 | if (result == 0) |
| 643 | throw new WrappedException( |
| 644 | new KustvaktException(username, StatusCodes.EXPIRED, "URI fragment expired", uriFragment), |
| 645 | StatusCodes.PASSWORD_RESET_FAILED, username, uriFragment); |
| 646 | else if (result == 1) |
| 647 | jlog.info("successfully reset password for user {}", username); |
| 648 | } |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 649 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 650 | public void confirmRegistration(String uriFragment, String username) throws KustvaktException { |
| 651 | try { |
| 652 | validator.validateEntry(username, Attributes.USERNAME); |
| 653 | } catch (KustvaktException e) { |
| 654 | jlog.error("Error: {}", e.string()); |
| 655 | throw new WrappedException(e, StatusCodes.ACCOUNT_CONFIRMATION_FAILED, username, uriFragment); |
| 656 | } |
| 657 | int r = entHandler.activateAccount(username, uriFragment); |
| 658 | if (r == 0) { |
| 659 | User user; |
| 660 | try { |
| 661 | user = entHandler.getAccount(username); |
| 662 | } catch (EmptyResultException e) { |
| 663 | throw new WrappedException(new KustvaktException(username, StatusCodes.BAD_CREDENTIALS), |
| 664 | StatusCodes.ACCOUNT_CONFIRMATION_FAILED, username, uriFragment); |
| 665 | } |
| 666 | entHandler.deleteAccount(user.getId()); |
| 667 | throw new WrappedException(new KustvaktException(user.getId(), StatusCodes.EXPIRED), |
| 668 | StatusCodes.ACCOUNT_CONFIRMATION_FAILED, username, uriFragment); |
| 669 | } else if (r == 1) |
| 670 | jlog.info("successfully confirmed user registration for user {}", username); |
| 671 | // register successful audit! |
| 672 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 673 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 674 | /** |
| 675 | * @param attributes |
| 676 | * @return |
| 677 | * @throws KustvaktException |
| 678 | */ |
| 679 | // fixme: remove clientinfo object (not needed), use json representation to |
| 680 | // get stuff |
| 681 | public User createUserAccount(Map<String, Object> attributes, boolean confirmation_required) |
| 682 | throws KustvaktException { |
| 683 | Map<String, Object> safeMap = validator.validateMap(attributes); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 684 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 685 | if (safeMap.get(Attributes.USERNAME) == null || ((String) safeMap.get(Attributes.USERNAME)).isEmpty()) |
| 686 | throw new KustvaktException(StatusCodes.ILLEGAL_ARGUMENT, "username must be set", "username"); |
| 687 | if (safeMap.get(Attributes.PASSWORD) == null || ((String) safeMap.get(Attributes.PASSWORD)).isEmpty()) |
| 688 | throw new KustvaktException(safeMap.get(Attributes.USERNAME), StatusCodes.ILLEGAL_ARGUMENT, |
| 689 | "password must be set", "password"); |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 690 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 691 | String username = validator.validateEntry((String) safeMap.get(Attributes.USERNAME), Attributes.USERNAME); |
| 692 | String password = validator.validateEntry((String) safeMap.get(Attributes.PASSWORD), Attributes.PASSWORD); |
| 693 | String hash; |
| 694 | try { |
| 695 | hash = crypto.secureHash(password); |
| 696 | } catch (UnsupportedEncodingException | NoSuchAlgorithmException e) { |
| 697 | jlog.error("Encryption error", e); |
| 698 | throw new KustvaktException(StatusCodes.ILLEGAL_ARGUMENT); |
| 699 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 700 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 701 | KorAPUser user = User.UserFactory.getUser(username); |
| 702 | Object id = attributes.get(Attributes.ID); |
| 703 | if (id != null && id instanceof Integer) |
| 704 | user.setId((Integer) id); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 705 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 706 | user.setAccountLocked(confirmation_required); |
| 707 | if (confirmation_required) { |
| 708 | URIParam param = new URIParam(crypto.createToken(), |
| 709 | TimeUtils.plusSeconds(config.getTokenTTL()).getMillis()); |
| 710 | user.addField(param); |
| 711 | } |
| 712 | user.setPassword(hash); |
| Michael Hanl | 7368aa4 | 2016-02-05 18:15:47 +0100 | [diff] [blame] | 713 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 714 | String o = (String) attributes.get(Attributes.IS_ADMIN); |
| margaretha | 62055f7 | 2017-04-11 19:17:43 +0200 | [diff] [blame] | 715 | boolean b = Boolean.parseBoolean(o); |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 716 | user.setSystemAdmin(b); |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 717 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 718 | try { |
| 719 | UserDetails details = new UserDetails(); |
| 720 | details.read(safeMap, true); |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 721 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 722 | UserSettings settings = new UserSettings(); |
| 723 | settings.read(safeMap, true); |
| Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 724 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 725 | jlog.info("Creating new user account for user {}", user.getUsername()); |
| 726 | entHandler.createAccount(user); |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 727 | if (user.isSystemAdmin() && user instanceof KorAPUser) { |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 728 | adminHandler.addAccount(user); |
| 729 | user.setCorpusAccess(CorpusAccess.ALL); |
| 730 | } |
| 731 | details.setUserId(user.getId()); |
| 732 | settings.setUserId(user.getId()); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 733 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 734 | UserDataDbIface dao = BeansFactory.getTypeFactory().getTypeInterfaceBean(userdatadaos, UserDetails.class); |
| 735 | // todo: remove this |
| 736 | assert dao != null; |
| 737 | dao.store(details); |
| 738 | dao = BeansFactory.getTypeFactory().getTypeInterfaceBean(userdatadaos, UserSettings.class); |
| 739 | assert dao != null; |
| 740 | dao.store(settings); |
| 741 | } catch (KustvaktException e) { |
| 742 | jlog.error("Error: {}", e.string()); |
| 743 | throw new WrappedException(e, StatusCodes.CREATE_ACCOUNT_FAILED, user.toString()); |
| 744 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 745 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 746 | auditing.audit(AuditRecord.serviceRecord(user.getUsername(), StatusCodes.CREATE_ACCOUNT_SUCCESSFUL)); |
| 747 | return user; |
| 748 | } |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 749 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 750 | // todo: |
| 751 | private ShibUser createShibbUserAccount(Map<String, Object> attributes) throws KustvaktException { |
| 752 | jlog.debug("creating shibboleth user account for user attr: {}", attributes); |
| 753 | Map<String, Object> safeMap = validator.validateMap(attributes); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 754 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 755 | // todo eppn non-unique.join with idp or use persistent_id as username |
| 756 | // identifier |
| 757 | ShibUser user = User.UserFactory.getShibInstance((String) safeMap.get(Attributes.EPPN), |
| 758 | (String) safeMap.get(Attributes.MAIL), (String) safeMap.get(Attributes.CN)); |
| 759 | user.setAffiliation((String) safeMap.get(Attributes.EDU_AFFIL)); |
| 760 | user.setAccountCreation(TimeUtils.getNow().getMillis()); |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 761 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 762 | UserDetails d = new UserDetails(); |
| 763 | d.read(attributes, true); |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 764 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 765 | UserSettings s = new UserSettings(); |
| 766 | s.read(attributes, true); |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 767 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 768 | entHandler.createAccount(user); |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 769 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 770 | s.setUserId(user.getId()); |
| 771 | d.setUserId(user.getId()); |
| Michael Hanl | 25aac54 | 2016-02-01 18:16:44 +0100 | [diff] [blame] | 772 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 773 | UserDataDbIface dao = BeansFactory.getTypeFactory().getTypeInterfaceBean(userdatadaos, UserDetails.class); |
| 774 | assert dao != null; |
| 775 | dao.store(d); |
| Michael Hanl | 25aac54 | 2016-02-01 18:16:44 +0100 | [diff] [blame] | 776 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 777 | dao = BeansFactory.getTypeFactory().getTypeInterfaceBean(userdatadaos, UserSettings.class); |
| 778 | assert dao != null; |
| 779 | dao.store(d); |
| Michael Hanl | 25aac54 | 2016-02-01 18:16:44 +0100 | [diff] [blame] | 780 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 781 | return user; |
| 782 | } |
| Michael Hanl | 25aac54 | 2016-02-01 18:16:44 +0100 | [diff] [blame] | 783 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 784 | /** |
| 785 | * link shibboleth and korap user account to one another. |
| 786 | * |
| 787 | * @param current |
| 788 | * currently logged in user |
| 789 | * @param for_name |
| 790 | * foreign user name the current account should be linked to |
| 791 | * @param transstrat |
| 792 | * transfer status of user data (details, settings, user queries) |
| 793 | * 0 = the currently logged in data should be kept 1 = the |
| 794 | * foreign account data should be kept |
| 795 | * @throws NotAuthorizedException |
| 796 | * @throws KustvaktException |
| 797 | */ |
| 798 | // todo: |
| 799 | public void accountLink(User current, String for_name, int transstrat) throws KustvaktException { |
| 800 | // User foreign = entHandler.getAccount(for_name); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 801 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 802 | // if (current.getAccountLink() == null && current.getAccountLink() |
| 803 | // .isEmpty()) { |
| 804 | // if (current instanceof KorAPUser && foreign instanceof ShibUser) { |
| 805 | // if (transstrat == 1) |
| 806 | // current.transfer(foreign); |
| 807 | //// foreign.setAccountLink(current.getUsername()); |
| 808 | //// current.setAccountLink(foreign.getUsername()); |
| 809 | // // entHandler.purgeDetails(foreign); |
| 810 | // // entHandler.purgeSettings(foreign); |
| 811 | // }else if (foreign instanceof KorAPUser |
| 812 | // && current instanceof ShibUser) { |
| 813 | // if (transstrat == 0) |
| 814 | // foreign.transfer(current); |
| 815 | //// current.setAccountLink(foreign.getUsername()); |
| 816 | // // entHandler.purgeDetails(current); |
| 817 | // // entHandler.purgeSettings(current); |
| 818 | // // entHandler.purgeSettings(current); |
| 819 | // } |
| 820 | // entHandler.updateAccount(current); |
| 821 | // entHandler.updateAccount(foreign); |
| 822 | // } |
| 823 | } |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 824 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 825 | // todo: test and rest usage?! |
| 826 | public boolean updateAccount(User user) throws KustvaktException { |
| 827 | boolean result; |
| 828 | if (user instanceof DemoUser) |
| 829 | throw new KustvaktException(user.getId(), StatusCodes.REQUEST_INVALID, |
| 830 | "account not updateable for demo user", user.getUsername()); |
| 831 | else { |
| 832 | // crypto.validate(user); |
| 833 | try { |
| 834 | result = entHandler.updateAccount(user) > 0; |
| 835 | } catch (KustvaktException e) { |
| 836 | jlog.error("Error: {}", e.string()); |
| 837 | throw new WrappedException(e, StatusCodes.UPDATE_ACCOUNT_FAILED); |
| 838 | } |
| 839 | } |
| 840 | if (result) { |
| 841 | // this.removeCacheEntry(user.getUsername()); |
| 842 | auditing.audit( |
| 843 | AuditRecord.serviceRecord(user.getId(), StatusCodes.UPDATE_ACCOUNT_SUCCESSFUL, user.toString())); |
| 844 | } |
| 845 | return result; |
| 846 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 847 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 848 | public boolean deleteAccount(User user) throws KustvaktException { |
| 849 | boolean result; |
| 850 | if (user instanceof DemoUser) |
| 851 | return true; |
| 852 | else { |
| 853 | try { |
| 854 | result = entHandler.deleteAccount(user.getId()) > 0; |
| 855 | } catch (KustvaktException e) { |
| 856 | jlog.error("Error: {}", e.string()); |
| 857 | throw new WrappedException(e, StatusCodes.DELETE_ACCOUNT_FAILED); |
| 858 | } |
| 859 | } |
| 860 | if (result) { |
| 861 | // this.removeCacheEntry(user.getUsername()); |
| 862 | auditing.audit(AuditRecord.serviceRecord(user.getUsername(), StatusCodes.DELETE_ACCOUNT_SUCCESSFUL, |
| 863 | user.toString())); |
| 864 | } |
| 865 | return result; |
| 866 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 867 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 868 | public Object[] validateResetPasswordRequest(String username, String email) throws KustvaktException { |
| 869 | String uritoken; |
| 870 | validator.validateEntry(email, Attributes.EMAIL); |
| 871 | User ident; |
| 872 | try { |
| 873 | ident = entHandler.getAccount(username); |
| 874 | if (ident instanceof DemoUser) |
| 875 | // throw new |
| 876 | // NotAuthorizedException(StatusCodes.PERMISSION_DENIED, |
| 877 | // "password reset now allowed for DemoUser", ""); |
| 878 | throw new WrappedException(username, StatusCodes.PASSWORD_RESET_FAILED, username); |
| 879 | } catch (EmptyResultException e) { |
| 880 | throw new WrappedException( |
| 881 | new KustvaktException(username, StatusCodes.ILLEGAL_ARGUMENT, "username not found", username), |
| 882 | StatusCodes.PASSWORD_RESET_FAILED, username); |
| 883 | } |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 884 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 885 | Userdata data = this.getUserData(ident, UserDetails.class); |
| 886 | KorAPUser user = (KorAPUser) ident; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 887 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 888 | if (!email.equals(data.get(Attributes.EMAIL))) |
| 889 | // throw new NotAuthorizedException(StatusCodes.ILLEGAL_ARGUMENT, |
| 890 | // "invalid parameter: email", "email"); |
| 891 | throw new WrappedException( |
| 892 | new KustvaktException(user.getId(), StatusCodes.ILLEGAL_ARGUMENT, "email invalid", email), |
| 893 | StatusCodes.PASSWORD_RESET_FAILED, email); |
| 894 | uritoken = crypto.encodeBase(); |
| 895 | URIParam param = new URIParam(uritoken, TimeUtils.plusHours(24).getMillis()); |
| 896 | user.addField(param); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 897 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 898 | try { |
| 899 | entHandler.updateAccount(user); |
| 900 | } catch (KustvaktException e) { |
| 901 | jlog.error("Error ", e.string()); |
| 902 | throw new WrappedException(e, StatusCodes.PASSWORD_RESET_FAILED); |
| 903 | } |
| 904 | return new Object[] { uritoken, TimeUtils.format(param.getUriExpiration()) }; |
| 905 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 906 | |
| margaretha | 38d530e | 2017-07-11 19:06:50 +0200 | [diff] [blame] | 907 | // EM: not in the new DB |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 908 | @Override |
| 909 | public <T extends Userdata> T getUserData(User user, Class<T> clazz) throws WrappedException { |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 910 | try { |
| 911 | UserDataDbIface<T> dao = BeansFactory.getTypeFactory() |
| 912 | .getTypeInterfaceBean(BeansFactory.getKustvaktContext().getUserDataProviders(), clazz); |
| 913 | T data = null; |
| 914 | if (dao != null) |
| 915 | data = dao.get(user); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 916 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 917 | if (data == null) |
| margaretha | f6d5a82 | 2017-10-19 19:51:20 +0200 | [diff] [blame] | 918 | throw new KustvaktException(user.getId(), StatusCodes.NO_RESULT_FOUND, "No data found!", |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 919 | clazz.getSimpleName()); |
| 920 | return data; |
| 921 | } catch (KustvaktException e) { |
| 922 | jlog.error("Error during user data retrieval: {}", e.getEntity()); |
| 923 | throw new WrappedException(e, StatusCodes.GET_ACCOUNT_FAILED); |
| 924 | } |
| 925 | } |
| Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 926 | |
| margaretha | ebc5496 | 2017-05-29 13:23:07 +0200 | [diff] [blame] | 927 | // todo: cache userdata outside of the user object! |
| 928 | @Override |
| 929 | public void updateUserData(Userdata data) throws WrappedException { |
| 930 | try { |
| 931 | data.validate(this.validator); |
| 932 | UserDataDbIface dao = BeansFactory.getTypeFactory() |
| 933 | .getTypeInterfaceBean(BeansFactory.getKustvaktContext().getUserDataProviders(), data.getClass()); |
| 934 | if (dao != null) |
| 935 | dao.update(data); |
| 936 | } catch (KustvaktException e) { |
| 937 | jlog.error("Error during update of user data!", e.getEntity()); |
| 938 | throw new WrappedException(e, StatusCodes.UPDATE_ACCOUNT_FAILED); |
| 939 | } |
| 940 | } |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 941 | |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 942 | } |