blob: ef16f9956b14f3aa8f9b76c417df8a9526b59c58 [file] [log] [blame]
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include <unordered_map>
#include "base/time/time.h"
#include "base/values.h"
#include "google_apis/gaia/gaia_auth_util.h"
#include "net/cookies/canonical_cookie.h"
#include "net/cookies/cookie_constants.h"
#include "url/gurl.h"
// Values for the 'status' field of multilogin responses. Used for UMA logging,
// do not remove or reorder values.
enum class OAuthMultiloginResponseStatus {
// Status could not be parsed.
kUnknownStatus = 0,
// The request was processed successfully, and the rest of this object
// contains the cookies to set across domains. The HTTP status code will be
// 200.
kOk = 1,
// Something happened while processing the request that made it fail. It is
// suspected to be a transient issue, so the client may retry at a later time
// with exponential backoff. The HTTP status code will be 503.
kRetry = 2,
// The input parameters were not as expected (wrong header format, missing
// parameters, etc). Retrying without changing input parameters will not work.
// The HTTP status code will be 400.
kInvalidInput = 3,
// At least one provided token could not be used to authenticate the
// corresponding user. This includes the case where the provided Gaia ID does
// not match with the corresponding OAuth token. The HTTP status code will be
// 403.
kInvalidTokens = 4,
// An error occurred while processing the request, and retrying is not
// expected to work. The HTTP status code will be 500.
kError = 5,
kMaxValue = kError,
// Parses the status field of the response.
OAuthMultiloginResponseStatus ParseOAuthMultiloginResponseStatus(
const std::string& status);
class OAuthMultiloginResult {
// Parses cookies and status from JSON response. Maps status to
// GoogleServiceAuthError::State values or sets error to
// UNEXPECTED_SERVER_RESPONSE if JSON string cannot be parsed.
OAuthMultiloginResult(const std::string& raw_data);
OAuthMultiloginResult(OAuthMultiloginResponseStatus status);
OAuthMultiloginResult(const OAuthMultiloginResult& other);
OAuthMultiloginResult& operator=(const OAuthMultiloginResult& other);
std::vector<net::CanonicalCookie> cookies() const { return cookies_; }
std::vector<std::string> failed_gaia_ids() const { return failed_gaia_ids_; }
OAuthMultiloginResponseStatus status() const { return status_; }
FRIEND_TEST_ALL_PREFIXES(OAuthMultiloginResultTest, TryParseCookiesFromValue);
// Response body that has a form of JSON contains protection characters
// against XSSI that have to be removed. See go/xssi.
static base::StringPiece StripXSSICharacters(const std::string& data);
void TryParseCookiesFromValue(base::Value* json_value);
// If error is INVALID_GAIA_CREDENTIALS response is expected to have a list of
// failed accounts for which tokens are not valid.
void TryParseFailedAccountsFromValue(base::Value* json_value);
std::vector<net::CanonicalCookie> cookies_;
std::vector<std::string> failed_gaia_ids_;
OAuthMultiloginResponseStatus status_;