-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for #49
- Loading branch information
Showing
12 changed files
with
646 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/main/java/io/kamax/mxisd/backend/memory/MemoryIdentityStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* mxisd - Matrix Identity Server Daemon | ||
* Copyright (C) 2018 Maxime Dor | ||
* | ||
* https://www.kamax.io/ | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package io.kamax.mxisd.backend.memory; | ||
|
||
import io.kamax.matrix.MatrixID; | ||
import io.kamax.matrix.ThreePid; | ||
import io.kamax.matrix._MatrixID; | ||
import io.kamax.mxisd.UserIdType; | ||
import io.kamax.mxisd.auth.provider.AuthenticatorProvider; | ||
import io.kamax.mxisd.auth.provider.BackendAuthResult; | ||
import io.kamax.mxisd.config.MatrixConfig; | ||
import io.kamax.mxisd.config.memory.MemoryIdentityConfig; | ||
import io.kamax.mxisd.config.memory.MemoryStoreConfig; | ||
import io.kamax.mxisd.config.memory.MemoryThreePid; | ||
import io.kamax.mxisd.lookup.SingleLookupReply; | ||
import io.kamax.mxisd.lookup.SingleLookupRequest; | ||
import io.kamax.mxisd.lookup.ThreePidMapping; | ||
import io.kamax.mxisd.lookup.provider.IThreePidProvider; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Component | ||
public class MemoryIdentityStore implements AuthenticatorProvider, IThreePidProvider { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(MemoryIdentityStore.class); | ||
|
||
private final MatrixConfig mxCfg; | ||
private final MemoryStoreConfig cfg; | ||
|
||
@Autowired | ||
public MemoryIdentityStore(MatrixConfig mxCfg, MemoryStoreConfig cfg) { | ||
this.mxCfg = mxCfg; | ||
this.cfg = cfg; | ||
} | ||
|
||
public Optional<MemoryIdentityConfig> findByUsername(String username) { | ||
return cfg.getIdentities().stream().filter(id -> StringUtils.equals(id.getUsername(), username)).findFirst(); | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
return cfg.isEnabled(); | ||
} | ||
|
||
@Override | ||
public boolean isLocal() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public int getPriority() { | ||
return Integer.MAX_VALUE; | ||
} | ||
|
||
@Override | ||
public Optional<SingleLookupReply> find(SingleLookupRequest request) { | ||
logger.info("Performing lookup {} of type {}", request.getThreePid(), request.getType()); | ||
ThreePid req = new ThreePid(request.getType(), request.getThreePid()); | ||
for (MemoryIdentityConfig id : cfg.getIdentities()) { | ||
for (MemoryThreePid threepid : id.getThreepids()) { | ||
if (req.equals(new ThreePid(threepid.getMedium(), threepid.getAddress()))) { | ||
return Optional.of(new SingleLookupReply(request, new MatrixID(id.getUsername(), mxCfg.getDomain()))); | ||
} | ||
} | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public List<ThreePidMapping> populate(List<ThreePidMapping> mappings) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
@Override | ||
public BackendAuthResult authenticate(_MatrixID mxid, String password) { | ||
return findByUsername(mxid.getLocalPart()).map(id -> { | ||
if (!StringUtils.equals(id.getUsername(), mxid.getLocalPart())) { | ||
return BackendAuthResult.failure(); | ||
} else { | ||
return BackendAuthResult.success(mxid.getId(), UserIdType.MatrixID, ""); | ||
} | ||
}).orElseGet(BackendAuthResult::failure); | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/io/kamax/mxisd/config/memory/MemoryIdentityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* mxisd - Matrix Identity Server Daemon | ||
* Copyright (C) 2018 Maxime Dor | ||
* | ||
* https://www.kamax.io/ | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package io.kamax.mxisd.config.memory; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Component | ||
public class MemoryIdentityConfig { | ||
|
||
private String username; | ||
private String password; | ||
private List<MemoryThreePid> threepids = new ArrayList<>(); | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
|
||
public List<MemoryThreePid> getThreepids() { | ||
return threepids; | ||
} | ||
|
||
public void setThreepids(List<MemoryThreePid> threepids) { | ||
this.threepids = threepids; | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/io/kamax/mxisd/config/memory/MemoryStoreConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* mxisd - Matrix Identity Server Daemon | ||
* Copyright (C) 2018 Maxime Dor | ||
* | ||
* https://www.kamax.io/ | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package io.kamax.mxisd.config.memory; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.util.List; | ||
|
||
@Configuration | ||
@ConfigurationProperties("memory") | ||
public class MemoryStoreConfig { | ||
|
||
private boolean enabled; | ||
private List<MemoryIdentityConfig> identities; | ||
|
||
public boolean isEnabled() { | ||
return enabled; | ||
} | ||
|
||
public void setEnabled(boolean enabled) { | ||
this.enabled = enabled; | ||
} | ||
|
||
public List<MemoryIdentityConfig> getIdentities() { | ||
return identities; | ||
} | ||
|
||
public void setIdentities(List<MemoryIdentityConfig> identities) { | ||
this.identities = identities; | ||
} | ||
|
||
} |
Oops, something went wrong.