Move BearerTokenAuthenticationPolicy to Http namespace/policy.hpp (#1104)

This commit is contained in:
Anton Kolesnyk 2020-12-08 17:04:03 -08:00 committed by GitHub
parent 6ff9b7b605
commit 035ba8509c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 91 additions and 99 deletions

View File

@ -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)

View File

@ -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

View File

@ -9,7 +9,6 @@
#pragma once
#include <azure/core/context.hpp>
#include <azure/core/http/policy.hpp>
#include <chrono>
#include <memory>
@ -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<TokenCredential const> const m_credential;
std::vector<std::string> 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<TokenCredential const> 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<TokenCredential const> credential,
std::vector<std::string> 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 <typename ScopesIterator>
explicit BearerTokenAuthenticationPolicy(
std::shared_ptr<TokenCredential const> credential,
ScopesIterator const& scopesBegin,
ScopesIterator const& scopesEnd)
: m_credential(std::move(credential)), m_scopes(scopesBegin, scopesEnd)
{
}
std::unique_ptr<HttpPolicy> Clone() const override
{
return std::make_unique<BearerTokenAuthenticationPolicy>(m_credential, m_scopes);
}
std::unique_ptr<Http::RawResponse> Send(
Context const& context,
Http::Request& request,
Http::NextHttpPolicy policy) const override;
};
}} // namespace Azure::Core

View File

@ -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<TokenCredential const> const m_credential;
std::vector<std::string> 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<TokenCredential const> 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<TokenCredential const> credential,
std::vector<std::string> 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 <typename ScopesIterator>
explicit BearerTokenAuthenticationPolicy(
std::shared_ptr<TokenCredential const> credential,
ScopesIterator const& scopesBegin,
ScopesIterator const& scopesEnd)
: m_credential(std::move(credential)), m_scopes(scopesBegin, scopesEnd)
{
}
std::unique_ptr<HttpPolicy> Clone() const override
{
return std::make_unique<BearerTokenAuthenticationPolicy>(m_credential, m_scopes);
}
std::unique_ptr<RawResponse> Send(
Context const& context,
Request& request,
NextHttpPolicy policy) const override;
};
/**
* @brief Logs every HTTP request.
*

View File

@ -1,14 +1,15 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT
#include <azure/core/credentials.hpp>
#include <azure/core/http/policy.hpp>
using namespace Azure::Core;
using Azure::Core::Context;
using namespace Azure::Core::Http;
std::unique_ptr<Http::RawResponse> BearerTokenAuthenticationPolicy::Send(
std::unique_ptr<RawResponse> BearerTokenAuthenticationPolicy::Send(
Context const& context,
Http::Request& request,
Http::NextHttpPolicy policy) const
Request& request,
NextHttpPolicy policy) const
{
{
std::lock_guard<std::mutex> lock(m_accessTokenMutex);

View File

@ -7,7 +7,6 @@
#include <cstring>
#include <memory>
#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<Storage::Details::StoragePerRetryPolicy>());
policies.emplace_back(std::make_unique<Core::BearerTokenAuthenticationPolicy>(
policies.emplace_back(std::make_unique<Core::Http::BearerTokenAuthenticationPolicy>(
credential, Storage::Details::StorageScope));
policies.emplace_back(
std::make_unique<Azure::Core::Http::TransportPolicy>(options.TransportPolicyOptions));
@ -161,7 +160,7 @@ namespace Azure { namespace Storage { namespace Blobs {
policies.emplace_back(p->Clone());
}
policies.emplace_back(std::make_unique<Storage::Details::StoragePerRetryPolicy>());
policies.emplace_back(std::make_unique<Core::BearerTokenAuthenticationPolicy>(
policies.emplace_back(std::make_unique<Core::Http::BearerTokenAuthenticationPolicy>(
credential, Storage::Details::StorageScope));
policies.emplace_back(std::make_unique<NoopTransportPolicy>());
m_subRequestPipeline = std::make_shared<Azure::Core::Http::HttpPipeline>(policies);

View File

@ -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<Storage::Details::StoragePerRetryPolicy>());
policies.emplace_back(std::make_unique<Core::BearerTokenAuthenticationPolicy>(
policies.emplace_back(std::make_unique<Core::Http::BearerTokenAuthenticationPolicy>(
credential, Storage::Details::StorageScope));
policies.emplace_back(
std::make_unique<Azure::Core::Http::TransportPolicy>(options.TransportPolicyOptions));

View File

@ -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<Storage::Details::StoragePerRetryPolicy>());
policies.emplace_back(std::make_unique<Core::BearerTokenAuthenticationPolicy>(
policies.emplace_back(std::make_unique<Core::Http::BearerTokenAuthenticationPolicy>(
credential, Storage::Details::StorageScope));
policies.emplace_back(
std::make_unique<Azure::Core::Http::TransportPolicy>(options.TransportPolicyOptions));

View File

@ -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<Storage::Details::StoragePerRetryPolicy>());
policies.emplace_back(std::make_unique<Core::BearerTokenAuthenticationPolicy>(
policies.emplace_back(std::make_unique<Core::Http::BearerTokenAuthenticationPolicy>(
credential, Storage::Details::StorageScope));
policies.emplace_back(
std::make_unique<Azure::Core::Http::TransportPolicy>(options.TransportPolicyOptions));

View File

@ -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<Storage::Details::StoragePerRetryPolicy>());
policies.emplace_back(std::make_unique<Core::BearerTokenAuthenticationPolicy>(
policies.emplace_back(std::make_unique<Core::Http::BearerTokenAuthenticationPolicy>(
credential, Azure::Storage::Details::StorageScope));
policies.emplace_back(
std::make_unique<Azure::Core::Http::TransportPolicy>(options.TransportPolicyOptions));

View File

@ -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<Storage::Details::StoragePerRetryPolicy>());
policies.emplace_back(std::make_unique<Core::BearerTokenAuthenticationPolicy>(
policies.emplace_back(std::make_unique<Core::Http::BearerTokenAuthenticationPolicy>(
credential, Azure::Storage::Details::StorageScope));
policies.emplace_back(
std::make_unique<Azure::Core::Http::TransportPolicy>(options.TransportPolicyOptions));

View File

@ -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<Storage::Details::StoragePerRetryPolicy>());
policies.emplace_back(std::make_unique<Core::BearerTokenAuthenticationPolicy>(
policies.emplace_back(std::make_unique<Core::Http::BearerTokenAuthenticationPolicy>(
credential, Azure::Storage::Details::StorageScope));
policies.emplace_back(
std::make_unique<Azure::Core::Http::TransportPolicy>(options.TransportPolicyOptions));

View File

@ -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<Storage::Details::StoragePerRetryPolicy>());
policies.emplace_back(std::make_unique<Core::BearerTokenAuthenticationPolicy>(
policies.emplace_back(std::make_unique<Core::Http::BearerTokenAuthenticationPolicy>(
credential, Azure::Storage::Details::StorageScope));
policies.emplace_back(
std::make_unique<Azure::Core::Http::TransportPolicy>(options.TransportPolicyOptions));

View File

@ -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<Storage::Details::StoragePerRetryPolicy>());
policies.emplace_back(std::make_unique<Core::BearerTokenAuthenticationPolicy>(
policies.emplace_back(std::make_unique<Core::Http::BearerTokenAuthenticationPolicy>(
credential, Azure::Storage::Details::StorageScope));
policies.emplace_back(
std::make_unique<Azure::Core::Http::TransportPolicy>(options.TransportPolicyOptions));