azure-identity
Loading...
Searching...
No Matches
client_credential_core.hpp
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4#pragma once
5
6#include "azure/identity/dll_import_export.hpp"
7
8#include <azure/core/credentials/credentials.hpp>
9#include <azure/core/internal/environment.hpp>
10#include <azure/core/url.hpp>
11
12#include <string>
13#include <vector>
14
15namespace Azure { namespace Identity { namespace _detail {
16 constexpr auto AzureAuthorityHostEnvVarName = "AZURE_AUTHORITY_HOST";
17 constexpr auto AzureTenantIdEnvVarName = "AZURE_TENANT_ID";
18 constexpr auto AzureClientIdEnvVarName = "AZURE_CLIENT_ID";
19 constexpr auto AzureFederatedTokenFileEnvVarName = "AZURE_FEDERATED_TOKEN_FILE";
20 const std::string AadGlobalAuthority = "https://login.microsoftonline.com/";
21
22 class DefaultOptionValues final {
23 DefaultOptionValues() = delete;
24 ~DefaultOptionValues() = delete;
25
26 public:
27 static std::string GetAuthorityHost()
28 {
29 const std::string envAuthHost
30 = Core::_internal::Environment::GetVariable(AzureAuthorityHostEnvVarName);
31
32 return envAuthHost.empty() ? AadGlobalAuthority : envAuthHost;
33 }
34
35 static std::string GetTenantId()
36 {
37 return Core::_internal::Environment::GetVariable(AzureTenantIdEnvVarName);
38 }
39
40 static std::string GetClientId()
41 {
42 return Core::_internal::Environment::GetVariable(AzureClientIdEnvVarName);
43 }
44
45 static std::string GetFederatedTokenFile()
46 {
47 return Core::_internal::Environment::GetVariable(AzureFederatedTokenFileEnvVarName);
48 }
49 };
50
51 class ClientCredentialCore final {
52 std::vector<std::string> m_additionallyAllowedTenants;
53 Core::Url m_authorityHost;
54 std::string m_tenantId;
55
56 public:
57 explicit ClientCredentialCore(
58 std::string tenantId,
59 std::string const& authorityHost,
60 std::vector<std::string> additionallyAllowedTenants);
61
62 Core::Url GetRequestUrl(std::string const& tenantId) const;
63
64 std::string GetScopesString(
65 std::string const& tenantId,
66 decltype(Core::Credentials::TokenRequestContext::Scopes) const& scopes) const;
67
68 std::string const& GetTenantId() const { return m_tenantId; }
69
70 std::vector<std::string> const& GetAdditionallyAllowedTenants() const
71 {
72 return m_additionallyAllowedTenants;
73 }
74 };
75}}} // namespace Azure::Identity::_detail