Repository navigation
Expand file tree
/
Copy pathDefaultAuthorizationService.cs
More file actions
102 lines (91 loc) · 3.45 KB
/
Copy pathDefaultAuthorizationService.cs
File metadata and controls
102 lines (91 loc) · 3.45 KB
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Security.Principal;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.Authorization
{
public class DefaultAuthorizationService : IAuthorizationService
{
private readonly IAuthorizationPolicyProvider _policyProvider;
private readonly IList<IAuthorizationHandler> _handlers;
private readonly ILogger _logger;
public DefaultAuthorizationService(IAuthorizationPolicyProvider policyProvider, IEnumerable<IAuthorizationHandler> handlers, ILogger<DefaultAuthorizationService> logger)
{
if (policyProvider == null)
{
throw new ArgumentNullException(nameof(policyProvider));
}
if (handlers == null)
{
throw new ArgumentNullException(nameof(handlers));
}
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}
_handlers = handlers.ToArray();
_policyProvider = policyProvider;
_logger = logger;
}
public async Task<bool> AuthorizeAsync(ClaimsPrincipal user, object resource, IEnumerable<IAuthorizationRequirement> requirements)
{
if (requirements == null)
{
throw new ArgumentNullException(nameof(requirements));
}
var authContext = new AuthorizationHandlerContext(requirements, user, resource);
foreach (var handler in _handlers)
{
await handler.HandleAsync(authContext);
}
if (authContext.HasSucceeded)
{
_logger.UserAuthorizationSucceeded(GetUserNameForLogging(user));
return true;
}
else
{
_logger.UserAuthorizationFailed(GetUserNameForLogging(user));
return false;
}
}
private string GetUserNameForLogging(ClaimsPrincipal user)
{
var identity = user?.Identity;
if (identity != null)
{
var name = identity.Name;
if (name != null)
{
return name;
}
return GetClaimValue(identity, "sub")
?? GetClaimValue(identity, ClaimTypes.Name)
?? GetClaimValue(identity, ClaimTypes.NameIdentifier);
}
return null;
}
private static string GetClaimValue(IIdentity identity, string claimsType)
{
return (identity as ClaimsIdentity)?.FindFirst(claimsType)?.Value;
}
public async Task<bool> AuthorizeAsync(ClaimsPrincipal user, object resource, string policyName)
{
if (policyName == null)
{
throw new ArgumentNullException(nameof(policyName));
}
var policy = await _policyProvider.GetPolicyAsync(policyName);
if (policy == null)
{
throw new InvalidOperationException($"No policy found: {policyName}.");
}
return await this.AuthorizeAsync(user, resource, policy);
}
}
}