Phone number lookups

This commit is contained in:
Maxime Dor
2017-04-03 01:17:03 +02:00
parent d53c9e4263
commit 8bd17d3ffa
4 changed files with 52 additions and 28 deletions

View File

@@ -61,7 +61,6 @@ ldap:
bindDn: 'CN=Matrix Identity Server,CN=Users,DC=example,DC=org' bindDn: 'CN=Matrix Identity Server,CN=Users,DC=example,DC=org'
bindPassword: 'password' bindPassword: 'password'
baseDn: 'CN=Users,DC=example,DC=org' baseDn: 'CN=Users,DC=example,DC=org'
query: '(|(mailPrimaryAddress=%3pid)(mail=%3pid)(otherMailbox=%3pid))'
# How should we resolve the Matrix ID in case of a match using the attribute. # How should we resolve the Matrix ID in case of a match using the attribute.
# #
@@ -81,6 +80,17 @@ ldap:
# are typically not used. # are typically not used.
attribute: 'sAMAccountName' attribute: 'sAMAccountName'
# Configure each 3PID type with a dedicated query.
mappings:
email: "(|(mailPrimaryAddress=%3pid)(mail=%3pid)(otherMailbox=%3pid))"
# Phone numbers query.
#
# Phone numbers use the MSISDN format: https://en.wikipedia.org/wiki/MSISDN
# This format does not include international prefix (+ or 00) and therefore has to be put in the query.
# Adapt this to your needs for each attribute.
msisdn: "(|(telephoneNumber=+%3pid)(mobile=+%3pid)(homePhone=+%3pid)(otherMobile=+3pid)(otherHomePhone=+%3pid)(otherTelephone=+%3pid))"
forward: forward:

View File

@@ -22,6 +22,7 @@ package io.kamax.mxisd.api
enum ThreePidType { enum ThreePidType {
email email,
msisdn,
} }

View File

@@ -20,21 +20,27 @@
package io.kamax.mxisd.config package io.kamax.mxisd.config
import io.kamax.mxisd.api.ThreePidType
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.InitializingBean
import org.springframework.boot.context.properties.ConfigurationProperties import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Configuration
@Configuration @Configuration
@ConfigurationProperties(prefix = "ldap") @ConfigurationProperties(prefix = "ldap")
class LdapConfig { class LdapConfig implements InitializingBean {
private Logger log = LoggerFactory.getLogger(LdapConfig.class)
private String host private String host
private int port private int port
private String baseDn private String baseDn
private String query
private String type private String type
private String attribute private String attribute
private String bindDn private String bindDn
private String bindPassword private String bindPassword
private Map<ThreePidType, String> mappings
String getHost() { String getHost() {
return host return host
@@ -60,14 +66,6 @@ class LdapConfig {
this.baseDn = baseDn this.baseDn = baseDn
} }
String getQuery() {
return query
}
void setQuery(String query) {
this.query = query
}
String getType() { String getType() {
return type return type
} }
@@ -100,4 +98,24 @@ class LdapConfig {
this.bindPassword = bindPassword this.bindPassword = bindPassword
} }
Map<ThreePidType, String> getMappings() {
return mappings
}
void setMappings(Map<ThreePidType, String> mappings) {
this.mappings = mappings
}
Optional<String> getMapping(ThreePidType type) {
if (mappings == null) {
return Optional.empty()
}
return Optional.ofNullable(mappings.get(type))
}
@Override
void afterPropertiesSet() throws Exception {
log.info("Matrix ID type: {}", getType())
}
} }

View File

@@ -31,12 +31,11 @@ import org.apache.directory.ldap.client.api.LdapConnection
import org.apache.directory.ldap.client.api.LdapNetworkConnection import org.apache.directory.ldap.client.api.LdapNetworkConnection
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.beans.factory.InitializingBean
import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component import org.springframework.stereotype.Component
@Component @Component
class LdapProvider implements ThreePidProvider, InitializingBean { class LdapProvider implements ThreePidProvider {
public static final String UID = "uid" public static final String UID = "uid"
public static final String MATRIX_ID = "mxid" public static final String MATRIX_ID = "mxid"
@@ -49,13 +48,6 @@ class LdapProvider implements ThreePidProvider, InitializingBean {
@Autowired @Autowired
private LdapConfig ldapCfg private LdapConfig ldapCfg
@Override
void afterPropertiesSet() throws Exception {
if (!Arrays.asList(UID, MATRIX_ID).contains(ldapCfg.getType())) {
throw new IllegalArgumentException(ldapCfg.getType() + " is not a valid LDAP lookup type")
}
}
@Override @Override
boolean isLocal() { boolean isLocal() {
return true return true
@@ -74,7 +66,13 @@ class LdapProvider implements ThreePidProvider, InitializingBean {
try { try {
conn.bind(ldapCfg.getBindDn(), ldapCfg.getBindPassword()) conn.bind(ldapCfg.getBindDn(), ldapCfg.getBindPassword())
String searchQuery = ldapCfg.getQuery().replaceAll("%3pid", request.getThreePid()) Optional<String> queryOpt = ldapCfg.getMapping(request.getType())
if (!queryOpt.isPresent()) {
log.warn("{} is not a supported 3PID type for LDAP lookup", request.getType())
return Optional.empty()
}
String searchQuery = queryOpt.get().replaceAll("%3pid", request.getThreePid())
EntryCursor cursor = conn.search(ldapCfg.getBaseDn(), searchQuery, SearchScope.SUBTREE, ldapCfg.getAttribute()) EntryCursor cursor = conn.search(ldapCfg.getBaseDn(), searchQuery, SearchScope.SUBTREE, ldapCfg.getAttribute())
try { try {
if (cursor.next()) { if (cursor.next()) {
@@ -90,16 +88,14 @@ class LdapProvider implements ThreePidProvider, InitializingBean {
// TODO Should we turn this block into a map of functions? // TODO Should we turn this block into a map of functions?
if (StringUtils.equals(UID, ldapCfg.getType())) { if (StringUtils.equals(UID, ldapCfg.getType())) {
matrixId.append("@").append(data).append(":").append(srvCfg.getName()) matrixId.append("@").append(data).append(":").append(srvCfg.getName())
} } else if (StringUtils.equals(MATRIX_ID, ldapCfg.getType())) {
if (StringUtils.equals(MATRIX_ID, ldapCfg.getType())) {
matrixId.append(data) matrixId.append(data)
} } else {
if (matrixId.length() < 1) {
log.warn("Bind was found but type ${ldapCfg.getType()} is not supported") log.warn("Bind was found but type ${ldapCfg.getType()} is not supported")
return Optional.empty() return Optional.empty()
} }
log.info("Found a match in LDAP")
return Optional.of([ return Optional.of([
address : request.getThreePid(), address : request.getThreePid(),
medium : request.getType(), medium : request.getType(),
@@ -121,5 +117,4 @@ class LdapProvider implements ThreePidProvider, InitializingBean {
return Optional.empty() return Optional.empty()
} }
} }