forked from LEGO/AsyncAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncApiSecurityScheme.cs
More file actions
189 lines (163 loc) · 7.34 KB
/
Copy pathAsyncApiSecurityScheme.cs
File metadata and controls
189 lines (163 loc) · 7.34 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (c) The LEGO Group. All rights reserved.
namespace LEGO.AsyncAPI.Models
{
using System;
using System.Collections.Generic;
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Writers;
public class AsyncApiSecurityScheme : IAsyncApiSerializable, IAsyncApiReferenceable, IAsyncApiExtensible
{
/// <summary>
/// REQUIRED. The type of the security scheme. Valid values are "userPassword", "apiKey", "X509", "symmetricEncryption", "asymmetricEncryption", "httpApiKey", "http", "oauth2", "openIdConnect", "plain", "scramSha256", "scramSha512", and "gssapi".
/// </summary>
public SecuritySchemeType Type { get; set; }
/// <summary>
/// A short description for security scheme. CommonMark syntax MAY be used for rich text representation.
/// </summary>
public string Description { get; set; }
/// <summary>
/// REQUIRED. The name of the header, query or cookie parameter to be used.
/// </summary>
public string Name { get; set; }
/// <summary>
/// REQUIRED. The location of the API key. Valid values are "user" and "password" for apiKey and "query", "header" or "cookie" for httpApiKey.
/// </summary>
public ParameterLocation In { get; set; }
/// <summary>
/// REQUIRED. The name of the HTTP Authorization scheme to be used
/// in the Authorization header as defined in RFC7235.
/// </summary>
public string Scheme { get; set; }
/// <summary>
/// A hint to the client to identify how the bearer token is formatted.
/// Bearer tokens are usually generated by an authorization server,
/// so this information is primarily for documentation purposes.
/// </summary>
public string BearerFormat { get; set; }
/// <summary>
/// REQUIRED. An object containing configuration information for the flow types supported.
/// </summary>
public AsyncApiOAuthFlows Flows { get; set; }
/// <summary>
/// REQUIRED. OpenId Connect URL to discover OAuth2 configuration values.
/// </summary>
public Uri OpenIdConnectUrl { get; set; }
/// <summary>
/// Specification Extensions.
/// </summary>
public IDictionary<string, IAsyncApiExtension> Extensions { get; set; } = new Dictionary<string, IAsyncApiExtension>();
/// <summary>
/// Indicates if object is populated with data or is just a reference to the data.
/// </summary>
public bool UnresolvedReference { get; set; }
/// <summary>
/// Reference object.
/// </summary>
public AsyncApiReference Reference { get; set; }
/// <summary>
/// Serialize <see cref="AsyncApiSecurityScheme"/> to Async Api v2.3.
/// </summary>
public void SerializeV2(IAsyncApiWriter writer)
{
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
if (this.Reference != null)
{
this.Reference.SerializeV2(writer);
return;
}
this.SerializeV2WithoutReference(writer);
}
/// <summary>
/// Serialize to AsyncApi V2 document without using reference.
/// </summary>
public void SerializeV2WithoutReference(IAsyncApiWriter writer)
{
writer.WriteStartObject();
// type
writer.WriteRequiredProperty(AsyncApiConstants.Type, this.Type.GetDisplayName());
// description
writer.WriteOptionalProperty(AsyncApiConstants.Description, this.Description);
switch (this.Type)
{
case SecuritySchemeType.UserPassword:
break;
case SecuritySchemeType.ApiKey:
writer.WriteOptionalProperty(AsyncApiConstants.In, this.In.GetDisplayName());
break;
case SecuritySchemeType.X509:
break;
case SecuritySchemeType.SymmetricEncryption:
break;
case SecuritySchemeType.AsymmetricEncryption:
break;
case SecuritySchemeType.HttpApiKey:
writer.WriteOptionalProperty(AsyncApiConstants.Name, this.Name);
writer.WriteOptionalProperty(AsyncApiConstants.In, this.In.GetDisplayName());
break;
case SecuritySchemeType.Http:
writer.WriteOptionalProperty(AsyncApiConstants.Scheme, this.Scheme);
writer.WriteOptionalProperty(AsyncApiConstants.BearerFormat, this.BearerFormat);
break;
case SecuritySchemeType.OAuth2:
writer.WriteOptionalObject(AsyncApiConstants.Flows, this.Flows, (w, o) => o.SerializeV2(w));
break;
case SecuritySchemeType.OpenIdConnect:
writer.WriteOptionalProperty(AsyncApiConstants.OpenIdConnectUrl, this.OpenIdConnectUrl?.ToString());
break;
case SecuritySchemeType.Plain:
break;
case SecuritySchemeType.ScramSha256:
break;
case SecuritySchemeType.ScramSha512:
break;
case SecuritySchemeType.Gssapi:
break;
default:
break;
}
// extensions
writer.WriteExtensions(this.Extensions);
writer.WriteEndObject();
}
/// <summary>
/// Arbitrarily chooses one <see cref="AsyncApiOAuthFlow"/> object from the <see cref="AsyncApiOAuthFlows"/>
/// to populate in V2 security scheme.
/// </summary>
private static void WriteOAuthFlowForV2(IAsyncApiWriter writer, AsyncApiOAuthFlows flows)
{
if (flows != null)
{
if (flows.Implicit != null)
{
WriteOAuthFlowForV2(writer, AsyncApiConstants.Implicit, flows.Implicit);
}
else if (flows.Password != null)
{
WriteOAuthFlowForV2(writer, AsyncApiConstants.Password, flows.Password);
}
else if (flows.ClientCredentials != null)
{
WriteOAuthFlowForV2(writer, AsyncApiConstants.Application, flows.ClientCredentials);
}
else if (flows.AuthorizationCode != null)
{
WriteOAuthFlowForV2(writer, AsyncApiConstants.AccessCode, flows.AuthorizationCode);
}
}
}
private static void WriteOAuthFlowForV2(IAsyncApiWriter writer, string flowValue, AsyncApiOAuthFlow flow)
{
// flow
writer.WriteOptionalProperty(AsyncApiConstants.Flow, flowValue);
// authorizationUrl
writer.WriteOptionalProperty(AsyncApiConstants.AuthorizationUrl, flow.AuthorizationUrl?.ToString());
// tokenUrl
writer.WriteOptionalProperty(AsyncApiConstants.TokenUrl, flow.TokenUrl?.ToString());
// scopes
writer.WriteOptionalMap(AsyncApiConstants.Scopes, flow.Scopes, (w, s) => w.WriteValue(s));
}
}
}