Skip to content

Commit 50100bc

Browse files
committed
workaccount: cleanup
1 parent 2538cf6 commit 50100bc

2 files changed

Lines changed: 37 additions & 27 deletions

File tree

play-services-auth-workaccount/core/src/main/kotlin/com/google/android/gms/auth/account/authenticator/WorkAccountAuthenticator.kt

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2026 e foundation
2+
* SPDX-FileCopyrightText: 2024 e foundation
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

@@ -38,8 +38,15 @@ class WorkAccountAuthenticator(val context: Context) : AbstractAccountAuthentica
3838
authTokenType: String?,
3939
requiredFeatures: Array<out String>?,
4040
options: Bundle
41-
): Bundle? {
42-
41+
): Bundle {
42+
/* Calls to this method are always initiated by other applications or by the user.
43+
* We refuse, because `accountCreationToken` is needed, and because only profile owner is
44+
* supposed to provision this account. Profile owner will use `WorkAccountAuthenticator`
45+
* instead, which calls the code in `addAccountInternal` directly.
46+
*
47+
* Also note: adding account with `AccountManager.addAccount` can be forbidden by device
48+
* policy.
49+
*/
4350
return Bundle().apply {
4451
putInt(AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION)
4552
putString(
@@ -49,17 +56,20 @@ class WorkAccountAuthenticator(val context: Context) : AbstractAccountAuthentica
4956
}
5057
}
5158

59+
/**
60+
* @return `null` if account creation fails, the newly created account otherwise
61+
*/
5262
fun addAccountInternal(
5363
accountCreationToken: String
5464
): Account? {
5565

5666
if (!WorkProfileSettings(context).allowCreateWorkAccount) {
57-
// TODO: communicate error to user (use `R.string.auth_work_authenticator_disabled_error)`)
67+
// TODO: communicate error to user (use `R.string.auth_work_authenticator_disabled_error`)
5868
Log.w(TAG, "creating a work account is disabled in microG settings")
5969
return null
6070
}
6171

62-
try {
72+
return try {
6373
val authResponse = AuthRequest().fromContext(context)
6474
.appIsGms()
6575
.callerIsGms()
@@ -72,36 +82,36 @@ class WorkAccountAuthenticator(val context: Context) : AbstractAccountAuthentica
7282

7383
val accountManager = AccountManager.get(context)
7484
val account = Account(authResponse.email, AuthConstants.WORK_ACCOUNT_TYPE)
75-
if (accountManager.addAccountExplicitly(
76-
account,
77-
authResponse.token, Bundle().apply {
78-
// Work accounts have no SID / LSID ("BAD_COOKIE") and no first/last name.
79-
if (authResponse.accountId.isNotBlank()) {
80-
putString(KEY_GOOGLE_USER_ID, authResponse.accountId)
81-
}
82-
putString(AuthConstants.KEY_ACCOUNT_CAPABILITIES, authResponse.capabilities)
83-
putString(AuthConstants.KEY_ACCOUNT_SERVICES, authResponse.services)
84-
if (authResponse.services != "android") {
85-
Log.i(
86-
TAG,
87-
"unexpected 'services' value ${authResponse.services} (usually 'android')"
88-
)
89-
}
85+
val accountAdded = accountManager.addAccountExplicitly(
86+
account,
87+
authResponse.token, Bundle().apply {
88+
// Work accounts have no SID / LSID ("BAD_COOKIE") and no first/last name.
89+
if (authResponse.accountId.isNotBlank()) {
90+
putString(KEY_GOOGLE_USER_ID, authResponse.accountId)
9091
}
91-
)
92-
) {
92+
putString(AuthConstants.KEY_ACCOUNT_CAPABILITIES, authResponse.capabilities)
93+
putString(AuthConstants.KEY_ACCOUNT_SERVICES, authResponse.services)
94+
if (authResponse.services != "android") {
95+
Log.i(
96+
TAG,
97+
"unexpected 'services' value ${authResponse.services} (usually 'android')"
98+
)
99+
}
100+
})
101+
102+
if (accountAdded) {
93103

94104
// Notify vending package
95105
context.sendBroadcast(
96106
Intent(WORK_ACCOUNT_CHANGED_BOARDCAST).setPackage("com.android.vending")
97107
)
98108

99109
// Report successful creation to caller
100-
return account
101-
} else return null
110+
account
111+
} else null
102112
} catch (exception: Exception) {
103-
Log.w(TAG, exception)
104-
return null
113+
Log.w(TAG, "Failed to add work account.", exception)
114+
null
105115
}
106116
}
107117

play-services-auth-workaccount/core/src/main/kotlin/org/microg/gms/auth/workaccount/WorkAccountService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2026 e foundation
2+
* SPDX-FileCopyrightText: 2024 e foundation
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

0 commit comments

Comments
 (0)