Skip to content

Commit

Permalink
CORS with Access-Control-Allow-Credentials (#877)
Browse files Browse the repository at this point in the history
* Added a CORSCredentials config option and --corsCredentials command line argument to enable Access-Control-Allow-Credentials.

* Changed name of the CORS credentials parameter from corsCredentials to cors-credentials.
  • Loading branch information
patrikn authored and ahmelsayed committed Nov 20, 2018
1 parent 7579b4c commit eb15d8b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
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; }
}
}

0 comments on commit eb15d8b

Please sign in to comment.