-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
OperationSecurityScopeProcessor.cs
74 lines (64 loc) · 3.1 KB
/
OperationSecurityScopeProcessor.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
//-----------------------------------------------------------------------
// <copyright file="OperationSecurityScopeProcessor.cs" company="NSwag">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/RicoSuter/NSwag/blob/master/LICENSE.md</license>
// <author>Rico Suter, [email protected]</author>
//-----------------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NSwag.Generation.Processors.Contexts;
namespace NSwag.Generation.Processors.Security
{
/// <summary>Generates the OAuth2 security scopes for an operation by reflecting the AuthorizeAttribute attributes.</summary>
public class OperationSecurityScopeProcessor : IOperationProcessor
{
private readonly string _name;
/// <summary>Initializes a new instance of the <see cref="OperationSecurityScopeProcessor"/> class with 'Bearer' name.</summary>
public OperationSecurityScopeProcessor() : this("Bearer")
{
}
/// <summary>Initializes a new instance of the <see cref="OperationSecurityScopeProcessor"/> class.</summary>
/// <param name="name">The security definition name.</param>
public OperationSecurityScopeProcessor(string name)
{
_name = name;
}
/// <summary>Processes the specified method information.</summary>
/// <param name="context"></param>
/// <returns>true if the operation should be added to the Swagger specification.</returns>
public bool Process(OperationProcessorContext context)
{
if (context.OperationDescription.Operation.Security == null)
{
context.OperationDescription.Operation.Security = new List<OpenApiSecurityRequirement>();
}
var scopes = GetScopes(context.OperationDescription, context.MethodInfo);
context.OperationDescription.Operation.Security.Add(new OpenApiSecurityRequirement
{
{ _name, scopes }
});
return true;
}
/// <summary>Gets the security scopes for an operation.</summary>
/// <param name="operationDescription">The operation description.</param>
/// <param name="methodInfo">The method information.</param>
/// <returns>The scopes.</returns>
protected virtual IEnumerable<string> GetScopes(OpenApiOperationDescription operationDescription, MethodInfo methodInfo)
{
var allAttributes = methodInfo.GetCustomAttributes().Concat(
methodInfo.DeclaringType.GetTypeInfo().GetCustomAttributes());
var authorizeAttributes = allAttributes.Where(a => a.GetType().Name == "AuthorizeAttribute").ToList();
if (!authorizeAttributes.Any())
{
return Enumerable.Empty<string>();
}
return authorizeAttributes
.Select(a => (dynamic)a)
.Where(a => a.Roles != null)
.SelectMany(a => ((string)a.Roles).Split(','))
.Distinct();
}
}
}