account_manager_util.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/account_manager_core/account_manager_util.h"
  5. #include "components/account_manager_core/account.h"
  6. #include "components/account_manager_core/account_addition_options.h"
  7. #include "components/account_manager_core/account_addition_result.h"
  8. #include "google_apis/gaia/google_service_auth_error.h"
  9. #include "third_party/abseil-cpp/absl/types/optional.h"
  10. namespace account_manager {
  11. namespace cm = crosapi::mojom;
  12. namespace {
  13. GoogleServiceAuthError::InvalidGaiaCredentialsReason
  14. FromMojoInvalidGaiaCredentialsReason(
  15. crosapi::mojom::GoogleServiceAuthError::InvalidGaiaCredentialsReason
  16. mojo_reason) {
  17. switch (mojo_reason) {
  18. case cm::GoogleServiceAuthError::InvalidGaiaCredentialsReason::kUnknown:
  19. return GoogleServiceAuthError::InvalidGaiaCredentialsReason::UNKNOWN;
  20. case cm::GoogleServiceAuthError::InvalidGaiaCredentialsReason::
  21. kCredentialsRejectedByServer:
  22. return GoogleServiceAuthError::InvalidGaiaCredentialsReason::
  23. CREDENTIALS_REJECTED_BY_SERVER;
  24. case cm::GoogleServiceAuthError::InvalidGaiaCredentialsReason::
  25. kCredentialsRejectedByClient:
  26. return GoogleServiceAuthError::InvalidGaiaCredentialsReason::
  27. CREDENTIALS_REJECTED_BY_CLIENT;
  28. case cm::GoogleServiceAuthError::InvalidGaiaCredentialsReason::
  29. kCredentialsMissing:
  30. return GoogleServiceAuthError::InvalidGaiaCredentialsReason::
  31. CREDENTIALS_MISSING;
  32. default:
  33. LOG(WARNING) << "Unknown "
  34. "crosapi::mojom::GoogleServiceAuthError::"
  35. "InvalidGaiaCredentialsReason: "
  36. << mojo_reason;
  37. return GoogleServiceAuthError::InvalidGaiaCredentialsReason::UNKNOWN;
  38. }
  39. }
  40. crosapi::mojom::GoogleServiceAuthError::InvalidGaiaCredentialsReason
  41. ToMojoInvalidGaiaCredentialsReason(
  42. GoogleServiceAuthError::InvalidGaiaCredentialsReason reason) {
  43. switch (reason) {
  44. case GoogleServiceAuthError::InvalidGaiaCredentialsReason::UNKNOWN:
  45. return cm::GoogleServiceAuthError::InvalidGaiaCredentialsReason::kUnknown;
  46. case GoogleServiceAuthError::InvalidGaiaCredentialsReason::
  47. CREDENTIALS_REJECTED_BY_SERVER:
  48. return cm::GoogleServiceAuthError::InvalidGaiaCredentialsReason::
  49. kCredentialsRejectedByServer;
  50. case GoogleServiceAuthError::InvalidGaiaCredentialsReason::
  51. CREDENTIALS_REJECTED_BY_CLIENT:
  52. return cm::GoogleServiceAuthError::InvalidGaiaCredentialsReason::
  53. kCredentialsRejectedByClient;
  54. case GoogleServiceAuthError::InvalidGaiaCredentialsReason::
  55. CREDENTIALS_MISSING:
  56. return cm::GoogleServiceAuthError::InvalidGaiaCredentialsReason::
  57. kCredentialsMissing;
  58. case GoogleServiceAuthError::InvalidGaiaCredentialsReason::NUM_REASONS:
  59. NOTREACHED();
  60. return cm::GoogleServiceAuthError::InvalidGaiaCredentialsReason::kUnknown;
  61. }
  62. }
  63. crosapi::mojom::GoogleServiceAuthError::State ToMojoGoogleServiceAuthErrorState(
  64. GoogleServiceAuthError::State state) {
  65. switch (state) {
  66. case GoogleServiceAuthError::State::NONE:
  67. return cm::GoogleServiceAuthError::State::kNone;
  68. case GoogleServiceAuthError::State::INVALID_GAIA_CREDENTIALS:
  69. return cm::GoogleServiceAuthError::State::kInvalidGaiaCredentials;
  70. case GoogleServiceAuthError::State::USER_NOT_SIGNED_UP:
  71. return cm::GoogleServiceAuthError::State::kUserNotSignedUp;
  72. case GoogleServiceAuthError::State::CONNECTION_FAILED:
  73. return cm::GoogleServiceAuthError::State::kConnectionFailed;
  74. case GoogleServiceAuthError::State::SERVICE_UNAVAILABLE:
  75. return cm::GoogleServiceAuthError::State::kServiceUnavailable;
  76. case GoogleServiceAuthError::State::REQUEST_CANCELED:
  77. return cm::GoogleServiceAuthError::State::kRequestCanceled;
  78. case GoogleServiceAuthError::State::UNEXPECTED_SERVICE_RESPONSE:
  79. return cm::GoogleServiceAuthError::State::kUnexpectedServiceResponse;
  80. case GoogleServiceAuthError::State::SERVICE_ERROR:
  81. return cm::GoogleServiceAuthError::State::kServiceError;
  82. case GoogleServiceAuthError::State::SCOPE_LIMITED_UNRECOVERABLE_ERROR:
  83. return cm::GoogleServiceAuthError::State::kScopeLimitedUnrecoverableError;
  84. case GoogleServiceAuthError::State::NUM_STATES:
  85. NOTREACHED();
  86. return cm::GoogleServiceAuthError::State::kNone;
  87. }
  88. }
  89. absl::optional<account_manager::AccountAdditionResult::Status>
  90. FromMojoAccountAdditionStatus(
  91. crosapi::mojom::AccountAdditionResult::Status mojo_status) {
  92. switch (mojo_status) {
  93. case cm::AccountAdditionResult::Status::kSuccess:
  94. return account_manager::AccountAdditionResult::Status::kSuccess;
  95. case cm::AccountAdditionResult::Status::kAlreadyInProgress:
  96. return account_manager::AccountAdditionResult::Status::kAlreadyInProgress;
  97. case cm::AccountAdditionResult::Status::kCancelledByUser:
  98. return account_manager::AccountAdditionResult::Status::kCancelledByUser;
  99. case cm::AccountAdditionResult::Status::kNetworkError:
  100. return account_manager::AccountAdditionResult::Status::kNetworkError;
  101. case cm::AccountAdditionResult::Status::kUnexpectedResponse:
  102. return account_manager::AccountAdditionResult::Status::
  103. kUnexpectedResponse;
  104. case cm::AccountAdditionResult::Status::kBlockedByPolicy:
  105. return account_manager::AccountAdditionResult::Status::kBlockedByPolicy;
  106. default:
  107. LOG(WARNING) << "Unknown crosapi::mojom::AccountAdditionResult::Status: "
  108. << mojo_status;
  109. return absl::nullopt;
  110. }
  111. }
  112. crosapi::mojom::AccountAdditionResult::Status ToMojoAccountAdditionStatus(
  113. account_manager::AccountAdditionResult::Status status) {
  114. switch (status) {
  115. case account_manager::AccountAdditionResult::Status::kSuccess:
  116. return cm::AccountAdditionResult::Status::kSuccess;
  117. case account_manager::AccountAdditionResult::Status::kAlreadyInProgress:
  118. return cm::AccountAdditionResult::Status::kAlreadyInProgress;
  119. case account_manager::AccountAdditionResult::Status::kCancelledByUser:
  120. return cm::AccountAdditionResult::Status::kCancelledByUser;
  121. case account_manager::AccountAdditionResult::Status::kNetworkError:
  122. return cm::AccountAdditionResult::Status::kNetworkError;
  123. case account_manager::AccountAdditionResult::Status::kUnexpectedResponse:
  124. return cm::AccountAdditionResult::Status::kUnexpectedResponse;
  125. case account_manager::AccountAdditionResult::Status::kBlockedByPolicy:
  126. return cm::AccountAdditionResult::Status::kBlockedByPolicy;
  127. }
  128. }
  129. } // namespace
  130. absl::optional<account_manager::Account> FromMojoAccount(
  131. const crosapi::mojom::AccountPtr& mojom_account) {
  132. const absl::optional<account_manager::AccountKey> account_key =
  133. FromMojoAccountKey(mojom_account->key);
  134. if (!account_key.has_value())
  135. return absl::nullopt;
  136. account_manager::Account account{account_key.value(),
  137. mojom_account->raw_email};
  138. return account;
  139. }
  140. crosapi::mojom::AccountPtr ToMojoAccount(
  141. const account_manager::Account& account) {
  142. crosapi::mojom::AccountPtr mojom_account = crosapi::mojom::Account::New();
  143. mojom_account->key = ToMojoAccountKey(account.key);
  144. mojom_account->raw_email = account.raw_email;
  145. return mojom_account;
  146. }
  147. absl::optional<account_manager::AccountKey> FromMojoAccountKey(
  148. const crosapi::mojom::AccountKeyPtr& mojom_account_key) {
  149. const absl::optional<account_manager::AccountType> account_type =
  150. FromMojoAccountType(mojom_account_key->account_type);
  151. if (!account_type.has_value())
  152. return absl::nullopt;
  153. if (mojom_account_key->id.empty())
  154. return absl::nullopt;
  155. return account_manager::AccountKey(mojom_account_key->id,
  156. account_type.value());
  157. }
  158. crosapi::mojom::AccountKeyPtr ToMojoAccountKey(
  159. const account_manager::AccountKey& account_key) {
  160. crosapi::mojom::AccountKeyPtr mojom_account_key =
  161. crosapi::mojom::AccountKey::New();
  162. mojom_account_key->id = account_key.id();
  163. mojom_account_key->account_type =
  164. ToMojoAccountType(account_key.account_type());
  165. return mojom_account_key;
  166. }
  167. absl::optional<account_manager::AccountType> FromMojoAccountType(
  168. const crosapi::mojom::AccountType& account_type) {
  169. switch (account_type) {
  170. case crosapi::mojom::AccountType::kGaia:
  171. static_assert(static_cast<int>(crosapi::mojom::AccountType::kGaia) ==
  172. static_cast<int>(account_manager::AccountType::kGaia),
  173. "Underlying enum values must match");
  174. return account_manager::AccountType::kGaia;
  175. case crosapi::mojom::AccountType::kActiveDirectory:
  176. static_assert(
  177. static_cast<int>(crosapi::mojom::AccountType::kActiveDirectory) ==
  178. static_cast<int>(account_manager::AccountType::kActiveDirectory),
  179. "Underlying enum values must match");
  180. return account_manager::AccountType::kActiveDirectory;
  181. default:
  182. // Don't consider this as as error to preserve forwards compatibility with
  183. // lacros.
  184. LOG(WARNING) << "Unknown account type: " << account_type;
  185. return absl::nullopt;
  186. }
  187. }
  188. crosapi::mojom::AccountType ToMojoAccountType(
  189. const account_manager::AccountType& account_type) {
  190. switch (account_type) {
  191. case account_manager::AccountType::kGaia:
  192. return crosapi::mojom::AccountType::kGaia;
  193. case account_manager::AccountType::kActiveDirectory:
  194. return crosapi::mojom::AccountType::kActiveDirectory;
  195. }
  196. }
  197. absl::optional<GoogleServiceAuthError> FromMojoGoogleServiceAuthError(
  198. const crosapi::mojom::GoogleServiceAuthErrorPtr& mojo_error) {
  199. switch (mojo_error->state) {
  200. case cm::GoogleServiceAuthError::State::kNone:
  201. return GoogleServiceAuthError::AuthErrorNone();
  202. case cm::GoogleServiceAuthError::State::kInvalidGaiaCredentials:
  203. return GoogleServiceAuthError::FromInvalidGaiaCredentialsReason(
  204. FromMojoInvalidGaiaCredentialsReason(
  205. mojo_error->invalid_gaia_credentials_reason));
  206. case cm::GoogleServiceAuthError::State::kConnectionFailed:
  207. return GoogleServiceAuthError::FromConnectionError(
  208. mojo_error->network_error);
  209. case cm::GoogleServiceAuthError::State::kServiceError:
  210. return GoogleServiceAuthError::FromServiceError(
  211. mojo_error->error_message);
  212. case cm::GoogleServiceAuthError::State::kUnexpectedServiceResponse:
  213. return GoogleServiceAuthError::FromUnexpectedServiceResponse(
  214. mojo_error->error_message);
  215. case cm::GoogleServiceAuthError::State::kUserNotSignedUp:
  216. return GoogleServiceAuthError(
  217. GoogleServiceAuthError::State::USER_NOT_SIGNED_UP);
  218. case cm::GoogleServiceAuthError::State::kServiceUnavailable:
  219. return GoogleServiceAuthError(
  220. GoogleServiceAuthError::State::SERVICE_UNAVAILABLE);
  221. case cm::GoogleServiceAuthError::State::kRequestCanceled:
  222. return GoogleServiceAuthError(
  223. GoogleServiceAuthError::State::REQUEST_CANCELED);
  224. case cm::GoogleServiceAuthError::State::kScopeLimitedUnrecoverableError:
  225. return GoogleServiceAuthError(
  226. GoogleServiceAuthError::State::SCOPE_LIMITED_UNRECOVERABLE_ERROR);
  227. default:
  228. LOG(WARNING) << "Unknown crosapi::mojom::GoogleServiceAuthError::State: "
  229. << mojo_error->state;
  230. return absl::nullopt;
  231. }
  232. }
  233. crosapi::mojom::GoogleServiceAuthErrorPtr ToMojoGoogleServiceAuthError(
  234. GoogleServiceAuthError error) {
  235. crosapi::mojom::GoogleServiceAuthErrorPtr mojo_result =
  236. crosapi::mojom::GoogleServiceAuthError::New();
  237. mojo_result->error_message = error.error_message();
  238. if (error.state() == GoogleServiceAuthError::State::CONNECTION_FAILED) {
  239. mojo_result->network_error = error.network_error();
  240. }
  241. if (error.state() ==
  242. GoogleServiceAuthError::State::INVALID_GAIA_CREDENTIALS) {
  243. mojo_result->invalid_gaia_credentials_reason =
  244. ToMojoInvalidGaiaCredentialsReason(
  245. error.GetInvalidGaiaCredentialsReason());
  246. }
  247. mojo_result->state = ToMojoGoogleServiceAuthErrorState(error.state());
  248. return mojo_result;
  249. }
  250. absl::optional<account_manager::AccountAdditionResult>
  251. FromMojoAccountAdditionResult(
  252. const crosapi::mojom::AccountAdditionResultPtr& mojo_result) {
  253. absl::optional<account_manager::AccountAdditionResult::Status> status =
  254. FromMojoAccountAdditionStatus(mojo_result->status);
  255. if (!status.has_value())
  256. return absl::nullopt;
  257. switch (status.value()) {
  258. case account_manager::AccountAdditionResult::Status::kSuccess: {
  259. absl::optional<account_manager::Account> account =
  260. FromMojoAccount(mojo_result->account);
  261. if (!account.has_value())
  262. return absl::nullopt;
  263. return account_manager::AccountAdditionResult::FromAccount(
  264. account.value());
  265. }
  266. case account_manager::AccountAdditionResult::Status::kNetworkError: {
  267. absl::optional<GoogleServiceAuthError> net_error =
  268. FromMojoGoogleServiceAuthError(mojo_result->error);
  269. if (!net_error.has_value())
  270. return absl::nullopt;
  271. return account_manager::AccountAdditionResult::FromError(
  272. net_error.value());
  273. }
  274. case account_manager::AccountAdditionResult::Status::kAlreadyInProgress:
  275. case account_manager::AccountAdditionResult::Status::kCancelledByUser:
  276. case account_manager::AccountAdditionResult::Status::kUnexpectedResponse:
  277. return account_manager::AccountAdditionResult::FromStatus(status.value());
  278. case account_manager::AccountAdditionResult::Status::kBlockedByPolicy:
  279. return account_manager::AccountAdditionResult::FromStatus(status.value());
  280. }
  281. }
  282. crosapi::mojom::AccountAdditionResultPtr ToMojoAccountAdditionResult(
  283. account_manager::AccountAdditionResult result) {
  284. crosapi::mojom::AccountAdditionResultPtr mojo_result =
  285. crosapi::mojom::AccountAdditionResult::New();
  286. mojo_result->status = ToMojoAccountAdditionStatus(result.status());
  287. if (result.account().has_value()) {
  288. mojo_result->account =
  289. account_manager::ToMojoAccount(result.account().value());
  290. }
  291. if (result.error().state() != GoogleServiceAuthError::NONE) {
  292. mojo_result->error = ToMojoGoogleServiceAuthError(result.error());
  293. }
  294. return mojo_result;
  295. }
  296. absl::optional<account_manager::AccountAdditionOptions>
  297. FromMojoAccountAdditionOptions(
  298. const crosapi::mojom::AccountAdditionOptionsPtr& mojo_options) {
  299. if (!mojo_options)
  300. return absl::nullopt;
  301. account_manager::AccountAdditionOptions result;
  302. result.is_available_in_arc = mojo_options->is_available_in_arc;
  303. result.show_arc_availability_picker =
  304. mojo_options->show_arc_availability_picker;
  305. return result;
  306. }
  307. } // namespace account_manager