Skip to content

Commit 06913c4

Browse files
committed
Populate ClaimsPrincipal when no auth key provided
1 parent 136e454 commit 06913c4

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

sample/CSharp/HttpTrigger-Identities/function.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"type": "httpTrigger",
55
"name": "req",
66
"direction": "in",
7+
"authLevel": "anonymous",
78
"methods": [ "get" ]
89
},
910
{

src/WebJobs.Script.WebHost/Security/Authentication/Keys/AuthenticationLevelHandler.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
4444
// Get the authorization level for the current request
4545
(string name, AuthorizationLevel requestAuthorizationLevel) = await GetAuthorizationKeyInfoAsync(Context.Request, _secretManagerProvider);
4646

47+
List<ClaimsIdentity> claimsIdentities = new List<ClaimsIdentity>();
48+
49+
if (_isEasyAuthEnabled)
50+
{
51+
ClaimsIdentity easyAuthIdentity = Context.Request.GetAppServiceIdentity();
52+
if (easyAuthIdentity != null)
53+
{
54+
claimsIdentities.Add(easyAuthIdentity);
55+
}
56+
}
57+
4758
if (requestAuthorizationLevel != AuthorizationLevel.Anonymous)
4859
{
4960
var claims = new List<Claim>
@@ -56,18 +67,12 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
5667
claims.Add(new Claim(SecurityConstants.AuthLevelKeyNameClaimType, name));
5768
}
5869

59-
List<ClaimsIdentity> claimsIdentities = new List<ClaimsIdentity>();
6070
var keyIdentity = new ClaimsIdentity(claims, AuthLevelAuthenticationDefaults.AuthenticationScheme);
61-
if (_isEasyAuthEnabled)
62-
{
63-
ClaimsIdentity easyAuthIdentity = Context.Request.GetAppServiceIdentity();
64-
if (easyAuthIdentity != null)
65-
{
66-
claimsIdentities.Add(easyAuthIdentity);
67-
}
68-
}
6971
claimsIdentities.Add(keyIdentity);
72+
}
7073

74+
if (claimsIdentities.Count > 0)
75+
{
7176
return AuthenticateResult.Success(new AuthenticationTicket(new ClaimsPrincipal(claimsIdentities), Scheme.Name));
7277
}
7378
else

test/WebJobs.Script.Tests.Integration/WebHostEndToEnd/SamplesEndToEndTests_CSharp.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,29 @@ public async Task HttpTrigger_Identities_Succeeds()
684684
}
685685
}
686686

687+
[Fact]
688+
public async Task HttpTrigger_Identities_AnonymousAccessSucceeds()
689+
{
690+
var vars = new Dictionary<string, string>
691+
{
692+
{ LanguageWorkerConstants.FunctionWorkerRuntimeSettingName, LanguageWorkerConstants.DotNetLanguageWorkerName},
693+
{ "WEBSITE_AUTH_ENABLED", "TRUE"}
694+
};
695+
using (var env = new TestScopedEnvironmentVariable(vars))
696+
{
697+
string uri = $"api/httptrigger-identities";
698+
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, uri);
699+
700+
MockEasyAuth(request, "facebook", "Connor McMahon", "10241897674253170");
701+
702+
HttpResponseMessage response = await this._fixture.Host.HttpClient.SendAsync(request);
703+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
704+
string responseContent = await response.Content.ReadAsStringAsync();
705+
string[] identityStrings = StripBookendQuotations(responseContent).Split(';');
706+
Assert.Equal("Identity: (facebook, Connor McMahon, 10241897674253170)", identityStrings[0]);
707+
}
708+
}
709+
687710
[Fact]
688711
public async Task HttpTrigger_Identities_BlocksSpoofedEasyAuthIdentity()
689712
{

0 commit comments

Comments
 (0)