From 035ba8509c6d4857026529ee6f6b25902ca95e46 Mon Sep 17 00:00:00 2001 From: Anton Kolesnyk <41349689+antkmsft@users.noreply.github.com> Date: Tue, 8 Dec 2020 17:04:03 -0800 Subject: [PATCH] Move BearerTokenAuthenticationPolicy to Http namespace/policy.hpp (#1104) --- sdk/core/azure-core/CHANGELOG.md | 1 + sdk/core/azure-core/CMakeLists.txt | 2 +- .../azure-core/inc/azure/core/credentials.hpp | 74 ------------------- .../azure-core/inc/azure/core/http/policy.hpp | 73 ++++++++++++++++++ .../bearer_token_authentication_policy.cpp} | 11 +-- .../src/blob_batch_client.cpp | 5 +- .../azure-storage-blobs/src/blob_client.cpp | 3 +- .../src/blob_container_client.cpp | 3 +- .../src/blob_service_client.cpp | 3 +- .../src/datalake_directory_client.cpp | 3 +- .../src/datalake_file_client.cpp | 3 +- .../src/datalake_file_system_client.cpp | 3 +- .../src/datalake_path_client.cpp | 3 +- .../src/datalake_service_client.cpp | 3 +- 14 files changed, 91 insertions(+), 99 deletions(-) rename sdk/core/azure-core/src/{credentials.cpp => http/bearer_token_authentication_policy.cpp} (67%) diff --git a/sdk/core/azure-core/CHANGELOG.md b/sdk/core/azure-core/CHANGELOG.md index 4a2ed4546..d1f93f444 100644 --- a/sdk/core/azure-core/CHANGELOG.md +++ b/sdk/core/azure-core/CHANGELOG.md @@ -4,6 +4,7 @@ ### Breaking Changes - Removed `DateTime::operator Duration()`. +- Moved `Azure::Core::BearerTokenAuthenticationPolicy`, defined in `azure/core/credentials.hpp` to `Azure::Core::Http` namespace in `azure/core/http/policy.hpp` header. ## 1.0.0-beta.3 (2020-11-11) diff --git a/sdk/core/azure-core/CMakeLists.txt b/sdk/core/azure-core/CMakeLists.txt index 37a16b886..fa0491ac2 100644 --- a/sdk/core/azure-core/CMakeLists.txt +++ b/sdk/core/azure-core/CMakeLists.txt @@ -45,8 +45,8 @@ include(CodeCoverage) add_library ( azure-core src/context.cpp - src/credentials.cpp src/datetime.cpp + src/http/bearer_token_authentication_policy.cpp src/http/body_stream.cpp ${CURL_TRANSPORT_ADAPTER_SRC} src/http/http.cpp diff --git a/sdk/core/azure-core/inc/azure/core/credentials.hpp b/sdk/core/azure-core/inc/azure/core/credentials.hpp index 9b9530b0a..57d0b06e9 100644 --- a/sdk/core/azure-core/inc/azure/core/credentials.hpp +++ b/sdk/core/azure-core/inc/azure/core/credentials.hpp @@ -9,7 +9,6 @@ #pragma once #include -#include #include #include @@ -74,77 +73,4 @@ namespace Azure { namespace Core { */ explicit AuthenticationException(std::string const& msg) : std::runtime_error(msg) {} }; - - /** - * @brief Bearer Token authentication policy. - */ - class BearerTokenAuthenticationPolicy : public Http::HttpPolicy { - private: - std::shared_ptr const m_credential; - std::vector m_scopes; - - mutable AccessToken m_accessToken; - mutable std::mutex m_accessTokenMutex; - - BearerTokenAuthenticationPolicy(BearerTokenAuthenticationPolicy const&) = delete; - void operator=(BearerTokenAuthenticationPolicy const&) = delete; - - public: - /** - * @brief Construct a Bearer Token authentication policy with single authentication scope. - * - * @param credential A #TokenCredential to use with this policy. - * @param scope Authentication scope. - */ - explicit BearerTokenAuthenticationPolicy( - std::shared_ptr credential, - std::string scope) - : m_credential(std::move(credential)) - { - m_scopes.emplace_back(std::move(scope)); - } - - /** - * @brief Construct a Bearer Token authentication policy with multiple authentication scopes. - * - * @param credential A #TokenCredential to use with this policy. - * @param scopes A vector of authentication scopes. - */ - explicit BearerTokenAuthenticationPolicy( - std::shared_ptr credential, - std::vector scopes) - : m_credential(std::move(credential)), m_scopes(std::move(scopes)) - { - } - - /** - * @brief Construct a Bearer Token authentication policy with multiple authentication scopes. - * - * @tparam A type of scopes sequence iterator. - * - * @param credential A #TokenCredential to use with this policy. - * @param scopesBegin An iterator pointing to begin of the sequence of scopes to use. - * @param scopesEnd An iterator pointing to an element after the last element in sequence of - * scopes to use. - */ - template - explicit BearerTokenAuthenticationPolicy( - std::shared_ptr credential, - ScopesIterator const& scopesBegin, - ScopesIterator const& scopesEnd) - : m_credential(std::move(credential)), m_scopes(scopesBegin, scopesEnd) - { - } - - std::unique_ptr Clone() const override - { - return std::make_unique(m_credential, m_scopes); - } - - std::unique_ptr Send( - Context const& context, - Http::Request& request, - Http::NextHttpPolicy policy) const override; - }; - }} // namespace Azure::Core diff --git a/sdk/core/azure-core/inc/azure/core/http/policy.hpp b/sdk/core/azure-core/inc/azure/core/http/policy.hpp index 668c6bb7d..73f949a0f 100644 --- a/sdk/core/azure-core/inc/azure/core/http/policy.hpp +++ b/sdk/core/azure-core/inc/azure/core/http/policy.hpp @@ -9,6 +9,7 @@ #pragma once #include "azure/core/context.hpp" +#include "azure/core/credentials.hpp" #include "azure/core/http/http.hpp" #include "azure/core/http/transport.hpp" #include "azure/core/logging/logging.hpp" @@ -303,6 +304,78 @@ namespace Azure { namespace Core { namespace Http { NextHttpPolicy nextHttpPolicy) const override; }; + /** + * @brief Bearer Token authentication policy. + */ + class BearerTokenAuthenticationPolicy : public HttpPolicy { + private: + std::shared_ptr const m_credential; + std::vector m_scopes; + + mutable AccessToken m_accessToken; + mutable std::mutex m_accessTokenMutex; + + BearerTokenAuthenticationPolicy(BearerTokenAuthenticationPolicy const&) = delete; + void operator=(BearerTokenAuthenticationPolicy const&) = delete; + + public: + /** + * @brief Construct a Bearer Token authentication policy with single authentication scope. + * + * @param credential A #TokenCredential to use with this policy. + * @param scope Authentication scope. + */ + explicit BearerTokenAuthenticationPolicy( + std::shared_ptr credential, + std::string scope) + : m_credential(std::move(credential)) + { + m_scopes.emplace_back(std::move(scope)); + } + + /** + * @brief Construct a Bearer Token authentication policy with multiple authentication scopes. + * + * @param credential A #TokenCredential to use with this policy. + * @param scopes A vector of authentication scopes. + */ + explicit BearerTokenAuthenticationPolicy( + std::shared_ptr credential, + std::vector scopes) + : m_credential(std::move(credential)), m_scopes(std::move(scopes)) + { + } + + /** + * @brief Construct a Bearer Token authentication policy with multiple authentication scopes. + * + * @tparam A type of scopes sequence iterator. + * + * @param credential A #TokenCredential to use with this policy. + * @param scopesBegin An iterator pointing to begin of the sequence of scopes to use. + * @param scopesEnd An iterator pointing to an element after the last element in sequence of + * scopes to use. + */ + template + explicit BearerTokenAuthenticationPolicy( + std::shared_ptr credential, + ScopesIterator const& scopesBegin, + ScopesIterator const& scopesEnd) + : m_credential(std::move(credential)), m_scopes(scopesBegin, scopesEnd) + { + } + + std::unique_ptr Clone() const override + { + return std::make_unique(m_credential, m_scopes); + } + + std::unique_ptr Send( + Context const& context, + Request& request, + NextHttpPolicy policy) const override; + }; + /** * @brief Logs every HTTP request. * diff --git a/sdk/core/azure-core/src/credentials.cpp b/sdk/core/azure-core/src/http/bearer_token_authentication_policy.cpp similarity index 67% rename from sdk/core/azure-core/src/credentials.cpp rename to sdk/core/azure-core/src/http/bearer_token_authentication_policy.cpp index 75575238b..30be96a2c 100644 --- a/sdk/core/azure-core/src/credentials.cpp +++ b/sdk/core/azure-core/src/http/bearer_token_authentication_policy.cpp @@ -1,14 +1,15 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT -#include +#include -using namespace Azure::Core; +using Azure::Core::Context; +using namespace Azure::Core::Http; -std::unique_ptr BearerTokenAuthenticationPolicy::Send( +std::unique_ptr BearerTokenAuthenticationPolicy::Send( Context const& context, - Http::Request& request, - Http::NextHttpPolicy policy) const + Request& request, + NextHttpPolicy policy) const { { std::lock_guard lock(m_accessTokenMutex); diff --git a/sdk/storage/azure-storage-blobs/src/blob_batch_client.cpp b/sdk/storage/azure-storage-blobs/src/blob_batch_client.cpp index 442aaf48c..fb14260c8 100644 --- a/sdk/storage/azure-storage-blobs/src/blob_batch_client.cpp +++ b/sdk/storage/azure-storage-blobs/src/blob_batch_client.cpp @@ -7,7 +7,6 @@ #include #include -#include "azure/core/credentials.hpp" #include "azure/core/http/policy.hpp" #include "azure/storage/blobs/version.hpp" #include "azure/storage/common/constants.hpp" @@ -145,7 +144,7 @@ namespace Azure { namespace Storage { namespace Blobs { policies.emplace_back(p->Clone()); } policies.emplace_back(std::make_unique()); - policies.emplace_back(std::make_unique( + policies.emplace_back(std::make_unique( credential, Storage::Details::StorageScope)); policies.emplace_back( std::make_unique(options.TransportPolicyOptions)); @@ -161,7 +160,7 @@ namespace Azure { namespace Storage { namespace Blobs { policies.emplace_back(p->Clone()); } policies.emplace_back(std::make_unique()); - policies.emplace_back(std::make_unique( + policies.emplace_back(std::make_unique( credential, Storage::Details::StorageScope)); policies.emplace_back(std::make_unique()); m_subRequestPipeline = std::make_shared(policies); diff --git a/sdk/storage/azure-storage-blobs/src/blob_client.cpp b/sdk/storage/azure-storage-blobs/src/blob_client.cpp index 74c4898cf..2ae7298de 100644 --- a/sdk/storage/azure-storage-blobs/src/blob_client.cpp +++ b/sdk/storage/azure-storage-blobs/src/blob_client.cpp @@ -3,7 +3,6 @@ #include "azure/storage/blobs/blob_client.hpp" -#include "azure/core/credentials.hpp" #include "azure/core/http/policy.hpp" #include "azure/storage/blobs/append_blob_client.hpp" #include "azure/storage/blobs/block_blob_client.hpp" @@ -88,7 +87,7 @@ namespace Azure { namespace Storage { namespace Blobs { policies.emplace_back(p->Clone()); } policies.emplace_back(std::make_unique()); - policies.emplace_back(std::make_unique( + policies.emplace_back(std::make_unique( credential, Storage::Details::StorageScope)); policies.emplace_back( std::make_unique(options.TransportPolicyOptions)); diff --git a/sdk/storage/azure-storage-blobs/src/blob_container_client.cpp b/sdk/storage/azure-storage-blobs/src/blob_container_client.cpp index 8219d3278..b4e9cf643 100644 --- a/sdk/storage/azure-storage-blobs/src/blob_container_client.cpp +++ b/sdk/storage/azure-storage-blobs/src/blob_container_client.cpp @@ -3,7 +3,6 @@ #include "azure/storage/blobs/blob_container_client.hpp" -#include "azure/core/credentials.hpp" #include "azure/core/http/policy.hpp" #include "azure/storage/blobs/append_blob_client.hpp" #include "azure/storage/blobs/block_blob_client.hpp" @@ -84,7 +83,7 @@ namespace Azure { namespace Storage { namespace Blobs { policies.emplace_back(p->Clone()); } policies.emplace_back(std::make_unique()); - policies.emplace_back(std::make_unique( + policies.emplace_back(std::make_unique( credential, Storage::Details::StorageScope)); policies.emplace_back( std::make_unique(options.TransportPolicyOptions)); diff --git a/sdk/storage/azure-storage-blobs/src/blob_service_client.cpp b/sdk/storage/azure-storage-blobs/src/blob_service_client.cpp index f1cff7019..ba21b5009 100644 --- a/sdk/storage/azure-storage-blobs/src/blob_service_client.cpp +++ b/sdk/storage/azure-storage-blobs/src/blob_service_client.cpp @@ -3,7 +3,6 @@ #include "azure/storage/blobs/blob_service_client.hpp" -#include "azure/core/credentials.hpp" #include "azure/core/http/policy.hpp" #include "azure/storage/blobs/version.hpp" #include "azure/storage/common/constants.hpp" @@ -78,7 +77,7 @@ namespace Azure { namespace Storage { namespace Blobs { policies.emplace_back(p->Clone()); } policies.emplace_back(std::make_unique()); - policies.emplace_back(std::make_unique( + policies.emplace_back(std::make_unique( credential, Storage::Details::StorageScope)); policies.emplace_back( std::make_unique(options.TransportPolicyOptions)); diff --git a/sdk/storage/azure-storage-files-datalake/src/datalake_directory_client.cpp b/sdk/storage/azure-storage-files-datalake/src/datalake_directory_client.cpp index ad490ec8d..dd199108d 100644 --- a/sdk/storage/azure-storage-files-datalake/src/datalake_directory_client.cpp +++ b/sdk/storage/azure-storage-files-datalake/src/datalake_directory_client.cpp @@ -3,7 +3,6 @@ #include "azure/storage/files/datalake/datalake_directory_client.hpp" -#include "azure/core/credentials.hpp" #include "azure/core/http/policy.hpp" #include "azure/storage/common/constants.hpp" #include "azure/storage/common/crypt.hpp" @@ -95,7 +94,7 @@ namespace Azure { namespace Storage { namespace Files { namespace DataLake { policies.emplace_back(p->Clone()); } policies.emplace_back(std::make_unique()); - policies.emplace_back(std::make_unique( + policies.emplace_back(std::make_unique( credential, Azure::Storage::Details::StorageScope)); policies.emplace_back( std::make_unique(options.TransportPolicyOptions)); diff --git a/sdk/storage/azure-storage-files-datalake/src/datalake_file_client.cpp b/sdk/storage/azure-storage-files-datalake/src/datalake_file_client.cpp index 86cbcc3c3..a5384ef5b 100644 --- a/sdk/storage/azure-storage-files-datalake/src/datalake_file_client.cpp +++ b/sdk/storage/azure-storage-files-datalake/src/datalake_file_client.cpp @@ -3,7 +3,6 @@ #include "azure/storage/files/datalake/datalake_file_client.hpp" -#include "azure/core/credentials.hpp" #include "azure/core/http/policy.hpp" #include "azure/storage/common/constants.hpp" #include "azure/storage/common/crypt.hpp" @@ -175,7 +174,7 @@ namespace Azure { namespace Storage { namespace Files { namespace DataLake { } policies.emplace_back(std::make_unique()); - policies.emplace_back(std::make_unique( + policies.emplace_back(std::make_unique( credential, Azure::Storage::Details::StorageScope)); policies.emplace_back( std::make_unique(options.TransportPolicyOptions)); diff --git a/sdk/storage/azure-storage-files-datalake/src/datalake_file_system_client.cpp b/sdk/storage/azure-storage-files-datalake/src/datalake_file_system_client.cpp index 921b76faa..307771c59 100644 --- a/sdk/storage/azure-storage-files-datalake/src/datalake_file_system_client.cpp +++ b/sdk/storage/azure-storage-files-datalake/src/datalake_file_system_client.cpp @@ -3,7 +3,6 @@ #include "azure/storage/files/datalake/datalake_file_system_client.hpp" -#include "azure/core/credentials.hpp" #include "azure/core/http/policy.hpp" #include "azure/storage/blobs/protocol/blob_rest_client.hpp" #include "azure/storage/common/constants.hpp" @@ -121,7 +120,7 @@ namespace Azure { namespace Storage { namespace Files { namespace DataLake { } policies.emplace_back(std::make_unique()); - policies.emplace_back(std::make_unique( + policies.emplace_back(std::make_unique( credential, Azure::Storage::Details::StorageScope)); policies.emplace_back( std::make_unique(options.TransportPolicyOptions)); diff --git a/sdk/storage/azure-storage-files-datalake/src/datalake_path_client.cpp b/sdk/storage/azure-storage-files-datalake/src/datalake_path_client.cpp index 31bd26bf4..0b1b4635f 100644 --- a/sdk/storage/azure-storage-files-datalake/src/datalake_path_client.cpp +++ b/sdk/storage/azure-storage-files-datalake/src/datalake_path_client.cpp @@ -3,7 +3,6 @@ #include "azure/storage/files/datalake/datalake_path_client.hpp" -#include "azure/core/credentials.hpp" #include "azure/core/http/policy.hpp" #include "azure/storage/common/constants.hpp" #include "azure/storage/common/crypt.hpp" @@ -159,7 +158,7 @@ namespace Azure { namespace Storage { namespace Files { namespace DataLake { } policies.emplace_back(std::make_unique()); - policies.emplace_back(std::make_unique( + policies.emplace_back(std::make_unique( credential, Azure::Storage::Details::StorageScope)); policies.emplace_back( std::make_unique(options.TransportPolicyOptions)); diff --git a/sdk/storage/azure-storage-files-datalake/src/datalake_service_client.cpp b/sdk/storage/azure-storage-files-datalake/src/datalake_service_client.cpp index 5c4f6b017..572526bb7 100644 --- a/sdk/storage/azure-storage-files-datalake/src/datalake_service_client.cpp +++ b/sdk/storage/azure-storage-files-datalake/src/datalake_service_client.cpp @@ -3,7 +3,6 @@ #include "azure/storage/files/datalake/datalake_service_client.hpp" -#include "azure/core/credentials.hpp" #include "azure/core/http/policy.hpp" #include "azure/storage/blobs/protocol/blob_rest_client.hpp" #include "azure/storage/common/constants.hpp" @@ -127,7 +126,7 @@ namespace Azure { namespace Storage { namespace Files { namespace DataLake { policies.emplace_back(p->Clone()); } policies.emplace_back(std::make_unique()); - policies.emplace_back(std::make_unique( + policies.emplace_back(std::make_unique( credential, Azure::Storage::Details::StorageScope)); policies.emplace_back( std::make_unique(options.TransportPolicyOptions));