File: account_manager_util.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (443 lines) | stat: -rw-r--r-- 19,406 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/account_manager_core/account_manager_util.h"

#include <optional>

#include "base/check_op.h"
#include "base/notreached.h"
#include "components/account_manager_core/account.h"
#include "components/account_manager_core/account_addition_options.h"
#include "components/account_manager_core/account_upsertion_result.h"
#include "google_apis/gaia/google_service_auth_error.h"

namespace account_manager {

namespace cm = crosapi::mojom;

namespace {

GoogleServiceAuthError::InvalidGaiaCredentialsReason
FromMojoInvalidGaiaCredentialsReason(
    crosapi::mojom::GoogleServiceAuthError::InvalidGaiaCredentialsReason
        mojo_reason) {
  switch (mojo_reason) {
    case cm::GoogleServiceAuthError::InvalidGaiaCredentialsReason::kUnknown:
      return GoogleServiceAuthError::InvalidGaiaCredentialsReason::UNKNOWN;
    case cm::GoogleServiceAuthError::InvalidGaiaCredentialsReason::
        kCredentialsRejectedByServer:
      return GoogleServiceAuthError::InvalidGaiaCredentialsReason::
          CREDENTIALS_REJECTED_BY_SERVER;
    case cm::GoogleServiceAuthError::InvalidGaiaCredentialsReason::
        kCredentialsRejectedByClient:
      return GoogleServiceAuthError::InvalidGaiaCredentialsReason::
          CREDENTIALS_REJECTED_BY_CLIENT;
    case cm::GoogleServiceAuthError::InvalidGaiaCredentialsReason::
        kCredentialsMissing:
      return GoogleServiceAuthError::InvalidGaiaCredentialsReason::
          CREDENTIALS_MISSING;
    default:
      LOG(WARNING) << "Unknown "
                      "crosapi::mojom::GoogleServiceAuthError::"
                      "InvalidGaiaCredentialsReason: "
                   << mojo_reason;
      return GoogleServiceAuthError::InvalidGaiaCredentialsReason::UNKNOWN;
  }
}

crosapi::mojom::GoogleServiceAuthError::InvalidGaiaCredentialsReason
ToMojoInvalidGaiaCredentialsReason(
    GoogleServiceAuthError::InvalidGaiaCredentialsReason reason) {
  switch (reason) {
    case GoogleServiceAuthError::InvalidGaiaCredentialsReason::UNKNOWN:
      return cm::GoogleServiceAuthError::InvalidGaiaCredentialsReason::kUnknown;
    case GoogleServiceAuthError::InvalidGaiaCredentialsReason::
        CREDENTIALS_REJECTED_BY_SERVER:
      return cm::GoogleServiceAuthError::InvalidGaiaCredentialsReason::
          kCredentialsRejectedByServer;
    case GoogleServiceAuthError::InvalidGaiaCredentialsReason::
        CREDENTIALS_REJECTED_BY_CLIENT:
      return cm::GoogleServiceAuthError::InvalidGaiaCredentialsReason::
          kCredentialsRejectedByClient;
    case GoogleServiceAuthError::InvalidGaiaCredentialsReason::
        CREDENTIALS_MISSING:
      return cm::GoogleServiceAuthError::InvalidGaiaCredentialsReason::
          kCredentialsMissing;
    case GoogleServiceAuthError::InvalidGaiaCredentialsReason::NUM_REASONS:
      NOTREACHED();
  }
}

GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason
FromMojoScopeLimitedUnrecoverableErrorReason(
    crosapi::mojom::GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason
        mojo_reason) {
  switch (mojo_reason) {
    case cm::GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason::
        kInvalidGrantRaptError:
      return GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason::
          kInvalidGrantRaptError;
    case cm::GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason::
        kInvalidScope:
      return GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason::
          kInvalidScope;
    case cm::GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason::
        kRestrictedClient:
      return GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason::
          kRestrictedClient;
    case cm::GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason::
        kAdminPolicyEnforced:
      return GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason::
          kAdminPolicyEnforced;
    case cm::GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason::
        kRemoteConsentResolutionRequired:
      return GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason::
          kRemoteConsentResolutionRequired;
    case cm::GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason::
        kAccessDenied:
      return GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason::
          kAccessDenied;
  }
}

crosapi::mojom::GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason
ToMojoScopeLimitedUnrecoverableErrorReason(
    GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason reason) {
  switch (reason) {
    case GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason::
        kInvalidGrantRaptError:
      return cm::GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason::
          kInvalidGrantRaptError;
    case GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason::
        kInvalidScope:
      return cm::GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason::
          kInvalidScope;
    case GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason::
        kRestrictedClient:
      return cm::GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason::
          kRestrictedClient;
    case GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason::
        kAdminPolicyEnforced:
      return cm::GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason::
          kAdminPolicyEnforced;
    case GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason::
        kRemoteConsentResolutionRequired:
      return cm::GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason::
          kRemoteConsentResolutionRequired;
    case GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason::
        kAccessDenied:
      return cm::GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason::
          kAccessDenied;
  }
}

crosapi::mojom::GoogleServiceAuthError::State ToMojoGoogleServiceAuthErrorState(
    GoogleServiceAuthError::State state) {
  switch (state) {
    case GoogleServiceAuthError::State::NONE:
      return cm::GoogleServiceAuthError::State::kNone;
    case GoogleServiceAuthError::State::INVALID_GAIA_CREDENTIALS:
      return cm::GoogleServiceAuthError::State::kInvalidGaiaCredentials;
    case GoogleServiceAuthError::State::USER_NOT_SIGNED_UP:
      return cm::GoogleServiceAuthError::State::kUserNotSignedUp;
    case GoogleServiceAuthError::State::CONNECTION_FAILED:
      return cm::GoogleServiceAuthError::State::kConnectionFailed;
    case GoogleServiceAuthError::State::SERVICE_UNAVAILABLE:
      return cm::GoogleServiceAuthError::State::kServiceUnavailable;
    case GoogleServiceAuthError::State::REQUEST_CANCELED:
      return cm::GoogleServiceAuthError::State::kRequestCanceled;
    case GoogleServiceAuthError::State::UNEXPECTED_SERVICE_RESPONSE:
      return cm::GoogleServiceAuthError::State::kUnexpectedServiceResponse;
    case GoogleServiceAuthError::State::SERVICE_ERROR:
      return cm::GoogleServiceAuthError::State::kServiceError;
    case GoogleServiceAuthError::State::SCOPE_LIMITED_UNRECOVERABLE_ERROR:
      return cm::GoogleServiceAuthError::State::kScopeLimitedUnrecoverableError;
    case GoogleServiceAuthError::State::CHALLENGE_RESPONSE_REQUIRED:
      return cm::GoogleServiceAuthError::State::kChallengeResponseRequired;
    case GoogleServiceAuthError::State::NUM_STATES:
      NOTREACHED();
  }
}

std::optional<account_manager::AccountUpsertionResult::Status>
FromMojoAccountAdditionStatus(
    crosapi::mojom::AccountUpsertionResult::Status mojo_status) {
  switch (mojo_status) {
    case cm::AccountUpsertionResult::Status::kSuccess:
      return account_manager::AccountUpsertionResult::Status::kSuccess;
    case cm::AccountUpsertionResult::Status::kAlreadyInProgress:
      return account_manager::AccountUpsertionResult::Status::
          kAlreadyInProgress;
    case cm::AccountUpsertionResult::Status::kCancelledByUser:
      return account_manager::AccountUpsertionResult::Status::kCancelledByUser;
    case cm::AccountUpsertionResult::Status::kNetworkError:
      return account_manager::AccountUpsertionResult::Status::kNetworkError;
    case cm::AccountUpsertionResult::Status::kUnexpectedResponse:
      return account_manager::AccountUpsertionResult::Status::
          kUnexpectedResponse;
    case cm::AccountUpsertionResult::Status::kBlockedByPolicy:
      return account_manager::AccountUpsertionResult::Status::kBlockedByPolicy;
    default:
      LOG(WARNING) << "Unknown crosapi::mojom::AccountUpsertionResult::Status: "
                   << mojo_status;
      return std::nullopt;
  }
}

crosapi::mojom::AccountUpsertionResult::Status ToMojoAccountAdditionStatus(
    account_manager::AccountUpsertionResult::Status status) {
  switch (status) {
    case account_manager::AccountUpsertionResult::Status::kSuccess:
      return cm::AccountUpsertionResult::Status::kSuccess;
    case account_manager::AccountUpsertionResult::Status::kAlreadyInProgress:
      return cm::AccountUpsertionResult::Status::kAlreadyInProgress;
    case account_manager::AccountUpsertionResult::Status::kCancelledByUser:
      return cm::AccountUpsertionResult::Status::kCancelledByUser;
    case account_manager::AccountUpsertionResult::Status::kNetworkError:
      return cm::AccountUpsertionResult::Status::kNetworkError;
    case account_manager::AccountUpsertionResult::Status::kUnexpectedResponse:
      return cm::AccountUpsertionResult::Status::kUnexpectedResponse;
    case account_manager::AccountUpsertionResult::Status::kBlockedByPolicy:
      return cm::AccountUpsertionResult::Status::kBlockedByPolicy;
    case account_manager::AccountUpsertionResult::Status::
        kMojoRemoteDisconnected:
    case account_manager::AccountUpsertionResult::Status::
        kIncompatibleMojoVersions:
      // `kMojoRemoteDisconnected` and `kIncompatibleMojoVersions` are generated
      // entirely on the remote side when the receiver can't even be reached.
      // They do not have any Mojo equivalent since they are never passed over
      // the wire in the first place.
      NOTREACHED() << "These statuses should not be passed over the wire";
  }
}

}  // namespace

std::optional<account_manager::Account> FromMojoAccount(
    const crosapi::mojom::AccountPtr& mojom_account) {
  if (mojom_account.is_null()) {
    return std::nullopt;
  }

  const std::optional<account_manager::AccountKey> account_key =
      FromMojoAccountKey(mojom_account->key);
  if (!account_key.has_value())
    return std::nullopt;

  account_manager::Account account{account_key.value(),
                                   mojom_account->raw_email};
  return account;
}

crosapi::mojom::AccountPtr ToMojoAccount(
    const account_manager::Account& account) {
  crosapi::mojom::AccountPtr mojom_account = crosapi::mojom::Account::New();
  mojom_account->key = ToMojoAccountKey(account.key);
  mojom_account->raw_email = account.raw_email;
  return mojom_account;
}

std::optional<account_manager::AccountKey> FromMojoAccountKey(
    const crosapi::mojom::AccountKeyPtr& mojom_account_key) {
  if (mojom_account_key.is_null()) {
    return std::nullopt;
  }

  const std::optional<account_manager::AccountType> account_type =
      FromMojoAccountType(mojom_account_key->account_type);
  if (!account_type.has_value())
    return std::nullopt;
  if (mojom_account_key->id.empty())
    return std::nullopt;

  return account_manager::AccountKey(mojom_account_key->id,
                                     account_type.value());
}

crosapi::mojom::AccountKeyPtr ToMojoAccountKey(
    const account_manager::AccountKey& account_key) {
  crosapi::mojom::AccountKeyPtr mojom_account_key =
      crosapi::mojom::AccountKey::New();
  mojom_account_key->id = account_key.id();
  mojom_account_key->account_type =
      ToMojoAccountType(account_key.account_type());
  return mojom_account_key;
}

std::optional<account_manager::AccountType> FromMojoAccountType(
    const crosapi::mojom::AccountType& account_type) {
  switch (account_type) {
    case crosapi::mojom::AccountType::kGaia:
      static_assert(static_cast<int>(crosapi::mojom::AccountType::kGaia) ==
                        static_cast<int>(account_manager::AccountType::kGaia),
                    "Underlying enum values must match");
      return account_manager::AccountType::kGaia;
    default:
      // Don't consider this as as error to preserve forwards compatibility with
      // lacros.
      LOG(WARNING) << "Unknown account type: " << account_type;
      return std::nullopt;
  }
}

crosapi::mojom::AccountType ToMojoAccountType(
    const account_manager::AccountType& account_type) {
  // Currently, we only support `kGaia` account type. Should a new type be added
  // in the future, consider removing the `CHECK_EQ()` below and handling the
  // new type accordingly.
  CHECK_EQ(account_type, account_manager::AccountType::kGaia);

  return crosapi::mojom::AccountType::kGaia;
}

std::optional<GoogleServiceAuthError> FromMojoGoogleServiceAuthError(
    const crosapi::mojom::GoogleServiceAuthErrorPtr& mojo_error) {
  if (mojo_error.is_null()) {
    return std::nullopt;
  }

  switch (mojo_error->state) {
    case cm::GoogleServiceAuthError::State::kNone:
      return GoogleServiceAuthError::AuthErrorNone();
    case cm::GoogleServiceAuthError::State::kInvalidGaiaCredentials:
      return GoogleServiceAuthError::FromInvalidGaiaCredentialsReason(
          FromMojoInvalidGaiaCredentialsReason(
              mojo_error->invalid_gaia_credentials_reason));
    case cm::GoogleServiceAuthError::State::kConnectionFailed:
      return GoogleServiceAuthError::FromConnectionError(
          mojo_error->network_error);
    case cm::GoogleServiceAuthError::State::kServiceError:
      return GoogleServiceAuthError::FromServiceError(
          mojo_error->error_message);
    case cm::GoogleServiceAuthError::State::kUnexpectedServiceResponse:
      return GoogleServiceAuthError::FromUnexpectedServiceResponse(
          mojo_error->error_message);
    case cm::GoogleServiceAuthError::State::kUserNotSignedUp:
      return GoogleServiceAuthError(
          GoogleServiceAuthError::State::USER_NOT_SIGNED_UP);
    case cm::GoogleServiceAuthError::State::kServiceUnavailable:
      return GoogleServiceAuthError(
          GoogleServiceAuthError::State::SERVICE_UNAVAILABLE);
    case cm::GoogleServiceAuthError::State::kRequestCanceled:
      return GoogleServiceAuthError(
          GoogleServiceAuthError::State::REQUEST_CANCELED);
    case cm::GoogleServiceAuthError::State::kScopeLimitedUnrecoverableError:
      return GoogleServiceAuthError::FromScopeLimitedUnrecoverableErrorReason(
          FromMojoScopeLimitedUnrecoverableErrorReason(
              mojo_error->scope_limited_unrecoverable_error_reason));
    case cm::GoogleServiceAuthError::State::kChallengeResponseRequired:
      return GoogleServiceAuthError::FromTokenBindingChallenge(
          mojo_error->token_binding_challenge.value_or(
              "MISSING_CHALLENGE_FROM_CROSAPI_MOJOM"));
    default:
      LOG(WARNING) << "Unknown crosapi::mojom::GoogleServiceAuthError::State: "
                   << mojo_error->state;
      return std::nullopt;
  }
}

crosapi::mojom::GoogleServiceAuthErrorPtr ToMojoGoogleServiceAuthError(
    GoogleServiceAuthError error) {
  crosapi::mojom::GoogleServiceAuthErrorPtr mojo_result =
      crosapi::mojom::GoogleServiceAuthError::New();
  mojo_result->error_message = error.error_message();
  if (error.state() == GoogleServiceAuthError::State::CONNECTION_FAILED) {
    mojo_result->network_error = error.network_error();
  }
  if (error.state() ==
      GoogleServiceAuthError::State::INVALID_GAIA_CREDENTIALS) {
    mojo_result->invalid_gaia_credentials_reason =
        ToMojoInvalidGaiaCredentialsReason(
            error.GetInvalidGaiaCredentialsReason());
  }
  if (error.state() ==
      GoogleServiceAuthError::State::SCOPE_LIMITED_UNRECOVERABLE_ERROR) {
    mojo_result->scope_limited_unrecoverable_error_reason =
        ToMojoScopeLimitedUnrecoverableErrorReason(
            error.GetScopeLimitedUnrecoverableErrorReason());
  }
  if (error.state() ==
      GoogleServiceAuthError::State::CHALLENGE_RESPONSE_REQUIRED) {
    mojo_result->token_binding_challenge = error.GetTokenBindingChallenge();
  }
  mojo_result->state = ToMojoGoogleServiceAuthErrorState(error.state());
  return mojo_result;
}

std::optional<account_manager::AccountUpsertionResult>
FromMojoAccountUpsertionResult(
    const crosapi::mojom::AccountUpsertionResultPtr& mojo_result) {
  if (mojo_result.is_null()) {
    return std::nullopt;
  }

  std::optional<account_manager::AccountUpsertionResult::Status> status =
      FromMojoAccountAdditionStatus(mojo_result->status);
  if (!status.has_value())
    return std::nullopt;

  switch (status.value()) {
    case account_manager::AccountUpsertionResult::Status::kSuccess: {
      std::optional<account_manager::Account> account =
          FromMojoAccount(mojo_result->account);
      if (!account.has_value())
        return std::nullopt;
      return account_manager::AccountUpsertionResult::FromAccount(
          account.value());
    }
    case account_manager::AccountUpsertionResult::Status::kNetworkError: {
      std::optional<GoogleServiceAuthError> net_error =
          FromMojoGoogleServiceAuthError(mojo_result->error);
      if (!net_error.has_value())
        return std::nullopt;
      return account_manager::AccountUpsertionResult::FromError(
          net_error.value());
    }
    case account_manager::AccountUpsertionResult::Status::kAlreadyInProgress:
    case account_manager::AccountUpsertionResult::Status::kCancelledByUser:
    case account_manager::AccountUpsertionResult::Status::kUnexpectedResponse:
    case account_manager::AccountUpsertionResult::Status::
        kMojoRemoteDisconnected:
    case account_manager::AccountUpsertionResult::Status::
        kIncompatibleMojoVersions:
      return account_manager::AccountUpsertionResult::FromStatus(
          status.value());
    case account_manager::AccountUpsertionResult::Status::kBlockedByPolicy:
      return account_manager::AccountUpsertionResult::FromStatus(
          status.value());
  }
}

crosapi::mojom::AccountUpsertionResultPtr ToMojoAccountUpsertionResult(
    account_manager::AccountUpsertionResult result) {
  crosapi::mojom::AccountUpsertionResultPtr mojo_result =
      crosapi::mojom::AccountUpsertionResult::New();
  mojo_result->status = ToMojoAccountAdditionStatus(result.status());
  if (result.account().has_value()) {
    mojo_result->account =
        account_manager::ToMojoAccount(result.account().value());
  }
  if (result.error().state() != GoogleServiceAuthError::NONE) {
    mojo_result->error = ToMojoGoogleServiceAuthError(result.error());
  }
  return mojo_result;
}

std::optional<account_manager::AccountAdditionOptions>
FromMojoAccountAdditionOptions(
    const crosapi::mojom::AccountAdditionOptionsPtr& mojo_options) {
  if (mojo_options.is_null()) {
    return std::nullopt;
  }

  account_manager::AccountAdditionOptions result;
  result.is_available_in_arc = mojo_options->is_available_in_arc;
  result.show_arc_availability_picker =
      mojo_options->show_arc_availability_picker;

  return result;
}

}  // namespace account_manager