@@ -13,18 +13,22 @@ public class AuthorizationMiddleware : OcelotMiddleware
13
13
private readonly RequestDelegate _next ;
14
14
private readonly IClaimsAuthorizer _claimsAuthorizer ;
15
15
private readonly IScopesAuthorizer _scopesAuthorizer ;
16
+ private readonly IRolesAuthorizer _rolesAuthorizer ;
16
17
17
18
public AuthorizationMiddleware ( RequestDelegate next ,
18
19
IClaimsAuthorizer claimsAuthorizer ,
19
20
IScopesAuthorizer scopesAuthorizer ,
21
+ IRolesAuthorizer rolesAuthorizer ,
20
22
IOcelotLoggerFactory loggerFactory )
21
23
: base ( loggerFactory . CreateLogger < AuthorizationMiddleware > ( ) )
22
24
{
23
25
_next = next ;
24
26
_claimsAuthorizer = claimsAuthorizer ;
25
27
_scopesAuthorizer = scopesAuthorizer ;
26
28
}
27
-
29
+ // Note roles is a duplicate of scopes - should refactor based on type
30
+ // Note scopes and roles are processed as OR
31
+ // todo create logic to process policies that we use in the API
28
32
public async Task Invoke ( HttpContext httpContext )
29
33
{
30
34
var downstreamRoute = httpContext . Items . DownstreamRoute ( ) ;
@@ -33,7 +37,7 @@ public async Task Invoke(HttpContext httpContext)
33
37
{
34
38
Logger . LogInformation ( "route is authenticated scopes must be checked" ) ;
35
39
36
- var authorized = _scopesAuthorizer . Authorize ( httpContext . User , downstreamRoute . AuthenticationOptions . AllowedScopes ) ;
40
+ var authorized = _scopesAuthorizer . Authorize ( httpContext . User , downstreamRoute . AuthenticationOptions . AllowedScopes , downstreamRoute . AuthenticationOptions . ScopeKey ) ;
37
41
38
42
if ( authorized . IsError )
39
43
{
@@ -56,6 +60,33 @@ public async Task Invoke(HttpContext httpContext)
56
60
}
57
61
}
58
62
63
+ if ( ! IsOptionsHttpMethod ( httpContext ) && IsAuthenticatedRoute ( downstreamRoute ) )
64
+ {
65
+ Logger . LogInformation ( "route and scope is authenticated role must be checked" ) ;
66
+
67
+ var authorizedRole = _rolesAuthorizer . Authorize ( httpContext . User , downstreamRoute . AuthenticationOptions . RequiredRole , downstreamRoute . AuthenticationOptions . RoleKey ) ;
68
+
69
+ if ( authorizedRole . IsError )
70
+ {
71
+ Logger . LogWarning ( "error authorizing user roles" ) ;
72
+
73
+ httpContext . Items . UpsertErrors ( authorizedRole . Errors ) ;
74
+ return ;
75
+ }
76
+
77
+ if ( IsAuthorized ( authorizedRole ) )
78
+ {
79
+ Logger . LogInformation ( "user has the required role and is authorized calling next authorization checks" ) ;
80
+ }
81
+ else
82
+ {
83
+ Logger . LogWarning ( "user does not have the required role and is not authorized setting pipeline error" ) ;
84
+
85
+ httpContext . Items . SetError ( new UnauthorizedError (
86
+ $ "{ httpContext . User . Identity . Name } unable to access { downstreamRoute . UpstreamPathTemplate . OriginalValue } ") ) ;
87
+ }
88
+ }
89
+
59
90
if ( ! IsOptionsHttpMethod ( httpContext ) && IsAuthorizedRoute ( downstreamRoute ) )
60
91
{
61
92
Logger . LogInformation ( "route is authorized" ) ;
0 commit comments