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

remove scope attribute from the templates for azure function #1030

Merged
merged 1 commit into from
Feb 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions ProjectTemplates/templates/Functions-CSharp/SampleFunc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ namespace Company.FunctionApp1
public class SampleFunc
{
private readonly ILogger<SampleFunc> _logger;
#if (!NoAuth)
// The web API will only accept tokens 1) for users, and 2) having the "api-scope" scope for this API
static readonly string[] scopeRequiredByApi = new string[] { "access_as_user" };
#endif
#if (GenerateApi)
private readonly IDownstreamWebApi _downstreamWebApi;

Expand All @@ -31,7 +35,6 @@ public SampleFunc(ILogger<SampleFunc> logger,
}

[FunctionName("SampleFunc")]
[RequiredScope("access_as_user")] // The Azure Function will only accept tokens 1) for users, and 2) having the "access_as_user" scope for this API
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req)
{
Expand All @@ -41,6 +44,8 @@ public async Task<IActionResult> Run(
await req.HttpContext.AuthenticateAzureFunctionAsync();
if (!authenticationStatus) return authenticationResponse;

req.HttpContext.VerifyUserHasAnyAcceptedScope(scopeRequiredByApi);

using var response = await _downstreamWebApi.CallWebApiForUserAsync("DownstreamApi").ConfigureAwait(false);

if (response.StatusCode == System.Net.HttpStatusCode.OK)
Expand Down Expand Up @@ -74,7 +79,6 @@ public SampleFunc(ILogger<SampleFunc> logger,
}

[FunctionName("SampleFunc")]
[RequiredScope("access_as_user")] // The Azure Function will only accept tokens 1) for users, and 2) having the "access_as_user" scope for this API
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req)
{
Expand All @@ -84,6 +88,8 @@ public async Task<IActionResult> Run(
await req.HttpContext.AuthenticateAzureFunctionAsync();
if (!authenticationStatus) return authenticationResponse;

req.HttpContext.VerifyUserHasAnyAcceptedScope(scopeRequiredByApi);

var user = await _graphServiceClient.Me.Request().GetAsync();

string responseMessage = string.IsNullOrEmpty(user.DisplayName)
Expand All @@ -100,7 +106,6 @@ public SampleFunc(ILogger<SampleFunc> logger)
}

[FunctionName("SampleFunc")]
[RequiredScope("access_as_user")] // The Azure Function will only accept tokens 1) for users, and 2) having the "access_as_user" scope for this API
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req)
{
Expand All @@ -110,6 +115,8 @@ public async Task<IActionResult> Run(
await req.HttpContext.AuthenticateAzureFunctionAsync();
if (!authenticationStatus) return authenticationResponse;

req.HttpContext.VerifyUserHasAnyAcceptedScope(scopeRequiredByApi);

string name = req.HttpContext.User.Identity.IsAuthenticated ? req.HttpContext.User.GetDisplayName() : null;

string responseMessage = string.IsNullOrEmpty(name)
Expand Down