| margaretha | d3c0fc9 | 2017-10-25 15:03:32 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.controller; |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 2 | |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 3 | import java.util.HashMap; |
| 4 | import java.util.Iterator; // 07.02.17/FB |
| 5 | import java.util.List; |
| 6 | import java.util.Locale; |
| 7 | import java.util.Map; |
| 8 | |
| 9 | import javax.ws.rs.Consumes; |
| 10 | import javax.ws.rs.GET; |
| 11 | import javax.ws.rs.HeaderParam; |
| 12 | import javax.ws.rs.POST; |
| 13 | import javax.ws.rs.Path; |
| 14 | import javax.ws.rs.Produces; |
| 15 | import javax.ws.rs.QueryParam; |
| 16 | import javax.ws.rs.core.Context; |
| 17 | import javax.ws.rs.core.HttpHeaders; |
| 18 | import javax.ws.rs.core.MediaType; |
| 19 | import javax.ws.rs.core.MultivaluedMap; |
| 20 | import javax.ws.rs.core.Response; |
| 21 | import javax.ws.rs.core.SecurityContext; |
| 22 | |
| 23 | import org.slf4j.Logger; |
| 24 | import org.springframework.beans.factory.annotation.Autowired; |
| 25 | import org.springframework.stereotype.Controller; |
| 26 | |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 27 | import com.sun.jersey.spi.container.ContainerRequest; |
| 28 | import com.sun.jersey.spi.container.ResourceFilters; |
| Bodmo | 3d6bd35 | 2017-04-25 11:31:39 +0200 | [diff] [blame] | 29 | |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame^] | 30 | import de.ids_mannheim.korap.authentication.framework.AuthorizationData; |
| 31 | import de.ids_mannheim.korap.authentication.framework.HttpAuthorizationHandler; |
| 32 | import de.ids_mannheim.korap.authentication.framework.TransferEncoding; |
| Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 33 | import de.ids_mannheim.korap.config.Attributes; |
| margaretha | 139d0f7 | 2017-11-14 18:56:22 +0100 | [diff] [blame] | 34 | import de.ids_mannheim.korap.config.AuthenticationType; |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 35 | import de.ids_mannheim.korap.config.BeansFactory; |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 36 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 37 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| 38 | import de.ids_mannheim.korap.interfaces.AuthenticationManagerIface; |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 39 | import de.ids_mannheim.korap.user.TokenContext; |
| 40 | import de.ids_mannheim.korap.user.User; |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 41 | import de.ids_mannheim.korap.utils.JsonUtils; |
| 42 | import de.ids_mannheim.korap.utils.KustvaktLogger; |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 43 | import de.ids_mannheim.korap.utils.ServiceInfo; |
| margaretha | fde771a | 2017-11-14 15:02:10 +0100 | [diff] [blame] | 44 | import de.ids_mannheim.korap.web.filter.AuthenticationFilter; |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 45 | import de.ids_mannheim.korap.web.filter.BlockingFilter; |
| 46 | import de.ids_mannheim.korap.web.filter.DemoUserFilter; |
| 47 | import de.ids_mannheim.korap.web.filter.PiwikFilter; |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 48 | import de.ids_mannheim.korap.web.utils.KustvaktResponseHandler; |
| Bodmo | 3d6bd35 | 2017-04-25 11:31:39 +0200 | [diff] [blame] | 49 | |
| margaretha | 139d0f7 | 2017-11-14 18:56:22 +0100 | [diff] [blame] | 50 | // import com.sun.xml.internal.messaging.saaj.util.Base64; |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * @author hanl |
| 54 | * @date 24/01/2014 |
| 55 | */ |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 56 | @Controller |
| margaretha | ecbe72d | 2017-07-26 17:13:32 +0200 | [diff] [blame] | 57 | @Path("/auth") |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 58 | @ResourceFilters({ PiwikFilter.class }) |
| 59 | @Produces(MediaType.TEXT_HTML + ";charset=utf-8") |
| margaretha | d3c0fc9 | 2017-10-25 15:03:32 +0200 | [diff] [blame] | 60 | public class AuthenticationController { |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 61 | |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 62 | @Autowired |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame^] | 63 | private KustvaktResponseHandler kustvaktResponseHandler; |
| 64 | |
| 65 | @Autowired |
| 66 | private HttpAuthorizationHandler authorizationHandler; |
| margaretha | 139d0f7 | 2017-11-14 18:56:22 +0100 | [diff] [blame] | 67 | |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame^] | 68 | @Autowired |
| 69 | private TransferEncoding transferEncoding; |
| 70 | |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 71 | private static Boolean DEBUG_LOG = true; |
| 72 | |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 73 | //todo: bootstrap function to transmit certain default configuration settings and examples (example user queries, |
| 74 | // default usersettings, etc.) |
| margaretha | 139d0f7 | 2017-11-14 18:56:22 +0100 | [diff] [blame] | 75 | private static Logger jlog = |
| 76 | KustvaktLogger.getLogger(AuthenticationController.class); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 77 | |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 78 | @Autowired |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 79 | private AuthenticationManagerIface controller; |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 80 | |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 81 | // private SendMail mail; |
| 82 | |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 83 | /** |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 84 | * represents json string with data. All GUI clients can access |
| 85 | * this method to get certain default values |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 86 | * --> security checks? |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 87 | * |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 88 | * @return String |
| 89 | */ |
| Michael Hanl | 25aac54 | 2016-02-01 18:16:44 +0100 | [diff] [blame] | 90 | @Deprecated |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 91 | @GET |
| 92 | @Path("bootstrap") |
| 93 | @Produces(MediaType.APPLICATION_JSON) |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 94 | public Response bootstrap () { |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 95 | Map m = new HashMap(); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 96 | // m.put("settings", new UserSettings().toObjectMap()); |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 97 | m.put("ql", BeansFactory.getKustvaktContext().getConfiguration() |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 98 | .getQueryLanguages()); |
| 99 | m.put("SortTypes", null); // types of sorting that are supported! |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 100 | m.put("version", ServiceInfo.getInfo().getVersion()); |
| margaretha | d479666 | 2017-11-09 16:11:40 +0100 | [diff] [blame] | 101 | try { |
| 102 | return Response.ok(JsonUtils.toJSON(m)).build(); |
| 103 | } |
| 104 | catch (KustvaktException e) { |
| 105 | throw kustvaktResponseHandler.throwit(e); |
| 106 | } |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 107 | } |
| 108 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 109 | |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 110 | // fixme: moved to user |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 111 | @GET |
| 112 | @Path("status") |
| margaretha | fde771a | 2017-11-14 15:02:10 +0100 | [diff] [blame] | 113 | @ResourceFilters({ AuthenticationFilter.class, DemoUserFilter.class, |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 114 | BlockingFilter.class }) |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 115 | public Response getStatus (@Context SecurityContext context, |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 116 | @HeaderParam(ContainerRequest.USER_AGENT) String agent, |
| 117 | @HeaderParam(ContainerRequest.HOST) String host, |
| 118 | @Context Locale locale) { |
| 119 | TokenContext ctx = (TokenContext) context.getUserPrincipal(); |
| margaretha | d479666 | 2017-11-09 16:11:40 +0100 | [diff] [blame] | 120 | try { |
| 121 | return Response.ok(ctx.toJson()).build(); |
| 122 | } |
| 123 | catch (KustvaktException e) { |
| 124 | throw kustvaktResponseHandler.throwit(e); |
| 125 | } |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 126 | } |
| 127 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 128 | |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 129 | @GET |
| 130 | @Path("apiToken") |
| Michael Hanl | 9be4e42 | 2016-07-21 14:13:27 +0200 | [diff] [blame] | 131 | //@ResourceFilters({HeaderFilter.class}) |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 132 | public Response requestAPIToken (@Context HttpHeaders headers, |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 133 | @Context Locale locale, |
| 134 | @HeaderParam(ContainerRequest.USER_AGENT) String agent, |
| 135 | @HeaderParam(ContainerRequest.HOST) String host, |
| 136 | @HeaderParam("referer-url") String referer, |
| Bodmo | 3d6bd35 | 2017-04-25 11:31:39 +0200 | [diff] [blame] | 137 | @QueryParam("scope") String scopes, |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 138 | // @Context WebServiceContext wsContext, // FB |
| Bodmo | 3d6bd35 | 2017-04-25 11:31:39 +0200 | [diff] [blame] | 139 | @Context SecurityContext secCtx) { |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 140 | |
| 141 | List<String> auth = |
| 142 | headers.getRequestHeader(ContainerRequest.AUTHORIZATION); |
| 143 | if (auth == null || auth.isEmpty()) { |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 144 | throw kustvaktResponseHandler |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 145 | .throwit(new KustvaktException(StatusCodes.MISSING_ARGUMENT, |
| 146 | "Authorization header is missing.", |
| 147 | "Authorization header")); |
| 148 | } |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame^] | 149 | |
| 150 | String[] values; |
| 151 | try { |
| 152 | AuthorizationData authorizationData = authorizationHandler. |
| 153 | parseAuthorizationHeader(auth.get(0)); |
| 154 | values = transferEncoding.decodeBase64(authorizationData.getToken()); |
| 155 | |
| 156 | } |
| 157 | catch (KustvaktException e) { |
| 158 | throw kustvaktResponseHandler.throwit(e); |
| 159 | } |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 160 | |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 161 | if (DEBUG_LOG == true) { |
| Bodmo | 3d6bd35 | 2017-04-25 11:31:39 +0200 | [diff] [blame] | 162 | System.out.printf("Debug: AuthService.requestAPIToken...:\n"); |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 163 | System.out.printf("Debug: auth.size=%d\n", auth.size()); |
| 164 | System.out.printf("auth.get(0)='%s'\n", auth.get(0)); |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 165 | /* hide password etc. - FB |
| 166 | if( auth.size() > 0 ) |
| 167 | { |
| 168 | Iterator it = auth.iterator(); |
| 169 | while( it.hasNext() ) |
| 170 | System.out.printf(" header '%s'\n", it.next()); |
| 171 | } |
| 172 | if( values.length > 0 ) |
| 173 | { |
| 174 | for(int i=0; i< values.length; i++) |
| 175 | { |
| 176 | System.out.printf(" values[%d]='%s'\n", i, values[i]); |
| 177 | } |
| 178 | } |
| 179 | */ |
| 180 | MultivaluedMap<String, String> headerMap = |
| 181 | headers.getRequestHeaders(); |
| 182 | if (headerMap != null && headerMap.size() > 0) { |
| 183 | Iterator<String> it = headerMap.keySet().iterator(); |
| 184 | while (it.hasNext()) { |
| 185 | String key = (String) it.next(); |
| 186 | List<String> vals = headerMap.get(key); |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame^] | 187 | // System.out.printf("Debug: requestAPIToken: '%s' = '%s'\n", |
| 188 | // key, vals); |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | } |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame^] | 192 | // System.out.printf("Debug: requestAPIToken: isSecure = %s.\n", |
| 193 | // secCtx.isSecure() ? "yes" : "no"); |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 194 | } // DEBUG_LOG |
| 195 | |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 196 | // "Invalid syntax for username and password" |
| 197 | if (values == null) |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 198 | throw kustvaktResponseHandler.throwit(StatusCodes.ACCESS_DENIED); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 199 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 200 | if (values[0].equalsIgnoreCase("null") |
| 201 | | values[1].equalsIgnoreCase("null")) |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 202 | // is actual an invalid request |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 203 | throw kustvaktResponseHandler.throwit(StatusCodes.REQUEST_INVALID); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 204 | |
| Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 205 | Map<String, Object> attr = new HashMap<>(); |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 206 | if (scopes != null && !scopes.isEmpty()) |
| 207 | attr.put(Attributes.SCOPES, scopes); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 208 | attr.put(Attributes.HOST, host); |
| 209 | attr.put(Attributes.USER_AGENT, agent); |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 210 | |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 211 | TokenContext context; |
| 212 | try { |
| Bodmo | 3d6bd35 | 2017-04-25 11:31:39 +0200 | [diff] [blame] | 213 | // User user = controller.authenticate(0, values[0], values[1], attr); Implementation by Hanl |
| margaretha | 139d0f7 | 2017-11-14 18:56:22 +0100 | [diff] [blame] | 214 | User user = controller.authenticate(AuthenticationType.LDAP, |
| 215 | values[0], values[1], attr); // Implementation with IdM/LDAP |
| Bodmo | 3d6bd35 | 2017-04-25 11:31:39 +0200 | [diff] [blame] | 216 | // Userdata data = this.controller.getUserData(user, UserDetails.class); // Implem. by Hanl |
| Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 217 | // todo: is this necessary? |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 218 | // attr.putAll(data.fields()); |
| Bodmo | ca3dcfb | 2017-05-24 16:36:00 +0200 | [diff] [blame] | 219 | controller.setAccessAndLocation(user, headers); |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 220 | if (DEBUG_LOG == true) System.out.printf( |
| 221 | "Debug: /apiToken/: location=%s, access='%s'.\n", |
| 222 | user.locationtoString(), user.accesstoString()); |
| Bodmo | ca3dcfb | 2017-05-24 16:36:00 +0200 | [diff] [blame] | 223 | attr.put(Attributes.LOCATION, user.getLocation()); |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 224 | attr.put(Attributes.CORPUS_ACCESS, user.getCorpusAccess()); |
| 225 | context = controller.createTokenContext(user, attr, |
| margaretha | 4de4119 | 2017-11-15 11:47:11 +0100 | [diff] [blame] | 226 | AuthenticationType.LDAP); |
| margaretha | 139d0f7 | 2017-11-14 18:56:22 +0100 | [diff] [blame] | 227 | //Attributes.API_AUTHENTICATION); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 228 | } |
| 229 | catch (KustvaktException e) { |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 230 | throw kustvaktResponseHandler.throwit(e); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 231 | } |
| 232 | |
| margaretha | d479666 | 2017-11-09 16:11:40 +0100 | [diff] [blame] | 233 | try { |
| 234 | return Response.ok(context.toJson()).build(); |
| 235 | } |
| 236 | catch (KustvaktException e) { |
| 237 | throw kustvaktResponseHandler.throwit(e); |
| 238 | } |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 239 | } |
| 240 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 241 | |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 242 | // todo: |
| 243 | @Deprecated |
| 244 | @GET |
| 245 | @Path("refresh") |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 246 | public Response refresh (@Context SecurityContext context, |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 247 | @Context Locale locale) { |
| 248 | TokenContext ctx = (TokenContext) context.getUserPrincipal(); |
| 249 | TokenContext newContext; |
| 250 | |
| 251 | // try { |
| 252 | // newContext = controller.refresh(ctx); |
| 253 | // }catch (KorAPException e) { |
| 254 | // KorAPLogger.ERROR_LOGGER.error("Exception encountered!", e); |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 255 | // throw KustvaktResponseHandler.throwit(e); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 256 | // } |
| 257 | // return Response.ok().entity(newContext.getToken()).build(); |
| 258 | return null; |
| 259 | } |
| 260 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 261 | |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 262 | @GET |
| 263 | @Path("sessionToken") |
| Michael Hanl | 9be4e42 | 2016-07-21 14:13:27 +0200 | [diff] [blame] | 264 | //@ResourceFilters({HeaderFilter.class}) |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 265 | public Response requestSession (@Context HttpHeaders headers, |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 266 | @Context Locale locale, |
| 267 | @HeaderParam(ContainerRequest.USER_AGENT) String agent, |
| 268 | @HeaderParam(ContainerRequest.HOST) String host) { |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 269 | List<String> auth = |
| 270 | headers.getRequestHeader(ContainerRequest.AUTHORIZATION); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 271 | |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame^] | 272 | String[] values; |
| 273 | try { |
| 274 | AuthorizationData authorizationData = authorizationHandler. |
| 275 | parseAuthorizationHeader(auth.get(0)); |
| 276 | values = transferEncoding.decodeBase64(authorizationData.getToken()); |
| 277 | |
| 278 | } |
| 279 | catch (KustvaktException e) { |
| 280 | throw kustvaktResponseHandler.throwit(e); |
| 281 | } |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 282 | // authentication = StringUtils.stripTokenType(authentication); |
| 283 | // String[] values = new String( |
| 284 | // DatatypeConverter.parseBase64Binary(authentication)).split(":"); |
| 285 | // String[] values = Base64.base64Decode(authentication).split(":"); |
| 286 | |
| 287 | // "Invalid syntax for username and password" |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 288 | |
| Bodmo | 3d6bd35 | 2017-04-25 11:31:39 +0200 | [diff] [blame] | 289 | // Implementation Hanl mit '|'. 16.02.17/FB |
| 290 | //if (values[0].equalsIgnoreCase("null") |
| 291 | // | values[1].equalsIgnoreCase("null")) |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 292 | if (values[0].equalsIgnoreCase("null") |
| Bodmo | 3d6bd35 | 2017-04-25 11:31:39 +0200 | [diff] [blame] | 293 | || values[1].equalsIgnoreCase("null")) |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 294 | throw kustvaktResponseHandler.throwit(StatusCodes.REQUEST_INVALID); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 295 | |
| Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 296 | Map<String, Object> attr = new HashMap<>(); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 297 | attr.put(Attributes.HOST, host); |
| 298 | attr.put(Attributes.USER_AGENT, agent); |
| 299 | TokenContext context; |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 300 | String contextJson; |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 301 | try { |
| margaretha | 139d0f7 | 2017-11-14 18:56:22 +0100 | [diff] [blame] | 302 | User user = controller.authenticate(AuthenticationType.SESSION, |
| 303 | values[0], values[1], attr); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 304 | context = controller.createTokenContext(user, attr, |
| margaretha | 4de4119 | 2017-11-15 11:47:11 +0100 | [diff] [blame] | 305 | AuthenticationType.SESSION); |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 306 | contextJson = context.toJson(); |
| 307 | jlog.debug(contextJson); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 308 | } |
| 309 | catch (KustvaktException e) { |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 310 | throw kustvaktResponseHandler.throwit(e); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 311 | } |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 312 | return Response.ok().entity(contextJson).build(); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 313 | } |
| 314 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 315 | |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 316 | // fixme: security issues: setup shibboleth compatible authentication system |
| 317 | // todo: will be purged with token authentication --> shib is client side |
| 318 | @POST |
| Michael Hanl | 6bfe400 | 2016-07-02 11:43:09 +0200 | [diff] [blame] | 319 | @Consumes(MediaType.APPLICATION_JSON) |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 320 | @Produces("application/json") |
| 321 | @Path("shibboleth") |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 322 | public Response loginshib (@Context HttpHeaders headers, |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 323 | @Context Locale locale, |
| 324 | @HeaderParam(ContainerRequest.USER_AGENT) String agent, |
| 325 | @HeaderParam(ContainerRequest.HOST) String host, |
| 326 | @QueryParam("client_id") String client_id) { |
| 327 | |
| 328 | // the shibfilter decrypted the values |
| 329 | // define default provider for returned access token strategy?! |
| 330 | |
| Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 331 | Map<String, Object> attr = new HashMap<>(); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 332 | attr.put(Attributes.HOST, host); |
| 333 | attr.put(Attributes.USER_AGENT, agent); |
| 334 | |
| 335 | TokenContext context; |
| 336 | |
| 337 | try { |
| 338 | // todo: distinguish type KorAP/Shibusers |
| margaretha | 139d0f7 | 2017-11-14 18:56:22 +0100 | [diff] [blame] | 339 | User user = controller.authenticate(AuthenticationType.SHIBBOLETH, |
| 340 | null, null, attr); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 341 | context = controller.createTokenContext(user, attr, null); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 342 | } |
| 343 | catch (KustvaktException e) { |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 344 | throw kustvaktResponseHandler.throwit(e); |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 345 | } |
| margaretha | d479666 | 2017-11-09 16:11:40 +0100 | [diff] [blame] | 346 | try { |
| 347 | return Response.ok().entity(context.toJson()).build(); |
| 348 | } |
| 349 | catch (KustvaktException e) { |
| 350 | throw kustvaktResponseHandler.throwit(e); |
| 351 | } |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 352 | } |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 353 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 354 | |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 355 | //fixme: moved from userservice |
| 356 | @GET |
| 357 | @Path("logout") |
| margaretha | fde771a | 2017-11-14 15:02:10 +0100 | [diff] [blame] | 358 | @ResourceFilters({ AuthenticationFilter.class, DemoUserFilter.class, |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 359 | PiwikFilter.class }) |
| 360 | public Response logout (@Context SecurityContext ctx, |
| 361 | @Context Locale locale) { |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 362 | TokenContext context = (TokenContext) ctx.getUserPrincipal(); |
| 363 | try { |
| 364 | controller.logout(context); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 365 | } |
| 366 | catch (KustvaktException e) { |
| Michael Hanl | 00ef546 | 2016-06-06 17:39:59 +0200 | [diff] [blame] | 367 | jlog.error("Logout Exception: {}", e.string()); |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 368 | throw kustvaktResponseHandler.throwit(e); |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 369 | } |
| 370 | return Response.ok().build(); |
| 371 | } |
| 372 | |
| Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 373 | } |