2017-05-05 16:36:04 +02:00
|
|
|
/* Copyright 2017 Thomas Schneider
|
|
|
|
*
|
|
|
|
* This file is a part of Mastodon Etalab for mastodon.etalab.gouv.fr
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Mastodon Etalab 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 General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Thomas Schneider; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
package fr.gouv.etalab.mastodon.activities;
|
|
|
|
|
2017-05-28 14:10:19 +02:00
|
|
|
import android.app.Activity;
|
2017-05-05 16:36:04 +02:00
|
|
|
import android.content.Context;
|
2017-05-26 17:20:36 +02:00
|
|
|
import android.content.Intent;
|
2017-05-05 16:36:04 +02:00
|
|
|
import android.content.SharedPreferences;
|
2017-05-26 17:20:36 +02:00
|
|
|
import android.graphics.Paint;
|
2017-05-25 13:33:33 +02:00
|
|
|
import android.os.AsyncTask;
|
2017-05-05 16:36:04 +02:00
|
|
|
import android.os.Bundle;
|
2017-05-28 14:10:19 +02:00
|
|
|
import android.support.design.widget.NavigationView;
|
2017-05-05 16:36:04 +02:00
|
|
|
import android.support.v7.app.AppCompatActivity;
|
2017-05-28 14:10:19 +02:00
|
|
|
import android.util.Log;
|
2017-05-05 16:36:04 +02:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.Button;
|
2017-05-25 13:33:33 +02:00
|
|
|
import android.widget.EditText;
|
2017-05-28 14:10:19 +02:00
|
|
|
import android.widget.LinearLayout;
|
2017-05-26 17:20:36 +02:00
|
|
|
import android.widget.TextView;
|
2017-05-05 16:36:04 +02:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
2017-05-25 13:33:33 +02:00
|
|
|
import com.loopj.android.http.AsyncHttpClient;
|
2017-05-05 16:36:04 +02:00
|
|
|
import com.loopj.android.http.AsyncHttpResponseHandler;
|
2017-05-21 16:57:59 +02:00
|
|
|
import com.loopj.android.http.RequestParams;
|
2017-05-05 16:36:04 +02:00
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2017-05-25 13:33:33 +02:00
|
|
|
import java.security.KeyManagementException;
|
|
|
|
import java.security.KeyStoreException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.security.UnrecoverableKeyException;
|
2017-05-05 16:36:04 +02:00
|
|
|
|
|
|
|
import cz.msebera.android.httpclient.Header;
|
2017-05-25 13:33:33 +02:00
|
|
|
import fr.gouv.etalab.mastodon.asynctasks.UpdateAccountInfoAsyncTask;
|
|
|
|
import fr.gouv.etalab.mastodon.client.MastalabSSLSocketFactory;
|
2017-05-05 16:36:04 +02:00
|
|
|
import fr.gouv.etalab.mastodon.client.OauthClient;
|
|
|
|
import fr.gouv.etalab.mastodon.helper.Helper;
|
|
|
|
import mastodon.etalab.gouv.fr.mastodon.R;
|
|
|
|
|
2017-05-25 13:33:33 +02:00
|
|
|
import static fr.gouv.etalab.mastodon.helper.Helper.USER_AGENT;
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Thomas on 23/04/2017.
|
|
|
|
* Login activity class which handles the connection
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class LoginActivity extends AppCompatActivity {
|
|
|
|
|
2017-05-26 17:20:36 +02:00
|
|
|
private String client_id;
|
|
|
|
private String client_secret;
|
|
|
|
private TextView login_two_step;
|
|
|
|
private static boolean client_id_for_webview = false;
|
2017-05-28 14:10:19 +02:00
|
|
|
private String instance;
|
|
|
|
private boolean addAccount = false;
|
|
|
|
private EditText login_instance;
|
2017-05-05 16:36:04 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_login);
|
|
|
|
|
2017-05-28 14:10:19 +02:00
|
|
|
final Button connectionButton = (Button) findViewById(R.id.login_button);
|
|
|
|
login_instance = (EditText) findViewById(R.id.login_instance);
|
2017-05-20 19:40:46 +02:00
|
|
|
connectionButton.setEnabled(false);
|
2017-05-26 17:20:36 +02:00
|
|
|
login_two_step = (TextView) findViewById(R.id.login_two_step);
|
|
|
|
login_two_step.setVisibility(View.GONE);
|
|
|
|
login_two_step.setPaintFlags(login_two_step.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
|
|
|
|
login_two_step.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
client_id_for_webview = true;
|
|
|
|
retrievesClientId();
|
|
|
|
}
|
|
|
|
});
|
2017-05-28 14:10:19 +02:00
|
|
|
|
|
|
|
Bundle b = getIntent().getExtras();
|
|
|
|
if(b != null)
|
|
|
|
addAccount = b.getBoolean("addAccount", false);
|
|
|
|
|
|
|
|
if( addAccount )
|
|
|
|
login_instance.setVisibility(View.VISIBLE);
|
|
|
|
|
|
|
|
if( addAccount) {
|
|
|
|
login_instance.setOnFocusChangeListener(new View.OnFocusChangeListener() {
|
|
|
|
@Override
|
|
|
|
public void onFocusChange(View v, boolean hasFocus) {
|
|
|
|
connectionButton.setEnabled(false);
|
|
|
|
login_two_step.setVisibility(View.INVISIBLE);
|
|
|
|
if (!hasFocus) {
|
|
|
|
retrievesClientId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-05-20 19:40:46 +02:00
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
|
2017-05-20 19:40:46 +02:00
|
|
|
@Override
|
|
|
|
protected void onResume(){
|
|
|
|
super.onResume();
|
|
|
|
Button connectionButton = (Button) findViewById(R.id.login_button);
|
2017-05-28 14:10:19 +02:00
|
|
|
if( !addAccount ) {
|
|
|
|
if (client_id_for_webview || !connectionButton.isEnabled()) {
|
|
|
|
connectionButton.setEnabled(false);
|
|
|
|
client_id_for_webview = false;
|
|
|
|
retrievesClientId();
|
|
|
|
}
|
|
|
|
}else {
|
|
|
|
if (login_instance.getText() != null && login_instance.getText().toString().length() > 0 && client_id_for_webview) {
|
|
|
|
connectionButton.setEnabled(false);
|
|
|
|
client_id_for_webview = false;
|
|
|
|
retrievesClientId();
|
|
|
|
}
|
2017-05-26 17:20:36 +02:00
|
|
|
}
|
2017-05-20 19:40:46 +02:00
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
|
2017-05-20 19:40:46 +02:00
|
|
|
private void retrievesClientId(){
|
|
|
|
final Button connectionButton = (Button) findViewById(R.id.login_button);
|
2017-05-28 14:10:19 +02:00
|
|
|
if( login_instance.getText() != null && login_instance.getText().length() > 0 )
|
|
|
|
instance = login_instance.getText().toString().trim();
|
|
|
|
else
|
|
|
|
instance = Helper.INSTANCE;
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
String action = "/api/v1/apps";
|
2017-05-24 19:35:07 +02:00
|
|
|
RequestParams parameters = new RequestParams();
|
2017-05-25 13:33:33 +02:00
|
|
|
parameters.add(Helper.CLIENT_NAME, Helper.OAUTH_REDIRECT_HOST);
|
2017-05-26 17:20:36 +02:00
|
|
|
parameters.add(Helper.REDIRECT_URIS, client_id_for_webview?Helper.REDIRECT_CONTENT_WEB:Helper.REDIRECT_CONTENT);
|
2017-05-25 13:33:33 +02:00
|
|
|
parameters.add(Helper.SCOPES, Helper.OAUTH_SCOPES);
|
2017-05-28 14:10:19 +02:00
|
|
|
parameters.add(Helper.WEBSITE,"https://" + Helper.getLiveInstance(getApplicationContext()));
|
|
|
|
new OauthClient(instance).post(action, parameters, new AsyncHttpResponseHandler() {
|
2017-05-05 16:36:04 +02:00
|
|
|
@Override
|
|
|
|
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
|
|
|
|
String response = new String(responseBody);
|
|
|
|
JSONObject resobj;
|
|
|
|
try {
|
|
|
|
resobj = new JSONObject(response);
|
2017-05-26 17:20:36 +02:00
|
|
|
client_id = resobj.get(Helper.CLIENT_ID).toString();
|
|
|
|
client_secret = resobj.get(Helper.CLIENT_SECRET).toString();
|
2017-05-05 16:36:04 +02:00
|
|
|
|
|
|
|
String id = resobj.get(Helper.ID).toString();
|
|
|
|
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
|
|
|
editor.putString(Helper.CLIENT_ID, client_id);
|
|
|
|
editor.putString(Helper.CLIENT_SECRET, client_secret);
|
|
|
|
editor.putString(Helper.ID, id);
|
|
|
|
editor.apply();
|
|
|
|
connectionButton.setEnabled(true);
|
2017-05-26 17:20:36 +02:00
|
|
|
login_two_step.setVisibility(View.VISIBLE);
|
|
|
|
if( client_id_for_webview){
|
|
|
|
Intent i = new Intent(LoginActivity.this, WebviewActivity.class);
|
2017-05-28 14:10:19 +02:00
|
|
|
i.putExtra("instance", instance);
|
2017-05-26 17:20:36 +02:00
|
|
|
startActivity(i);
|
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
|
|
|
|
error.printStackTrace();
|
|
|
|
Toast.makeText(LoginActivity.this,R.string.client_error, Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
connectionButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2017-05-25 13:33:33 +02:00
|
|
|
connectionButton.setEnabled(false);
|
2017-05-26 17:20:36 +02:00
|
|
|
if( client_id_for_webview ){
|
|
|
|
client_id_for_webview = false;
|
|
|
|
retrievesClientId();
|
|
|
|
return;
|
|
|
|
}
|
2017-05-25 13:33:33 +02:00
|
|
|
AsyncHttpClient client = new AsyncHttpClient();
|
|
|
|
RequestParams requestParams = new RequestParams();
|
|
|
|
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
requestParams.add(Helper.CLIENT_ID, sharedpreferences.getString(Helper.CLIENT_ID, null));
|
|
|
|
requestParams.add(Helper.CLIENT_SECRET, sharedpreferences.getString(Helper.CLIENT_SECRET, null));
|
|
|
|
requestParams.add("grant_type", "password");
|
|
|
|
EditText login_uid = (EditText) findViewById(R.id.login_uid);
|
|
|
|
EditText login_passwd = (EditText) findViewById(R.id.login_passwd);
|
|
|
|
requestParams.add("username",login_uid.getText().toString().trim());
|
|
|
|
requestParams.add("password",login_passwd.getText().toString().trim());
|
|
|
|
requestParams.add("scope"," read write follow");
|
|
|
|
client.setUserAgent(USER_AGENT);
|
|
|
|
try {
|
|
|
|
client.setSSLSocketFactory(new MastalabSSLSocketFactory(MastalabSSLSocketFactory.getKeystore()));
|
2017-05-28 14:10:19 +02:00
|
|
|
client.post("https://" + instance+ "/oauth/token", requestParams, new AsyncHttpResponseHandler() {
|
2017-05-25 13:33:33 +02:00
|
|
|
@Override
|
|
|
|
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
|
|
|
|
String response = new String(responseBody);
|
|
|
|
JSONObject resobj;
|
|
|
|
try {
|
|
|
|
resobj = new JSONObject(response);
|
|
|
|
String token = resobj.get("access_token").toString();
|
|
|
|
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
|
|
|
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, token);
|
|
|
|
editor.apply();
|
|
|
|
//Update the account with the token;
|
2017-05-28 14:10:19 +02:00
|
|
|
new UpdateAccountInfoAsyncTask(LoginActivity.this, token, instance).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2017-05-25 13:33:33 +02:00
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
|
|
|
|
connectionButton.setEnabled(true);
|
2017-05-28 14:10:19 +02:00
|
|
|
error.printStackTrace();
|
2017-05-25 13:33:33 +02:00
|
|
|
Toast.makeText(getApplicationContext(),R.string.toast_error_login,Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (NoSuchAlgorithmException | KeyManagementException | UnrecoverableKeyException | KeyStoreException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-28 14:10:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-05-24 19:35:07 +02:00
|
|
|
}
|