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
Merged

Conversation

MrCustomizer
Copy link
Collaborator

No description provided.

Copy link
Member

@maxidorius maxidorius left a comment

Choose a reason for hiding this comment

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

One big issue with this current code: login is mandatory and/or immutable.

The CS spec doesn't require login for some endpoints, therefore login() should exist and take a _MatrixCredentials (or similar) argument to perform login and assign internal values (token, device ID, etc.) properly.

@@ -61,6 +64,9 @@

public AMatrixHttpClient(MatrixClientContext context) {
this.context = context;
if (!context.getToken().isPresent()) {
Copy link
Member

Choose a reason for hiding this comment

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

The CS API don't require login everywhere, like fetching media or getting the list of supported protocol version. This cannot be made mandatory.

@@ -75,14 +81,27 @@ public _MatrixHomeserver getHomeserver() {

@Override
public String getAccessToken() {
return context.getToken();
return context.getToken()
.orElseThrow(() -> new IllegalStateException("This method can only be used with a valid token."));
Copy link
Member

Choose a reason for hiding this comment

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

The CS API don't require login everywhere, like fetching media or getting the list of supported protocol version. This cannot be made mandatory.

}

protected URI getClientPath(String action) {
try {
return getClientPathBuilder(action).build();
return getPathBuilder(getPath("client", "r0", action)).build();
Copy link
Member

Choose a reason for hiding this comment

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

Why change this for the login feature?

} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
}

protected URI getMediaPath(String action) {
try {
return getMediaPathBuilder(action).build();
return getPathBuilder(getPath("media", "v1", action)).build();
Copy link
Member

Choose a reason for hiding this comment

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

Why change this for the login feature?


private void login() {
HttpPost request = new HttpPost(getLoginClientPath());
_MatrixID user = context.getUser();
Copy link
Member

Choose a reason for hiding this comment

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

Login doesn't require a Matrix ID, only a localpart. This is a signed of flowed implementation.

@@ -145,7 +136,7 @@ public void invite(_MatrixID mxId) {

@Override
public List<_MatrixID> getJoinedUsers() {
URI path = getClientPath("/rooms/{roomId}/joined_members");
URI path = getClientPath("/rooms/" + roomId + "/joined_members");
Copy link
Member

Choose a reason for hiding this comment

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

Why change this for the login feature?

@@ -33,4 +35,8 @@

_MatrixID getUser();

Optional<String> getDeviceId();

void logout();
Copy link
Member

Choose a reason for hiding this comment

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

Not sure this belongs in _MatrixClientRaw... _MatrixClient might be better. I wish I added javadoc! I remember I split for a reason, but not the reason... let's discuss...

@@ -47,7 +43,7 @@ protected _MatrixID getMatrixId(String localpart) {

@Override
public void setDisplayName(String name) {
URI path = getClientPath("/profile/{userId}/displayname");
URI path = getClientPath("/profile/" + context.getUser().getId() + "/displayname");
Copy link
Member

Choose a reason for hiding this comment

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

Why change this for the login feature?


public interface _MatrixHomeserver {

String getDomain();

URIBuilder getClientEndpoint();
URI getClientEndpoint();
Copy link
Member

Choose a reason for hiding this comment

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

Why change this for the login feature?

return user;
}

public String getPassword() {
Copy link
Member

Choose a reason for hiding this comment

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

Security issue, should not be able to just get the password.

@@ -258,6 +272,16 @@ protected URI getClientPath(String action) {
}
}

protected URI getMediaPathWithAccessToken(String action) {
Copy link
Member

Choose a reason for hiding this comment

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

  • Has nothing to do with the login feature
  • No media retrieval requires a token if following the CS spec. While I would definitely add this down the road, it's out of scope here and premature I think

@@ -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());
Copy link
Member

Choose a reason for hiding this comment

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

Not related to login feature

@@ -32,4 +34,10 @@

_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?


_MatrixID getUser();
String getAccessTokenOrThrow();
Copy link
Member

Choose a reason for hiding this comment

The 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.

@@ -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");
Copy link
Member

Choose a reason for hiding this comment

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

{userId} should remain, just like {roomId} remained

MatrixPasswordLoginCredentials credentials = new MatrixPasswordLoginCredentials(user.getLocalPart(), password);
client.login(credentials);

assertEquals(deviceId, client.getDeviceId().get());
Copy link
Member

Choose a reason for hiding this comment

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

See loginWithDeviceId() comment

client.login(credentials);

assertEquals(deviceId, client.getDeviceId().get());
}
Copy link
Member

@maxidorius maxidorius Oct 24, 2017

Choose a reason for hiding this comment

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

What about validating the required fields are not blank (StringUtils.isNotBlank()) as per spec:

  • Matrix ID
  • Access token
  • Device ID
  • Homeserver hostname


stubFor(post(urlEqualTo(logoutUrl)));
client.logout();
}
Copy link
Member

Choose a reason for hiding this comment

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

See loginWithDeviceId() comment

.willReturn(aResponse().withStatus(200)
.withBody("{\"user_id\": \"" + user.getId() + "\"," + //
"\"access_token\": \"" + testToken + "\"," + //
"\"home_server\": \"" + new MatrixHomeserver(domain, baseUrl) + "\"," + //
Copy link
Member

Choose a reason for hiding this comment

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

See loginWithDeviceId() comment

MatrixPasswordLoginCredentials credentials = new MatrixPasswordLoginCredentials(user.getLocalPart(), password);
client.login(credentials);

assertEquals(deviceId, client.getDeviceId().get());
Copy link
Member

Choose a reason for hiding this comment

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

See loginWithDeviceId() comment

@maxidorius maxidorius merged commit faa050a into kamax-matrix:master Oct 25, 2017
@MrCustomizer MrCustomizer deleted the loginWithPassword branch November 27, 2017 16:31
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants