1
- namespace AzureFunctions . Extensions . OpenIDConnect
1
+ using Microsoft . AspNetCore . Authorization ;
2
+
3
+ namespace AzureFunctions . Extensions . OpenIDConnect
2
4
{
3
5
using System . Net ;
4
6
using System . Threading ;
@@ -11,28 +13,49 @@ public class AuthorizeFilter : FunctionInvocationFilterAttribute
11
13
private readonly IHttpContextAccessor _httpContextAccessor ;
12
14
private readonly IAuthenticationService _authenticationService ;
13
15
private readonly IRouteGuardian _routeGuardian ;
16
+ private readonly IAuthorizationService _authorizationService ;
17
+ private readonly IAuthorizationRequirementsRetriever _requirementsRetriever ;
14
18
15
- public AuthorizeFilter ( IHttpContextAccessor httpContextAccessor , IAuthenticationService authenticationService , IRouteGuardian routeGuardian )
19
+ public AuthorizeFilter ( IHttpContextAccessor httpContextAccessor ,
20
+ IAuthenticationService authenticationService , IRouteGuardian routeGuardian ,
21
+ IAuthorizationService authorizationService , IAuthorizationRequirementsRetriever requirementsRetriever )
16
22
{
17
23
_httpContextAccessor = httpContextAccessor ;
18
24
_authenticationService = authenticationService ;
19
25
_routeGuardian = routeGuardian ;
26
+ _authorizationService = authorizationService ;
27
+ _requirementsRetriever = requirementsRetriever ;
20
28
}
21
29
22
30
public override async Task OnExecutingAsync ( FunctionExecutingContext executingContext , CancellationToken cancellationToken )
23
31
{
24
- if ( await _routeGuardian . ShouldAuthorize ( executingContext . FunctionName ) )
32
+ if ( _routeGuardian . IsProtectedRoute ( executingContext . FunctionName ) )
25
33
{
26
34
var httpContext = _httpContextAccessor . HttpContext ;
27
35
28
36
// Authenticate the user
29
- var authResult = await _authenticationService . AuthenticateAsync ( httpContext . Request . Headers ) ;
30
-
31
- if ( authResult . Failed )
37
+ var authenticationResult = await _authenticationService . AuthenticateAsync ( httpContext . Request . Headers ) ;
38
+
39
+ if ( authenticationResult . Failed )
32
40
{
33
41
await Unauthorized ( httpContext , cancellationToken ) ;
34
42
return ;
35
43
}
44
+
45
+ httpContext . User = authenticationResult . User ;
46
+
47
+ var attribute = _routeGuardian . GetAuthorizationConfiguration ( executingContext . FunctionName ) ;
48
+ var requirements = _requirementsRetriever . ForAttribute ( attribute ) ;
49
+
50
+ if ( requirements != null )
51
+ {
52
+ var authorizationResult = await _authorizationService . AuthorizeAsync ( httpContext . User , null , requirements ) ;
53
+ if ( ! authorizationResult . Succeeded )
54
+ {
55
+ await Forbidden ( httpContext , authorizationResult . Failure , cancellationToken ) ;
56
+ }
57
+ }
58
+
36
59
}
37
60
await base . OnExecutingAsync ( executingContext , cancellationToken ) ;
38
61
}
@@ -42,5 +65,11 @@ private async Task Unauthorized(HttpContext httpContext, CancellationToken cance
42
65
httpContext . Response . StatusCode = ( int ) HttpStatusCode . Unauthorized ;
43
66
await httpContext . Response . WriteAsync ( string . Empty , cancellationToken ) ;
44
67
}
68
+
69
+ private async Task Forbidden ( HttpContext httpContext , AuthorizationFailure failure , CancellationToken cancellationToken )
70
+ {
71
+ httpContext . Response . StatusCode = ( int ) HttpStatusCode . Forbidden ;
72
+ await httpContext . Response . WriteAsync ( failure . FailedRequirements . ToString ( ) , cancellationToken ) ;
73
+ }
45
74
}
46
75
}
0 commit comments