-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathSecurityDefinitionAppender.cs
60 lines (53 loc) · 2.82 KB
/
SecurityDefinitionAppender.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
//-----------------------------------------------------------------------
// <copyright file="SecurityDefinitionAppender.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 NSwag.Generation.Processors.Contexts;
namespace NSwag.Generation.Processors.Security
{
/// <summary>Appends the OAuth2 security scheme to the document's security definitions.</summary>
public class SecurityDefinitionAppender : IDocumentProcessor
{
private readonly string _name;
private readonly IEnumerable<string> _scopeNames;
private readonly OpenApiSecurityScheme _swaggerSecurityScheme;
/// <summary>Initializes a new instance of the <see cref="SecurityDefinitionAppender" /> class where the security requirement must be manually added.</summary>
/// <param name="name">The name/key of the security scheme/definition.</param>
/// <param name="swaggerSecurityScheme">The Swagger security scheme.</param>
public SecurityDefinitionAppender(string name, OpenApiSecurityScheme swaggerSecurityScheme)
{
_name = name;
_swaggerSecurityScheme = swaggerSecurityScheme;
}
/// <summary>Initializes a new instance of the <see cref="SecurityDefinitionAppender" /> class.</summary>
/// <param name="name">The name/key of the security scheme/definition.</param>
/// <param name="globalScopeNames">The global scope names to add to as security requirement with the scheme name in the document's 'security' property (can be an empty list).</param>
/// <param name="swaggerSecurityScheme">The Swagger security scheme.</param>
public SecurityDefinitionAppender(string name, IEnumerable<string> globalScopeNames, OpenApiSecurityScheme swaggerSecurityScheme)
{
_name = name;
_scopeNames = globalScopeNames ?? throw new ArgumentNullException(nameof(globalScopeNames));
_swaggerSecurityScheme = swaggerSecurityScheme;
}
/// <summary>Processes the specified Swagger document.</summary>
/// <param name="context"></param>
public void Process(DocumentProcessorContext context)
{
context.Document.SecurityDefinitions[_name] = _swaggerSecurityScheme;
if (_scopeNames != null)
{
if (context.Document.Security == null)
{
context.Document.Security = [];
}
context.Document.Security.Add(new OpenApiSecurityRequirement
{
{ _name, _scopeNames }
});
}
}
}
}