|
1 | 1 | using System;
|
2 |
| -using System.Security.Claims; |
3 |
| -using System.Text; |
4 |
| -using System.Threading.Tasks; |
5 |
| -using DarkLoop.Azure.Functions.Authorize; |
| 2 | +using DarkLoop.Azure.Functions.Authorize.Security; |
6 | 3 | using Microsoft.AspNetCore.Authentication;
|
7 |
| -using Microsoft.AspNetCore.Authentication.JwtBearer; |
8 | 4 | using Microsoft.Azure.Functions.Extensions.DependencyInjection;
|
9 |
| -using Microsoft.Azure.WebJobs.Extensions.Http; |
10 | 5 | using Microsoft.Extensions.Options;
|
11 |
| -using Microsoft.IdentityModel.Tokens; |
12 | 6 |
|
13 | 7 | namespace Microsoft.Extensions.DependencyInjection
|
14 | 8 | {
|
15 | 9 | public static class AuthenticationExtensions
|
16 | 10 | {
|
17 |
| - private const string AuthLevelClaimType = "http://schemas.microsoft.com/2017/07/functions/claims/authlevel"; |
| 11 | + /// <summary> |
| 12 | + /// Adds Functions built-in authentication. |
| 13 | + /// </summary> |
| 14 | + public static FunctionsAuthenticationBuilder AddFunctionsAuthentication(this IServiceCollection services) |
| 15 | + { |
| 16 | + if (services is null) throw new ArgumentNullException(nameof(services)); |
| 17 | + |
| 18 | + return services.AddFunctionsAuthentication(delegate { }); |
| 19 | + } |
18 | 20 |
|
19 |
| - public static AuthenticationBuilder AddAuthentication(this IFunctionsHostBuilder builder) |
| 21 | + /// <summary> |
| 22 | + /// Configures authentication for the Azure Functions app. It will setup Functions built-in authentication. |
| 23 | + /// </summary> |
| 24 | + /// <param name="builder">The <see cref="IFunctionsHostBuilder"/> for the current application.</param> |
| 25 | + /// <returns>A <see cref="FunctionsAuthenticationBuilder"/> instance to configure authentication schemes.</returns> |
| 26 | + public static FunctionsAuthenticationBuilder AddAuthentication(this IFunctionsHostBuilder builder) |
20 | 27 | {
|
21 |
| - if (builder == null) |
22 |
| - { |
23 |
| - throw new ArgumentNullException(nameof(builder)); |
24 |
| - } |
| 28 | + if (builder is null) throw new ArgumentNullException(nameof(builder)); |
25 | 29 |
|
26 |
| - return builder.AddAuthentication(null); |
| 30 | + return builder.Services.AddFunctionsAuthentication(delegate { }); |
27 | 31 | }
|
28 | 32 |
|
29 |
| - public static AuthenticationBuilder AddAuthentication( |
| 33 | + /// <summary> |
| 34 | + /// Configures authentication for the Azure Functions app. It will setup Functions built-in authentication. |
| 35 | + /// </summary> |
| 36 | + /// <param name="builder">The <see cref="IFunctionsHostBuilder"/> for the current application.</param> |
| 37 | + /// <param name="configure">The <see cref="AuthenticationOptions"/> configuration logic.</param> |
| 38 | + /// <returns>A <see cref="FunctionsAuthenticationBuilder"/> instance to configure authentication schemes.</returns> |
| 39 | + /// <exception cref="ArgumentNullException">When builder is null.</exception> |
| 40 | + public static FunctionsAuthenticationBuilder AddAuthentication( |
30 | 41 | this IFunctionsHostBuilder builder, Action<AuthenticationOptions>? configure)
|
31 | 42 | {
|
32 |
| - if (builder == null) |
| 43 | + if (builder is null) throw new ArgumentNullException(nameof(builder)); |
| 44 | + |
| 45 | + return builder.Services.AddFunctionsAuthentication(configure); |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Configures authentication for the Azure Functions app. It will setup Functions built-in authentication. |
| 50 | + /// </summary> |
| 51 | + /// <param name="configure">The <see cref="AuthenticationOptions"/> configuration logic.</param> |
| 52 | + public static FunctionsAuthenticationBuilder AddFunctionsAuthentication( |
| 53 | + this IServiceCollection services, Action<AuthenticationOptions>? configure) |
| 54 | + { |
| 55 | + var authBuilder = new FunctionsAuthenticationBuilder(services); |
| 56 | + |
| 57 | + if (AuthHelper.EnableAuth) |
33 | 58 | {
|
34 |
| - throw new ArgumentNullException(nameof(builder)); |
| 59 | + EnabledAuthHelper.AddBuiltInFunctionsAuthentication(services); |
35 | 60 | }
|
36 |
| - |
| 61 | + else |
| 62 | + { |
| 63 | + services.AddAuthentication(); |
| 64 | + AuthHelper.AddScriptJwtBearer(authBuilder); |
| 65 | + DisabledAuthHelper.AddScriptAuthLevel(authBuilder); |
| 66 | + DisabledAuthHelper.AddArmToken(authBuilder); |
| 67 | + } |
| 68 | + |
37 | 69 | if (configure != null)
|
38 | 70 | {
|
39 |
| - builder.Services.AddSingleton<IConfigureOptions<AuthenticationOptions>>(provider => |
| 71 | + services.AddSingleton<IConfigureOptions<AuthenticationOptions>>(provider => |
40 | 72 | new ConfigureOptions<AuthenticationOptions>(options =>
|
41 | 73 | {
|
42 | 74 | configure(options);
|
43 | 75 | }));
|
44 | 76 | }
|
45 | 77 |
|
46 |
| - return builder.Services |
47 |
| - .AddAuthentication() |
48 |
| - .AddScriptFunctionsJwtBearer(); |
49 |
| - } |
50 |
| - |
51 |
| - private static AuthenticationBuilder AddScriptFunctionsJwtBearer(this AuthenticationBuilder builder) |
52 |
| - { |
53 |
| - return builder.AddJwtBearer(Constants.WebJobsAuthScheme, options => |
54 |
| - { |
55 |
| - options.Events = new JwtBearerEvents |
56 |
| - { |
57 |
| - OnMessageReceived = c => |
58 |
| - { |
59 |
| - options.TokenValidationParameters = CreateTokenValidationParameters(); |
60 |
| - return Task.CompletedTask; |
61 |
| - }, |
62 |
| - |
63 |
| - OnTokenValidated = c => |
64 |
| - { |
65 |
| - c.Principal.AddIdentity(new ClaimsIdentity(new Claim[] { new Claim(AuthLevelClaimType, AuthorizationLevel.Admin.ToString()) })); |
66 |
| - c.Success(); |
67 |
| - return Task.CompletedTask; |
68 |
| - } |
69 |
| - }; |
70 |
| - |
71 |
| - options.TokenValidationParameters = CreateTokenValidationParameters(); |
72 |
| - }); |
73 |
| - |
74 |
| - TokenValidationParameters CreateTokenValidationParameters() |
75 |
| - { |
76 |
| - var defaultKey = "2d3a0617-f369-492c-ab7a-f21ec1631376"; |
77 |
| - var result = new TokenValidationParameters(); |
78 |
| - |
79 |
| - if (defaultKey != null) |
80 |
| - { |
81 |
| - result.IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(defaultKey)); |
82 |
| - result.ValidateAudience = true; |
83 |
| - result.ValidateIssuer = true; |
84 |
| - result.ValidAudience = string.Format("https://{0}.azurewebsites.net/azurefunctions", "func"); |
85 |
| - result.ValidIssuer = string.Format("https://{0}.scm.azurewebsites.net", "func"); |
86 |
| - } |
87 |
| - |
88 |
| - return result; |
89 |
| - } |
| 78 | + return authBuilder; |
90 | 79 | }
|
91 | 80 | }
|
92 | 81 | }
|
0 commit comments