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 OidcRequestUrlEnvVarName = "SYSTEM_OIDCREQUESTURI";
21 const std::string AadGlobalAuthority = "https://login.microsoftonline.com/";
22
23 class DefaultOptionValues final {
24 DefaultOptionValues() = delete;
25 ~DefaultOptionValues() = delete;
26
27 public:
28 static std::string GetAuthorityHost()
29 {
30 const std::string envAuthHost
31 = Core::_internal::Environment::GetVariable(AzureAuthorityHostEnvVarName);
32
33 return envAuthHost.empty() ? AadGlobalAuthority : envAuthHost;
34 }
35
36 static std::string GetTenantId()
37 {
38 return Core::_internal::Environment::GetVariable(AzureTenantIdEnvVarName);
39 }
40
41 static std::string GetClientId()
42 {
43 return Core::_internal::Environment::GetVariable(AzureClientIdEnvVarName);
44 }
45
46 static std::string GetFederatedTokenFile()
47 {
48 return Core::_internal::Environment::GetVariable(AzureFederatedTokenFileEnvVarName);
49 }
50
51 static std::string GetOidcRequestUrl()
52 {
53 return Core::_internal::Environment::GetVariable(OidcRequestUrlEnvVarName.c_str());
54 }
55 };
56
57 class ClientCredentialCore final {
58 std::vector<std::string> m_additionallyAllowedTenants;
59 Core::Url m_authorityHost;
60 std::string m_tenantId;
61
62 public:
63 explicit ClientCredentialCore(
64 std::string tenantId,
65 std::string const& authorityHost,
66 std::vector<std::string> additionallyAllowedTenants);
67
68 Core::Url GetRequestUrl(std::string const& tenantId) const;
69
70 std::string GetScopesString(
71 std::string const& tenantId,
72 decltype(Core::Credentials::TokenRequestContext::Scopes) const& scopes) const;
73
74 std::string const& GetTenantId() const { return m_tenantId; }
75
76 std::vector<std::string> const& GetAdditionallyAllowedTenants() const
77 {
78 return m_additionallyAllowedTenants;
79 }
80 };
81}}} // namespace Azure::Identity::_detail