-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a fsharp test project to ease testing (#74)
* Add a fsharp test project to ease testing --------- Co-authored-by: Helge René Urholm <[email protected]>
- Loading branch information
Showing
11 changed files
with
247 additions
and
0 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
5 changes: 5 additions & 0 deletions
5
sample/SampleIsolatedFunctionsFSharp.V4/.vscode/extensions.json
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,5 @@ | ||
{ | ||
"recommendations": [ | ||
"ms-azuretools.vscode-azurefunctions" | ||
] | ||
} |
43 changes: 43 additions & 0 deletions
43
sample/SampleIsolatedFunctionsFSharp.V4/HelperFunctions.fs
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,43 @@ | ||
namespace SampleInProcFunctions.V4 | ||
|
||
open System.Security.Claims | ||
open Common.Tests | ||
open Microsoft.AspNetCore.Http | ||
open Microsoft.AspNetCore.Mvc | ||
open Microsoft.Azure.Functions.Worker | ||
open Microsoft.Extensions.Logging | ||
open System | ||
|
||
type HelperFunctions() = | ||
|
||
[<Function("GetTestToken")>] | ||
member _.Run( | ||
[<HttpTrigger("get", Route = null)>] | ||
req: HttpRequest, | ||
log: ILogger) = | ||
task { | ||
|
||
let firstName = "Test" | ||
let lastName = "User" | ||
let email = "[email protected]" | ||
let token = JwtUtils.GenerateJwtToken( | ||
[ | ||
new Claim("aud", "api://default") | ||
new Claim("iss", "https://localhost/jwt/") | ||
new Claim("scp", "user_impersonation") | ||
new Claim("tid", Guid.NewGuid().ToString()) | ||
new Claim("oid", Guid.NewGuid().ToString()) | ||
new Claim("name", $"{firstName} {lastName}") | ||
new Claim(ClaimTypes.Name, email) | ||
new Claim(ClaimTypes.Upn, email) | ||
new Claim(ClaimTypes.Email, email) | ||
new Claim(ClaimTypes.GivenName, firstName) | ||
new Claim(ClaimTypes.Surname, lastName) | ||
new Claim("role", "Just a user") | ||
new Claim("role", "admin") | ||
]) | ||
|
||
return OkObjectResult(token) | ||
} | ||
|
||
|
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,59 @@ | ||
module Program | ||
|
||
open Common.Tests | ||
open DarkLoop.Azure.Functions.Authorization | ||
open Microsoft.Azure.Functions.Worker | ||
open Microsoft.Extensions.DependencyInjection | ||
open Microsoft.Extensions.Hosting | ||
open Microsoft.IdentityModel.Tokens | ||
|
||
// IMPORTANT: because local.settings.json is not included in the repository, you must create it manually | ||
// If you don't create it. the isolated function will not run. Ensure that the file has the following content: | ||
// | ||
// { | ||
// "IsEncrypted": false, | ||
// "Values": { | ||
// "AzureWebJobsStorage": "UseDevelopmentStorage=true", | ||
// "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated" | ||
// } | ||
// } | ||
|
||
let host = | ||
HostBuilder() | ||
.ConfigureFunctionsWebApplication(fun builder -> | ||
//This is needed to make F# variants of startup work nicely | ||
FunctionsAuthorizationExtensionStartup().Configure(builder) | ||
builder.UseFunctionsAuthorization() |> ignore ) | ||
.ConfigureServices(fun services -> | ||
services | ||
.AddFunctionsAuthentication(JwtFunctionsBearerDefaults.AuthenticationScheme) | ||
.AddJwtFunctionsBearer(fun options -> | ||
// this line is here to bypass the token validation | ||
// and test the functionality of this library. | ||
// you can create a dummy token by executing the GetTestToken function in HelperFunctions.cs | ||
// THE FOLLOWING LINE SHOULD BE REMOVED IN A REAL-WORLD SCENARIO | ||
options.SecurityTokenValidators.Add(TestTokenValidator()) | ||
|
||
// this is what you should look for in a real-world scenario | ||
// comment the lines if you cloned this repository and want to test the library | ||
options.Authority <- "https://login.microsoftonline.com/<your-tenant>" | ||
options.Audience <- "<your-audience>" | ||
options.TokenValidationParameters <- TokenValidationParameters | ||
( | ||
ValidateIssuer = true, | ||
ValidateAudience = true, | ||
ValidateLifetime = true, | ||
ValidateIssuerSigningKey = true | ||
) | ||
() | ||
) |> ignore | ||
|
||
services | ||
.AddFunctionsAuthorization(fun options -> | ||
// Add your policies here | ||
() | ||
) |> ignore | ||
) | ||
.Build() | ||
|
||
host.Run() |
11 changes: 11 additions & 0 deletions
11
sample/SampleIsolatedFunctionsFSharp.V4/Properties/serviceDependencies.json
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,11 @@ | ||
{ | ||
"dependencies": { | ||
"appInsights1": { | ||
"type": "appInsights" | ||
}, | ||
"storage1": { | ||
"type": "storage", | ||
"connectionId": "AzureWebJobsStorage" | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
sample/SampleIsolatedFunctionsFSharp.V4/Properties/serviceDependencies.local.json
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,11 @@ | ||
{ | ||
"dependencies": { | ||
"appInsights1": { | ||
"type": "appInsights.sdk" | ||
}, | ||
"storage1": { | ||
"type": "storage.emulator", | ||
"connectionId": "AzureWebJobsStorage" | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
sample/SampleIsolatedFunctionsFSharp.V4/SampleIsolatedFunctionsFSharp_V4.fsproj
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,47 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<AzureFunctionsVersion>v4</AzureFunctionsVersion> | ||
<OutputType>Exe</OutputType> | ||
<UserSecretsId>17c2def3-36ba-461c-8cf2-2305557bb98b</UserSecretsId> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="HelperFunctions.fs" /> | ||
<Compile Include="TestFunction.fs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Content Include="host.json" /> | ||
<Content Include="local.settings.json" /> | ||
<Compile Include="Program.fs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
<PackageReference Include="Azure.Data.Tables" Version="12.9.1" /> | ||
<PackageReference Include="Azure.Identity" Version="1.13.0" /> | ||
<PackageReference Include="Azure.Storage.Blobs" Version="12.22.2" /> | ||
<PackageReference Include="Azure.Storage.Files.Shares" Version="12.20.1" /> | ||
<PackageReference Include="Azure.Storage.Queues" Version="12.20.1" /> | ||
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.1.0" /> | ||
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.4" /> | ||
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" /> | ||
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.1.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Azure" Version="1.7.6" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.1" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\isolated\DarkLoop.Azure.Functions.Authorization.Isolated.csproj" /> | ||
<ProjectReference Include="..\..\test\Common.Tests\Common.Tests.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Update="host.json"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
<None Update="local.settings.json"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
<CopyToPublishDirectory>Never</CopyToPublishDirectory> | ||
</None> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" /> | ||
</ItemGroup> | ||
</Project> |
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,43 @@ | ||
namespace SampleIsolatedFunctionsFSharp.V4 | ||
|
||
open System.Text | ||
open DarkLoop.Azure.Functions.Authorization | ||
open Microsoft.AspNetCore.Authentication | ||
open Microsoft.AspNetCore.Authorization | ||
open Microsoft.AspNetCore.Http | ||
open Microsoft.AspNetCore.Mvc | ||
open Microsoft.Azure.Functions.Worker | ||
open Microsoft.Extensions.DependencyInjection | ||
open Microsoft.Extensions.Logging | ||
|
||
|
||
[<FunctionAuthorize(AuthenticationSchemes = "FunctionsBearer")>] | ||
type TestFunction(logger:ILogger<TestFunction>) = | ||
let _logger = logger | ||
|
||
[<Function("TestFunction")>] | ||
[<Authorize(Roles = "admin")>] | ||
member _.Run([<HttpTrigger("get", "post")>] req:HttpRequest) = | ||
task { | ||
_logger.LogInformation("F# HTTP trigger function processed a request.") | ||
|
||
let provider = req.HttpContext.RequestServices | ||
let schProvider = provider.GetService<IAuthenticationSchemeProvider>() | ||
|
||
let sb = new StringBuilder() | ||
sb.AppendLine("Authentication schemes:") |> ignore | ||
|
||
if (schProvider <> null) then | ||
let! allScheme = schProvider.GetAllSchemesAsync() | ||
for scheme in allScheme do | ||
sb.AppendLine($" {scheme.Name} -> {scheme.HandlerType}") |> ignore | ||
|
||
|
||
sb.AppendLine()|> ignore | ||
sb.AppendLine($"User:")|> ignore | ||
sb.AppendLine($" Name -> {req.HttpContext.User.Identity.Name}")|> ignore | ||
let email = req.HttpContext.User.FindFirst("email")|> Option.ofObj|>Option.map _.Value | ||
sb.AppendLine($" Email -> {email}")|> ignore | ||
|
||
return OkObjectResult(sb.ToString()) | ||
} |
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,12 @@ | ||
{ | ||
"version": "2.0", | ||
"logging": { | ||
"applicationInsights": { | ||
"samplingSettings": { | ||
"isEnabled": true, | ||
"excludedTypes": "Request" | ||
}, | ||
"enableLiveMetricsFilters": true | ||
} | ||
} | ||
} |
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,7 @@ | ||
{ | ||
"IsEncrypted": false, | ||
"Values": { | ||
"AzureWebJobsStorage": "UseDevelopmentStorage=true", | ||
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated" | ||
} | ||
} |
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