-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Handle and store application password #21859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
adalpari
merged 36 commits into
trunk
from
feat/CMM-330-Handle-and-store-application-password
May 13, 2025
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
52479ae
Adding logic to the LoginViewModel
adalpari af180ed
detekt and style
adalpari dfb4deb
Extrractinv the URKl construction into a helper
adalpari 0cab150
Adding tests
adalpari 9e44279
clean imports
adalpari 1003e3d
Removing previous iteration code
adalpari 1250aa9
Some refactoring
adalpari 8ed1d19
Fixing null annotation warning
adalpari cf59f99
Adding new check function
adalpari ae77d90
Saving credentials into database
adalpari 1a9ea1a
Removing the launch on login
adalpari 135a172
Running the discovery process inside the site fragment
adalpari d9f62e2
Showing login flow inside the site fragment
adalpari e43434d
Fixing the flow
adalpari a4d1fb2
Showing dialog
adalpari c834ad5
Capturing the callback in a different activity
adalpari e9f770f
Capturing the correct flow
adalpari 8141bff
Do not show the dialog the first time the site is opened
adalpari e976259
Detakt and style
adalpari afbb4cf
Cleaning some code
adalpari d87dd25
Injecting SharedPreferences
adalpari 2671679
Adding some tests
adalpari 0c46638
Adding tests
adalpari 76a10cc
detekt
adalpari 8b510b2
Adding tests to the ApplicationPasswordLoginHelper
adalpari 8098256
Making helper async with coroutines
adalpari 3f90a7d
Some refactor
adalpari 69d41ea
Some cleaning
adalpari 30a529b
Merge branch 'trunk' into feat/CMM-328-Show-Password-Login-webview-fo…
adalpari 83aa1bd
Adding authorization checks and tests
adalpari 60373e9
General cleaning
adalpari bfd6e10
Merge branch 'feat/CMM-328-Show-Password-Login-webview-for-background…
adalpari a33c41a
Using proper string for saving confirmation
adalpari fa0b9f4
Update WordPress/src/main/java/org/wordpress/android/ui/mysite/MySite…
adalpari dfcb1b7
Merge remote-tracking branch 'origin/trunk' into feat/CMM-330-Handle-…
adalpari 3f8a9b5
Merge branch 'trunk' into feat/CMM-330-Handle-and-store-application-p…
adalpari 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 hidden or 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
52 changes: 52 additions & 0 deletions
52
...Press/src/main/java/org/wordpress/android/ui/accounts/ApplicationPasswordLoginActivity.kt
This file contains hidden or 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,52 @@ | ||
package org.wordpress.android.ui.accounts | ||
|
||
import android.content.Intent | ||
import android.os.Bundle | ||
import androidx.lifecycle.lifecycleScope | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import kotlinx.coroutines.launch | ||
import org.wordpress.android.R | ||
import org.wordpress.android.ui.accounts.login.ApplicationPasswordLoginHelper | ||
import org.wordpress.android.ui.main.BaseAppCompatActivity | ||
import org.wordpress.android.ui.main.WPMainActivity | ||
import org.wordpress.android.util.ToastUtils | ||
import javax.inject.Inject | ||
|
||
@AndroidEntryPoint | ||
class ApplicationPasswordLoginActivity: BaseAppCompatActivity() { | ||
@Inject | ||
lateinit var applicationPasswordLoginHelper: ApplicationPasswordLoginHelper | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
tryToSaveCredentialsAndRunMain() | ||
} | ||
|
||
private fun tryToSaveCredentialsAndRunMain() { | ||
lifecycleScope.launch { | ||
val dataString = intent.dataString.orEmpty() | ||
val credentialsStored = | ||
applicationPasswordLoginHelper.storeApplicationPasswordCredentialsFrom(dataString) | ||
|
||
if (credentialsStored) { | ||
ToastUtils.showToast( | ||
this@ApplicationPasswordLoginActivity, | ||
getString( | ||
R.string.application_password_credentials_stored, | ||
applicationPasswordLoginHelper.getSiteUrlFromUrl(dataString) | ||
) | ||
) | ||
intent.setData(null) | ||
} | ||
|
||
val mainActivityIntent = | ||
Intent(this@ApplicationPasswordLoginActivity, WPMainActivity::class.java) | ||
mainActivityIntent.setFlags( | ||
(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK | ||
or Intent.FLAG_ACTIVITY_CLEAR_TASK) | ||
) | ||
startActivity(mainActivityIntent) | ||
finish() | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
81 changes: 81 additions & 0 deletions
81
...s/src/main/java/org/wordpress/android/ui/accounts/login/ApplicationPasswordLoginHelper.kt
This file contains hidden or 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,81 @@ | ||
package org.wordpress.android.ui.accounts.login | ||
|
||
import android.util.Log | ||
import androidx.core.net.toUri | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.withContext | ||
import org.wordpress.android.fluxc.persistence.SiteSqlUtils | ||
import org.wordpress.android.modules.BG_THREAD | ||
import javax.inject.Inject | ||
import javax.inject.Named | ||
|
||
class ApplicationPasswordLoginHelper @Inject constructor( | ||
@param:Named(BG_THREAD) private val bgDispatcher: CoroutineDispatcher, | ||
private val siteSqlUtils: SiteSqlUtils, | ||
private val uriLoginWrapper: UriLoginWrapper | ||
) { | ||
private var processedAppPasswordData: String? = null | ||
|
||
@Suppress("ReturnCount") | ||
suspend fun storeApplicationPasswordCredentialsFrom(url: String): Boolean { | ||
if (url.isEmpty() || url == processedAppPasswordData) { | ||
return false | ||
} | ||
|
||
return withContext(bgDispatcher) { | ||
val uriLogin = uriLoginWrapper.parseUriLogin(url) | ||
|
||
if (uriLogin.user.isNullOrEmpty() || uriLogin.password.isNullOrEmpty() ) { | ||
false | ||
} else { | ||
val site = siteSqlUtils.getSites().firstOrNull { it.url == uriLogin.siteUrl } | ||
if (site != null) { | ||
site.apiRestUsername = uriLogin.user | ||
site.apiRestPassword = uriLogin.password | ||
siteSqlUtils.insertOrUpdateSite(site) | ||
Log.d("WP_RS", "Saved application password credentials for: ${uriLogin.siteUrl}") | ||
processedAppPasswordData = url | ||
true | ||
} else { | ||
Log.e("WP_RS", "Cannot save application password credentials for: ${uriLogin.siteUrl}") | ||
false | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun getSiteUrlFromUrl(url: String): String { | ||
return uriLoginWrapper.parseUriLogin(url).siteUrl.orEmpty() | ||
} | ||
|
||
fun appendParamsToRestAuthorizationUrl(authorizationUrl: String?): String { | ||
return if (authorizationUrl.isNullOrEmpty()) { | ||
authorizationUrl.orEmpty() | ||
} else { | ||
authorizationUrl.toUri().buildUpon().apply { | ||
appendQueryParameter("app_name", "android-jetpack-client") | ||
appendQueryParameter("success_url", "jetpack://app-pass-authorize") | ||
}.build().toString() | ||
} | ||
} | ||
|
||
/** | ||
* This class is created to wrap the Uri calls and let us unit test the login helper | ||
*/ | ||
class UriLoginWrapper @Inject constructor() { | ||
fun parseUriLogin(url: String): UriLogin { | ||
val uri = url.toUri() | ||
return UriLogin( | ||
uri.getQueryParameter("site_url"), | ||
uri.getQueryParameter("user_login"), | ||
uri.getQueryParameter("password") | ||
) | ||
} | ||
} | ||
|
||
data class UriLogin( | ||
val siteUrl: String?, | ||
val user: String?, | ||
val password: String? | ||
) | ||
} |
This file contains hidden or 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 hidden or 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
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.
Check warning
Code scanning / Android Lint
Application has custom scheme intent filters with missing autoVerify attributes Warning