azure-identity
Loading...
Searching...
No Matches
managed_identity_credential.hpp
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
9#pragma once
10
11#include <azure/core/credentials/credentials.hpp>
12#include <azure/core/credentials/token_credential_options.hpp>
13#include <azure/core/resource_identifier.hpp>
14
15#include <memory>
16#include <string>
17
18#if defined(_azure_TESTING_BUILD)
19// Define the class used from tests
20namespace Azure { namespace Identity { namespace Test {
21 class ManagedIdentityId_Basic_Test;
22 class ManagedIdentityId_Invalid_Test;
23}}} // namespace Azure::Identity::Test
24#endif
25
26namespace Azure { namespace Identity {
27 namespace _detail {
28 class ManagedIdentitySource;
29
38 enum class ManagedIdentityIdKind
39 {
40 SystemAssigned,
41 ClientId,
42 ObjectId,
43 ResourceId,
44 };
45 } // namespace _detail
46
53 class ManagedIdentityId final {
54 friend class ManagedIdentityCredential;
55#if defined(_azure_TESTING_BUILD)
56 // make tests classes friends to validate ManagedIdentityId behavior
57 friend class Azure::Identity::Test::ManagedIdentityId_Basic_Test;
58 friend class Azure::Identity::Test::ManagedIdentityId_Invalid_Test;
59#endif
60
61 private:
62 _detail::ManagedIdentityIdKind m_idKind;
63 std::string m_id;
64
65 public:
71 explicit ManagedIdentityId() : m_idKind(_detail::ManagedIdentityIdKind::SystemAssigned) {}
72
78
85 {
86 return ManagedIdentityId(_detail::ManagedIdentityIdKind::ClientId, std::move(id));
87 }
88
95 {
96 return ManagedIdentityId(_detail::ManagedIdentityIdKind::ObjectId, std::move(id));
97 }
98
105 static ManagedIdentityId FromUserAssignedResourceId(Azure::Core::ResourceIdentifier id)
106 {
107 return ManagedIdentityId(_detail::ManagedIdentityIdKind::ResourceId, id.ToString());
108 }
109
110 private:
123 explicit ManagedIdentityId(_detail::ManagedIdentityIdKind idKind, std::string id)
124 : m_idKind(idKind), m_id(id)
125 {
126 if (idKind == _detail::ManagedIdentityIdKind::SystemAssigned && !id.empty())
127 {
128 throw std::invalid_argument(
129 "There is no need to provide an ID (such as client, object, or resource ID) if you are "
130 "using system-assigned managed identity.");
131 }
132
133 if (id.empty()
134 && (idKind == _detail::ManagedIdentityIdKind::ClientId
135 || idKind == _detail::ManagedIdentityIdKind::ObjectId
136 || idKind == _detail::ManagedIdentityIdKind::ResourceId))
137 {
138 throw std::invalid_argument(
139 "Provide the value of the client, object, or resource ID corresponding to the "
140 "ManagedIdentityIdKind specified. The provided ID should not be empty in the case of "
141 "user-assigned managed identity.");
142 }
143 }
144
150 std::string const& GetId() const { return m_id; }
151
156 _detail::ManagedIdentityIdKind GetManagedIdentityIdKind() const { return m_idKind; }
157 };
158
163 struct ManagedIdentityCredentialOptions final : public Core::Credentials::TokenCredentialOptions
164 {
170 };
171
179 class ManagedIdentityCredential final : public Core::Credentials::TokenCredential {
180 private:
181 std::unique_ptr<_detail::ManagedIdentitySource> m_managedIdentitySource;
182
183 public:
189
197 std::string const& clientId = std::string(),
198 Azure::Core::Credentials::TokenCredentialOptions const& options
199 = Azure::Core::Credentials::TokenCredentialOptions());
200
208
215 Azure::Core::Credentials::TokenCredentialOptions const& options);
216
227 Core::Credentials::AccessToken GetToken(
228 Core::Credentials::TokenRequestContext const& tokenRequestContext,
229 Core::Context const& context) const override;
230 };
231
232}} // namespace Azure::Identity
Attempts authentication using a managed identity that has been assigned to the deployment environment...
Definition managed_identity_credential.hpp:179
Core::Credentials::AccessToken GetToken(Core::Credentials::TokenRequestContext const &tokenRequestContext, Core::Context const &context) const override
Gets an authentication token.
Definition managed_identity_credential.cpp:95
~ManagedIdentityCredential() override
Destructs TokenCredential.
The type of managed identity and its corresponding identifier.
Definition managed_identity_credential.hpp:53
ManagedIdentityId()
Constructs the type of managed identity.
Definition managed_identity_credential.hpp:71
static ManagedIdentityId SystemAssigned()
Create an instance of ManagedIdentityId for a system-assigned managed identity.
Definition managed_identity_credential.hpp:77
static ManagedIdentityId FromUserAssignedResourceId(Azure::Core::ResourceIdentifier id)
Create an instance of ManagedIdentityId for a user-assigned managed identity.
Definition managed_identity_credential.hpp:105
static ManagedIdentityId FromUserAssignedClientId(std::string id)
Create an instance of ManagedIdentityId for a user-assigned managed identity.
Definition managed_identity_credential.hpp:84
static ManagedIdentityId FromUserAssignedObjectId(std::string id)
Create an instance of ManagedIdentityId for a user-assigned managed identity.
Definition managed_identity_credential.hpp:94
Options for managed identity credential.
Definition managed_identity_credential.hpp:164
ManagedIdentityId IdentityId
Specifies the type of managed identity and its corresponding identifier, based on how it was configur...
Definition managed_identity_credential.hpp:169