Repository navigation
Expand file tree
/
Copy pathDefaultAuthorizationService.cs
More file actions
80 lines (71 loc) · 2.72 KB
/
Copy pathDefaultAuthorizationService.cs
File metadata and controls
80 lines (71 loc) · 2.72 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
// 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.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNet.Authorization
{
public class DefaultAuthorizationService : IAuthorizationService
{
private readonly IList<IAuthorizationHandler> _handlers;
private readonly AuthorizationOptions _options;
private readonly ILogger _logger;
public DefaultAuthorizationService(IOptions<AuthorizationOptions> options, IEnumerable<IAuthorizationHandler> handlers, ILogger<DefaultAuthorizationService> logger)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
if (handlers == null)
{
throw new ArgumentNullException(nameof(handlers));
}
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}
_handlers = handlers.ToArray();
_options = options.Value;
_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 AuthorizationContext(requirements, user, resource);
foreach (var handler in _handlers)
{
await handler.HandleAsync(authContext);
}
if (authContext.HasSucceeded)
{
_logger.LogInformation(0, "Authorization was successful for user: {userName}.", user?.Identity?.Name);
return true;
}
else
{
_logger.LogInformation(1, "Authorization failed for user: {userName}.", user?.Identity?.Name);
return false;
}
}
public Task<bool> AuthorizeAsync(ClaimsPrincipal user, object resource, string policyName)
{
if (policyName == null)
{
throw new ArgumentNullException(nameof(policyName));
}
var policy = _options.GetPolicy(policyName);
if (policy == null)
{
throw new InvalidOperationException($"No policy found: {policyName}.");
}
return this.AuthorizeAsync(user, resource, policy);
}
}
}