Skip to content
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

CORS with Access-Control-Allow-Credentials #877

Merged
merged 2 commits into from
Nov 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/Azure.Functions.Cli/Actions/HostActions/StartHostAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ internal class StartHostAction : BaseAction

public string CorsOrigins { get; set; }

public bool CorsCredentials { get; set; }

public int Timeout { get; set; }

public bool UseHttps { get; set; }
Expand Down Expand Up @@ -81,6 +83,12 @@ public override ICommandLineParserResult ParseArgs(string[] args)
.SetDefault(hostSettings.Cors ?? string.Empty)
.Callback(c => CorsOrigins = c);

Parser
.Setup<bool>("cors-credentials")
.WithDescription($"Allow cross-origin authenticated requests (i.e. cookies and the Authentication header)")
.SetDefault(hostSettings.CorsCredentials)
.Callback(v => CorsCredentials = v);

Parser
.Setup<int>('t', "timeout")
.WithDescription($"Timeout for on the functions host to start in seconds. Default: {DefaultTimeout} seconds.")
Expand Down Expand Up @@ -150,7 +158,7 @@ private async Task<IWebHost> BuildWebHost(ScriptApplicationHostOptions hostOptio
loggingBuilder.AddDefaultWebJobsFilters();
loggingBuilder.AddProvider(new ColoredConsoleLoggerProvider((cat, level) => level >= LogLevel.Information));
})
.ConfigureServices((context, services) => services.AddSingleton<IStartup>(new Startup(context, hostOptions, CorsOrigins)))
.ConfigureServices((context, services) => services.AddSingleton<IStartup>(new Startup(context, hostOptions, CorsOrigins, CorsCredentials)))
.Build();
}

Expand Down Expand Up @@ -393,15 +401,17 @@ public class Startup : IStartup
private readonly WebHostBuilderContext _builderContext;
private readonly ScriptApplicationHostOptions _hostOptions;
private readonly string[] _corsOrigins;
private readonly bool _corsCredentials;

public Startup(WebHostBuilderContext builderContext, ScriptApplicationHostOptions hostOptions, string corsOrigins)
public Startup(WebHostBuilderContext builderContext, ScriptApplicationHostOptions hostOptions, string corsOrigins, bool corsCredentials)
{
_builderContext = builderContext;
_hostOptions = hostOptions;

if (!string.IsNullOrEmpty(corsOrigins))
{
_corsOrigins = corsOrigins.Split(',', StringSplitOptions.RemoveEmptyEntries);
_corsCredentials = corsCredentials;
}
}

Expand Down Expand Up @@ -444,9 +454,13 @@ public void Configure(IApplicationBuilder app)
{
app.UseCors(builder =>
{
builder.WithOrigins(_corsOrigins)
var origins = builder.WithOrigins(_corsOrigins)
.AllowAnyHeader()
.AllowAnyMethod();
if (_corsCredentials)
{
origins.AllowCredentials();
}
});
}

Expand Down
3 changes: 3 additions & 0 deletions src/Azure.Functions.Cli/Common/HostStartSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ public class HostStartSettings

[JsonProperty("CORS", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string Cors { get; set; }

[JsonProperty("CORSCredentials", DefaultValueHandling = DefaultValueHandling.Ignore)]
public bool CorsCredentials { get; set; }
}
}