-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a webview for WP.com login (#21414)
* Use a webview for WP.com login * Use Custom Chrome Tabs * Rename showEmailLoginScreen->showWPcomLoginScreen * Use showWPcomLoginScreen to reauthenticate * Don’t compile if secrets are missing * Use the latest secrets.properties * Changed start and exit animations * Preload the login page * Fix Detekt issues * Move redirect URI into build.grade It’s not a secret * Fix merge issue --------- Co-authored-by: Nick Bradbury <[email protected]>
- Loading branch information
Showing
19 changed files
with
273 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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
Binary file not shown.
This file contains 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 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 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 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 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 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 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
4 changes: 3 additions & 1 deletion
4
WordPress/src/main/java/org/wordpress/android/ui/accounts/login/LoginPrologueListener.kt
This file contains 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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
package org.wordpress.android.ui.accounts.login | ||
|
||
import android.content.Context | ||
|
||
interface LoginPrologueListener { | ||
// Login Prologue callbacks | ||
fun showEmailLoginScreen() | ||
fun showWPcomLoginScreen(context: Context) | ||
fun loginViaSiteAddress() | ||
} |
95 changes: 95 additions & 0 deletions
95
WordPress/src/main/java/org/wordpress/android/ui/accounts/login/WPcomLoginHelper.kt
This file contains 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,95 @@ | ||
package org.wordpress.android.ui.accounts.login | ||
|
||
import android.content.ComponentName | ||
import android.content.Context | ||
import android.net.Uri | ||
import android.util.Log | ||
import androidx.browser.customtabs.CustomTabsCallback | ||
import androidx.browser.customtabs.CustomTabsClient | ||
import androidx.browser.customtabs.CustomTabsServiceConnection | ||
import androidx.browser.customtabs.CustomTabsSession | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.runBlocking | ||
import org.wordpress.android.fluxc.network.rest.wpapi.WPcomLoginClient | ||
import org.wordpress.android.fluxc.network.rest.wpcom.auth.AppSecrets | ||
import org.wordpress.android.fluxc.store.AccountStore | ||
import javax.inject.Inject | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
class WPcomLoginHelper @Inject constructor( | ||
private val loginClient: WPcomLoginClient, | ||
private val accountStore: AccountStore, | ||
appSecrets: AppSecrets | ||
) { | ||
private val context: CoroutineContext = Dispatchers.IO | ||
|
||
val wpcomLoginUri = loginClient.loginUri(appSecrets.redirectUri) | ||
private val customTabsServiceConnection = ServiceConnection(wpcomLoginUri) | ||
|
||
fun tryLoginWithDataString(data: String?) { | ||
if (data == null) { | ||
return | ||
} | ||
|
||
val code = this.codeFromAuthorizationUri(data) ?: return | ||
|
||
runBlocking { | ||
val tokenResult = loginClient.exchangeAuthCodeForToken(code) | ||
accountStore.updateAccessToken(tokenResult.getOrThrow()) | ||
Log.i("WPCOM_LOGIN", "Login Successful") | ||
} | ||
} | ||
|
||
fun isLoggedIn(): Boolean { | ||
return accountStore.hasAccessToken() | ||
} | ||
|
||
fun dispose() { | ||
context.cancel() | ||
} | ||
|
||
fun bindCustomTabsService(context: Context) { | ||
customTabsServiceConnection.bind(context) | ||
} | ||
|
||
private fun codeFromAuthorizationUri(string: String): String? { | ||
return Uri.parse(string).getQueryParameter("code") | ||
} | ||
} | ||
|
||
class ServiceConnection( | ||
var uri: Uri | ||
): CustomTabsServiceConnection() { | ||
private var client: CustomTabsClient? = null | ||
private var session: CustomTabsSession? = null | ||
|
||
override fun onCustomTabsServiceConnected(name: ComponentName, client: CustomTabsClient) { | ||
client.warmup(0) | ||
this.client = client | ||
|
||
val session = client.newSession(CustomTabsCallback()) | ||
session?.mayLaunchUrl(uri, null, null) | ||
session?.mayLaunchUrl(Uri.parse("https://wordpress.com/log-in/"), null, null) | ||
|
||
this.session = session | ||
} | ||
|
||
override fun onServiceDisconnected(name: ComponentName?) { | ||
this.client = null | ||
this.session = null | ||
} | ||
|
||
fun bind(context: Context) { | ||
// Do nothing if there is an existing service connection | ||
if (this.client != null) { | ||
return | ||
} | ||
|
||
// Get the default browser package name, this will be null if | ||
// the default browser does not provide a CustomTabsService | ||
val packageName = CustomTabsClient.getPackageName(context, null) ?: return | ||
|
||
CustomTabsClient.bindCustomTabsService(context, packageName, this) | ||
} | ||
} |
This file contains 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 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 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 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.