Skip to content

Commit

Permalink
Fix bug with multiple Shaarli accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
dimtion committed Aug 25, 2020
1 parent 38ca283 commit e0f7c68
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 65 deletions.
2 changes: 1 addition & 1 deletion Shaarlier.iml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id="Shaarlier" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<module external.linked.project.id="Shaarlier" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="java-gradle" name="Java-Gradle">
<configuration>
Expand Down
6 changes: 3 additions & 3 deletions app/app.iml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id=":app" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<module external.linked.project.id=":app" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="Shaarlier" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="android-gradle" name="Android-Gradle">
<configuration>
Expand All @@ -19,8 +19,8 @@
<option name="ALLOW_USER_CONFIGURATION" value="false" />
<option name="MANIFEST_FILE_RELATIVE_PATH" value="/src/main/AndroidManifest.xml" />
<option name="RES_FOLDER_RELATIVE_PATH" value="/src/main/res" />
<option name="RES_FOLDERS_RELATIVE_PATH" value="file://$MODULE_DIR$/src/main/res" />
<option name="TEST_RES_FOLDERS_RELATIVE_PATH" value="" />
<option name="RES_FOLDERS_RELATIVE_PATH" value="file://$MODULE_DIR$/src/main/res;file://$MODULE_DIR$/src/debug/res;file://$MODULE_DIR$/build/generated/res/rs/debug" />
<option name="TEST_RES_FOLDERS_RELATIVE_PATH" value="file://$MODULE_DIR$/src/androidTest/res;file://$MODULE_DIR$/src/test/res;file://$MODULE_DIR$/src/androidTestDebug/res;file://$MODULE_DIR$/src/testDebug/res;file://$MODULE_DIR$/build/generated/res/rs/androidTest/debug" />
<option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets" />
</configuration>
</facet>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public void pushLink(Link link) throws IOException {
* @return
*/
private Connection newConnection(String url, Connection.Method method) {
Log.i("RestAPI", "Creating new connection " + url + " : " + method);
return Jsoup.connect(url)
.header("Authorization", "Bearer " + this.getJwt())
.header("Content-Type", "application/json")
Expand Down Expand Up @@ -215,13 +216,14 @@ public List<String> retrieveTags() throws Exception {
* @return JWT encoded in base 64
*/
String getJwt() {
// TODO: we are obligated to stay with jjwt 0.9.1 because of Shaarli week keys
// TODO: we are obligated to stay with jjwt 0.9.1 because of Shaarli weak keys

Log.i("JWT", "Generating JWT for account " + this.mAccount);
// iat in the payload
Date date = new Date();
// During debugging I found that given that some servers and phones are not absolutely in sync
// It happens that the token would looked like being generated in the future
// To compensate that we remove 5 from the actual date.
// To compensate that we remove 5 seconds from the actual date.
date.setTime(date.getTime() - 5000);
// The key used to sign the token, you can find it by logging to your Shaarli instance
// and then going to "Tools"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import android.widget.Toast;

import com.dimtion.shaarlier.R;
import com.dimtion.shaarlier.helpers.AccountsSource;
import com.dimtion.shaarlier.helpers.NetworkManager;
import com.dimtion.shaarlier.helpers.NetworkUtils;
import com.dimtion.shaarlier.utils.Link;
Expand Down Expand Up @@ -50,7 +49,6 @@ public class NetworkService extends IntentService {
private Context mContext;
private Handler mToastHandler;
private Exception mError;
private ShaarliAccount mShaarliAccount;
private String loadedDescription;

public NetworkService() {
Expand All @@ -77,6 +75,7 @@ protected void onHandleIntent(Intent intent) {
Exception object = shaarliLstatus == NETWORK_ERROR ? mError : null;
sendBackMessage(intent, shaarliLstatus, object);
break;

case INTENT_POST:
Link link = (Link) intent.getSerializableExtra("link");

Expand All @@ -88,26 +87,14 @@ protected void onHandleIntent(Intent intent) {
link.setDescription(this.loadedDescription);
this.loadedDescription = null;
}
long accountId = intent.getLongExtra("chosenAccountId", -1);

try {
AccountsSource acs = new AccountsSource(this);
mShaarliAccount = (accountId != -1 ? acs.getShaarliAccountById(accountId) : acs.getDefaultAccount());
} catch (Exception e) {
e.printStackTrace();
sendNotificationShareError(link);
}
postLink(link);
stopSelf();
break;

case INTENT_PREFETCH:
Link sharedLink = (Link) intent.getSerializableExtra("link");
mShaarliAccount = sharedLink.getAccount();

Link prefetchedLink = prefetchLink(sharedLink);

sendBackMessage(intent, PREFETCH_LINK, prefetchedLink);

break;

case INTENT_RETRIEVE_TITLE_AND_DESCRIPTION:
Expand Down Expand Up @@ -152,43 +139,6 @@ private void sendBackMessage(@NonNull Intent intent, int message_id, @Nullable O
}
}

/**
* Display Toast in the main thread
* Thanks : http://stackoverflow.com/a/3955826
*/
private class DisplayToast implements Runnable{
private final String mText;

public DisplayToast(String text){
mText = text;
}

public void run(){
Toast.makeText(mContext, mText, Toast.LENGTH_SHORT).show();
}
}

/**
* Check if the given credentials are correct
* @param account The account with the credentials
* @return NO_ERROR if nothing is wrong
*/
private int checkShaarli(ShaarliAccount account){
NetworkManager manager = NetworkUtils.getNetworkManager(account);
try {
if (!manager.isCompatibleShaarli()) {
return TOKEN_ERROR;
}
if (!manager.login()) {
return LOGIN_ERROR;
}
} catch (IOException e) {
mError = e;
return NETWORK_ERROR;
}
return NO_ERROR;
}

/**
* Try to prefetch the data of the link
* Will return exactly the same link if the link does not exist
Expand All @@ -205,7 +155,7 @@ private Link prefetchLink(Link sharedLink) {
if (manager.isCompatibleShaarli() && manager.login()) {
prefetchedLink = manager.prefetchLinkData(sharedLink);
} else {
mError = new Exception("Could not connect to the shaarli. Possibles causes : unhandled shaarli, bad username or password");
mError = new Exception("Could not connect to shaarli. Possibles causes: unhandled shaarli, bad username or password");
Log.e("ERROR", mError.getMessage());
}
} catch (IOException | NullPointerException e) {
Expand All @@ -215,12 +165,33 @@ private Link prefetchLink(Link sharedLink) {
return prefetchedLink;
}

/**
* Check if the given credentials are correct
*
* @param account The account with the credentials
* @return NO_ERROR if nothing is wrong
*/
private int checkShaarli(ShaarliAccount account) {
NetworkManager manager = NetworkUtils.getNetworkManager(account);
try {
if (!manager.isCompatibleShaarli()) {
return TOKEN_ERROR;
}
if (!manager.login()) {
return LOGIN_ERROR;
}
} catch (IOException e) {
mError = e;
return NETWORK_ERROR;
}
return NO_ERROR;
}

private void postLink(Link link) {
boolean posted = true; // Assume it is shared
try {
// Connect the user to the site :
NetworkManager manager = NetworkUtils.getNetworkManager(mShaarliAccount);
NetworkManager manager = NetworkUtils.getNetworkManager(link.getAccount());
if (manager.isCompatibleShaarli() && manager.login()) {
manager.pushLink(link);
} else {
Expand All @@ -241,13 +212,30 @@ private void postLink(Link link) {
}
}

/**
* Display Toast in the main thread
* Thanks: http://stackoverflow.com/a/3955826
*/
private class DisplayToast implements Runnable {
private final String mText;

public DisplayToast(String text) {
mText = text;
}

public void run() {
Toast.makeText(mContext, mText, Toast.LENGTH_SHORT).show();
}
}

/**
* Retrieve the title of a page
*
* @param url the page to get the title
* @return the title page, "" if there is an error
*/
@NonNull
private String[] getPageTitleAndDescription(String url){
private String[] getPageTitleAndDescription(String url) {
return NetworkUtils.loadTitleAndDescription(url);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ public class ShaarliAccount implements Serializable {
@NonNull
@Override
public String toString() {
if (!this.shortName.equals("")) {
if (this.shortName != null && !this.shortName.equals("")) {
return shortName;
} else if (!this.username.equals("")) {
return this.username;
} else {
return this.urlShaarli;
}
return this.urlShaarli;
}

public long getId() {
Expand Down

0 comments on commit e0f7c68

Please sign in to comment.