Skip to content
This repository was archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
New AccountManager component, update samples apps to use it
Browse files Browse the repository at this point in the history
This introduces an account manager component, and updates two sample apps
to use it - logins and history sync.

AccountManager encapsulates state transitions exposed by Rust liblogins,
and provides a simple observable API and a few public methods to interact
with a FirefoxAccount.

Internally it's implemented as a state machine. Consumers are expected to
handle interactions with a web view that are necessary to complete an OAUTH flow.
  • Loading branch information
Grisha Kruglov authored and grigoryk committed Jan 26, 2019
1 parent b3f7420 commit f6f4a2a
Show file tree
Hide file tree
Showing 8 changed files with 1,123 additions and 117 deletions.
4 changes: 4 additions & 0 deletions components/service/firefox-accounts/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ dependencies {
// Parts of this dependency are typealiase'd or are otherwise part of this module's public API.
api Dependencies.mozilla_fxa

// Observable is part of public API of the FxaAccountManager.
api project(':support-base')

implementation Dependencies.kotlin_stdlib
implementation Dependencies.kotlin_coroutines

testImplementation project(':support-test')
testImplementation Dependencies.testing_junit
testImplementation Dependencies.testing_robolectric
testImplementation Dependencies.testing_mockito
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mozilla.components.service.fxa

import android.content.Context
import android.content.SharedPreferences

const val FXA_STATE_PREFS_KEY = "fxaAppState"
const val FXA_STATE_KEY = "fxaState"

interface AccountStorage {
@Throws(Exception::class)
fun read(): FirefoxAccountShaped?
fun write(account: FirefoxAccountShaped)
fun clear()
}

class SharedPrefAccountStorage(val context: Context) : AccountStorage {
/**
* @throws FxaException if JSON failed to parse into a [FirefoxAccount].
*/
override fun read(): FirefoxAccountShaped? {
val savedJSON = accountPreferences().getString(FXA_STATE_KEY, null)
?: return null

// May throw a generic FxaException if it fails to process saved JSON.
return FirefoxAccount.fromJSONString(savedJSON)
}

override fun write(account: FirefoxAccountShaped) {
accountPreferences()
.edit()
.putString(FXA_STATE_KEY, account.toJSONString())
.apply()
}

override fun clear() {
accountPreferences()
.edit()
.remove(FXA_STATE_KEY)
.apply()
}

private fun accountPreferences(): SharedPreferences {
return context.getSharedPreferences(FXA_STATE_PREFS_KEY, Context.MODE_PRIVATE)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@ import org.mozilla.fxaclient.internal.FxaException.Unauthorized as Unauthorized
/**
* Facilitates testing consumers of FirefoxAccount.
*/
interface FirefoxAccountShaped {
interface FirefoxAccountShaped : AutoCloseable {
fun beginOAuthFlow(scopes: Array<String>, wantsKeys: Boolean): Deferred<String>
fun beginPairingFlow(pairingUrl: String, scopes: Array<String>): Deferred<String>
fun getProfile(ignoreCache: Boolean): Deferred<Profile>
fun getProfile(): Deferred<Profile>
fun completeOAuthFlow(code: String, state: String): Deferred<Unit>
fun getAccessToken(singleScope: String): Deferred<AccessTokenInfo>
fun getTokenServerEndpointURL(): String
fun toJSONString(): String
}

/**
* FirefoxAccount represents the authentication state of a client.
*/
class FirefoxAccount internal constructor(private val inner: InternalFxAcct) : AutoCloseable, FirefoxAccountShaped {
class FirefoxAccount internal constructor(private val inner: InternalFxAcct) : FirefoxAccountShaped {

private val job = SupervisorJob()
private val scope = CoroutineScope(Dispatchers.IO) + job
Expand All @@ -50,11 +56,11 @@ class FirefoxAccount internal constructor(private val inner: InternalFxAcct) : A
* @param wantsKeys Fetch keys for end-to-end encryption of data from Mozilla-hosted services
* @return Deferred<String> that resolves to the flow URL when complete
*/
fun beginOAuthFlow(scopes: Array<String>, wantsKeys: Boolean): Deferred<String> {
override fun beginOAuthFlow(scopes: Array<String>, wantsKeys: Boolean): Deferred<String> {
return scope.async { inner.beginOAuthFlow(scopes, wantsKeys) }
}

fun beginPairingFlow(pairingUrl: String, scopes: Array<String>): Deferred<String> {
override fun beginPairingFlow(pairingUrl: String, scopes: Array<String>): Deferred<String> {
return scope.async { inner.beginPairingFlow(pairingUrl, scopes) }
}

Expand All @@ -67,7 +73,7 @@ class FirefoxAccount internal constructor(private val inner: InternalFxAcct) : A
* @throws Unauthorized We couldn't find any suitable access token to make that call.
* The caller should then start the OAuth Flow again with the "profile" scope.
*/
fun getProfile(ignoreCache: Boolean): Deferred<Profile> {
override fun getProfile(ignoreCache: Boolean): Deferred<Profile> {
return scope.async {
val internalProfile = inner.getProfile(ignoreCache)
Profile(
Expand All @@ -87,7 +93,7 @@ class FirefoxAccount internal constructor(private val inner: InternalFxAcct) : A
* @throws Unauthorized We couldn't find any suitable access token to make that call.
* The caller should then start the OAuth Flow again with the "profile" scope.
*/
fun getProfile(): Deferred<Profile> = getProfile(false)
override fun getProfile(): Deferred<Profile> = getProfile(false)

/**
* Fetches the token server endpoint, for authentication using the SAML bearer flow.
Expand All @@ -109,7 +115,7 @@ class FirefoxAccount internal constructor(private val inner: InternalFxAcct) : A
*
* Modifies the FirefoxAccount state.
*/
fun completeOAuthFlow(code: String, state: String): Deferred<Unit> {
override fun completeOAuthFlow(code: String, state: String): Deferred<Unit> {
return scope.async { inner.completeOAuthFlow(code, state) }
}

Expand All @@ -135,7 +141,7 @@ class FirefoxAccount internal constructor(private val inner: InternalFxAcct) : A
*
* @return String containing the authentication details in JSON format
*/
fun toJSONString(): String = inner.toJSONString()
override fun toJSONString(): String = inner.toJSONString()

companion object {
/**
Expand Down
Loading

0 comments on commit f6f4a2a

Please sign in to comment.