Commit 71222bb2 authored by Thomas Markiewicz's avatar Thomas Markiewicz
Browse files

Implemented password grant login and error handling on the new native login screen

parent ed48a065
Pipeline #50382 passed with stages
in 13 minutes and 49 seconds
......@@ -19,13 +19,17 @@ import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import androidx.browser.customtabs.CustomTabsIntent
import android.util.Log
import android.view.MenuItem
import android.view.View
import android.webkit.WebView
import android.widget.EditText
import androidx.appcompat.app.AlertDialog
import androidx.browser.customtabs.CustomTabsIntent
import com.google.android.material.snackbar.Snackbar
import com.keylesspalace.tusky.di.Injectable
import com.keylesspalace.tusky.entity.AccessToken
import com.keylesspalace.tusky.entity.AppCredentials
......@@ -65,12 +69,6 @@ class LoginActivity : BaseActivity(), Injectable {
setContentView(R.layout.activity_login)
/*
val isLibremSocial = getString(R.string.app_name) == "Librem Social"
if(isLibremSocial) {
domain = "social.librem.one"
}
*/
if (savedInstanceState != null) {
domain = savedInstanceState.getString(DOMAIN)!!
......@@ -78,14 +76,12 @@ class LoginActivity : BaseActivity(), Injectable {
clientSecret = savedInstanceState.getString(CLIENT_SECRET)
}
/* domainEditText.setText(domain) */
preferences = getSharedPreferences(
getString(R.string.preferences_file_key), Context.MODE_PRIVATE)
loginButton.setOnClickListener { onLoginButtonClick() }
loginWithBrowserButton.setOnClickListener { onLoginWithBrowserButtonClick() }
forgotPassphraseButton.setOnClickListener { onLoginWithBrowserButtonClick() }
forgotPassphraseButton.setOnClickListener { onForgotPassphraseButtonClick() }
if (isAdditionalLogin()) {
setSupportActionBar(toolbar)
......@@ -95,12 +91,6 @@ class LoginActivity : BaseActivity(), Injectable {
toolbar.visibility = View.GONE
}
/*
if(isLibremSocial && isFirstTime()) {
// automatically transition to login screen assuming librem one domain
onButtonClick()
}
*/
}
......@@ -130,6 +120,16 @@ class LoginActivity : BaseActivity(), Injectable {
super.onSaveInstanceState(outState)
}
private fun onForgotPassphraseButtonClick() {
val url = getString(R.string.forgot_passphrase_url)
val wv = WebView(this)
wv.loadUrl(url)
AlertDialog.Builder(this)
.setView(wv)
.setPositiveButton(android.R.string.ok, null)
.show()
}
private fun onLoginButtonClick() {
loginButton.isEnabled = false
......@@ -236,6 +236,39 @@ class LoginActivity : BaseActivity(), Injectable {
private fun authorizeAndLogin() {
Log.d(TAG, "Authorizing user...")
val callback = object : Callback<AccessToken> {
override fun onResponse(call: Call<AccessToken>, response: Response<AccessToken>) {
if (response.isSuccessful) {
onLoginSuccess(response.body()!!.accessToken)
} else {
Log.e(TAG, String.format("%s %s",
getString(R.string.error_retrieving_oauth_token),
response.message()))
setLoading(false)
val parentLayout = findViewById<View>(android.R.id.content)
Snackbar.make(parentLayout, getString(R.string.error_login_failed), Snackbar.LENGTH_LONG)
.setAction("Close", View.OnClickListener { })
.setActionTextColor(Color.WHITE)
.setTextColor(Color.WHITE)
.setBackgroundTint(Color.RED)
.show()
}
}
override fun onFailure(call: Call<AccessToken>, t: Throwable) {
setLoading(false)
domainTextInputLayout.error = getString(R.string.error_retrieving_oauth_token)
Log.e(TAG, String.format("%s %s",
getString(R.string.error_retrieving_oauth_token),
t.message))
}
}
mastodonApi.fetchOAuthTokenWithPassword(domain, clientId!!, clientSecret!!,
"password", usernameEditText.text.toString(), passphraseEditText.text.toString(), OAUTH_SCOPES).enqueue(callback)
}
private fun redirectUserToAuthorizeAndLogin(editText: EditText) {
......@@ -427,10 +460,5 @@ class LoginActivity : BaseActivity(), Injectable {
return true
}
private fun isFirstTime(): Boolean {
val firstTime = _isFirstTime
_isFirstTime = false
return firstTime;
}
}
}
......@@ -379,6 +379,18 @@ interface MastodonApi {
@Field("grant_type") grantType: String
): Call<AccessToken>
@FormUrlEncoded
@POST("oauth/token")
fun fetchOAuthTokenWithPassword(
@Header(DOMAIN_HEADER) domain: String,
@Field("client_id") clientId: String,
@Field("client_secret") clientSecret: String,
@Field("grant_type") grantType: String,
@Field("username") username: String,
@Field("password") password: String,
@Field("scope") scope: String
): Call<AccessToken>
@FormUrlEncoded
@POST("api/v1/lists")
fun createList(
......
......@@ -25,7 +25,7 @@
<ImageView
android:layout_width="192dp"
android:layout_height="192dp"
android:layout_marginBottom="10dp"
android:layout_marginBottom="16dp"
android:contentDescription="@null"
app:srcCompat="@drawable/splash_with_name" />
......@@ -62,6 +62,7 @@
android:id="@+id/usernameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusedByDefault="true"
android:inputType="textEmailAddress" />
</com.google.android.material.textfield.TextInputLayout>
......
......@@ -20,6 +20,7 @@
<string name="error_media_upload_sending">The upload failed.</string>
<string name="error_report_too_few_statuses">At least one status must be reported.</string>
<string name="error_sender_account_gone">Error sending post.</string>
<string name="error_login_failed">Login failed</string>
<string name="title_home">Home</string>
<string name="title_notifications">Notifications</string>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment