forked from Azure/azure-functions-host
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
supporting EasyAuth for Linux Consumption in the functions host (Azur…
…e#6006) * cherry pick v2 changes * fixed csproj
- Loading branch information
1 parent
242a4c5
commit 1103b5f
Showing
11 changed files
with
140 additions
and
5 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/WebJobs.Script.WebHost/Configuration/HostEasyAuthOptions.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 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace Microsoft.Azure.WebJobs.Script.WebHost.Configuration | ||
{ | ||
public class HostEasyAuthOptions | ||
{ | ||
public bool SiteAuthEnabled { get; set; } | ||
|
||
public string SiteAuthClientId { get; set; } | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/WebJobs.Script.WebHost/Configuration/HostEasyAuthOptionsSetup.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,43 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.WebJobs.Script.Configuration; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.Azure.WebJobs.Script.WebHost.Configuration | ||
{ | ||
public class HostEasyAuthOptionsSetup : IConfigureOptions<HostEasyAuthOptions> | ||
{ | ||
private readonly IEnvironment _env; | ||
|
||
public HostEasyAuthOptionsSetup(IEnvironment env) | ||
{ | ||
_env = env; | ||
} | ||
|
||
public void Configure(HostEasyAuthOptions options) | ||
{ | ||
options.SiteAuthEnabled = IsSiteAuthEnabled(); | ||
options.SiteAuthClientId = _env.GetEnvironmentVariable(EnvironmentSettingNames.EasyAuthClientId); | ||
} | ||
|
||
private bool IsSiteAuthEnabled() | ||
{ | ||
string enabledString = _env.GetEnvironmentVariable(EnvironmentSettingNames.EasyAuthEnabled); | ||
if (bool.TryParse(enabledString, out bool result)) | ||
{ | ||
return result; | ||
} | ||
if (int.TryParse(enabledString, out int enabledInt)) | ||
{ | ||
return Convert.ToBoolean(enabledInt); | ||
} | ||
return 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
50 changes: 50 additions & 0 deletions
50
src/WebJobs.Script.WebHost/Middleware/JobHostEasyAuthMiddleware.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,50 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Azure.AppService.Middleware.AspNetCoreMiddleware; | ||
using Microsoft.Azure.WebJobs.Script.Middleware; | ||
using Microsoft.Azure.WebJobs.Script.WebHost.Configuration; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.Azure.WebJobs.Script.WebHost.Middleware | ||
{ | ||
public class JobHostEasyAuthMiddleware : IJobHostHttpMiddleware | ||
{ | ||
private readonly IConfiguration _configuration; | ||
private RequestDelegate _invoke; | ||
|
||
public JobHostEasyAuthMiddleware(IOptions<HostEasyAuthOptions> hostEasyAuthOptions, IConfiguration configuration) | ||
{ | ||
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); | ||
|
||
RequestDelegate contextNext = async context => | ||
{ | ||
if (context.Items.Remove(ScriptConstants.EasyAuthMiddlewareRequestDelegate, out object requestDelegate) && requestDelegate is RequestDelegate next) | ||
{ | ||
await next(context); | ||
} | ||
}; | ||
if (hostEasyAuthOptions.Value.SiteAuthEnabled) | ||
{ | ||
var easyAuthMiddleware = new EasyAuthMiddleware(contextNext, _configuration, AppService.Middleware.Functions.FunctionsHostingEnvironment.LinuxConsumption); | ||
_invoke = easyAuthMiddleware.InvokeAsync; | ||
} | ||
else | ||
{ | ||
_invoke = contextNext; | ||
} | ||
} | ||
|
||
public async Task Invoke(HttpContext context, RequestDelegate next) | ||
{ | ||
context.Items.Add(ScriptConstants.EasyAuthMiddlewareRequestDelegate, next); | ||
await _invoke(context); | ||
} | ||
} | ||
} |
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
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
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