Fix issue #29 - Accept URL for instances

This commit is contained in:
Thomas 2020-11-06 18:52:29 +01:00
parent 54ffbe2e15
commit 253b61fb11
1 changed files with 21 additions and 7 deletions

View File

@ -32,6 +32,8 @@ import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays; import java.util.Arrays;
import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI; import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI;
@ -139,15 +141,27 @@ public class LoginActivity extends AppCompatActivity {
} }
instance = host = binding.loginInstance.getText().toString().trim().toLowerCase(); instance = host = binding.loginInstance.getText().toString().trim().toLowerCase();
} }
if( instance.startsWith("http")) {
try {
URL url = new URL(instance);
instance = url.getHost();
host = instance;
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
if (!Patterns.WEB_URL.matcher("https://"+instance).matches()) { if (!Patterns.WEB_URL.matcher("https://"+instance).matches()) {
Toasty.error(LoginActivity.this, getString(R.string.not_valide_instance)).show(); Toasty.error(LoginActivity.this, getString(R.string.not_valide_instance)).show();
binding.loginButton.setEnabled(true); binding.loginButton.setEnabled(true);
return; return;
} }
String finalInstance = instance;
String finalHost = host;
if (Arrays.asList(Helper.openid).contains(host) && !BuildConfig.full_instances) { if (Arrays.asList(Helper.openid).contains(host) && !BuildConfig.full_instances) {
new Thread(() -> { new Thread(() -> {
try { try {
Oauth oauth = new RetrofitPeertubeAPI(LoginActivity.this, instance, null).oauthClient(null, null, null, null); Oauth oauth = new RetrofitPeertubeAPI(LoginActivity.this, finalInstance, null).oauthClient(null, null, null, null);
if (oauth == null) { if (oauth == null) {
runOnUiThread(() -> { runOnUiThread(() -> {
binding.loginButton.setEnabled(true); binding.loginButton.setEnabled(true);
@ -164,7 +178,7 @@ public class LoginActivity extends AppCompatActivity {
editor.apply(); editor.apply();
Intent intent = new Intent(LoginActivity.this, WebviewConnectActivity.class); Intent intent = new Intent(LoginActivity.this, WebviewConnectActivity.class);
Bundle b = new Bundle(); Bundle b = new Bundle();
b.putString("url", "https://" + Helper.getPeertubeUrl(host) + "/plugins/auth-openid-connect/0.0.1/auth/openid-connect"); b.putString("url", "https://" + Helper.getPeertubeUrl(finalHost) + "/plugins/auth-openid-connect/0.0.1/auth/openid-connect");
intent.putExtras(b); intent.putExtras(b);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent); startActivity(intent);
@ -179,7 +193,7 @@ public class LoginActivity extends AppCompatActivity {
}).start(); }).start();
} else { } else {
new Thread(() -> { new Thread(() -> {
Oauth oauth = new RetrofitPeertubeAPI(LoginActivity.this, instance, null).oauthClient(Helper.CLIENT_NAME_VALUE, Helper.WEBSITE_VALUE, Helper.OAUTH_SCOPES_PEERTUBE, Helper.WEBSITE_VALUE); Oauth oauth = new RetrofitPeertubeAPI(LoginActivity.this, finalInstance, null).oauthClient(Helper.CLIENT_NAME_VALUE, Helper.WEBSITE_VALUE, Helper.OAUTH_SCOPES_PEERTUBE, Helper.WEBSITE_VALUE);
if (oauth == null) { if (oauth == null) {
runOnUiThread(() -> { runOnUiThread(() -> {
binding.loginButton.setEnabled(true); binding.loginButton.setEnabled(true);
@ -206,13 +220,13 @@ public class LoginActivity extends AppCompatActivity {
oauthParams.setPassword(binding.loginPasswd.getText().toString()); oauthParams.setPassword(binding.loginPasswd.getText().toString());
} }
try { try {
Token token = new RetrofitPeertubeAPI(LoginActivity.this, instance, null).manageToken(oauthParams); Token token = new RetrofitPeertubeAPI(LoginActivity.this, finalInstance, null).manageToken(oauthParams);
proceedLogin(token, host); proceedLogin(token, finalHost);
} catch (final Exception | Error e) { } catch (final Exception | Error e) {
oauthParams.setUsername(binding.loginUid.getText().toString().toLowerCase().trim()); oauthParams.setUsername(binding.loginUid.getText().toString().toLowerCase().trim());
try { try {
Token token = new RetrofitPeertubeAPI(LoginActivity.this, instance, null).manageToken(oauthParams); Token token = new RetrofitPeertubeAPI(LoginActivity.this, finalInstance, null).manageToken(oauthParams);
proceedLogin(token, host); proceedLogin(token, finalHost);
} catch (Error error) { } catch (Error error) {
Error.displayError(LoginActivity.this, error); Error.displayError(LoginActivity.this, error);
error.printStackTrace(); error.printStackTrace();