-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support dynamic
OpenApiHttpTriggerAuthorization
(#435)
- Loading branch information
1 parent
9072106
commit 5dfe8cc
Showing
17 changed files
with
783 additions
and
592 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,37 +45,59 @@ namespace MyFunctionApp | |
{ | ||
public override void Configure(IFunctionsHostBuilder builder) | ||
{ | ||
/* ⬇️⬇️⬇️ Add this ⬇️⬇️⬇️ */ | ||
builder.Services.AddSingleton<IOpenApiConfigurationOptions>(_ => | ||
{ | ||
var options = new OpenApiConfigurationOptions() | ||
{ | ||
Info = new OpenApiInfo() | ||
{ | ||
Version = "1.0.0", | ||
Title = "Swagger Petstore", | ||
Description = "This is a sample server Petstore API designed by [http://swagger.io](http://swagger.io).", | ||
TermsOfService = new Uri("https://github.com/Azure/azure-functions-openapi-extension"), | ||
Contact = new OpenApiContact() | ||
{ | ||
Name = "Enquiry", | ||
Email = "[email protected]", | ||
Url = new Uri("https://github.com/Azure/azure-functions-openapi-extension/issues"), | ||
}, | ||
License = new OpenApiLicense() | ||
{ | ||
Name = "MIT", | ||
Url = new Uri("http://opensource.org/licenses/MIT"), | ||
} | ||
}, | ||
Servers = DefaultOpenApiConfigurationOptions.GetHostNames(), | ||
OpenApiVersion = OpenApiVersionType.V2, | ||
IncludeRequestingHostName = true, | ||
ForceHttps = false, | ||
ForceHttp = false, | ||
}; | ||
|
||
return options; | ||
}); | ||
{ | ||
var options = new OpenApiConfigurationOptions() | ||
{ | ||
Info = new OpenApiInfo() | ||
{ | ||
Version = "1.0.0", | ||
Title = "Swagger Petstore", | ||
Description = "This is a sample server Petstore API designed by [http://swagger.io](http://swagger.io).", | ||
TermsOfService = new Uri("https://github.com/Azure/azure-functions-openapi-extension"), | ||
Contact = new OpenApiContact() | ||
{ | ||
Name = "Enquiry", | ||
Email = "[email protected]", | ||
Url = new Uri("https://github.com/Azure/azure-functions-openapi-extension/issues"), | ||
}, | ||
License = new OpenApiLicense() | ||
{ | ||
Name = "MIT", | ||
Url = new Uri("http://opensource.org/licenses/MIT"), | ||
} | ||
}, | ||
Servers = DefaultOpenApiConfigurationOptions.GetHostNames(), | ||
OpenApiVersion = OpenApiVersionType.V2, | ||
IncludeRequestingHostName = true, | ||
ForceHttps = false, | ||
ForceHttp = false, | ||
}; | ||
|
||
return options; | ||
}); | ||
/* ⬆️⬆️⬆️ Add this ⬆️⬆️⬆️ */ | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### Injecting `OpenApiHttpTriggerAuthorization` during Startup ### | ||
|
||
You may want to inject the `OpenApiHttpTriggerAuthorization` instance during startup, through the `Startup.cs` class. Here's the example: | ||
|
||
```csharp | ||
[assembly: FunctionsStartup(typeof(MyFunctionApp.Startup))] | ||
namespace MyFunctionApp | ||
{ | ||
public class Startup : FunctionsStartup | ||
{ | ||
public override void Configure(IFunctionsHostBuilder builder) | ||
{ | ||
/* ⬇️⬇️⬇️ Add this ⬇️⬇️⬇️ */ | ||
builder.Services.AddSingleton<IOpenApiHttpTriggerAuthorization, MyOpenApiHttpTriggerAuthorization>(); | ||
/* ⬆️⬆️⬆️ Add this ⬆️⬆️⬆️ */ | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...ensions.OpenApi.FunctionApp.OutOfProc/Configurations/MyOpenApiHttpTriggerAuthorization.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Threading.Tasks; | ||
|
||
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions; | ||
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations; | ||
|
||
namespace Microsoft.Azure.Functions.Worker.Extensions.OpenApi.FunctionApp.OutOfProc.Configurations | ||
{ | ||
public class MyOpenApiHttpTriggerAuthorization : DefaultOpenApiHttpTriggerAuthorization | ||
{ | ||
public override async Task<OpenApiAuthorizationResult> AuthorizeAsync(IHttpRequestDataObject req) | ||
{ | ||
var result = default(OpenApiAuthorizationResult); | ||
|
||
// Put your custom logic here! | ||
|
||
return await Task.FromResult(result).ConfigureAwait(false); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...Extensions.OpenApi.FunctionApp.InProc/Configurations/MyOpenApiHttpTriggerAuthorization.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
|
||
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions; | ||
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations; | ||
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Extensions; | ||
|
||
namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.InProc.Configurations | ||
{ | ||
public class MyOpenApiHttpTriggerAuthorization : DefaultOpenApiHttpTriggerAuthorization | ||
{ | ||
public override async Task<OpenApiAuthorizationResult> AuthorizeAsync(IHttpRequestDataObject req) | ||
{ | ||
var result = default(OpenApiAuthorizationResult); | ||
var authtoken = (string)req.Headers["Authorization"]; | ||
if (authtoken.IsNullOrWhiteSpace()) | ||
{ | ||
result = new OpenApiAuthorizationResult() | ||
{ | ||
StatusCode = HttpStatusCode.Unauthorized, | ||
ContentType = "text/plain", | ||
Payload = "Unauthorized", | ||
}; | ||
|
||
return await Task.FromResult(result).ConfigureAwait(false); | ||
} | ||
|
||
if (authtoken.StartsWith("Bearer", ignoreCase: true, CultureInfo.InvariantCulture) == false) | ||
{ | ||
result = new OpenApiAuthorizationResult() | ||
{ | ||
StatusCode = HttpStatusCode.Unauthorized, | ||
ContentType = "text/plain", | ||
Payload = "Invalid auth format", | ||
}; | ||
|
||
return await Task.FromResult(result).ConfigureAwait(false); | ||
} | ||
|
||
var token = authtoken.Split(' ').Last(); | ||
if (token != "secret") | ||
{ | ||
result = new OpenApiAuthorizationResult() | ||
{ | ||
StatusCode = HttpStatusCode.Forbidden, | ||
ContentType = "text/plain", | ||
Payload = "Invalid auth token", | ||
}; | ||
|
||
return await Task.FromResult(result).ConfigureAwait(false); | ||
} | ||
|
||
return await Task.FromResult(result).ConfigureAwait(false); | ||
} | ||
} | ||
} |
58 changes: 0 additions & 58 deletions
58
...s.Extensions.OpenApi.FunctionApp.InProc/Configurations/OpenApiHttpTriggerAuthorization.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...Jobs.Extensions.OpenApi.Core/Attributes/OpenApiHttpTriggerAuthorizationIgnoreAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
|
||
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions; | ||
|
||
namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes | ||
{ | ||
/// <summary> | ||
/// This represents the attribute entity for <see cref="IOpenApiConfigurationOptions"/> to be excluded from auto-loading. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)] | ||
public class OpenApiHttpTriggerAuthorizationIgnoreAttribute : Attribute | ||
{ | ||
} | ||
} |
Oops, something went wrong.