|
4 | 4 | using System.Threading.Tasks; |
5 | 5 |
|
6 | 6 | using Microsoft.Azure.Functions.Worker.Http; |
| 7 | +using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions; |
7 | 8 | using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Extensions; |
8 | 9 | using Microsoft.Extensions.Logging; |
9 | 10 |
|
@@ -37,20 +38,34 @@ public async Task<HttpResponseData> RenderSwaggerDocument(HttpRequestData req, s |
37 | 38 | log.LogInformation($"swagger.{extension} was requested."); |
38 | 39 |
|
39 | 40 | var fi = new FileInfo(ctx.FunctionDefinition.PathToAssembly); |
| 41 | + var request = new HttpRequestObject(req); |
40 | 42 | var result = default(string); |
41 | 43 | var response = default(HttpResponseData); |
42 | 44 | try |
43 | 45 | { |
44 | | - result = await (await this._context.SetApplicationAssemblyAsync(fi.Directory.FullName, appendBin: false)) |
45 | | - .Document |
46 | | - .InitialiseDocument() |
47 | | - .AddMetadata(this._context.OpenApiConfigurationOptions.Info) |
48 | | - .AddServer(new HttpRequestObject(req), this._context.HttpSettings.RoutePrefix, this._context.OpenApiConfigurationOptions) |
49 | | - .AddNamingStrategy(this._context.NamingStrategy) |
50 | | - .AddVisitors(this._context.GetVisitorCollection()) |
51 | | - .Build(this._context.ApplicationAssembly, this._context.OpenApiConfigurationOptions.OpenApiVersion) |
52 | | - .RenderAsync(this._context.GetOpenApiSpecVersion(this._context.OpenApiConfigurationOptions.OpenApiVersion), this._context.GetOpenApiFormat(extension)) |
53 | | - .ConfigureAwait(false); |
| 46 | + var auth = await this._context |
| 47 | + .SetApplicationAssemblyAsync(fi.Directory.FullName, appendBin: false) |
| 48 | + .AuthorizeAsync(request) |
| 49 | + .ConfigureAwait(false); |
| 50 | + if (!auth.IsNullOrDefault()) |
| 51 | + { |
| 52 | + response = req.CreateResponse(auth.StatusCode); |
| 53 | + response.Headers.Add("Content-Type", auth.ContentType); |
| 54 | + await response.WriteStringAsync(auth.Payload).ConfigureAwait(false); |
| 55 | + |
| 56 | + return response; |
| 57 | + } |
| 58 | + |
| 59 | + result = await this._context |
| 60 | + .Document |
| 61 | + .InitialiseDocument() |
| 62 | + .AddMetadata(this._context.OpenApiConfigurationOptions.Info) |
| 63 | + .AddServer(request, this._context.HttpSettings.RoutePrefix, this._context.OpenApiConfigurationOptions) |
| 64 | + .AddNamingStrategy(this._context.NamingStrategy) |
| 65 | + .AddVisitors(this._context.GetVisitorCollection()) |
| 66 | + .Build(this._context.ApplicationAssembly, this._context.OpenApiConfigurationOptions.OpenApiVersion) |
| 67 | + .RenderAsync(this._context.GetOpenApiSpecVersion(this._context.OpenApiConfigurationOptions.OpenApiVersion), this._context.GetOpenApiFormat(extension)) |
| 68 | + .ConfigureAwait(false); |
54 | 69 |
|
55 | 70 | response = req.CreateResponse(HttpStatusCode.OK); |
56 | 71 | response.Headers.Add("Content-Type", this._context.GetOpenApiFormat(extension).GetContentType()); |
@@ -82,20 +97,34 @@ public async Task<HttpResponseData> RenderOpenApiDocument(HttpRequestData req, s |
82 | 97 | log.LogInformation($"{version}.{extension} was requested."); |
83 | 98 |
|
84 | 99 | var fi = new FileInfo(ctx.FunctionDefinition.PathToAssembly); |
| 100 | + var request = new HttpRequestObject(req); |
85 | 101 | var result = default(string); |
86 | 102 | var response = default(HttpResponseData); |
87 | 103 | try |
88 | 104 | { |
89 | | - result = await (await this._context.SetApplicationAssemblyAsync(fi.Directory.FullName, appendBin: false)) |
90 | | - .Document |
91 | | - .InitialiseDocument() |
92 | | - .AddMetadata(this._context.OpenApiConfigurationOptions.Info) |
93 | | - .AddServer(new HttpRequestObject(req), this._context.HttpSettings.RoutePrefix, this._context.OpenApiConfigurationOptions) |
94 | | - .AddNamingStrategy(this._context.NamingStrategy) |
95 | | - .AddVisitors(this._context.GetVisitorCollection()) |
96 | | - .Build(this._context.ApplicationAssembly, this._context.GetOpenApiVersionType(version)) |
97 | | - .RenderAsync(this._context.GetOpenApiSpecVersion(version), this._context.GetOpenApiFormat(extension)) |
98 | | - .ConfigureAwait(false); |
| 105 | + var auth = await this._context |
| 106 | + .SetApplicationAssemblyAsync(fi.Directory.FullName, appendBin: false) |
| 107 | + .AuthorizeAsync(request) |
| 108 | + .ConfigureAwait(false); |
| 109 | + if (!auth.IsNullOrDefault()) |
| 110 | + { |
| 111 | + response = req.CreateResponse(auth.StatusCode); |
| 112 | + response.Headers.Add("Content-Type", auth.ContentType); |
| 113 | + await response.WriteStringAsync(auth.Payload).ConfigureAwait(false); |
| 114 | + |
| 115 | + return response; |
| 116 | + } |
| 117 | + |
| 118 | + result = await this._context |
| 119 | + .Document |
| 120 | + .InitialiseDocument() |
| 121 | + .AddMetadata(this._context.OpenApiConfigurationOptions.Info) |
| 122 | + .AddServer(request, this._context.HttpSettings.RoutePrefix, this._context.OpenApiConfigurationOptions) |
| 123 | + .AddNamingStrategy(this._context.NamingStrategy) |
| 124 | + .AddVisitors(this._context.GetVisitorCollection()) |
| 125 | + .Build(this._context.ApplicationAssembly, this._context.GetOpenApiVersionType(version)) |
| 126 | + .RenderAsync(this._context.GetOpenApiSpecVersion(version), this._context.GetOpenApiFormat(extension)) |
| 127 | + .ConfigureAwait(false); |
99 | 128 |
|
100 | 129 | response = req.CreateResponse(HttpStatusCode.OK); |
101 | 130 | response.Headers.Add("Content-Type", this._context.GetOpenApiFormat(extension).GetContentType()); |
@@ -126,17 +155,31 @@ public async Task<HttpResponseData> RenderSwaggerUI(HttpRequestData req, Functio |
126 | 155 | log.LogInformation("SwaggerUI page was requested."); |
127 | 156 |
|
128 | 157 | var fi = new FileInfo(ctx.FunctionDefinition.PathToAssembly); |
| 158 | + var request = new HttpRequestObject(req); |
129 | 159 | var result = default(string); |
130 | 160 | var response = default(HttpResponseData); |
131 | 161 | try |
132 | 162 | { |
133 | | - result = await (await this._context.SetApplicationAssemblyAsync(fi.Directory.FullName, appendBin: false)) |
134 | | - .SwaggerUI |
135 | | - .AddMetadata(this._context.OpenApiConfigurationOptions.Info) |
136 | | - .AddServer(new HttpRequestObject(req), this._context.HttpSettings.RoutePrefix, this._context.OpenApiConfigurationOptions) |
137 | | - .BuildAsync(this._context.PackageAssembly, this._context.OpenApiCustomUIOptions) |
138 | | - .RenderAsync("swagger.json", this._context.GetDocumentAuthLevel(), this._context.GetSwaggerAuthKey()) |
139 | | - .ConfigureAwait(false); |
| 163 | + var auth = await this._context |
| 164 | + .SetApplicationAssemblyAsync(fi.Directory.FullName, appendBin: false) |
| 165 | + .AuthorizeAsync(request) |
| 166 | + .ConfigureAwait(false); |
| 167 | + if (!auth.IsNullOrDefault()) |
| 168 | + { |
| 169 | + response = req.CreateResponse(auth.StatusCode); |
| 170 | + response.Headers.Add("Content-Type", auth.ContentType); |
| 171 | + await response.WriteStringAsync(auth.Payload).ConfigureAwait(false); |
| 172 | + |
| 173 | + return response; |
| 174 | + } |
| 175 | + |
| 176 | + result = await this._context |
| 177 | + .SwaggerUI |
| 178 | + .AddMetadata(this._context.OpenApiConfigurationOptions.Info) |
| 179 | + .AddServer(request, this._context.HttpSettings.RoutePrefix, this._context.OpenApiConfigurationOptions) |
| 180 | + .BuildAsync(this._context.PackageAssembly, this._context.OpenApiCustomUIOptions) |
| 181 | + .RenderAsync("swagger.json", this._context.GetDocumentAuthLevel(), this._context.GetSwaggerAuthKey()) |
| 182 | + .ConfigureAwait(false); |
140 | 183 |
|
141 | 184 | response = req.CreateResponse(HttpStatusCode.OK); |
142 | 185 | response.Headers.Add("Content-Type", ContentTypeHtml); |
@@ -167,16 +210,21 @@ public async Task<HttpResponseData> RenderOAuth2Redirect(HttpRequestData req, Fu |
167 | 210 | log.LogInformation("The oauth2-redirect.html page was requested."); |
168 | 211 |
|
169 | 212 | var fi = new FileInfo(ctx.FunctionDefinition.PathToAssembly); |
| 213 | + var request = new HttpRequestObject(req); |
170 | 214 | var result = default(string); |
171 | 215 | var response = default(HttpResponseData); |
172 | 216 | try |
173 | 217 | { |
174 | | - result = await (await this._context.SetApplicationAssemblyAsync(fi.Directory.FullName, appendBin: false)) |
175 | | - .SwaggerUI |
176 | | - .AddServer(new HttpRequestObject(req), this._context.HttpSettings.RoutePrefix, this._context.OpenApiConfigurationOptions) |
177 | | - .BuildOAuth2RedirectAsync(this._context.PackageAssembly) |
178 | | - .RenderOAuth2RedirectAsync("oauth2-redirect.html", this._context.GetDocumentAuthLevel(), this._context.GetSwaggerAuthKey()) |
179 | | - .ConfigureAwait(false); |
| 218 | + await this._context |
| 219 | + .SetApplicationAssemblyAsync(fi.Directory.FullName, appendBin: false) |
| 220 | + .ConfigureAwait(false); |
| 221 | + |
| 222 | + result = await this._context |
| 223 | + .SwaggerUI |
| 224 | + .AddServer(request, this._context.HttpSettings.RoutePrefix, this._context.OpenApiConfigurationOptions) |
| 225 | + .BuildOAuth2RedirectAsync(this._context.PackageAssembly) |
| 226 | + .RenderOAuth2RedirectAsync("oauth2-redirect.html", this._context.GetDocumentAuthLevel(), this._context.GetSwaggerAuthKey()) |
| 227 | + .ConfigureAwait(false); |
180 | 228 |
|
181 | 229 | response = req.CreateResponse(HttpStatusCode.OK); |
182 | 230 | response.Headers.Add("Content-Type", ContentTypeHtml); |
|
0 commit comments