|
| 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.eperson.clarin; |
| 9 | + |
| 10 | +import java.sql.SQLException; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.Date; |
| 13 | +import java.util.LinkedHashSet; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Set; |
| 16 | + |
| 17 | +import org.apache.commons.lang3.ArrayUtils; |
| 18 | +import org.apache.commons.lang3.StringUtils; |
| 19 | +import org.apache.logging.log4j.LogManager; |
| 20 | +import org.apache.logging.log4j.Logger; |
| 21 | +import org.dspace.core.Context; |
| 22 | +import org.dspace.eperson.EPerson; |
| 23 | +import org.dspace.eperson.dao.clarin.EPersonNetidAliasDAO; |
| 24 | +import org.dspace.eperson.service.EPersonService; |
| 25 | +import org.dspace.eperson.service.clarin.ClarinIdentityService; |
| 26 | +import org.dspace.services.ConfigurationService; |
| 27 | +import org.springframework.beans.factory.annotation.Autowired; |
| 28 | + |
| 29 | +/** |
| 30 | + * @see ClarinIdentityService |
| 31 | + * |
| 32 | + * @author Ondrej Kosarko |
| 33 | + */ |
| 34 | +public class ClarinIdentityServiceImpl implements ClarinIdentityService { |
| 35 | + |
| 36 | + private static final Logger log = LogManager.getLogger(ClarinIdentityServiceImpl.class); |
| 37 | + |
| 38 | + public static final String SOURCE_MIGRATION = "migration"; |
| 39 | + public static final String SOURCE_AUTO_VOPERSON = "auto-voperson"; |
| 40 | + public static final String SOURCE_ADMIN = "admin"; |
| 41 | + |
| 42 | + @Autowired |
| 43 | + private EPersonNetidAliasDAO ePersonNetidAliasDAO; |
| 44 | + |
| 45 | + @Autowired |
| 46 | + private ConfigurationService configurationService; |
| 47 | + |
| 48 | + @Autowired |
| 49 | + private EPersonService ePersonService; |
| 50 | + |
| 51 | + @Override |
| 52 | + public EPerson resolve(Context context, String netid) throws SQLException { |
| 53 | + if (StringUtils.isBlank(netid)) { |
| 54 | + return null; |
| 55 | + } |
| 56 | + EPersonNetidAlias alias = ePersonNetidAliasDAO.findByNetid(context, netid); |
| 57 | + if (alias != null) { |
| 58 | + return alias.getEPerson(); |
| 59 | + } |
| 60 | + // Fall back to the legacy denormalized eperson.netid column: EPersons whose netid was |
| 61 | + // set directly (e.g. ClarinShibAuthentication#updateEPerson locking a legacy account to |
| 62 | + // its first-seen netid) never get an alias row of their own, only pre-existing netids |
| 63 | + // captured by the migration backfill do. Without this fallback such accounts would |
| 64 | + // silently stop resolving by netid once login moved to alias-only lookup. |
| 65 | + return ePersonService.findByNetid(context, netid); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public EPersonNetidAlias attach(Context context, EPerson ePerson, String netid, String source, EPerson createdBy) |
| 70 | + throws SQLException { |
| 71 | + EPersonNetidAlias existing = ePersonNetidAliasDAO.findByNetid(context, netid); |
| 72 | + if (existing != null) { |
| 73 | + if (!existing.getEPerson().getID().equals(ePerson.getID())) { |
| 74 | + throw new IllegalStateException( |
| 75 | + "netid '" + netid + "' is already attached to a different EPerson (" + |
| 76 | + existing.getEPerson().getID() + "), refusing to reattach to " + ePerson.getID()); |
| 77 | + } |
| 78 | + return existing; |
| 79 | + } |
| 80 | + |
| 81 | + EPersonNetidAlias alias = new EPersonNetidAlias(); |
| 82 | + alias.setEPerson(ePerson); |
| 83 | + alias.setNetid(netid); |
| 84 | + alias.setSource(source); |
| 85 | + alias.setCreatedBy(createdBy); |
| 86 | + alias.setCreatedDate(new Date()); |
| 87 | + return ePersonNetidAliasDAO.create(context, alias); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public List<EPersonNetidAlias> findAliases(Context context, EPerson ePerson) throws SQLException { |
| 92 | + return ePersonNetidAliasDAO.findByEPerson(context, ePerson); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void detach(Context context, EPersonNetidAlias alias) throws SQLException { |
| 97 | + ePersonNetidAliasDAO.delete(context, alias); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public boolean isAllowlistedProxy(String authority) { |
| 102 | + if (StringUtils.isBlank(authority)) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + String[] allowlist = configurationService.getArrayProperty("identity.auto-link.proxy-allowlist"); |
| 106 | + return ArrayUtils.contains(allowlist, authority); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public EPerson autoLink(Context context, List<String> releasedEppns, String proxyAuthority, String proxyNetid) |
| 111 | + throws SQLException { |
| 112 | + if (releasedEppns == null || releasedEppns.isEmpty() || StringUtils.isBlank(proxyNetid)) { |
| 113 | + return null; |
| 114 | + } |
| 115 | + if (!isAllowlistedProxy(proxyAuthority)) { |
| 116 | + log.warn("Auto-link via proxy '{}' refused: proxy is not on the identity.auto-link.proxy-allowlist.", |
| 117 | + proxyAuthority); |
| 118 | + return null; |
| 119 | + } |
| 120 | + |
| 121 | + // Already linked (e.g. concurrent/repeat login racing this method) - nothing to do. |
| 122 | + EPerson alreadyLinked = resolve(context, proxyNetid); |
| 123 | + if (alreadyLinked != null) { |
| 124 | + return alreadyLinked; |
| 125 | + } |
| 126 | + |
| 127 | + Set<EPerson> matches = new LinkedHashSet<>(); |
| 128 | + List<String> matchedOn = new ArrayList<>(); |
| 129 | + for (String eppn : releasedEppns) { |
| 130 | + if (StringUtils.isBlank(eppn)) { |
| 131 | + continue; |
| 132 | + } |
| 133 | + for (EPersonNetidAlias alias : ePersonNetidAliasDAO.findByValuePrefix(context, eppn)) { |
| 134 | + if (matches.add(alias.getEPerson())) { |
| 135 | + matchedOn.add(eppn); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + if (matches.isEmpty()) { |
| 141 | + return null; |
| 142 | + } |
| 143 | + if (matches.size() > 1) { |
| 144 | + log.warn("Auto-link via proxy '{}' matched {} different EPersons for released identities {} - " + |
| 145 | + "refusing to guess, flag for admin merge (proxy netid '{}').", |
| 146 | + proxyAuthority, matches.size(), matchedOn, proxyNetid); |
| 147 | + return null; |
| 148 | + } |
| 149 | + |
| 150 | + EPerson matched = matches.iterator().next(); |
| 151 | + attach(context, matched, proxyNetid, SOURCE_AUTO_VOPERSON, null); |
| 152 | + log.info("Auto-linked netid '{}' to EPerson {} via proxy '{}' voperson_external_id match.", |
| 153 | + proxyNetid, matched.getID(), proxyAuthority); |
| 154 | + return matched; |
| 155 | + } |
| 156 | +} |
0 commit comments