Refactor lookup providers
- Create lookup strategy object to handle several provider - New root ID server provider - Refactor code into appropriate classes
This commit is contained in:
@@ -21,14 +21,9 @@
|
||||
package io.kamax.mxisd.controller.v1
|
||||
|
||||
import groovy.json.JsonOutput
|
||||
import io.kamax.mxisd.config.LdapConfig
|
||||
import io.kamax.mxisd.exception.BadRequestException
|
||||
import io.kamax.mxisd.api.ThreePidType
|
||||
import io.kamax.mxisd.lookup.LookupStrategy
|
||||
import io.kamax.mxisd.signature.SignatureManager
|
||||
import org.apache.directory.api.ldap.model.cursor.EntryCursor
|
||||
import org.apache.directory.api.ldap.model.entry.Attribute
|
||||
import org.apache.directory.api.ldap.model.message.SearchScope
|
||||
import org.apache.directory.ldap.client.api.LdapConnection
|
||||
import org.apache.directory.ldap.client.api.LdapNetworkConnection
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RequestParam
|
||||
@@ -40,50 +35,25 @@ import static org.springframework.web.bind.annotation.RequestMethod.GET
|
||||
class MappingController {
|
||||
|
||||
@Autowired
|
||||
private LdapConfig ldapCfg
|
||||
private LookupStrategy strategy
|
||||
|
||||
@Autowired
|
||||
private SignatureManager signMgr
|
||||
|
||||
@RequestMapping(value = "/_matrix/identity/api/v1/lookup", method = GET)
|
||||
String lookup(@RequestParam String medium, @RequestParam String address) {
|
||||
if (!"email".contentEquals(medium)) {
|
||||
throw new BadRequestException("Invalid medium type")
|
||||
ThreePidType type = ThreePidType.valueOf(medium)
|
||||
Optional<?> lookupOpt = strategy.find(type, address)
|
||||
if (!lookupOpt.isPresent()) {
|
||||
return JsonOutput.toJson([])
|
||||
}
|
||||
|
||||
LdapConnection conn = new LdapNetworkConnection(ldapCfg.getHost(), ldapCfg.getPort())
|
||||
try {
|
||||
conn.bind(ldapCfg.getBindDn(), ldapCfg.getBindPassword())
|
||||
|
||||
String searchQuery = ldapCfg.getQuery().replaceAll("%3pid", address)
|
||||
EntryCursor cursor = conn.search(ldapCfg.getBaseDn(), searchQuery, SearchScope.SUBTREE, ldapCfg.getAttribute())
|
||||
try {
|
||||
if (!cursor.next()) {
|
||||
return JsonOutput.toJson([])
|
||||
}
|
||||
|
||||
Attribute attribute = cursor.get().get(ldapCfg.getAttribute())
|
||||
if (attribute == null) {
|
||||
return JsonOutput.toJson([])
|
||||
}
|
||||
|
||||
def data = new LinkedHashMap([
|
||||
address : address,
|
||||
medium : medium,
|
||||
mxid : attribute.get().toString(),
|
||||
not_before: 0,
|
||||
not_after : 9223372036854775807,
|
||||
ts : 0
|
||||
])
|
||||
|
||||
data['signatures'] = signMgr.signMessage(JsonOutput.toJson(data))
|
||||
return JsonOutput.toJson(data)
|
||||
} finally {
|
||||
cursor.close()
|
||||
}
|
||||
} finally {
|
||||
conn.close()
|
||||
def lookup = lookupOpt.get()
|
||||
if (lookup['signatures'] == null) {
|
||||
lookup['signatures'] = signMgr.signMessage(JsonOutput.toJson(lookup))
|
||||
}
|
||||
|
||||
return JsonOutput.toJson(lookup)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user