Skip to content

Commit 6e4b2eb

Browse files
committed
Implementation of the ArmAuthenticationHandler.
Enhancements to authorization handler to support multile AuthLevel type claims.
1 parent c88cc95 commit 6e4b2eb

20 files changed

+297
-26
lines changed

src/WebJobs.Script.WebHost/Management/WebFunctionsManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Microsoft.Azure.WebJobs.Script.Management.Models;
1616
using Microsoft.Azure.WebJobs.Script.WebHost.Extensions;
1717
using Microsoft.Azure.WebJobs.Script.WebHost.Helpers;
18+
using Microsoft.Azure.WebJobs.Script.WebHost.Security;
1819
using Microsoft.Extensions.Logging;
1920
using Newtonsoft.Json;
2021
using Newtonsoft.Json.Linq;

src/WebJobs.Script.WebHost/Models/EncryptedHostAssignmentContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
5-
using Microsoft.Azure.WebJobs.Script.WebHost.Helpers;
5+
using Microsoft.Azure.WebJobs.Script.WebHost.Security;
66
using Newtonsoft.Json;
77

88
namespace Microsoft.Azure.WebJobs.Script.WebHost.Models
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
9+
namespace Microsoft.Azure.WebJobs.Script.WebHost.Security
10+
{
11+
public static class ArmAuthenticationDefaults
12+
{
13+
public const string AuthenticationScheme = "ArmToken";
14+
}
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
using Microsoft.AspNetCore.Authentication;
9+
using Microsoft.Azure.WebJobs.Script.WebHost.Security;
10+
using Microsoft.Azure.WebJobs.Script.WebHost.Security.Authentication;
11+
12+
namespace Microsoft.Extensions.DependencyInjection
13+
{
14+
public static class ArmAuthenticationExtensions
15+
{
16+
public static AuthenticationBuilder AddArmToken(this AuthenticationBuilder builder)
17+
=> builder.AddScheme<ArmAuthenticationOptions, ArmAuthenticationHandler>(ArmAuthenticationDefaults.AuthenticationScheme, _ => { });
18+
}
19+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Security.Claims;
8+
using System.Text.Encodings.Web;
9+
using System.Threading.Tasks;
10+
using Microsoft.AspNetCore.Authentication;
11+
using Microsoft.Azure.WebJobs.Extensions.Http;
12+
using Microsoft.Extensions.Logging;
13+
using Microsoft.Extensions.Options;
14+
using Microsoft.Extensions.Primitives;
15+
16+
namespace Microsoft.Azure.WebJobs.Script.WebHost.Security.Authentication
17+
{
18+
public class ArmAuthenticationHandler : AuthenticationHandler<ArmAuthenticationOptions>
19+
{
20+
internal const string ArmTokenHeaderName = "x-ms-site-restricted-token";
21+
22+
private readonly ILogger _logger;
23+
24+
public ArmAuthenticationHandler(IOptionsMonitor<ArmAuthenticationOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
25+
: base(options, logger, encoder, clock)
26+
{
27+
_logger = logger.CreateLogger<ArmAuthenticationHandler>();
28+
}
29+
30+
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
31+
{
32+
AuthenticateResult result = HandleAuthenticate();
33+
34+
return Task.FromResult(result);
35+
}
36+
37+
private AuthenticateResult HandleAuthenticate()
38+
{
39+
string token = null;
40+
if (!Context.Request.Headers.TryGetValue(ArmTokenHeaderName, out StringValues values))
41+
{
42+
return AuthenticateResult.NoResult();
43+
}
44+
45+
token = values.First();
46+
47+
try
48+
{
49+
if (!SimpleWebTokenHelper.ValidateToken(token, Clock))
50+
{
51+
return AuthenticateResult.Fail("Token validation failed.");
52+
}
53+
54+
var claims = new List<Claim>
55+
{
56+
new Claim(SecurityConstants.AuthLevelClaimType, AuthorizationLevel.Admin.ToString())
57+
};
58+
59+
var identity = new ClaimsIdentity(claims, ArmAuthenticationDefaults.AuthenticationScheme);
60+
return AuthenticateResult.Success(new AuthenticationTicket(new ClaimsPrincipal(identity), Scheme.Name));
61+
}
62+
catch (Exception exc)
63+
{
64+
_logger.LogError(exc, "ARM authentication token validation failed.");
65+
return AuthenticateResult.Fail(exc);
66+
}
67+
}
68+
}
69+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
using Microsoft.AspNetCore.Authentication;
9+
10+
namespace Microsoft.Azure.WebJobs.Script.WebHost.Security.Authentication
11+
{
12+
public class ArmAuthenticationOptions : AuthenticationSchemeOptions
13+
{
14+
}
15+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)