-
Notifications
You must be signed in to change notification settings - Fork 195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support dynamic OpenApiHttpTriggerAuthorization
#435
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
19 changes: 19 additions & 0 deletions
19
...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,19 @@ | ||
using System.Threading.Tasks; | ||
|
||
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions; | ||
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations; | ||
|
||
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); | ||
|
||
// Put your custom logic here! | ||
|
||
return await Task.FromResult(result).ConfigureAwait(false); | ||
} | ||
} | ||
} |
116 changes: 58 additions & 58 deletions
116
...s.Extensions.OpenApi.FunctionApp.InProc/Configurations/OpenApiHttpTriggerAuthorization.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 |
---|---|---|
@@ -1,58 +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 OpenApiHttpTriggerAuthorization : 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); | ||
// } | ||
// } | ||
// } | ||
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 OpenApiHttpTriggerAuthorization : DefaultOpenApiHttpTriggerAuthorization | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use this as |
||
{ | ||
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); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And remove this?