-
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.
- Loading branch information
Showing
11 changed files
with
178 additions
and
7 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
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 showEmailLoginScreen(context: Context) | ||
fun loginViaSiteAddress() | ||
} |
51 changes: 51 additions & 0 deletions
51
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,51 @@ | ||
package org.wordpress.android.ui.accounts.login | ||
|
||
import android.net.Uri | ||
import android.util.Log | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.runBlocking | ||
import org.wordpress.android.BuildConfig | ||
import org.wordpress.android.fluxc.network.rest.wpapi.WPcomLoginClient | ||
import org.wordpress.android.fluxc.store.AccountStore | ||
import org.wordpress.android.util.config.AppConfig | ||
import javax.inject.Inject | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
class WPcomLoginHelper @Inject constructor( | ||
private val loginClient: WPcomLoginClient, | ||
private val accountStore: AccountStore, | ||
private val appConfig: AppConfig | ||
) { | ||
private val context: CoroutineContext = Dispatchers.IO | ||
|
||
fun loginUri(): Uri { | ||
return loginClient.loginUri(BuildConfig.OAUTH_REDIRECT_URI) | ||
} | ||
|
||
fun tryLoginWithDataString(data: String?) { | ||
if (data == null) { | ||
return | ||
} | ||
|
||
val code = this.codeFromAuthorizationUri(data) ?: return | ||
|
||
runBlocking { | ||
val tokenResult = loginClient.exchangeAuthCodeForToken(code, BuildConfig.OAUTH_REDIRECT_URI) | ||
accountStore.updateAccessToken(tokenResult.getOrThrow()) | ||
Log.i("WPCOM_LOGIN", "Login Successful") | ||
} | ||
} | ||
|
||
fun isLoggedIn(): Boolean { | ||
return accountStore.hasAccessToken() | ||
} | ||
|
||
fun dispose() { | ||
context.cancel() | ||
} | ||
|
||
private fun codeFromAuthorizationUri(string: String): String? { | ||
return Uri.parse(string).getQueryParameter("code") | ||
} | ||
} |
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
75 changes: 75 additions & 0 deletions
75
libs/fluxc/src/main/java/org/wordpress/android/fluxc/network/rest/wpapi/WPcomLoginClient.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,75 @@ | ||
package org.wordpress.android.fluxc.network.rest.wpapi | ||
|
||
import android.net.Uri | ||
import android.util.Log | ||
import com.google.gson.Gson | ||
import kotlinx.coroutines.withContext | ||
import okhttp3.FormBody | ||
import okhttp3.OkHttpClient | ||
import okhttp3.Request | ||
import org.wordpress.android.fluxc.network.rest.wpapi.applicationpasswords.WPcomAuthorizationCodeResponse | ||
import org.wordpress.android.fluxc.network.rest.wpcom.auth.AppSecrets | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
@Singleton | ||
class WPcomLoginClient @Inject constructor( | ||
private val context: CoroutineContext, | ||
private val appSecrets: AppSecrets | ||
) { | ||
private val client = OkHttpClient() | ||
|
||
fun loginUri(redirectUri: String): Uri { | ||
return Uri.Builder().scheme("https") | ||
.authority("public-api.wordpress.com") | ||
.path("/oauth2/authorize") | ||
.appendQueryParameter("client_id", appSecrets.appId) | ||
.appendQueryParameter("redirect_uri", redirectUri) | ||
.appendQueryParameter("response_type", "code") | ||
.appendQueryParameter("scope", "global") | ||
.build() | ||
} | ||
|
||
suspend fun exchangeAuthCodeForToken(code: String, redirectUri: String): Result<String> { | ||
val tokenUrl = Uri.Builder() | ||
.scheme("https") | ||
.authority("public-api.wordpress.com") | ||
.path("oauth2/token") | ||
.build() | ||
.toString() | ||
|
||
val formBody = FormBody.Builder() | ||
|
||
mutableMapOf( | ||
"client_id" to appSecrets.appId, | ||
"redirect_uri" to redirectUri, | ||
"client_secret" to appSecrets.appSecret, | ||
"code" to code, | ||
"grant_type" to "authorization_code", | ||
).forEach { (t, u) -> formBody.add(t, u) } | ||
|
||
val request = Request.Builder() | ||
.url(tokenUrl) | ||
.post(formBody.build()) | ||
.build() | ||
|
||
return withContext(context) { | ||
val response = client.newCall(request).execute() | ||
|
||
if (!response.isSuccessful) { | ||
response.body?.let { Log.e("WPCOM_LOGIN", it.string()) } | ||
Result.failure(WPcomLoginError.AccessDenied) | ||
} else { | ||
val json = response.body?.string() ?: return@withContext Result.failure(WPcomLoginError.InvalidResponse) | ||
val gson = Gson().fromJson(json, WPcomAuthorizationCodeResponse::class.java) | ||
Result.success(gson.access_token) | ||
} | ||
} | ||
} | ||
} | ||
|
||
sealed class WPcomLoginError(val code: Int): Throwable() { | ||
data object AccessDenied: WPcomLoginError(1) | ||
data object InvalidResponse: WPcomLoginError(2) | ||
} |
9 changes: 9 additions & 0 deletions
9
...s/android/fluxc/network/rest/wpapi/applicationpasswords/WPcomAuthorizationCodeResponse.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,9 @@ | ||
package org.wordpress.android.fluxc.network.rest.wpapi.applicationpasswords | ||
|
||
data class WPcomAuthorizationCodeResponse( | ||
val access_token: String, | ||
val token_type: String, | ||
val blog_id: String, | ||
val blog_url: String, | ||
val scope: String | ||
) |
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