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) { ...@@ -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 is specified, use it to build the request.
if (auth_data_) { if (auth_data_) {
if (!auth_data_->gaia_token().empty()) { if (auth_data_->has_gaia_token()) {
rr->headers.SetHeader( rr->headers.SetHeader(
dm_protocol::kAuthHeader, dm_protocol::kAuthHeader,
std::string(dm_protocol::kServiceTokenAuthHeaderPrefix) + std::string(dm_protocol::kServiceTokenAuthHeaderPrefix) +
auth_data_->gaia_token()); auth_data_->gaia_token());
} }
if (!auth_data_->dm_token().empty()) { if (auth_data_->has_dm_token()) {
rr->headers.SetHeader(dm_protocol::kAuthHeader, rr->headers.SetHeader(dm_protocol::kAuthHeader,
std::string(dm_protocol::kDMTokenAuthHeaderPrefix) + std::string(dm_protocol::kDMTokenAuthHeaderPrefix) +
auth_data_->dm_token()); auth_data_->dm_token());
} }
if (!auth_data_->enrollment_token().empty()) { if (auth_data_->has_enrollment_token()) {
rr->headers.SetHeader( rr->headers.SetHeader(
dm_protocol::kAuthHeader, dm_protocol::kAuthHeader,
std::string(dm_protocol::kEnrollmentTokenAuthHeaderPrefix) + std::string(dm_protocol::kEnrollmentTokenAuthHeaderPrefix) +
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "components/policy/core/common/cloud/dm_auth.h" #include "components/policy/core/common/cloud/dm_auth.h"
#include "base/memory/ptr_util.h"
namespace policy { namespace policy {
DMAuth::DMAuth() = default; DMAuth::DMAuth() = default;
...@@ -11,52 +13,42 @@ DMAuth::~DMAuth() = default; ...@@ -11,52 +13,42 @@ DMAuth::~DMAuth() = default;
// static // static
std::unique_ptr<DMAuth> DMAuth::FromDMToken(const std::string& dm_token) { std::unique_ptr<DMAuth> DMAuth::FromDMToken(const std::string& dm_token) {
std::unique_ptr<DMAuth> result = std::make_unique<DMAuth>(); return base::WrapUnique(new DMAuth(dm_token, DMAuthTokenType::kDm));
result->dm_token_ = dm_token;
return result;
} }
// static // static
std::unique_ptr<DMAuth> DMAuth::FromGaiaToken(const std::string& gaia_token) { std::unique_ptr<DMAuth> DMAuth::FromGaiaToken(const std::string& gaia_token) {
std::unique_ptr<DMAuth> result = std::make_unique<DMAuth>(); return base::WrapUnique(new DMAuth(gaia_token, DMAuthTokenType::kGaia));
result->gaia_token_ = gaia_token;
return result;
} }
// static // static
std::unique_ptr<DMAuth> DMAuth::FromOAuthToken(const std::string& oauth_token) { std::unique_ptr<DMAuth> DMAuth::FromOAuthToken(const std::string& oauth_token) {
std::unique_ptr<DMAuth> result = std::make_unique<DMAuth>(); return base::WrapUnique(new DMAuth(oauth_token, DMAuthTokenType::kOauth));
result->oauth_token_ = oauth_token;
return result;
} }
// static // static
std::unique_ptr<DMAuth> DMAuth::FromEnrollmentToken( std::unique_ptr<DMAuth> DMAuth::FromEnrollmentToken(
const std::string& enrollment_token) { const std::string& enrollment_token) {
std::unique_ptr<DMAuth> result = std::make_unique<DMAuth>(); return base::WrapUnique(
result->enrollment_token_ = enrollment_token; new DMAuth(enrollment_token, DMAuthTokenType::kEnrollment));
return result;
} }
// static // static
std::unique_ptr<DMAuth> DMAuth::NoAuth() { std::unique_ptr<DMAuth> DMAuth::NoAuth() {
return std::make_unique<DMAuth>(); 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> DMAuth::Clone() const {
std::unique_ptr<DMAuth> result = std::make_unique<DMAuth>(); std::unique_ptr<DMAuth> result = std::make_unique<DMAuth>();
*result = *this; *result = *this;
return result; 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 { bool DMAuth::Equals(const DMAuth& other) const {
return gaia_token_ == other.gaia_token_ && dm_token_ == other.dm_token_ && return token_ == other.token_ && token_type_ == other.token_type_;
enrollment_token_ == other.enrollment_token_ &&
oauth_token_ == other.oauth_token_;
} }
} // namespace policy } // namespace policy
...@@ -7,11 +7,22 @@ ...@@ -7,11 +7,22 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include "base/logging.h"
#include "base/macros.h" #include "base/macros.h"
#include "components/policy/policy_export.h" #include "components/policy/policy_export.h"
namespace policy { 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 // Class that encapsulates different authentication methods to interact with
// device management service. // device management service.
// We currently have 4 methods for authentication: // We currently have 4 methods for authentication:
...@@ -38,25 +49,41 @@ class POLICY_EXPORT DMAuth { ...@@ -38,25 +49,41 @@ class POLICY_EXPORT DMAuth {
std::unique_ptr<DMAuth> Clone() const; std::unique_ptr<DMAuth> Clone() const;
// Checks if no authentication is provided. // Checks if no authentication is provided.
bool empty() const; bool empty() const { return token_type_ == DMAuthTokenType::kNoAuth; }
bool Equals(const DMAuth& other) const; bool Equals(const DMAuth& other) const;
std::string gaia_token() const { return gaia_token_; } std::string gaia_token() const {
bool has_gaia_token() const { return !gaia_token_.empty(); } DCHECK_EQ(DMAuthTokenType::kGaia, token_type_);
std::string dm_token() const { return dm_token_; } return token_;
bool has_dm_token() const { return !dm_token_.empty(); } }
std::string enrollment_token() const { return enrollment_token_; } bool has_gaia_token() const { return token_type_ == DMAuthTokenType::kGaia; }
bool has_enrollment_token() const { return !enrollment_token_.empty(); } std::string dm_token() const {
std::string oauth_token() const { return oauth_token_; } DCHECK_EQ(DMAuthTokenType::kDm, token_type_);
bool has_oauth_token() const { return !oauth_token_.empty(); } 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: private:
DMAuth(const std::string& token, DMAuthTokenType token_type);
DMAuth& operator=(const DMAuth&) = default; DMAuth& operator=(const DMAuth&) = default;
std::string gaia_token_; std::string token_;
std::string dm_token_; DMAuthTokenType token_type_ = DMAuthTokenType::kNoAuth;
std::string enrollment_token_;
std::string oauth_token_;
DISALLOW_COPY(DMAuth); 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