Skip to content
This repository was archived by the owner on Jun 21, 2019. It is now read-only.

Login with password #10

Merged
merged 13 commits into from
Oct 25, 2017
29 changes: 22 additions & 7 deletions src/main/java/io/kamax/matrix/client/AMatrixHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,17 @@ public _MatrixHomeserver getHomeserver() {
}

@Override
public String getAccessToken() {
return context.getToken();
public Optional<String> getAccessToken() {
return Optional.ofNullable(context.getToken());
}

public String getAccessTokenOrThrow() {
return getAccessToken()
.orElseThrow(() -> new IllegalStateException("This method can only be used with a valid token."));
}

@Override
public _MatrixID getUser() {
public Optional<_MatrixID> getUser() {
return context.getUser();
}

Expand Down Expand Up @@ -233,10 +238,8 @@ private void log(HttpRequestBase req) {
protected URIBuilder getPathBuilder(String module, String version, String action) {
URIBuilder builder = context.getHs().getClientEndpoint();
builder.setPath(builder.getPath() + "/_matrix/" + module + "/" + version + action);
builder.setParameter("access_token", context.getToken());
builder.setPath(builder.getPath().replace("{userId}", context.getUser().getId()));
if (context.isVirtualUser()) {
builder.setParameter("user_id", context.getUser().getId());
context.getUser().ifPresent(user -> builder.setParameter("user_id", user.getId()));
}

return builder;
Expand All @@ -250,6 +253,16 @@ protected URIBuilder getMediaPathBuilder(String action) {
return getPathBuilder("media", "v1", action);
}

protected URI getClientPathWithAccessToken(String action) {
try {
URIBuilder builder = getClientPathBuilder(action);
builder.setParameter("access_token", getAccessTokenOrThrow());
return builder.build();
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
}

protected URI getClientPath(String action) {
try {
return getClientPathBuilder(action).build();
Expand All @@ -260,7 +273,9 @@ protected URI getClientPath(String action) {

protected URI getMediaPath(String action) {
try {
return getMediaPathBuilder(action).build();
URIBuilder builder = getMediaPathBuilder(action);
builder.setParameter("access_token", getAccessTokenOrThrow());
return builder.build();
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
Expand Down
43 changes: 39 additions & 4 deletions src/main/java/io/kamax/matrix/client/MatrixClientContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,73 @@
import io.kamax.matrix._MatrixID;
import io.kamax.matrix.hs._MatrixHomeserver;

import java.util.Optional;

public class MatrixClientContext {

private _MatrixHomeserver hs;
private _MatrixID user;
private String token;
private boolean isVirtualUser;
private String deviceId;

public MatrixClientContext(_MatrixHomeserver hs, _MatrixID user, String token) {
this(hs, user, token, false);
}

public MatrixClientContext(_MatrixHomeserver hs, _MatrixID user, String token, boolean isVirtualUser) {
this.hs = hs;
this.user = user;
this(hs, user, isVirtualUser);
this.token = token;
}

public MatrixClientContext(_MatrixHomeserver hs) {
this(hs, false);
}

public MatrixClientContext(_MatrixHomeserver hs, String deviceId) {
this(hs, false);
this.deviceId = deviceId;
}

private MatrixClientContext(_MatrixHomeserver hs, _MatrixID user, boolean isVirtualUser) {
this(hs, isVirtualUser);
this.user = user;
}

private MatrixClientContext(_MatrixHomeserver hs, boolean isVirtualUser) {
this.hs = hs;
this.isVirtualUser = isVirtualUser;
}

public _MatrixHomeserver getHs() {
return hs;
}

public _MatrixID getUser() {
return user;
public Optional<_MatrixID> getUser() {
return Optional.ofNullable(user);
}

public void setUser(_MatrixID user) {
this.user = user;
}

public String getToken() {
return token;
}

public void setToken(String token) {
this.token = token;
}

public boolean isVirtualUser() {
return isVirtualUser;
}

public Optional<String> getDeviceId() {
return Optional.ofNullable(deviceId);
}

public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
}
14 changes: 7 additions & 7 deletions src/main/java/io/kamax/matrix/client/MatrixHttpRoom.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
URIBuilder builder = super.getClientPathBuilder(action);
builder.setPath(builder.getPath().replace("{roomId}", roomId));
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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<>();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/kamax/matrix/client/MatrixHttpUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public _MatrixID getId() {

@Override
public Optional<String> getName() {
URI path = getClientPath("/profile/" + mxId.getId() + "/displayname");
URI path = getClientPathWithAccessToken("/profile/" + mxId.getId() + "/displayname");

MatrixHttpRequest request = new MatrixHttpRequest(new HttpGet(path));
request.addIgnoredErrorCode(404);
Expand All @@ -61,7 +61,7 @@ public Optional<String> getName() {

@Override
public Optional<_MatrixContent> getAvatar() {
URI path = getClientPath("/profile/" + mxId.getId() + "/avatar_url");
URI path = getClientPathWithAccessToken("/profile/" + mxId.getId() + "/avatar_url");

MatrixHttpRequest request = new MatrixHttpRequest(new HttpGet(path));
request.addIgnoredErrorCode(404);
Expand Down
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;
}
}
8 changes: 8 additions & 0 deletions src/main/java/io/kamax/matrix/client/_MatrixClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -32,4 +34,10 @@ public interface _MatrixClient extends _MatrixClientRaw {

_MatrixUser getUser(_MatrixID mxId);
Copy link
Member

Choose a reason for hiding this comment

The 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();

}
7 changes: 4 additions & 3 deletions src/main/java/io/kamax/matrix/client/_MatrixClientRaw.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
import io.kamax.matrix._MatrixID;
import io.kamax.matrix.hs._MatrixHomeserver;

import java.util.Optional;

public interface _MatrixClientRaw {

MatrixClientContext getContext();

_MatrixHomeserver getHomeserver();

String getAccessToken();

_MatrixID getUser();
Optional<String> getAccessToken();

Optional<_MatrixID> getUser();
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ public MatrixApplicationServiceClient(_MatrixHomeserver hs, String token, String

private MatrixHttpClient createClient(String localpart) {
return new MatrixHttpClient(
new MatrixClientContext(getHomeserver(), getMatrixId(localpart), getAccessToken(), true));
new MatrixClientContext(getHomeserver(), getMatrixId(localpart), getAccessTokenOrThrow(), true));
}

@Override
public _MatrixClient createUser(String localpart) {
log.debug("Creating new user {}", localpart);
URI path = getClientPath("/register");
URI path = getClientPathWithAccessToken("/register");
HttpPost req = new HttpPost(path);
req.setEntity(getJsonEntity(new VirtualUserRegistrationBody(localpart)));
execute(req);
Expand Down
53 changes: 48 additions & 5 deletions src/main/java/io/kamax/matrix/client/regular/MatrixHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@
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 org.apache.http.client.utils.URIBuilder;

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);
}
Expand All @@ -45,9 +46,16 @@ protected _MatrixID getMatrixId(String localpart) {
return new MatrixID(localpart, getHomeserver().getDomain());
}

@Override
protected URIBuilder getClientPathBuilder(String action) {
URIBuilder builder = super.getClientPathBuilder(action);
context.getUser().ifPresent(user -> builder.setPath(builder.getPath().replace("{userId}", user.getId())));
return builder;
}

@Override
public void setDisplayName(String name) {
URI path = getClientPath("/profile/{userId}/displayname");
URI path = getClientPathWithAccessToken("/profile/{userId}/displayname");
HttpPut req = new HttpPut(path);
req.setEntity(getJsonEntity(new UserDisplaynameSetBody(name)));
execute(req);
Expand All @@ -63,4 +71,39 @@ 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(response.getUserId()));

// FIXME spec returns hostname which we might not be the same as what has been used in baseUrl to login. Must
// update internals accordingly
}

@Override
public void logout() {
URI path = getClientPathWithAccessToken("/logout");
HttpPost req = new HttpPost(path);
execute(req);
context.setToken(null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about Device ID and User ID?

context.setUser(null);
context.setDeviceId(null);
}

}
Loading