This repository was archived by the owner on Jun 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Login with password #10
Merged
maxidorius
merged 13 commits into
kamax-matrix:master
from
MrCustomizer:loginWithPassword
Oct 25, 2017
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
cd114dc
Preparations for login capability
9536e91
Add login and logout capability
ba67820
Refactor method names
c52f0b2
Restore original method name
9e6e14e
Restore original method name (reverted from commit c52f0b2a575a78b28e…
00cafb5
Add test case for failed login (wrong password)
8f17ad9
Fix some of the points mentioned in the review
c3d6afd
Refactor handling of context, client and login credentials
dc439f6
Only use local part in credentials
b258f95
Remove getters
c177d8e
Add user to context after login
76f8372
Code cosmetics
f8fb978
Fix the issues mentioned in the review
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
39 changes: 39 additions & 0 deletions
39
src/main/java/io/kamax/matrix/client/MatrixPasswordLoginCredentials.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,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; | ||
} | ||
} |
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
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
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
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 |
---|---|---|
|
@@ -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); | ||
} | ||
|
@@ -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); | ||
|
@@ -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); | ||
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? |
||
context.setUser(null); | ||
context.setDeviceId(null); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Shouldn't this be optional as well now?