10
10
using System . Threading . Tasks ;
11
11
using System . Web . Http ;
12
12
using System . Web . Http . Controllers ;
13
+ using System . Web . Http . Dependencies ;
13
14
using Microsoft . Azure . WebJobs . Script . Description ;
14
15
using Microsoft . Azure . WebJobs . Script . WebHost . Filters ;
15
16
using Microsoft . Azure . WebJobs . Script . WebHost . WebHooks ;
@@ -32,36 +33,52 @@ public FunctionsController(WebScriptHostManager scriptHostManager, WebHookReceiv
32
33
33
34
public override async Task < HttpResponseMessage > ExecuteAsync ( HttpControllerContext controllerContext , CancellationToken cancellationToken )
34
35
{
35
- HttpRequestMessage request = controllerContext . Request ;
36
-
37
- // First see if the request maps to an HTTP function
38
- FunctionDescriptor function = _scriptHostManager . GetHttpFunctionOrNull ( request ) ;
36
+ var request = controllerContext . Request ;
37
+ var function = _scriptHostManager . GetHttpFunctionOrNull ( request ) ;
39
38
if ( function == null )
40
39
{
40
+ // request does not map to an HTTP function
41
41
return new HttpResponseMessage ( HttpStatusCode . NotFound ) ;
42
42
}
43
+ request . SetProperty ( ScriptConstants . AzureFunctionsHttpFunctionKey , function ) ;
43
44
44
- // Determine the authorization level of the request
45
- ISecretManager secretManager = controllerContext . Configuration . DependencyResolver . GetService < ISecretManager > ( ) ;
46
- var settings = controllerContext . Configuration . DependencyResolver . GetService < WebHostSettings > ( ) ;
47
- var authorizationLevel = settings . IsAuthDisabled
48
- ? AuthorizationLevel . Admin
49
- : await AuthorizationLevelAttribute . GetAuthorizationLevelAsync ( request , secretManager , functionName : function . Name ) ;
50
- request . SetAuthorizationLevel ( authorizationLevel ) ;
51
-
45
+ var authorizationLevel = await DetermineAuthorizationLevelAsync ( request , function , controllerContext . Configuration . DependencyResolver ) ;
52
46
if ( function . Metadata . IsExcluded ||
53
- ( function . Metadata . IsDisabled && authorizationLevel != AuthorizationLevel . Admin ) )
47
+ ( function . Metadata . IsDisabled && authorizationLevel != AuthorizationLevel . Admin ) )
54
48
{
55
49
// disabled functions are not publicly addressable w/o Admin level auth,
56
50
// and excluded functions are also ignored here (though the check above will
57
51
// already exclude them)
58
52
return new HttpResponseMessage ( HttpStatusCode . NotFound ) ;
59
53
}
60
54
61
- // Dispatch the request
55
+ Func < HttpRequestMessage , CancellationToken , Task < HttpResponseMessage > > processRequestHandler = async ( req , ct ) =>
56
+ {
57
+ return await ProcessRequestAsync ( req , function , ct ) ;
58
+ } ;
59
+ return await _scriptHostManager . HttpRequestManager . ProcessRequestAsync ( request , processRequestHandler , cancellationToken ) ;
60
+ }
61
+
62
+ public static async Task < AuthorizationLevel > DetermineAuthorizationLevelAsync ( HttpRequestMessage request , FunctionDescriptor function , IDependencyResolver resolver )
63
+ {
64
+ var secretManager = resolver . GetService < ISecretManager > ( ) ;
65
+ var settings = resolver . GetService < WebHostSettings > ( ) ;
66
+
67
+ var authorizationLevel = settings . IsAuthDisabled
68
+ ? AuthorizationLevel . Admin
69
+ : await AuthorizationLevelAttribute . GetAuthorizationLevelAsync ( request , secretManager , functionName : function . Name ) ;
70
+ request . SetAuthorizationLevel ( authorizationLevel ) ;
71
+
72
+ return authorizationLevel ;
73
+ }
74
+
75
+ private async Task < HttpResponseMessage > ProcessRequestAsync ( HttpRequestMessage request , FunctionDescriptor function , CancellationToken cancellationToken )
76
+ {
62
77
HttpTriggerBindingMetadata httpFunctionMetadata = ( HttpTriggerBindingMetadata ) function . Metadata . InputBindings . FirstOrDefault ( p => string . Compare ( "HttpTrigger" , p . Type , StringComparison . OrdinalIgnoreCase ) == 0 ) ;
63
78
bool isWebHook = ! string . IsNullOrEmpty ( httpFunctionMetadata . WebHookType ) ;
79
+ var authorizationLevel = request . GetAuthorizationLevel ( ) ;
64
80
HttpResponseMessage response = null ;
81
+
65
82
if ( isWebHook )
66
83
{
67
84
if ( authorizationLevel == AuthorizationLevel . Admin )
0 commit comments