Skip to content

Commit

Permalink
remove scope attribute from the templates for azure function (#1030)
Browse files Browse the repository at this point in the history
  • Loading branch information
jennyf19 authored Feb 27, 2021
1 parent fd1cffe commit 0bae831
Showing 1 changed file with 10 additions and 3 deletions.
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

0 comments on commit 0bae831

Please sign in to comment.