| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.security.auth; |
| 2 | |
| 3 | import de.ids_mannheim.korap.auditing.AuditRecord; |
| Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 4 | import de.ids_mannheim.korap.config.Attributes; |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 5 | import de.ids_mannheim.korap.config.BeansFactory; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 6 | import de.ids_mannheim.korap.config.KustvaktConfiguration; |
| 7 | import de.ids_mannheim.korap.config.URIParam; |
| 8 | import de.ids_mannheim.korap.exceptions.*; |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 9 | import de.ids_mannheim.korap.interfaces.AuthenticationIface; |
| 10 | import de.ids_mannheim.korap.interfaces.AuthenticationManagerIface; |
| 11 | import de.ids_mannheim.korap.interfaces.EncryptionIface; |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 12 | import de.ids_mannheim.korap.interfaces.ValidatorIface; |
| Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 13 | import de.ids_mannheim.korap.interfaces.db.AuditingIface; |
| 14 | import de.ids_mannheim.korap.interfaces.db.EntityHandlerIface; |
| Michael Hanl | 415276b | 2016-01-29 16:39:37 +0100 | [diff] [blame] | 15 | import de.ids_mannheim.korap.interfaces.db.UserDataDbIface; |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 16 | import de.ids_mannheim.korap.interfaces.defaults.ApacheValidator; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 17 | import de.ids_mannheim.korap.user.*; |
| Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 18 | import de.ids_mannheim.korap.utils.StringUtils; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 19 | import de.ids_mannheim.korap.utils.TimeUtils; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 20 | import org.slf4j.Logger; |
| Michael Hanl | ac113e5 | 2016-01-19 15:49:20 +0100 | [diff] [blame] | 21 | import org.slf4j.LoggerFactory; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 22 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 23 | import java.io.IOException; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 24 | import java.io.UnsupportedEncodingException; |
| 25 | import java.security.NoSuchAlgorithmException; |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 26 | import java.util.Collection; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 27 | import java.util.Map; |
| 28 | |
| 29 | /** |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 30 | * contains the logic to authentication and registration processes. |
| 31 | * Uses |
| 32 | * interface implementations (AuthenticationIface) for different |
| 33 | * databases and handlers |
| 34 | * |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 35 | * @author hanl |
| 36 | */ |
| 37 | public class KustvaktAuthenticationManager extends AuthenticationManagerIface { |
| 38 | |
| Michael Hanl | ac113e5 | 2016-01-19 15:49:20 +0100 | [diff] [blame] | 39 | private static Logger jlog = LoggerFactory |
| Michael Hanl | fdd9a01 | 2015-11-13 15:56:38 +0100 | [diff] [blame] | 40 | .getLogger(KustvaktAuthenticationManager.class); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 41 | private EncryptionIface crypto; |
| 42 | private EntityHandlerIface entHandler; |
| 43 | private AuditingIface auditing; |
| Michael Hanl | e17eaa5 | 2016-01-22 20:55:05 +0100 | [diff] [blame] | 44 | private KustvaktConfiguration config; |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 45 | private Collection userdatadaos; |
| 46 | private LoginCounter counter; |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 47 | private ValidatorIface validator; |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 48 | |
| 49 | public KustvaktAuthenticationManager (EntityHandlerIface userdb, |
| 50 | EncryptionIface crypto, |
| 51 | KustvaktConfiguration config, |
| 52 | AuditingIface auditer, |
| 53 | Collection<UserDataDbIface> userdatadaos) { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 54 | this.entHandler = userdb; |
| Michael Hanl | e17eaa5 | 2016-01-22 20:55:05 +0100 | [diff] [blame] | 55 | this.config = config; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 56 | this.crypto = crypto; |
| 57 | this.auditing = auditer; |
| 58 | this.counter = new LoginCounter(config); |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 59 | this.userdatadaos = userdatadaos; |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 60 | // todo: load via beancontext |
| 61 | try { |
| 62 | this.validator = new ApacheValidator(); |
| 63 | } catch (IOException e) { |
| 64 | e.printStackTrace(); |
| 65 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 66 | } |
| 67 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 68 | |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 69 | /** |
| 70 | * get session object if token was a session token |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 71 | * |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 72 | * @param token |
| 73 | * @param host |
| 74 | * @param useragent |
| 75 | * @return |
| 76 | * @throws KustvaktException |
| 77 | */ |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 78 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 79 | public TokenContext getTokenStatus (String token, String host, |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 80 | String useragent) throws KustvaktException { |
| Michael Hanl | 3520dcd | 2016-02-08 19:11:37 +0100 | [diff] [blame] | 81 | jlog.info("getting session status of token type '{}'", |
| 82 | token.split(" ")[0]); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 83 | AuthenticationIface provider = getProvider( |
| Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 84 | StringUtils.getTokenType(token), null); |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 85 | |
| 86 | if (provider == null) |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 87 | // throw exception for missing type parameter |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 88 | throw new KustvaktException(StatusCodes.ILLEGAL_ARGUMENT, |
| 89 | "token type not defined or found", "token_type"); |
| 90 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 91 | TokenContext context = provider.getTokenContext(token); |
| 92 | System.out.println("CONTEXT "+ context.toResponse()); |
| 93 | if (context != null && TimeUtils.isExpired(context.getExpirationTime())) |
| 94 | throw new KustvaktException(StatusCodes.EXPIRED); |
| 95 | |
| Michael Hanl | f1e85e7 | 2016-01-21 16:55:45 +0100 | [diff] [blame] | 96 | // if (!matchStatus(host, useragent, context)) |
| 97 | // provider.removeUserSession(token); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 98 | return context; |
| 99 | } |
| 100 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 101 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 102 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 103 | public User getUser (String username) throws KustvaktException { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 104 | //User user; |
| 105 | //Object value = this.getCacheValue(username); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 106 | |
| Michael Hanl | c444602 | 2016-02-12 18:03:17 +0100 | [diff] [blame] | 107 | if (User.UserFactory.isDemo(username)) |
| 108 | return User.UserFactory.getDemoUser(); |
| 109 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 110 | //if (value != null) { |
| 111 | // Map map = (Map) value; |
| 112 | // user = User.UserFactory.toUser(map); |
| 113 | //} |
| 114 | // else { |
| 115 | // user = entHandler.getAccount(username); |
| 116 | // this.storeInCache(username, user.toCache()); |
| Michael Hanl | 7368aa4 | 2016-02-05 18:15:47 +0100 | [diff] [blame] | 117 | // todo: not valid. for the duration of the session, the host should not change! |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 118 | //} |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 119 | //todo: |
| 120 | // user.addField(Attributes.HOST, context.getHostAddress()); |
| 121 | // user.addField(Attributes.USER_AGENT, context.getUserAgent()); |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 122 | return entHandler.getAccount(username); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 123 | } |
| 124 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 125 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 126 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 127 | public TokenContext refresh (TokenContext context) throws KustvaktException { |
| 128 | AuthenticationIface provider = getProvider(context.getTokenType(), null); |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 129 | if (provider == null) { |
| Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 130 | //todo: |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 133 | try { |
| 134 | provider.removeUserSession(context.getToken()); |
| 135 | User user = getUser(context.getUsername()); |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 136 | return provider.createTokenContext(user, context.params()); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 137 | } |
| 138 | catch (KustvaktException e) { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 139 | throw new WrappedException(e, StatusCodes.LOGIN_FAILED); |
| 140 | } |
| 141 | } |
| 142 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 143 | |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 144 | /** |
| 145 | * @param type |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 146 | * @param attributes |
| 147 | * contains username and password to authenticate the |
| 148 | * user. |
| 149 | * Depending of the authentication schema, may contain |
| 150 | * other values as well |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 151 | * @return User |
| 152 | * @throws KustvaktException |
| 153 | */ |
| Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 154 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 155 | public User authenticate (int type, String username, String password, |
| Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 156 | Map<String, Object> attributes) throws KustvaktException { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 157 | User user; |
| 158 | switch (type) { |
| 159 | case 1: |
| 160 | // todo: |
| 161 | user = authenticateShib(attributes); |
| 162 | break; |
| 163 | default: |
| 164 | user = authenticate(username, password, attributes); |
| 165 | break; |
| 166 | } |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 167 | auditing.audit(AuditRecord.serviceRecord(user.getId(), |
| 168 | StatusCodes.LOGIN_SUCCESSFUL, user.toString())); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 169 | return user; |
| 170 | } |
| 171 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 172 | |
| Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 173 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 174 | public TokenContext createTokenContext (User user, |
| 175 | Map<String, Object> attr, String provider_key) |
| 176 | throws KustvaktException { |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 177 | AuthenticationIface provider = getProvider(provider_key, |
| 178 | Attributes.API_AUTHENTICATION); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 179 | |
| 180 | if (attr.get(Attributes.SCOPES) != null) |
| Michael Hanl | 5dd931a | 2016-01-29 16:40:38 +0100 | [diff] [blame] | 181 | this.getUserData(user, UserDetails.class); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 182 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 183 | TokenContext context = provider.createTokenContext(user, attr); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 184 | if (context == null) |
| 185 | throw new KustvaktException(StatusCodes.NOT_SUPPORTED); |
| Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 186 | context.setUserAgent((String) attr.get(Attributes.USER_AGENT)); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 187 | context.setHostAddress(Attributes.HOST); |
| 188 | return context; |
| 189 | } |
| 190 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 191 | |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 192 | //todo: test |
| Michael Hanl | f1e85e7 | 2016-01-21 16:55:45 +0100 | [diff] [blame] | 193 | @Deprecated |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 194 | private boolean matchStatus (String host, String useragent, |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 195 | TokenContext context) { |
| 196 | if (host.equals(context.getHostAddress())) { |
| 197 | if (useragent.equals(context.getUserAgent())) |
| 198 | return true; |
| 199 | } |
| 200 | return false; |
| 201 | } |
| 202 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 203 | |
| 204 | private User authenticateShib (Map<String, Object> attributes) |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 205 | throws KustvaktException { |
| 206 | // todo use persistent id, since eppn is not unique |
| 207 | String eppn = (String) attributes.get(Attributes.EPPN); |
| 208 | |
| 209 | if (eppn == null || eppn.isEmpty()) |
| 210 | throw new KustvaktException(StatusCodes.REQUEST_INVALID); |
| 211 | |
| 212 | if (!attributes.containsKey(Attributes.EMAIL) |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 213 | && validator.isValid(eppn, Attributes.EMAIL)) |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 214 | attributes.put(Attributes.EMAIL, eppn); |
| 215 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 216 | User user = null; |
| 217 | if (isRegistered(eppn)) |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 218 | user = createShibbUserAccount(attributes); |
| 219 | return user; |
| 220 | } |
| 221 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 222 | |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 223 | //todo: what if attributes null? |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 224 | private User authenticate (String username, String password, |
| Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 225 | Map<String, Object> attr) throws KustvaktException { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 226 | Map<String, Object> attributes = validator.validateMap(attr); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 227 | User unknown; |
| 228 | // just to make sure that the plain password does not appear anywhere in the logs! |
| 229 | |
| 230 | try { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 231 | validator.validateEntry(username, Attributes.USERNAME); |
| 232 | } catch (KustvaktException e) { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 233 | throw new WrappedException(e, StatusCodes.LOGIN_FAILED, username); |
| 234 | } |
| 235 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 236 | if (username == null || username.isEmpty()) |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 237 | throw new WrappedException(new KustvaktException(username, |
| 238 | StatusCodes.BAD_CREDENTIALS), StatusCodes.LOGIN_FAILED); |
| 239 | else { |
| 240 | try { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 241 | unknown = entHandler.getAccount(username); |
| 242 | } catch (EmptyResultException e) { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 243 | // mask exception to disable user guessing in possible attacks |
| 244 | throw new WrappedException(new KustvaktException(username, |
| 245 | StatusCodes.BAD_CREDENTIALS), StatusCodes.LOGIN_FAILED, |
| 246 | username); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 247 | } |
| 248 | catch (KustvaktException e) { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 249 | jlog.error("Error: {}", e); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 250 | throw new WrappedException(e, StatusCodes.LOGIN_FAILED, |
| 251 | attributes.toString()); |
| 252 | } |
| 253 | } |
| Michael Hanl | e17eaa5 | 2016-01-22 20:55:05 +0100 | [diff] [blame] | 254 | jlog.trace("Authentication: found username " + unknown.getUsername()); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 255 | if (unknown instanceof KorAPUser) { |
| 256 | if (password == null || password.isEmpty()) |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 257 | throw new WrappedException(new KustvaktException( |
| 258 | unknown.getId(), StatusCodes.BAD_CREDENTIALS), |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 259 | StatusCodes.LOGIN_FAILED, username); |
| 260 | |
| 261 | KorAPUser user = (KorAPUser) unknown; |
| 262 | boolean check = crypto.checkHash(password, user.getPassword()); |
| 263 | |
| 264 | if (!check) { |
| 265 | // the fail counter only applies for wrong password |
| 266 | jlog.warn("Wrong Password!"); |
| 267 | processLoginFail(unknown); |
| 268 | throw new WrappedException(new KustvaktException(user.getId(), |
| 269 | StatusCodes.BAD_CREDENTIALS), StatusCodes.LOGIN_FAILED, |
| 270 | username); |
| 271 | } |
| 272 | |
| 273 | // bad credentials error has presedence over account locked or unconfirmed codes |
| 274 | // since latter can lead to account guessing of third parties |
| 275 | if (user.isAccountLocked()) { |
| 276 | URIParam param = (URIParam) user.getField(URIParam.class); |
| 277 | |
| 278 | if (param.hasValues()) { |
| 279 | jlog.debug("Account is not yet activated for user '{}'", |
| 280 | user.getUsername()); |
| 281 | if (TimeUtils.getNow().isAfter(param.getUriExpiration())) { |
| Michael Hanl | ac113e5 | 2016-01-19 15:49:20 +0100 | [diff] [blame] | 282 | jlog.error( |
| 283 | "URI token is expired. Deleting account for user {}", |
| 284 | user.getUsername()); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 285 | deleteAccount(user); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 286 | throw new WrappedException(new KustvaktException( |
| 287 | unknown.getId(), StatusCodes.EXPIRED, |
| 288 | "account confirmation uri has expired", |
| 289 | param.getUriFragment()), |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 290 | StatusCodes.LOGIN_FAILED, username); |
| 291 | } |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 292 | throw new WrappedException(new KustvaktException( |
| 293 | unknown.getId(), StatusCodes.UNCONFIRMED_ACCOUNT), |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 294 | StatusCodes.LOGIN_FAILED, username); |
| 295 | } |
| Michael Hanl | ac113e5 | 2016-01-19 15:49:20 +0100 | [diff] [blame] | 296 | jlog.error("ACCESS DENIED: account not active for '{}'", |
| 297 | unknown.getUsername()); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 298 | throw new WrappedException(new KustvaktException( |
| 299 | unknown.getId(), StatusCodes.ACCOUNT_DEACTIVATED), |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 300 | StatusCodes.LOGIN_FAILED, username); |
| 301 | } |
| 302 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 303 | } |
| 304 | else if (unknown instanceof ShibUser) { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 305 | //todo |
| 306 | } |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 307 | jlog.debug("Authentication done: " + username); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 308 | return unknown; |
| 309 | } |
| 310 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 311 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 312 | public boolean isRegistered (String username) { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 313 | User user; |
| 314 | if (username == null || username.isEmpty()) |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 315 | return false; |
| 316 | // throw new KustvaktException(username, StatusCodes.ILLEGAL_ARGUMENT, |
| 317 | // "username must be set", username); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 318 | |
| 319 | try { |
| 320 | user = entHandler.getAccount(username); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 321 | } |
| 322 | catch (EmptyResultException e) { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 323 | jlog.debug("user does not exist ({})", username); |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 324 | return false; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 325 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 326 | } |
| 327 | catch (KustvaktException e) { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 328 | jlog.error("KorAPException", e.string()); |
| 329 | return false; |
| 330 | //throw new KustvaktException(username, StatusCodes.ILLEGAL_ARGUMENT, |
| 331 | // "username invalid", username); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 332 | } |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 333 | return user != null; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 334 | } |
| 335 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 336 | |
| 337 | public void logout (TokenContext context) throws KustvaktException { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 338 | try { |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 339 | AuthenticationIface provider = getProvider(context.getTokenType(), |
| 340 | null); |
| 341 | |
| 342 | if (provider == null) { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 343 | throw new KustvaktException(StatusCodes.ILLEGAL_ARGUMENT, |
| 344 | "provider not supported!", context.getTokenType()); |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 345 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 346 | provider.removeUserSession(context.getToken()); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 347 | } |
| 348 | catch (KustvaktException e) { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 349 | throw new WrappedException(e, StatusCodes.LOGOUT_FAILED, |
| 350 | context.toString()); |
| 351 | } |
| 352 | auditing.audit(AuditRecord.serviceRecord(context.getUsername(), |
| 353 | StatusCodes.LOGOUT_SUCCESSFUL, context.toString())); |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 354 | this.removeCacheEntry(context.getToken()); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 355 | } |
| 356 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 357 | |
| 358 | private void processLoginFail (User user) throws KustvaktException { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 359 | counter.registerFail(user.getUsername()); |
| 360 | if (!counter.validate(user.getUsername())) { |
| 361 | try { |
| 362 | this.lockAccount(user); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 363 | } |
| 364 | catch (KustvaktException e) { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 365 | jlog.error("user account could not be locked", e); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 366 | throw new WrappedException(e, StatusCodes.UPDATE_ACCOUNT_FAILED); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 367 | } |
| 368 | throw new WrappedException(new KustvaktException(user.getId(), |
| 369 | StatusCodes.ACCOUNT_DEACTIVATED), StatusCodes.LOGIN_FAILED); |
| 370 | } |
| 371 | } |
| 372 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 373 | |
| 374 | public void lockAccount (User user) throws KustvaktException { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 375 | if (!(user instanceof KorAPUser)) |
| 376 | throw new KustvaktException(StatusCodes.REQUEST_INVALID); |
| 377 | |
| 378 | KorAPUser u = (KorAPUser) user; |
| 379 | u.setAccountLocked(true); |
| 380 | jlog.info("locking account for user: {}", user.getUsername()); |
| 381 | entHandler.updateAccount(u); |
| 382 | } |
| 383 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 384 | |
| 385 | public KorAPUser checkPasswordAllowance (KorAPUser user, |
| 386 | String oldPassword, String newPassword) throws KustvaktException { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 387 | String dbPassword = user.getPassword(); |
| 388 | |
| 389 | if (oldPassword.trim().equals(newPassword.trim())) { |
| 390 | // TODO: special error StatusCodes for this? |
| 391 | throw new WrappedException(new KustvaktException(user.getId(), |
| 392 | StatusCodes.ILLEGAL_ARGUMENT), |
| 393 | StatusCodes.PASSWORD_RESET_FAILED, newPassword); |
| 394 | } |
| 395 | |
| 396 | boolean check = crypto.checkHash(oldPassword, dbPassword); |
| 397 | |
| 398 | if (!check) |
| 399 | throw new WrappedException(new KustvaktException(user.getId(), |
| 400 | StatusCodes.BAD_CREDENTIALS), |
| 401 | StatusCodes.PASSWORD_RESET_FAILED); |
| 402 | |
| 403 | try { |
| Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 404 | user.setPassword(crypto.secureHash(newPassword)); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 405 | } |
| 406 | catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 407 | throw new WrappedException(new KustvaktException(user.getId(), |
| 408 | StatusCodes.ILLEGAL_ARGUMENT, "password invalid", |
| 409 | newPassword), StatusCodes.PASSWORD_RESET_FAILED, |
| 410 | user.toString(), newPassword); |
| 411 | } |
| 412 | return user; |
| 413 | } |
| 414 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 415 | |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 416 | //fixme: use clientinfo for logging/auditing?! = from where did he access the reset function? |
| 417 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 418 | public void resetPassword (String uriFragment, String username, |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 419 | String newPassphrase) throws KustvaktException { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 420 | try { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 421 | validator.validateEntry(username, Attributes.USERNAME); |
| 422 | validator.validateEntry(newPassphrase, Attributes.PASSWORD); |
| 423 | } catch (KustvaktException e) { |
| 424 | jlog.error("Error: {}", e.string()); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 425 | throw new WrappedException(new KustvaktException(username, |
| 426 | StatusCodes.ILLEGAL_ARGUMENT, "password invalid", |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 427 | newPassphrase), StatusCodes.PASSWORD_RESET_FAILED, |
| 428 | username, newPassphrase); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | try { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 432 | newPassphrase= crypto.secureHash(newPassphrase); |
| 433 | } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { |
| Michael Hanl | ac113e5 | 2016-01-19 15:49:20 +0100 | [diff] [blame] | 434 | jlog.error("Encoding/Algorithm Error", e); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 435 | throw new WrappedException(new KustvaktException(username, |
| 436 | StatusCodes.ILLEGAL_ARGUMENT, "password invalid", |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 437 | newPassphrase), StatusCodes.PASSWORD_RESET_FAILED, |
| 438 | username, uriFragment, newPassphrase); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 439 | } |
| 440 | int result = entHandler |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 441 | .resetPassphrase(username, uriFragment, newPassphrase); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 442 | |
| 443 | if (result == 0) |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 444 | throw new WrappedException(new KustvaktException(username, |
| 445 | StatusCodes.EXPIRED, "URI fragment expired", uriFragment), |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 446 | StatusCodes.PASSWORD_RESET_FAILED, username, uriFragment); |
| 447 | else if (result == 1) |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 448 | jlog.info("successfully reset password for user {}", username); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 449 | } |
| 450 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 451 | |
| 452 | public void confirmRegistration (String uriFragment, String username) |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 453 | throws KustvaktException { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 454 | try { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 455 | validator.validateEntry(username, Attributes.USERNAME); |
| 456 | } catch (KustvaktException e) { |
| 457 | jlog.error("Error: {}", e.string()); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 458 | throw new WrappedException(e, |
| 459 | StatusCodes.ACCOUNT_CONFIRMATION_FAILED, username, |
| 460 | uriFragment); |
| 461 | } |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 462 | int r = entHandler.activateAccount(username, uriFragment); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 463 | if (r == 0) { |
| 464 | User user; |
| 465 | try { |
| 466 | user = entHandler.getAccount(username); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 467 | } |
| 468 | catch (EmptyResultException e) { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 469 | throw new WrappedException(new KustvaktException(username, |
| 470 | StatusCodes.BAD_CREDENTIALS), |
| 471 | StatusCodes.ACCOUNT_CONFIRMATION_FAILED, username, |
| 472 | uriFragment); |
| 473 | } |
| 474 | entHandler.deleteAccount(user.getId()); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 475 | throw new WrappedException(new KustvaktException(user.getId(), |
| 476 | StatusCodes.EXPIRED), |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 477 | StatusCodes.ACCOUNT_CONFIRMATION_FAILED, username, |
| 478 | uriFragment); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 479 | } |
| 480 | else if (r == 1) |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 481 | jlog.info("successfully confirmed user registration for user {}", |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 482 | username); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 483 | // register successful audit! |
| 484 | } |
| 485 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 486 | |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 487 | /** |
| 488 | * @param attributes |
| 489 | * @return |
| 490 | * @throws KustvaktException |
| 491 | */ |
| 492 | //fixme: remove clientinfo object (not needed), use json representation to get stuff |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 493 | public User createUserAccount (Map<String, Object> attributes, |
| Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 494 | boolean confirmation_required) throws KustvaktException { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 495 | Map<String, Object> safeMap = validator.validateMap(attributes); |
| 496 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 497 | if (safeMap.get(Attributes.USERNAME) == null |
| 498 | || ((String) safeMap.get(Attributes.USERNAME)).isEmpty()) |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 499 | throw new KustvaktException(StatusCodes.ILLEGAL_ARGUMENT, |
| 500 | "username must be set", "username"); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 501 | if (safeMap.get(Attributes.PASSWORD) == null |
| 502 | || ((String) safeMap.get(Attributes.PASSWORD)).isEmpty()) |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 503 | throw new KustvaktException(safeMap.get(Attributes.USERNAME), |
| 504 | StatusCodes.ILLEGAL_ARGUMENT, "password must be set", |
| 505 | "password"); |
| 506 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 507 | String username = validator.validateEntry( |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 508 | (String) safeMap.get(Attributes.USERNAME), Attributes.USERNAME); |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 509 | String password = validator.validateEntry( |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 510 | (String) safeMap.get(Attributes.PASSWORD), Attributes.PASSWORD); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 511 | String hash; |
| 512 | try { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 513 | hash = crypto.secureHash(password); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 514 | } |
| 515 | catch (UnsupportedEncodingException | NoSuchAlgorithmException e) { |
| Michael Hanl | ac113e5 | 2016-01-19 15:49:20 +0100 | [diff] [blame] | 516 | jlog.error("Encryption error", e); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 517 | throw new KustvaktException(StatusCodes.ILLEGAL_ARGUMENT); |
| 518 | } |
| 519 | |
| Michael Hanl | e17eaa5 | 2016-01-22 20:55:05 +0100 | [diff] [blame] | 520 | KorAPUser user = User.UserFactory.getUser(username); |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 521 | Object id = attributes.get(Attributes.ID); |
| 522 | if (id != null && id instanceof Integer) |
| 523 | user.setId((Integer) id); |
| Michael Hanl | 7368aa4 | 2016-02-05 18:15:47 +0100 | [diff] [blame] | 524 | |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 525 | user.setAccountLocked(confirmation_required); |
| Michael Hanl | e17eaa5 | 2016-01-22 20:55:05 +0100 | [diff] [blame] | 526 | if (confirmation_required) { |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 527 | URIParam param = new URIParam(crypto.createToken(), TimeUtils |
| 528 | .plusSeconds(config.getExpiration()).getMillis()); |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 529 | user.addField(param); |
| 530 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 531 | user.setPassword(hash); |
| 532 | try { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 533 | UserDetails details = new UserDetails(); |
| 534 | details.read(safeMap, true); |
| 535 | |
| 536 | UserSettings settings = new UserSettings(); |
| 537 | settings.read(safeMap, true); |
| 538 | |
| Michael Hanl | c444602 | 2016-02-12 18:03:17 +0100 | [diff] [blame] | 539 | jlog.info("Creating new user account for user {}", |
| 540 | user.getUsername()); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 541 | entHandler.createAccount(user); |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 542 | details.setUserId(user.getId()); |
| 543 | settings.setUserId(user.getId()); |
| Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 544 | |
| Michael Hanl | f8fcc7a | 2016-06-03 17:41:07 +0200 | [diff] [blame] | 545 | UserDataDbIface dao = BeansFactory.getTypeFactory() |
| 546 | .getTypeInterfaceBean(userdatadaos, UserDetails.class); |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 547 | //todo: remove this |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 548 | assert dao != null; |
| 549 | dao.store(details); |
| Michael Hanl | f8fcc7a | 2016-06-03 17:41:07 +0200 | [diff] [blame] | 550 | dao = BeansFactory.getTypeFactory().getTypeInterfaceBean( |
| 551 | userdatadaos, UserSettings.class); |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 552 | assert dao != null; |
| 553 | dao.store(settings); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 554 | } |
| 555 | catch (KustvaktException e) { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 556 | jlog.error("Error: {}", e.string()); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 557 | throw new WrappedException(e, StatusCodes.CREATE_ACCOUNT_FAILED, |
| 558 | user.toString()); |
| 559 | } |
| 560 | |
| 561 | auditing.audit(AuditRecord.serviceRecord(user.getUsername(), |
| 562 | StatusCodes.CREATE_ACCOUNT_SUCCESSFUL)); |
| 563 | return user; |
| 564 | } |
| 565 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 566 | |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 567 | //todo: |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 568 | private ShibUser createShibbUserAccount (Map<String, Object> attributes) |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 569 | throws KustvaktException { |
| 570 | jlog.debug("creating shibboleth user account for user attr: {}", |
| 571 | attributes); |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 572 | Map<String, Object> safeMap = validator.validateMap(attributes); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 573 | |
| 574 | //todo eppn non-unique.join with idp or use persistent_id as username identifier |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 575 | ShibUser user = User.UserFactory.getShibInstance( |
| 576 | (String) safeMap.get(Attributes.EPPN), |
| 577 | (String) safeMap.get(Attributes.MAIL), |
| 578 | (String) safeMap.get(Attributes.CN)); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 579 | user.setAffiliation((String) safeMap.get(Attributes.EDU_AFFIL)); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 580 | user.setAccountCreation(TimeUtils.getNow().getMillis()); |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 581 | |
| 582 | |
| 583 | UserDetails d = new UserDetails(); |
| 584 | d.read(attributes, true); |
| 585 | |
| 586 | UserSettings s = new UserSettings(); |
| 587 | s.read(attributes, true); |
| 588 | |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 589 | entHandler.createAccount(user); |
| Michael Hanl | 25aac54 | 2016-02-01 18:16:44 +0100 | [diff] [blame] | 590 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 591 | s.setUserId(user.getId()); |
| 592 | d.setUserId(user.getId()); |
| Michael Hanl | 25aac54 | 2016-02-01 18:16:44 +0100 | [diff] [blame] | 593 | |
| Michael Hanl | f8fcc7a | 2016-06-03 17:41:07 +0200 | [diff] [blame] | 594 | UserDataDbIface dao = BeansFactory.getTypeFactory() |
| 595 | .getTypeInterfaceBean(userdatadaos, UserDetails.class); |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 596 | assert dao != null; |
| 597 | dao.store(d); |
| Michael Hanl | 25aac54 | 2016-02-01 18:16:44 +0100 | [diff] [blame] | 598 | |
| Michael Hanl | f8fcc7a | 2016-06-03 17:41:07 +0200 | [diff] [blame] | 599 | dao = BeansFactory.getTypeFactory().getTypeInterfaceBean(userdatadaos, |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 600 | UserSettings.class); |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 601 | assert dao != null; |
| 602 | dao.store(d); |
| Michael Hanl | 25aac54 | 2016-02-01 18:16:44 +0100 | [diff] [blame] | 603 | |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 604 | return user; |
| 605 | } |
| 606 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 607 | |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 608 | /** |
| 609 | * link shibboleth and korap user account to one another. |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 610 | * |
| 611 | * @param current |
| 612 | * currently logged in user |
| 613 | * @param for_name |
| 614 | * foreign user name the current account should be |
| 615 | * linked to |
| 616 | * @param transstrat |
| 617 | * transfer status of user data (details, settings, |
| 618 | * user queries) |
| 619 | * 0 = the currently logged in data should be kept |
| 620 | * 1 = the foreign account data should be kept |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 621 | * @throws NotAuthorizedException |
| 622 | * @throws KustvaktException |
| 623 | */ |
| 624 | // todo: |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 625 | public void accountLink (User current, String for_name, int transstrat) |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 626 | throws KustvaktException { |
| 627 | // User foreign = entHandler.getAccount(for_name); |
| 628 | |
| 629 | // if (current.getAccountLink() == null && current.getAccountLink() |
| 630 | // .isEmpty()) { |
| 631 | // if (current instanceof KorAPUser && foreign instanceof ShibUser) { |
| 632 | // if (transstrat == 1) |
| 633 | // current.transfer(foreign); |
| 634 | //// foreign.setAccountLink(current.getUsername()); |
| 635 | //// current.setAccountLink(foreign.getUsername()); |
| 636 | // // entHandler.purgeDetails(foreign); |
| 637 | // // entHandler.purgeSettings(foreign); |
| 638 | // }else if (foreign instanceof KorAPUser |
| 639 | // && current instanceof ShibUser) { |
| 640 | // if (transstrat == 0) |
| 641 | // foreign.transfer(current); |
| 642 | //// current.setAccountLink(foreign.getUsername()); |
| 643 | // // entHandler.purgeDetails(current); |
| 644 | // // entHandler.purgeSettings(current); |
| 645 | // // entHandler.purgeSettings(current); |
| 646 | // } |
| 647 | // entHandler.updateAccount(current); |
| 648 | // entHandler.updateAccount(foreign); |
| 649 | // } |
| 650 | } |
| 651 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 652 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 653 | // todo: test and rest usage?! |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 654 | public boolean updateAccount (User user) throws KustvaktException { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 655 | boolean result; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 656 | if (user instanceof DemoUser) |
| 657 | throw new KustvaktException(user.getId(), |
| 658 | StatusCodes.REQUEST_INVALID, |
| 659 | "account not updateable for demo user", user.getUsername()); |
| 660 | else { |
| Michael Hanl | e17eaa5 | 2016-01-22 20:55:05 +0100 | [diff] [blame] | 661 | // crypto.validate(user); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 662 | try { |
| 663 | result = entHandler.updateAccount(user) > 0; |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 664 | } |
| 665 | catch (KustvaktException e) { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 666 | jlog.error("Error: {}", e.string()); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 667 | throw new WrappedException(e, StatusCodes.UPDATE_ACCOUNT_FAILED); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | if (result) { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 671 | // this.removeCacheEntry(user.getUsername()); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 672 | auditing.audit(AuditRecord.serviceRecord(user.getId(), |
| 673 | StatusCodes.UPDATE_ACCOUNT_SUCCESSFUL, user.toString())); |
| 674 | } |
| 675 | return result; |
| 676 | } |
| 677 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 678 | |
| 679 | public boolean deleteAccount (User user) throws KustvaktException { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 680 | boolean result; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 681 | if (user instanceof DemoUser) |
| 682 | return true; |
| 683 | else { |
| 684 | try { |
| 685 | result = entHandler.deleteAccount(user.getId()) > 0; |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 686 | } |
| 687 | catch (KustvaktException e) { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 688 | jlog.error("Error: {}", e.string()); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 689 | throw new WrappedException(e, StatusCodes.DELETE_ACCOUNT_FAILED); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 690 | } |
| 691 | } |
| 692 | if (result) { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 693 | // this.removeCacheEntry(user.getUsername()); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 694 | auditing.audit(AuditRecord.serviceRecord(user.getUsername(), |
| 695 | StatusCodes.DELETE_ACCOUNT_SUCCESSFUL, user.toString())); |
| 696 | } |
| 697 | return result; |
| 698 | } |
| 699 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 700 | |
| 701 | public Object[] validateResetPasswordRequest (String username, String email) |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 702 | throws KustvaktException { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 703 | String uritoken; |
| 704 | validator.validateEntry(email, Attributes.EMAIL); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 705 | User ident; |
| 706 | try { |
| 707 | ident = entHandler.getAccount(username); |
| 708 | if (ident instanceof DemoUser) |
| 709 | // throw new NotAuthorizedException(StatusCodes.PERMISSION_DENIED, |
| 710 | // "password reset now allowed for DemoUser", ""); |
| 711 | throw new WrappedException(username, |
| 712 | StatusCodes.PASSWORD_RESET_FAILED, username); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 713 | } |
| 714 | catch (EmptyResultException e) { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 715 | throw new WrappedException(new KustvaktException(username, |
| 716 | StatusCodes.ILLEGAL_ARGUMENT, "username not found", |
| 717 | username), StatusCodes.PASSWORD_RESET_FAILED, username); |
| 718 | } |
| 719 | |
| Michael Hanl | 5dd931a | 2016-01-29 16:40:38 +0100 | [diff] [blame] | 720 | Userdata data = this.getUserData(ident, UserDetails.class); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 721 | KorAPUser user = (KorAPUser) ident; |
| Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 722 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 723 | if (!email.equals(data.get(Attributes.EMAIL))) |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 724 | // throw new NotAuthorizedException(StatusCodes.ILLEGAL_ARGUMENT, |
| 725 | // "invalid parameter: email", "email"); |
| 726 | throw new WrappedException(new KustvaktException(user.getId(), |
| 727 | StatusCodes.ILLEGAL_ARGUMENT, "email invalid", email), |
| 728 | StatusCodes.PASSWORD_RESET_FAILED, email); |
| 729 | uritoken = crypto.encodeBase(); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 730 | URIParam param = new URIParam(uritoken, TimeUtils.plusHours(24) |
| 731 | .getMillis()); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 732 | user.addField(param); |
| 733 | |
| 734 | try { |
| 735 | entHandler.updateAccount(user); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 736 | } |
| 737 | catch (KustvaktException e) { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 738 | jlog.error("Error ", e.string()); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 739 | throw new WrappedException(e, StatusCodes.PASSWORD_RESET_FAILED); |
| 740 | } |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 741 | return new Object[] { uritoken, TimeUtils.format(param.getUriExpiration()) }; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 742 | } |
| 743 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 744 | |
| Michael Hanl | c2a9f62 | 2016-01-28 16:40:06 +0100 | [diff] [blame] | 745 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 746 | public <T extends Userdata> T getUserData (User user, Class<T> clazz) |
| Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 747 | throws WrappedException { |
| 748 | |
| 749 | try { |
| Michael Hanl | f8fcc7a | 2016-06-03 17:41:07 +0200 | [diff] [blame] | 750 | UserDataDbIface<T> dao = BeansFactory.getTypeFactory() |
| 751 | .getTypeInterfaceBean( |
| 752 | BeansFactory.getKustvaktContext() |
| 753 | .getUserDataProviders(), clazz); |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 754 | T data = null; |
| 755 | if (dao != null) |
| 756 | data = dao.get(user); |
| 757 | |
| Michael Hanl | 7368aa4 | 2016-02-05 18:15:47 +0100 | [diff] [blame] | 758 | if (data == null) |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 759 | throw new KustvaktException(user.getId(), |
| Michael Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 760 | StatusCodes.EMPTY_RESULTS, "No data found!", |
| 761 | clazz.getSimpleName()); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 762 | return data; |
| 763 | } |
| 764 | catch (KustvaktException e) { |
| Michael Hanl | 00ef546 | 2016-06-06 17:39:59 +0200 | [diff] [blame] | 765 | jlog.error("Error during user data retrieval: {}", e.getEntity()); |
| Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 766 | throw new WrappedException(e, StatusCodes.GET_ACCOUNT_FAILED); |
| 767 | } |
| Michael Hanl | 4f9002d | 2016-01-27 23:21:45 +0100 | [diff] [blame] | 768 | } |
| 769 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 770 | |
| Michael Hanl | 4f9002d | 2016-01-27 23:21:45 +0100 | [diff] [blame] | 771 | //todo: cache userdata outside of the user object! |
| Michael Hanl | c2a9f62 | 2016-01-28 16:40:06 +0100 | [diff] [blame] | 772 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 773 | public void updateUserData (Userdata data) throws WrappedException { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 774 | try { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame^] | 775 | data.validate(this.validator); |
| Michael Hanl | f8fcc7a | 2016-06-03 17:41:07 +0200 | [diff] [blame] | 776 | UserDataDbIface dao = BeansFactory.getTypeFactory() |
| 777 | .getTypeInterfaceBean( |
| 778 | BeansFactory.getKustvaktContext() |
| 779 | .getUserDataProviders(), data.getClass()); |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 780 | if (dao != null) |
| 781 | dao.update(data); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 782 | } |
| 783 | catch (KustvaktException e) { |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 784 | jlog.error("Error during update of user data!", e.getEntity()); |
| Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 785 | throw new WrappedException(e, StatusCodes.UPDATE_ACCOUNT_FAILED); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 786 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 787 | } |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 788 | } |