-
Notifications
You must be signed in to change notification settings - Fork 219
/
Copy pathScopesRequiredHttpContextExtensions.cs
89 lines (78 loc) · 3.65 KB
/
ScopesRequiredHttpContextExtensions.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Claims;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
namespace Microsoft.Identity.Web.Resource
{
/// <summary>
/// Extension class providing the extension
/// methods for <see cref="HttpContent"/> that
/// can be used in web APIs to validate scopes in controller actions.
/// We recommend using instead the RequiredScope Attribute on the controller, the page or the action.
/// See https://aka.ms/ms-id-web/required-scope-attribute.
/// </summary>
public static class ScopesRequiredHttpContextExtensions
{
/// <summary>
/// When applied to an <see cref="HttpContext"/>, verifies that the user authenticated in the
/// web API has any of the accepted scopes.
/// If there is no authenticated user, the response is a 401 (Unauthenticated).
/// If the authenticated user does not have any of these <paramref name="acceptedScopes"/>, the
/// method updates the HTTP response providing a status code 403 (Forbidden)
/// and writes to the response body a message telling which scopes are expected in the token.
/// We recommend using instead the RequiredScope Attribute on the controller, the page or the action.
/// See https://aka.ms/ms-id-web/required-scope-attribute.
/// </summary>
/// <param name="context">HttpContext (from the controller).</param>
/// <param name="acceptedScopes">Scopes accepted by this web API.</param>
public static void VerifyUserHasAnyAcceptedScope(this HttpContext context, params string[] acceptedScopes)
{
_ = Throws.IfNull(acceptedScopes);
_ = Throws.IfNull(context);
IEnumerable<Claim> userClaims;
ClaimsPrincipal user;
// Need to lock due to https://learn.microsoft.com/aspnet/core/performance/performance-best-practices?#do-not-access-httpcontext-from-multiple-threads
lock (context)
{
user = context.User;
userClaims = user.Claims;
}
if (user == null || userClaims == null || !userClaims.Any())
{
lock (context)
{
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
}
throw new UnauthorizedAccessException(IDWebErrorMessage.UnauthenticatedUser);
}
else
{
// Attempt with Scp claim
Claim? scopeClaim = user.FindFirst(ClaimConstants.Scp);
// Fallback to Scope claim name
if (scopeClaim == null)
{
scopeClaim = user.FindFirst(ClaimConstants.Scope);
}
if (scopeClaim == null || !scopeClaim.Value.Split(' ').Intersect(acceptedScopes).Any())
{
string message = string.Format(CultureInfo.InvariantCulture, IDWebErrorMessage.MissingScopes, string.Join(",", acceptedScopes));
lock (context)
{
context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
_ = context.Response.WriteAsync(message);
_ = context.Response.CompleteAsync();
}
throw new UnauthorizedAccessException(message);
}
}
}
}
}