-
Notifications
You must be signed in to change notification settings - Fork 14
Login with password #10
Changes from 12 commits
cd114dc
9536e91
ba67820
c52f0b2
9e6e14e
00cafb5
8f17ad9
c3d6afd
dc439f6
b258f95
c177d8e
76f8372
f8fb978
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,7 +59,7 @@ private synchronized void load() { | |
if (!StringUtils.equalsIgnoreCase("mxc", address.getScheme())) { | ||
log.debug("{} is not a supported protocol for avatars, ignoring", address.getScheme()); | ||
} else { | ||
URI path = getMediaPath("/download/" + address.getHost() + address.getPath()); | ||
URI path = getMediaPathWithAccessToken("/download/" + address.getHost() + address.getPath()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not related to login feature |
||
|
||
MatrixHttpRequest request = new MatrixHttpRequest(new HttpGet(path)); | ||
result = executeContentRequest(request); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,10 +51,10 @@ public class MatrixHttpRoom extends AMatrixHttpClient implements _MatrixRoom { | |
|
||
public MatrixHttpRoom(MatrixClientContext context, String roomId) { | ||
super(context); | ||
|
||
this.roomId = roomId; | ||
} | ||
|
||
@Override | ||
protected URIBuilder getClientPathBuilder(String action) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why was it removed? |
||
URIBuilder builder = super.getClientPathBuilder(action); | ||
builder.setPath(builder.getPath().replace("{roomId}", roomId)); | ||
|
@@ -69,7 +69,7 @@ public String getAddress() { | |
|
||
@Override | ||
public Optional<String> getName() { | ||
URI path = getClientPath("/rooms/{roomId}/state/m.room.name"); | ||
URI path = getClientPathWithAccessToken("/rooms/{roomId}/state/m.room.name"); | ||
|
||
MatrixHttpRequest request = new MatrixHttpRequest(new HttpGet(path)); | ||
request.addIgnoredErrorCode(404); | ||
|
@@ -79,7 +79,7 @@ public Optional<String> getName() { | |
|
||
@Override | ||
public Optional<String> getTopic() { | ||
URI path = getClientPath("/rooms/{roomId}/state/m.room.topic"); | ||
URI path = getClientPathWithAccessToken("/rooms/{roomId}/state/m.room.topic"); | ||
MatrixHttpRequest matrixRequest = new MatrixHttpRequest(new HttpGet(path)); | ||
matrixRequest.addIgnoredErrorCode(404); | ||
String body = execute(matrixRequest); | ||
|
@@ -88,13 +88,13 @@ public Optional<String> getTopic() { | |
|
||
@Override | ||
public void join() { | ||
URI path = getClientPath("/rooms/{roomId}/join"); | ||
URI path = getClientPathWithAccessToken("/rooms/{roomId}/join"); | ||
execute(new HttpPost(path)); | ||
} | ||
|
||
@Override | ||
public void leave() { | ||
URI path = getClientPath("/rooms/{roomId}/leave"); | ||
URI path = getClientPathWithAccessToken("/rooms/{roomId}/leave"); | ||
MatrixHttpRequest request = new MatrixHttpRequest(new HttpPost(path)); | ||
|
||
// TODO Find a better way to handle room objects for unknown rooms | ||
|
@@ -109,7 +109,7 @@ public void leave() { | |
} | ||
|
||
private void sendMessage(RoomMessageTextPutBody content) { | ||
URI path = getClientPath("/rooms/{roomId}/send/m.room.message/" + System.currentTimeMillis()); | ||
URI path = getClientPathWithAccessToken("/rooms/{roomId}/send/m.room.message/" + System.currentTimeMillis()); | ||
HttpPut httpPut = new HttpPut(path); | ||
httpPut.setEntity(getJsonEntity(content)); | ||
execute(httpPut); | ||
|
@@ -145,7 +145,7 @@ public void invite(_MatrixID mxId) { | |
|
||
@Override | ||
public List<_MatrixID> getJoinedUsers() { | ||
URI path = getClientPath("/rooms/{roomId}/joined_members"); | ||
URI path = getClientPathWithAccessToken("/rooms/{roomId}/joined_members"); | ||
String body = execute(new HttpGet(path)); | ||
|
||
List<_MatrixID> ids = new ArrayList<>(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* matrix-java-sdk - Matrix Client SDK for Java | ||
* Copyright (C) 2017 Arne Augenstein | ||
* | ||
* https://max.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.matrix.client; | ||
|
||
public class MatrixPasswordLoginCredentials { | ||
private final String localPart; | ||
private final String password; | ||
|
||
public MatrixPasswordLoginCredentials(String localPart, String password) { | ||
this.localPart = localPart; | ||
this.password = password; | ||
} | ||
|
||
public String getLocalPart() { | ||
return localPart; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,8 @@ | |
import io.kamax.matrix._MatrixUser; | ||
import io.kamax.matrix.hs._MatrixRoom; | ||
|
||
import java.util.Optional; | ||
|
||
public interface _MatrixClient extends _MatrixClientRaw { | ||
|
||
void setDisplayName(String name); | ||
|
@@ -32,4 +34,10 @@ public interface _MatrixClient extends _MatrixClientRaw { | |
|
||
_MatrixUser getUser(_MatrixID mxId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be optional as well now? |
||
|
||
Optional<String> getDeviceId(); | ||
|
||
void login(MatrixPasswordLoginCredentials credentials); | ||
|
||
void logout(); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,14 +23,17 @@ | |
import io.kamax.matrix._MatrixID; | ||
import io.kamax.matrix.hs._MatrixHomeserver; | ||
|
||
import java.util.Optional; | ||
|
||
public interface _MatrixClientRaw { | ||
|
||
MatrixClientContext getContext(); | ||
|
||
_MatrixHomeserver getHomeserver(); | ||
|
||
String getAccessToken(); | ||
Optional<String> getAccessToken(); | ||
|
||
_MatrixID getUser(); | ||
String getAccessTokenOrThrow(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure this should be in the interface. While the implementation is useful, seems like bloat for "external" use with a specific exception forced. |
||
|
||
_MatrixID getUser(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,18 +25,18 @@ | |
import io.kamax.matrix._MatrixUser; | ||
import io.kamax.matrix.client.*; | ||
import io.kamax.matrix.hs._MatrixRoom; | ||
import io.kamax.matrix.json.LoginPostBody; | ||
import io.kamax.matrix.json.LoginResponse; | ||
import io.kamax.matrix.json.UserDisplaynameSetBody; | ||
|
||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.client.methods.HttpPut; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.net.URI; | ||
import java.util.Optional; | ||
|
||
public class MatrixHttpClient extends AMatrixHttpClient implements _MatrixClient { | ||
|
||
private Logger log = LoggerFactory.getLogger(MatrixHttpClient.class); | ||
|
||
public MatrixHttpClient(MatrixClientContext context) { | ||
super(context); | ||
} | ||
|
@@ -47,7 +47,7 @@ protected _MatrixID getMatrixId(String localpart) { | |
|
||
@Override | ||
public void setDisplayName(String name) { | ||
URI path = getClientPath("/profile/{userId}/displayname"); | ||
URI path = getClientPathWithAccessToken("/profile/" + context.getUser().getId() + "/displayname"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
HttpPut req = new HttpPut(path); | ||
req.setEntity(getJsonEntity(new UserDisplaynameSetBody(name))); | ||
execute(req); | ||
|
@@ -63,4 +63,34 @@ public _MatrixUser getUser(_MatrixID mxId) { | |
return new MatrixHttpUser(getContext(), mxId); | ||
} | ||
|
||
@Override | ||
public Optional<String> getDeviceId() { | ||
return context.getDeviceId(); | ||
} | ||
|
||
@Override | ||
public void login(MatrixPasswordLoginCredentials credentials) { | ||
HttpPost request = new HttpPost(getClientPath("/login")); | ||
if (context.getDeviceId().isPresent()) { | ||
request.setEntity(getJsonEntity(new LoginPostBody(credentials.getLocalPart(), credentials.getPassword(), | ||
context.getDeviceId().get()))); | ||
} else { | ||
request.setEntity(getJsonEntity(new LoginPostBody(credentials.getLocalPart(), credentials.getPassword()))); | ||
} | ||
|
||
String body = execute(request); | ||
LoginResponse response = gson.fromJson(body, LoginResponse.class); | ||
context.setToken(response.getAccessToken()); | ||
context.setDeviceId(response.getDeviceId()); | ||
context.setUser(new MatrixID(credentials.getLocalPart(), context.getHs().getDomain())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per spec, the Matrix ID of the user is returned in the response body, so we should not build it ourself. |
||
} | ||
|
||
@Override | ||
public void logout() { | ||
URI path = getClientPathWithAccessToken("/logout"); | ||
HttpPost req = new HttpPost(path); | ||
execute(req); | ||
context.setToken(null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about Device ID and User ID? |
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.