-
Notifications
You must be signed in to change notification settings - Fork 219
/
Copy pathITokenAcquisition.cs
278 lines (265 loc) · 18.8 KB
/
ITokenAcquisition.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Identity.Client;
namespace Microsoft.Identity.Web
{
/// <summary>
/// Interface for the token acquisition service (encapsulating MSAL.NET).
/// </summary>
public interface ITokenAcquisition
{
/// <summary>
/// Typically used from an ASP.NET Core web app or web API controller. This method gets an access token
/// for a downstream API on behalf of the user account for which the claims are provided in the <see cref="HttpContext.User"/>
/// member of the controller's <see cref="HttpContext"/> parameter.
/// </summary>
/// <param name="scopes">Scopes to request for the downstream API to call.</param>
/// <param name="tenantId">Enables to override the tenant/account for the same identity. This is useful in the
/// cases where a given account is guest in other tenants, and you want to acquire tokens for a specific tenant.</param>
/// <param name="userFlow">Azure AD B2C UserFlow to target.</param>
/// <param name="user">Optional claims principal representing the user. If not provided, will use the signed-in
/// user (in a web app), or the user for which the token was received (in a web API)
/// cases where a given account is guest in other tenants, and you want to acquire tokens for a specific tenant, like where the user is a guest in.</param>
/// <param name="tokenAcquisitionOptions">Options passed-in to create the token acquisition object which calls into MSAL .NET.</param>
/// <returns>An access token to call on behalf of the user, the downstream API characterized by its scopes.</returns>
Task<string> GetAccessTokenForUserAsync(
IEnumerable<string> scopes,
string? tenantId = null,
string? userFlow = null,
ClaimsPrincipal? user = null,
TokenAcquisitionOptions? tokenAcquisitionOptions = null)
{
return GetAccessTokenForUserAsync(
scopes,
null,
tenantId,
userFlow,
user,
tokenAcquisitionOptions);
}
/// <summary>
/// Typically used from an ASP.NET Core web app or web API controller. This method gets an access token
/// for a downstream API on behalf of the user account for which the claims are provided in the <see cref="HttpContext.User"/>
/// member of the controller's <see cref="HttpContext"/> parameter.
/// </summary>
/// <param name="scopes">Scopes to request for the downstream API to call.</param>
/// <param name="authenticationScheme">Authentication scheme. If null, will use OpenIdConnectDefault.AuthenticationScheme
/// if called from a web app, and JwtBearerDefault.AuthenticationScheme if called from a web API.</param>
/// <param name="tenantId">Enables to override the tenant/account for the same identity. This is useful in the
/// cases where a given account is guest in other tenants, and you want to acquire tokens for a specific tenant.</param>
/// <param name="userFlow">Azure AD B2C UserFlow to target.</param>
/// <param name="user">Optional claims principal representing the user. If not provided, will use the signed-in
/// user (in a web app), or the user for which the token was received (in a web API)
/// cases where a given account is guest in other tenants, and you want to acquire tokens for a specific tenant, like where the user is a guest in.</param>
/// <param name="tokenAcquisitionOptions">Options passed-in to create the token acquisition object which calls into MSAL .NET.</param>
/// <returns>An access token to call on behalf of the user, the downstream API characterized by its scopes.</returns>
Task<string> GetAccessTokenForUserAsync(
IEnumerable<string> scopes,
string? authenticationScheme,
string? tenantId = null,
string? userFlow = null,
ClaimsPrincipal? user = null,
TokenAcquisitionOptions? tokenAcquisitionOptions = null);
/// <summary>
/// Typically used from an ASP.NET Core web app or web API controller. This method gets an access token
/// for a downstream API on behalf of the user account for which the claims are provided in the <see cref="HttpContext.User"/>
/// member of the controller's <see cref="HttpContext"/> parameter.
/// </summary>
/// <param name="scopes">Scopes to request for the downstream API to call.</param>
/// <param name="tenantId">Enables to override the tenant/account for the same identity. This is useful in the
/// cases where a given account is a guest in other tenants, and you want to acquire tokens for a specific tenant.</param>
/// <param name="userFlow">Azure AD B2C UserFlow to target.</param>
/// <param name="user">Optional claims principal representing the user. If not provided, will use the signed-in
/// user (in a web app), or the user for which the token was received (in a web API)
/// cases where a given account is a guest in other tenants, and you want to acquire tokens for a specific tenant, like where the user is a guest in.</param>
/// <param name="tokenAcquisitionOptions">Options passed-in to create the token acquisition object which calls into MSAL .NET.</param>
/// <returns>An <see cref="AuthenticationResult"/> to call on behalf of the user, the downstream API characterized by its scopes.</returns>
Task<AuthenticationResult> GetAuthenticationResultForUserAsync(
IEnumerable<string> scopes,
string? tenantId = null,
string? userFlow = null,
ClaimsPrincipal? user = null,
TokenAcquisitionOptions? tokenAcquisitionOptions = null)
{
return GetAuthenticationResultForUserAsync(
scopes,
null,
tenantId,
userFlow,
user,
tokenAcquisitionOptions);
}
/// <summary>
/// Typically used from an ASP.NET Core web app or web API controller. This method gets an access token
/// for a downstream API on behalf of the user account for which the claims are provided in the <see cref="HttpContext.User"/>
/// member of the controller's <see cref="HttpContext"/> parameter.
/// </summary>
/// <param name="scopes">Scopes to request for the downstream API to call.</param>
/// <param name="authenticationScheme">Authentication scheme. If null, will use OpenIdConnectDefault.AuthenticationScheme
/// if called from a web app, and JwtBearerDefault.AuthenticationScheme if called from a web APIs.</param>
/// <param name="tenantId">Enables to override the tenant/account for the same identity. This is useful in the
/// cases where a given account is a guest in other tenants, and you want to acquire tokens for a specific tenant.</param>
/// <param name="userFlow">Azure AD B2C UserFlow to target.</param>
/// <param name="user">Optional claims principal representing the user. If not provided, will use the signed-in
/// user (in a web app), or the user for which the token was received (in a web API)
/// cases where a given account is a guest in other tenants, and you want to acquire tokens for a specific tenant, like where the user is a guest in.</param>
/// <param name="tokenAcquisitionOptions">Options passed-in to create the token acquisition object which calls into MSAL .NET.</param>
/// <returns>An <see cref="AuthenticationResult"/> to call on behalf of the user, the downstream API characterized by its scopes.</returns>
Task<AuthenticationResult> GetAuthenticationResultForUserAsync(
IEnumerable<string> scopes,
string? authenticationScheme,
string? tenantId = null,
string? userFlow = null,
ClaimsPrincipal? user = null,
TokenAcquisitionOptions? tokenAcquisitionOptions = null);
/// <summary>
/// Acquires a token from the authority configured in the app, for the confidential client itself (not on behalf of a user)
/// using the client credentials flow. See https://aka.ms/msal-net-client-credentials.
/// </summary>
/// <param name="scope">The scope requested to access a protected API. For this flow (client credentials), the scope
/// should be of the form "{ResourceIdUri/.default}" for instance <c>https://management.azure.net/.default</c> or, for Microsoft
/// Graph, <c>https://graph.microsoft.com/.default</c> as the requested scopes are defined statically with the application registration
/// in the portal, cannot be overridden in the application, as you can request a token for only one resource at a time (use
/// several calls to get tokens for other resources).</param>
/// <param name="tenant">Enables overriding of the tenant/account for the same identity. This is useful in the
/// cases where a given account is a guest in other tenants, and you want to acquire tokens for a specific tenant.</param>
/// <param name="tokenAcquisitionOptions">Options passed-in to create the token acquisition object which calls into MSAL .NET.</param>
/// <returns>An access token for the app itself, based on its scopes.</returns>
Task<string> GetAccessTokenForAppAsync(
string scope,
string? tenant = null,
TokenAcquisitionOptions? tokenAcquisitionOptions = null)
{
return GetAccessTokenForAppAsync(
scope,
null,
tenant,
tokenAcquisitionOptions);
}
/// <summary>
/// Acquires a token from the authority configured in the app, for the confidential client itself (not on behalf of a user)
/// using the client credentials flow. See https://aka.ms/msal-net-client-credentials.
/// </summary>
/// <param name="scope">The scope requested to access a protected API. For this flow (client credentials), the scope
/// should be of the form "{ResourceIdUri/.default}" for instance <c>https://management.azure.net/.default</c> or, for Microsoft
/// Graph, <c>https://graph.microsoft.com/.default</c> as the requested scopes are defined statically with the application registration
/// in the portal, cannot be overridden in the application, as you can request a token for only one resource at a time (use
/// several calls to get tokens for other resources).</param>
/// <param name="authenticationScheme">Authentication scheme. If null, will use OpenIdConnectDefault.AuthenticationScheme
/// if called from a web app, and JwtBearerDefault.AuthenticationScheme if called from a web API.</param>
/// <param name="tenant">Enables overriding of the tenant/account for the same identity. This is useful in the
/// cases where a given account is a guest in other tenants, and you want to acquire tokens for a specific tenant.</param>
/// <param name="tokenAcquisitionOptions">Options passed-in to create the token acquisition object which calls into MSAL .NET.</param>
/// <returns>An access token for the app itself, based on its scopes.</returns>
Task<string> GetAccessTokenForAppAsync(
string scope,
string? authenticationScheme,
string? tenant = null,
TokenAcquisitionOptions? tokenAcquisitionOptions = null);
/// <summary>
/// Acquires an authentication result from the authority configured in the app, for the confidential client itself (not on behalf of a user)
/// using the client credentials flow. See https://aka.ms/msal-net-client-credentials.
/// </summary>
/// <param name="scope">The scope requested to access a protected API. For this flow (client credentials), the scope
/// should be of the form "{ResourceIdUri/.default}" for instance <c>https://management.azure.net/.default</c> or, for Microsoft
/// Graph, <c>https://graph.microsoft.com/.default</c> as the requested scopes are defined statically with the application registration
/// in the portal, and cannot be overridden in the application, as you can request a token for only one resource at a time (use
/// several calls to get tokens for other resources).</param>
/// <param name="tenant">Enables overriding of the tenant/account for the same identity. This is useful
/// for multi tenant apps or daemons.</param>
/// <param name="tokenAcquisitionOptions">Options passed-in to create the token acquisition object which calls into MSAL .NET.</param>
/// <returns>An authentication result for the app itself, based on its scopes.</returns>
Task<AuthenticationResult> GetAuthenticationResultForAppAsync(
string scope,
string? tenant = null,
TokenAcquisitionOptions? tokenAcquisitionOptions = null)
{
return GetAuthenticationResultForAppAsync(
scope,
null,
tenant,
tokenAcquisitionOptions);
}
/// <summary>
/// Acquires an authentication result from the authority configured in the app, for the confidential client itself (not on behalf of a user)
/// using the client credentials flow. See https://aka.ms/msal-net-client-credentials.
/// </summary>
/// <param name="scope">The scope requested to access a protected API. For this flow (client credentials), the scope
/// should be of the form "{ResourceIdUri/.default}" for instance <c>https://management.azure.net/.default</c> or, for Microsoft
/// Graph, <c>https://graph.microsoft.com/.default</c> as the requested scopes are defined statically with the application registration
/// in the portal, and cannot be overridden in the application, as you can request a token for only one resource at a time (use
/// several calls to get tokens for other resources).</param>
/// <param name="authenticationScheme">Authentication scheme. If null, will use OpenIdConnectDefault.AuthenticationScheme
/// if called from a web app, and JwtBearerDefault.AuthenticationScheme if called from a web API.</param>
/// <param name="tenant">Enables overriding of the tenant/account for the same identity. This is useful
/// for multi tenant apps or daemons.</param>
/// <param name="tokenAcquisitionOptions">Options passed-in to create the token acquisition object which calls into MSAL .NET.</param>
/// <returns>An authentication result for the app itself, based on its scopes.</returns>
Task<AuthenticationResult> GetAuthenticationResultForAppAsync(
string scope,
string? authenticationScheme,
string? tenant = null,
TokenAcquisitionOptions? tokenAcquisitionOptions = null);
/// <summary>
/// Used in web APIs (which therefore cannot have an interaction with the user).
/// Replies to the client through the HttpResponse by sending a 403 (forbidden) and populating wwwAuthenticateHeaders so that
/// the client can trigger an interaction with the user so the user can consent to more scopes.
/// </summary>
/// <param name="scopes">Scopes to consent to.</param>
/// <param name="msalServiceException"><see cref="MsalUiRequiredException"/> triggering the challenge.</param>
/// <param name="httpResponse">The <see cref="HttpResponse"/> to update.</param>
void ReplyForbiddenWithWwwAuthenticateHeader(
IEnumerable<string> scopes,
MsalUiRequiredException msalServiceException,
HttpResponse? httpResponse = null)
{
ReplyForbiddenWithWwwAuthenticateHeader(
scopes,
msalServiceException,
null,
httpResponse);
}
/// <summary>
/// Used in web APIs (which therefore cannot have an interaction with the user).
/// Replies to the client through the HttpResponse by sending a 403 (forbidden) and populating wwwAuthenticateHeaders so that
/// the client can trigger an interaction with the user so the user can consent to more scopes.
/// </summary>
/// <param name="scopes">Scopes to consent to.</param>
/// <param name="msalServiceException"><see cref="MsalUiRequiredException"/> triggering the challenge.</param>
/// <param name="authenticationScheme">Authentication scheme. If null, will use OpenIdConnectDefault.AuthenticationScheme
/// if called from a web app, and JwtBearerDefault.AuthenticationScheme if called from a web API.</param>
/// <param name="httpResponse">The <see cref="HttpResponse"/> to update.</param>
void ReplyForbiddenWithWwwAuthenticateHeader(
IEnumerable<string> scopes,
MsalUiRequiredException msalServiceException,
string? authenticationScheme,
HttpResponse? httpResponse = null);
/// <summary>
/// Get the effective authentication scheme based on the context.
/// </summary>
/// <param name="authenticationScheme">Proposed authentication scheme.</param>
/// <returns>Effective authenticationScheme which is the authentication scheme
/// if it's not null, or otherwise OpenIdConnectDefault.AuthenticationScheme
/// if called from a web app, and JwtBearerDefault.AuthenticationScheme if called from a web API.</returns>
string GetEffectiveAuthenticationScheme(string? authenticationScheme);
/// <summary>
/// Used in web APIs (which therefore cannot have an interaction with the user).
/// Replies to the client through the HttpResponse by sending a 403 (forbidden) and populating wwwAuthenticateHeaders so that
/// the client can trigger an interaction with the user so the user can consent to more scopes.
/// </summary>
/// <param name="scopes">Scopes to consent to.</param>
/// <param name="msalServiceException"><see cref="MsalUiRequiredException"/> triggering the challenge.</param>
/// <param name="httpResponse">The <see cref="HttpResponse"/> to update.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
[Obsolete(IDWebErrorMessage.ReplyForbiddenWithWwwAuthenticateHeaderAsyncIsObsolete, false)]
Task ReplyForbiddenWithWwwAuthenticateHeaderAsync(
IEnumerable<string> scopes,
MsalUiRequiredException msalServiceException,
HttpResponse? httpResponse = null);
}
}