-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathOpenApiSecuritySchemeDeserializer.cs
More file actions
131 lines (118 loc) · 4.64 KB
/
OpenApiSecuritySchemeDeserializer.cs
File metadata and controls
131 lines (118 loc) · 4.64 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Reader.ParseNodes;
namespace Microsoft.OpenApi.Reader.V2
{
/// <summary>
/// Class containing logic to deserialize Open API V2 document into
/// runtime Open API object model.
/// </summary>
internal static partial class OpenApiV2Deserializer
{
private static string _flowValue;
private static OpenApiOAuthFlow _flow;
private static readonly FixedFieldMap<OpenApiSecurityScheme> _securitySchemeFixedFields =
new()
{
{
"type",
(o, n, _) =>
{
var type = n.GetScalarValue();
switch (type)
{
case "basic":
o.Type = SecuritySchemeType.Http;
o.Scheme = OpenApiConstants.Basic;
break;
case "apiKey":
o.Type = SecuritySchemeType.ApiKey;
break;
case "oauth2":
o.Type = SecuritySchemeType.OAuth2;
break;
default:
n.Context.Diagnostic.Errors.Add(new OpenApiError(n.Context.GetLocation(), $"Security scheme type {type} is not recognized."));
break;
}
}
},
{"description", (o, n, _) => o.Description = n.GetScalarValue()},
{"name", (o, n, _) => o.Name = n.GetScalarValue()},
{"in", (o, n, _) =>
{
if (!n.GetScalarValue().TryGetEnumFromDisplayName<ParameterLocation>(n.Context, out var _in))
{
return;
}
o.In = _in;
}
},
{
"flow", (_, n, _) => _flowValue = n.GetScalarValue()
},
{
"authorizationUrl",
(_, n, _) => _flow.AuthorizationUrl = new(n.GetScalarValue(), UriKind.RelativeOrAbsolute)
},
{
"tokenUrl",
(_, n, _) => _flow.TokenUrl = new(n.GetScalarValue(), UriKind.RelativeOrAbsolute)
},
{
"scopes", (_, n, _) => _flow.Scopes = n.CreateSimpleMap(LoadString)
}
};
private static readonly PatternFieldMap<OpenApiSecurityScheme> _securitySchemePatternFields =
new()
{
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};
public static OpenApiSecurityScheme LoadSecurityScheme(ParseNode node, OpenApiDocument hostDocument = null)
{
// Reset the local variables every time this method is called.
// TODO: Change _flow to a tempStorage variable to make the deserializer thread-safe.
_flowValue = null;
_flow = new();
var mapNode = node.CheckMapNode("securityScheme");
var securityScheme = new OpenApiSecurityScheme();
foreach (var property in mapNode)
{
property.ParseField(securityScheme, _securitySchemeFixedFields, _securitySchemePatternFields);
}
// Put the Flow object in the right Flows property based on the string in "flow"
if (_flowValue == OpenApiConstants.Implicit)
{
securityScheme.Flows = new()
{
Implicit = _flow
};
}
else if (_flowValue == OpenApiConstants.Password)
{
securityScheme.Flows = new()
{
Password = _flow
};
}
else if (_flowValue == OpenApiConstants.Application)
{
securityScheme.Flows = new()
{
ClientCredentials = _flow
};
}
else if (_flowValue == OpenApiConstants.AccessCode)
{
securityScheme.Flows = new()
{
AuthorizationCode = _flow
};
}
return securityScheme;
}
}
}