Skip to content

Commit

Permalink
Apply review
Browse files Browse the repository at this point in the history
  • Loading branch information
fynngodau committed Oct 28, 2024
1 parent 04afa56 commit ed745dc
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.microg.gms.common.PackageUtils
import org.microg.gms.auth.AuthRequest
import org.microg.gms.auth.AuthResponse
import java.io.IOException
import kotlin.jvm.Throws

class WorkAccountAuthenticator(val context: Context) : AbstractAccountAuthenticator(context) {

Expand Down Expand Up @@ -60,44 +61,7 @@ class WorkAccountAuthenticator(val context: Context) : AbstractAccountAuthentica
val oauthToken: String = options.getString(KEY_ACCOUNT_CREATION_TOKEN)!!

try {
val authResponse = AuthRequest().fromContext(context)
.appIsGms()
.callerIsGms()
.service("ac2dm")
.token(oauthToken).isAccessToken()
.addAccount()
.getAccountId()
.droidguardResults(null)
.response

val accountManager = AccountManager.get(context)
if (accountManager.addAccountExplicitly(
Account(authResponse.email, AuthConstants.WORK_ACCOUNT_TYPE),
authResponse.token, Bundle().apply {
// Work accounts have no SID / LSID ("BAD_COOKIE") and no first/last name.
if (authResponse.accountId.isNotBlank()) {
putString(KEY_GOOGLE_USER_ID, authResponse.accountId)
}
putString(AuthConstants.KEY_ACCOUNT_CAPABILITIES, authResponse.capabilities)
putString(AuthConstants.KEY_ACCOUNT_SERVICES, authResponse.services)
if (authResponse.services != "android") {
Log.i(TAG, "unexpected 'services' value ${authResponse.services} (usually 'android')")
}
}
)) {

// Notify vending package
context.sendBroadcast(
Intent(WORK_ACCOUNT_CHANGED_BOARDCAST).setPackage("com.android.vending")
)

// Report successful creation to caller
response.onResult(Bundle().apply {
putString(AccountManager.KEY_ACCOUNT_NAME, authResponse.email)
putString(AccountManager.KEY_ACCOUNT_TYPE, AuthConstants.WORK_ACCOUNT_TYPE)
})
}

tryAddAccount(oauthToken, response)
} catch (exception: Exception) {
response.onResult(Bundle().apply {
putInt(
Expand All @@ -116,6 +80,54 @@ class WorkAccountAuthenticator(val context: Context) : AbstractAccountAuthentica
return null
}

@Throws(Exception::class)
private fun tryAddAccount(
oauthToken: String,
response: AccountAuthenticatorResponse
) {
val authResponse = AuthRequest().fromContext(context)
.appIsGms()
.callerIsGms()
.service("ac2dm")
.token(oauthToken).isAccessToken()
.addAccount()
.getAccountId()
.droidguardResults(null)
.response

val accountManager = AccountManager.get(context)
if (accountManager.addAccountExplicitly(
Account(authResponse.email, AuthConstants.WORK_ACCOUNT_TYPE),
authResponse.token, Bundle().apply {
// Work accounts have no SID / LSID ("BAD_COOKIE") and no first/last name.
if (authResponse.accountId.isNotBlank()) {
putString(KEY_GOOGLE_USER_ID, authResponse.accountId)
}
putString(AuthConstants.KEY_ACCOUNT_CAPABILITIES, authResponse.capabilities)
putString(AuthConstants.KEY_ACCOUNT_SERVICES, authResponse.services)
if (authResponse.services != "android") {
Log.i(
TAG,
"unexpected 'services' value ${authResponse.services} (usually 'android')"
)
}
}
)
) {

// Notify vending package
context.sendBroadcast(
Intent(WORK_ACCOUNT_CHANGED_BOARDCAST).setPackage("com.android.vending")
)

// Report successful creation to caller
response.onResult(Bundle().apply {
putString(AccountManager.KEY_ACCOUNT_NAME, authResponse.email)
putString(AccountManager.KEY_ACCOUNT_TYPE, AuthConstants.WORK_ACCOUNT_TYPE)
})
}
}

override fun confirmCredentials(
response: AccountAuthenticatorResponse?,
account: Account?,
Expand Down
2 changes: 1 addition & 1 deletion vending-app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
android:exported="false"/>

<!-- Work account store -->
<activity android:name="org.microg.vending.ui.VendingActivity"
<activity android:name="org.microg.vending.ui.WorkAppsActivity"
android:exported="true"
android:theme="@style/Theme.Material3.DayNight"
android:label="@string/vending_activity_name"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import android.content.pm.PackageManager
import android.os.Build
import android.util.Log
import org.microg.gms.auth.AuthConstants
import org.microg.vending.ui.VendingActivity
import org.microg.vending.ui.WorkAppsActivity

class WorkAccountChangedReceiver : BroadcastReceiver() {

Expand All @@ -23,7 +23,7 @@ class WorkAccountChangedReceiver : BroadcastReceiver() {

val componentName = ComponentName(
context,
VendingActivity::class.java
WorkAppsActivity::class.java
)
context.packageManager.setComponentEnabledSetting(
componentName,
Expand Down
Loading

0 comments on commit ed745dc

Please sign in to comment.