Commit d19e7b51 authored by Igor's avatar Igor Committed by Commit Bot

Refactor DMAuth class to work with a single token.

The class contains four types of tokens, but only one token can be used.
This change makes the class to keep the data in a single token and have an
easy way to find the token type.

BUG=None
Test=None

Change-Id: If9a73b5c6c2a6a5a2f6c9a6c5c7bb672e7eac765
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1943244Reviewed-by: default avatarIgor <igorcov@chromium.org>
Reviewed-by: default avatarMaksim Ivanov <emaxx@chromium.org>
Commit-Queue: Igor <igorcov@chromium.org>
Cr-Commit-Position: refs/heads/master@{#726370}
parent 3c491295
......@@ -284,18 +284,18 @@ JobConfigurationBase::GetResourceRequest(bool bypass_proxy, int last_error) {
// If auth data is specified, use it to build the request.
if (auth_data_) {
if (!auth_data_->gaia_token().empty()) {
if (auth_data_->has_gaia_token()) {
rr->headers.SetHeader(
dm_protocol::kAuthHeader,
std::string(dm_protocol::kServiceTokenAuthHeaderPrefix) +
auth_data_->gaia_token());
}
if (!auth_data_->dm_token().empty()) {
if (auth_data_->has_dm_token()) {
rr->headers.SetHeader(dm_protocol::kAuthHeader,
std::string(dm_protocol::kDMTokenAuthHeaderPrefix) +
auth_data_->dm_token());
}
if (!auth_data_->enrollment_token().empty()) {
if (auth_data_->has_enrollment_token()) {
rr->headers.SetHeader(
dm_protocol::kAuthHeader,
std::string(dm_protocol::kEnrollmentTokenAuthHeaderPrefix) +
......
......@@ -4,6 +4,8 @@
#include "components/policy/core/common/cloud/dm_auth.h"
#include "base/memory/ptr_util.h"
namespace policy {
DMAuth::DMAuth() = default;
......@@ -11,52 +13,42 @@ DMAuth::~DMAuth() = default;
// static
std::unique_ptr<DMAuth> DMAuth::FromDMToken(const std::string& dm_token) {
std::unique_ptr<DMAuth> result = std::make_unique<DMAuth>();
result->dm_token_ = dm_token;
return result;
return base::WrapUnique(new DMAuth(dm_token, DMAuthTokenType::kDm));
}
// static
std::unique_ptr<DMAuth> DMAuth::FromGaiaToken(const std::string& gaia_token) {
std::unique_ptr<DMAuth> result = std::make_unique<DMAuth>();
result->gaia_token_ = gaia_token;
return result;
return base::WrapUnique(new DMAuth(gaia_token, DMAuthTokenType::kGaia));
}
// static
std::unique_ptr<DMAuth> DMAuth::FromOAuthToken(const std::string& oauth_token) {
std::unique_ptr<DMAuth> result = std::make_unique<DMAuth>();
result->oauth_token_ = oauth_token;
return result;
return base::WrapUnique(new DMAuth(oauth_token, DMAuthTokenType::kOauth));
}
// static
std::unique_ptr<DMAuth> DMAuth::FromEnrollmentToken(
const std::string& enrollment_token) {
std::unique_ptr<DMAuth> result = std::make_unique<DMAuth>();
result->enrollment_token_ = enrollment_token;
return result;
return base::WrapUnique(
new DMAuth(enrollment_token, DMAuthTokenType::kEnrollment));
}
// static
std::unique_ptr<DMAuth> DMAuth::NoAuth() {
return std::make_unique<DMAuth>();
}
DMAuth::DMAuth(const std::string& token, DMAuthTokenType token_type)
: token_(token), token_type_(token_type) {}
std::unique_ptr<DMAuth> DMAuth::Clone() const {
std::unique_ptr<DMAuth> result = std::make_unique<DMAuth>();
*result = *this;
return result;
}
bool DMAuth::empty() const {
return gaia_token_.empty() && dm_token_.empty() &&
enrollment_token_.empty() && oauth_token_.empty();
}
bool DMAuth::Equals(const DMAuth& other) const {
return gaia_token_ == other.gaia_token_ && dm_token_ == other.dm_token_ &&
enrollment_token_ == other.enrollment_token_ &&
oauth_token_ == other.oauth_token_;
return token_ == other.token_ && token_type_ == other.token_type_;
}
} // namespace policy
......@@ -7,11 +7,22 @@
#include <memory>
#include <string>
#include "base/logging.h"
#include "base/macros.h"
#include "components/policy/policy_export.h"
namespace policy {
// The enum type for the token used for authentication. Explicit values are
// set to allow easy identification of value from the logs.
enum class DMAuthTokenType {
kNoAuth = 0,
kGaia = 1,
kDm = 2,
kEnrollment = 3,
kOauth = 4,
};
// Class that encapsulates different authentication methods to interact with
// device management service.
// We currently have 4 methods for authentication:
......@@ -38,25 +49,41 @@ class POLICY_EXPORT DMAuth {
std::unique_ptr<DMAuth> Clone() const;
// Checks if no authentication is provided.
bool empty() const;
bool empty() const { return token_type_ == DMAuthTokenType::kNoAuth; }
bool Equals(const DMAuth& other) const;
std::string gaia_token() const { return gaia_token_; }
bool has_gaia_token() const { return !gaia_token_.empty(); }
std::string dm_token() const { return dm_token_; }
bool has_dm_token() const { return !dm_token_.empty(); }
std::string enrollment_token() const { return enrollment_token_; }
bool has_enrollment_token() const { return !enrollment_token_.empty(); }
std::string oauth_token() const { return oauth_token_; }
bool has_oauth_token() const { return !oauth_token_.empty(); }
std::string gaia_token() const {
DCHECK_EQ(DMAuthTokenType::kGaia, token_type_);
return token_;
}
bool has_gaia_token() const { return token_type_ == DMAuthTokenType::kGaia; }
std::string dm_token() const {
DCHECK_EQ(DMAuthTokenType::kDm, token_type_);
return token_;
}
bool has_dm_token() const { return token_type_ == DMAuthTokenType::kDm; }
std::string enrollment_token() const {
DCHECK_EQ(DMAuthTokenType::kEnrollment, token_type_);
return token_;
}
bool has_enrollment_token() const {
return token_type_ == DMAuthTokenType::kEnrollment;
}
std::string oauth_token() const {
DCHECK_EQ(DMAuthTokenType::kOauth, token_type_);
return token_;
}
bool has_oauth_token() const {
return token_type_ == DMAuthTokenType::kOauth;
}
DMAuthTokenType token_type() const { return token_type_; }
private:
DMAuth(const std::string& token, DMAuthTokenType token_type);
DMAuth& operator=(const DMAuth&) = default;
std::string gaia_token_;
std::string dm_token_;
std::string enrollment_token_;
std::string oauth_token_;
std::string token_;
DMAuthTokenType token_type_ = DMAuthTokenType::kNoAuth;
DISALLOW_COPY(DMAuth);
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment