azure-data-tables
Loading...
Searching...
No Matches
tenant_bearer_token_policy.hpp
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
6#include <azure/core/http/policies/policy.hpp>
7
8#include <mutex>
9#include <shared_mutex>
10
11namespace Azure { namespace Data { namespace Tables { namespace _detail { namespace Policies {
12
13 class TenantBearerTokenAuthenticationPolicy final
14 : public Core::Http::Policies::_internal::BearerTokenAuthenticationPolicy {
15 public:
23 explicit TenantBearerTokenAuthenticationPolicy(
24 std::shared_ptr<const Azure::Core::Credentials::TokenCredential> credential,
25 Azure::Core::Credentials::TokenRequestContext tokenRequestContext,
26 bool enableTenantDiscovery)
27 : BearerTokenAuthenticationPolicy(std::move(credential), tokenRequestContext),
28 m_scopes{tokenRequestContext.Scopes}, m_safeTenantId{tokenRequestContext.TenantId},
29 m_enableTenantDiscovery{enableTenantDiscovery}
30 {
31 }
32
33 ~TenantBearerTokenAuthenticationPolicy() override {}
34
35 std::unique_ptr<HttpPolicy> Clone() const override
36 {
37 return std::unique_ptr<HttpPolicy>(new TenantBearerTokenAuthenticationPolicy(*this));
38 }
39
40 private:
41 struct SafeTenantId
42 {
43 public:
44 explicit SafeTenantId(std::string tenantId) : m_tenantId{std::move(tenantId)} {}
45
46 SafeTenantId(const SafeTenantId& other) : m_tenantId{other.Get()} {}
47
48 std::string Get() const
49 {
50 std::shared_lock<std::shared_timed_mutex> lock(m_tenantIdMutex);
51 return m_tenantId;
52 }
53
54 void Set(const std::string& tenantId)
55 {
56 std::unique_lock<std::shared_timed_mutex> lock(m_tenantIdMutex);
57 m_tenantId = tenantId;
58 }
59
60 private:
61 std::string m_tenantId;
62 mutable std::shared_timed_mutex m_tenantIdMutex;
63 };
64
65 std::vector<std::string> m_scopes;
66 mutable SafeTenantId m_safeTenantId;
67 bool m_enableTenantDiscovery;
68
69 std::unique_ptr<Azure::Core::Http::RawResponse> AuthorizeAndSendRequest(
70 Azure::Core::Http::Request& request,
71 Azure::Core::Http::Policies::NextHttpPolicy& nextPolicy,
72 Azure::Core::Context const& context) const override;
73
74 bool AuthorizeRequestOnChallenge(
75 std::string const& challenge,
76 Azure::Core::Http ::Request& request,
77 Azure::Core::Context const& context) const override;
78 };
79
80}}}}} // namespace Azure::Data::Tables::_detail::Policies