|
| 1 | +/** |
| 2 | + * The contents of this file are subject to the license and copyright |
| 3 | + * detailed in the LICENSE and NOTICE files at the root of the source |
| 4 | + * tree and available online at |
| 5 | + * |
| 6 | + * http://www.dspace.org/license/ |
| 7 | + */ |
| 8 | +package org.dspace.app.rest.security; |
| 9 | + |
| 10 | +import java.sql.SQLException; |
| 11 | +import java.util.UUID; |
| 12 | + |
| 13 | +import org.apache.commons.lang3.StringUtils; |
| 14 | +import org.apache.logging.log4j.LogManager; |
| 15 | +import org.apache.logging.log4j.Logger; |
| 16 | +import org.dspace.app.requestitem.service.RequestItemService; |
| 17 | +import org.dspace.app.rest.utils.ContextUtil; |
| 18 | +import org.dspace.authorize.AuthorizationBitstreamUtils; |
| 19 | +import org.dspace.authorize.AuthorizeException; |
| 20 | +import org.dspace.content.Bitstream; |
| 21 | +import org.dspace.content.service.BitstreamService; |
| 22 | +import org.dspace.core.Context; |
| 23 | +import org.dspace.services.ConfigurationService; |
| 24 | +import org.dspace.services.RequestService; |
| 25 | +import org.dspace.services.model.Request; |
| 26 | +import org.springframework.beans.factory.annotation.Autowired; |
| 27 | +import org.springframework.stereotype.Component; |
| 28 | + |
| 29 | +/** |
| 30 | + * Methods of this class are used on PreAuthorize annotations to decide whether a request-a-copy access |
| 31 | + * token may authorize a bitstream download. |
| 32 | + * <p> |
| 33 | + * CLARIN gates every restricted download behind its own licence flow. Vanilla DSpace 9 added a second, |
| 34 | + * independent way in: {@code BitstreamRestController.retrieve} accepted a request-a-copy access token as |
| 35 | + * proof of authorization on its own. A non-null token therefore streamed the content past the resource |
| 36 | + * policies <i>and</i> past the CLARIN licence gate. |
| 37 | + * <p> |
| 38 | + * Request-a-copy stays enabled (owner decision O-8): a token still grants access to a file the caller has |
| 39 | + * no READ policy for, but only after the same CLARIN licence check a normal download goes through. That |
| 40 | + * check is not re-implemented here - {@link AuthorizationBitstreamUtils#authorizeBitstream} is the single |
| 41 | + * implementation, and it is the very method |
| 42 | + * {@code AuthorizeServiceImpl.authorizeAction} calls for a download without a token. Wiring the token path |
| 43 | + * into it is what keeps the {@code dtoken} licence flow and the {@code accessToken} flow in agreement. |
| 44 | + * |
| 45 | + * @author Milan Majchrak (milan.majchrak at dataquest.sk) |
| 46 | + */ |
| 47 | +@Component(value = "clarinBitstreamAccessTokenSecurity") |
| 48 | +public class ClarinBitstreamAccessTokenSecurityBean { |
| 49 | + |
| 50 | + private static final Logger log = LogManager.getLogger(ClarinBitstreamAccessTokenSecurityBean.class); |
| 51 | + |
| 52 | + @Autowired |
| 53 | + private BitstreamService bitstreamService; |
| 54 | + @Autowired |
| 55 | + private RequestItemService requestItemService; |
| 56 | + @Autowired |
| 57 | + private AuthorizationBitstreamUtils authorizationBitstreamUtils; |
| 58 | + @Autowired |
| 59 | + private ConfigurationService configurationService; |
| 60 | + @Autowired |
| 61 | + private RequestService requestService; |
| 62 | + |
| 63 | + /** |
| 64 | + * Check whether the supplied request-a-copy access token authorizes downloading the given bitstream. |
| 65 | + * <p> |
| 66 | + * The token is accepted only when <b>both</b> hold: |
| 67 | + * <ol> |
| 68 | + * <li>request-a-copy is enabled and the token is valid for this bitstream (accepted request, not |
| 69 | + * expired, right bitstream) - {@link RequestItemService#authorizeAccessByAccessToken};</li> |
| 70 | + * <li>the CLARIN licence gate lets the current user have the bitstream - |
| 71 | + * {@link AuthorizationBitstreamUtils#authorizeBitstream}.</li> |
| 72 | + * </ol> |
| 73 | + * A caller holding a valid token for a bitstream behind a CLARIN licence they have not agreed to gets |
| 74 | + * {@code false} here, so Spring Security answers 401/403 exactly as it does for a normal download that |
| 75 | + * the licence gate refuses, and the UI can send the user to the licence page. |
| 76 | + * |
| 77 | + * @param uuid bitstream ID from the request path |
| 78 | + * @param accessToken request-a-copy access token from the request, may be null |
| 79 | + * @return true only if the token is valid AND the CLARIN licence gate passes |
| 80 | + */ |
| 81 | + public boolean canDownloadWithAccessToken(UUID uuid, String accessToken) { |
| 82 | + if (uuid == null || StringUtils.isBlank(accessToken)) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + // If request-a-copy is switched off, no access token authorizes anything. |
| 87 | + if (configurationService.getProperty("request.item.type") == null) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + Request currentRequest = requestService.getCurrentRequest(); |
| 92 | + if (currentRequest == null || currentRequest.getHttpServletRequest() == null) { |
| 93 | + return false; |
| 94 | + } |
| 95 | + Context context = ContextUtil.obtainContext(currentRequest.getHttpServletRequest()); |
| 96 | + if (context == null) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + try { |
| 101 | + Bitstream bitstream = bitstreamService.find(context, uuid); |
| 102 | + if (bitstream == null || bitstream.isDeleted()) { |
| 103 | + // Let the REST layer answer 404; a token must not turn a missing bitstream into a 200. |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + // 1. The access token itself must be valid for this bitstream. |
| 108 | + requestItemService.authorizeAccessByAccessToken(context, bitstream, accessToken); |
| 109 | + |
| 110 | + // 2. And the CLARIN licence gate must pass, the same call a download without a token makes |
| 111 | + // through AuthorizeServiceImpl.authorizeAction. Throws MissingLicenseAgreementException or |
| 112 | + // DownloadTokenExpiredException (both AuthorizeException) when the licence is not satisfied. |
| 113 | + authorizationBitstreamUtils.authorizeBitstream(context, bitstream); |
| 114 | + |
| 115 | + return true; |
| 116 | + } catch (AuthorizeException e) { |
| 117 | + log.debug("Access token did not authorize download of bitstream {}: {}", uuid, e.getMessage()); |
| 118 | + return false; |
| 119 | + } catch (SQLException e) { |
| 120 | + log.error("Failed to check the access token for bitstream " + uuid, e); |
| 121 | + return false; |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments