Skip to content

Commit 734f1b7

Browse files
gmottajrgersonAniruddh25
authored
Move to file scoped namesspace project Auth (#1727)
It was requested to move to file scoped namespace. --------- Co-authored-by: gerson <[email protected]> Co-authored-by: Aniruddh Munde <[email protected]>
1 parent 8b57c82 commit 734f1b7

File tree

6 files changed

+376
-382
lines changed

6 files changed

+376
-382
lines changed

src/Auth/AuthorizationMetadataHelpers.cs

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,70 +3,69 @@
33

44
using Azure.DataApiBuilder.Config.ObjectModel;
55

6-
namespace Azure.DataApiBuilder.Auth
6+
namespace Azure.DataApiBuilder.Auth;
7+
8+
/// <summary>
9+
/// Represents the permission metadata of an entity.
10+
/// An entity's top-level permission structure is a collection
11+
/// of roles.
12+
/// </summary>
13+
public class EntityMetadata
714
{
815
/// <summary>
9-
/// Represents the permission metadata of an entity.
10-
/// An entity's top-level permission structure is a collection
11-
/// of roles.
16+
/// Given the key (roleName) returns the associated RoleMetadata object.
17+
/// To retrieve all roles associated with an entity -> RoleToOperationMap.Keys().
18+
/// Since the roleNames are case insensitive, we use IEqualityComparer for ignoring
19+
/// the case.
1220
/// </summary>
13-
public class EntityMetadata
14-
{
15-
/// <summary>
16-
/// Given the key (roleName) returns the associated RoleMetadata object.
17-
/// To retrieve all roles associated with an entity -> RoleToOperationMap.Keys().
18-
/// Since the roleNames are case insensitive, we use IEqualityComparer for ignoring
19-
/// the case.
20-
/// </summary>
21-
public Dictionary<string, RoleMetadata> RoleToOperationMap { get; set; } = new(StringComparer.OrdinalIgnoreCase);
22-
23-
/// <summary>
24-
/// Field to operation to role mapping.
25-
/// Given the key (Field aka. column name) returns a key/value collection of operation to Roles
26-
/// i.e. ID column
27-
/// Key(field): id -> Dictionary(operations)
28-
/// each entry in the dictionary contains operation to role map.
29-
/// Create: permitted in {Role1, Role2, ..., RoleN}
30-
/// Delete: permitted in {Role1, RoleN}
31-
/// </summary>
32-
public Dictionary<string, Dictionary<EntityActionOperation, List<string>>> FieldToRolesMap { get; set; } = new();
21+
public Dictionary<string, RoleMetadata> RoleToOperationMap { get; set; } = new(StringComparer.OrdinalIgnoreCase);
3322

34-
/// <summary>
35-
/// Given the key (operation) returns a collection of roles
36-
/// defining config permissions for the operation.
37-
/// i.e. Read operation is permitted in {Role1, Role2, ..., RoleN}
38-
/// </summary>
39-
public Dictionary<EntityActionOperation, List<string>> OperationToRolesMap { get; set; } = new();
23+
/// <summary>
24+
/// Field to operation to role mapping.
25+
/// Given the key (Field aka. column name) returns a key/value collection of operation to Roles
26+
/// i.e. ID column
27+
/// Key(field): id -> Dictionary(operations)
28+
/// each entry in the dictionary contains operation to role map.
29+
/// Create: permitted in {Role1, Role2, ..., RoleN}
30+
/// Delete: permitted in {Role1, RoleN}
31+
/// </summary>
32+
public Dictionary<string, Dictionary<EntityActionOperation, List<string>>> FieldToRolesMap { get; set; } = new();
4033

41-
/// <summary>
42-
/// Set of Http verbs enabled for Stored Procedure entities that have their REST endpoint enabled.
43-
/// </summary>
44-
public HashSet<SupportedHttpVerb> StoredProcedureHttpVerbs { get; set; } = new();
45-
}
34+
/// <summary>
35+
/// Given the key (operation) returns a collection of roles
36+
/// defining config permissions for the operation.
37+
/// i.e. Read operation is permitted in {Role1, Role2, ..., RoleN}
38+
/// </summary>
39+
public Dictionary<EntityActionOperation, List<string>> OperationToRolesMap { get; set; } = new();
4640

4741
/// <summary>
48-
/// Represents the permission metadata of a role
49-
/// A role's top-level permission structure is a collection of
50-
/// Operations allowed for that role: Create, Read, Update, Delete, All (wildcard operation)
42+
/// Set of Http verbs enabled for Stored Procedure entities that have their REST endpoint enabled.
5143
/// </summary>
52-
public class RoleMetadata
53-
{
54-
/// <summary>
55-
/// Given the key (operation) returns the associated OperationMetadata object.
56-
/// </summary>
57-
public Dictionary<EntityActionOperation, OperationMetadata> OperationToColumnMap { get; set; } = new();
58-
}
44+
public HashSet<SupportedHttpVerb> StoredProcedureHttpVerbs { get; set; } = new();
45+
}
5946

47+
/// <summary>
48+
/// Represents the permission metadata of a role
49+
/// A role's top-level permission structure is a collection of
50+
/// Operations allowed for that role: Create, Read, Update, Delete, All (wildcard operation)
51+
/// </summary>
52+
public class RoleMetadata
53+
{
6054
/// <summary>
61-
/// Represents the permission metadata of an operation
62-
/// An operation lists both columns that are included and/or excluded
63-
/// for that operation.
55+
/// Given the key (operation) returns the associated OperationMetadata object.
6456
/// </summary>
65-
public class OperationMetadata
66-
{
67-
public string? DatabasePolicy { get; set; }
68-
public HashSet<string> Included { get; set; } = new();
69-
public HashSet<string> Excluded { get; set; } = new();
70-
public HashSet<string> AllowedExposedColumns { get; set; } = new();
71-
}
57+
public Dictionary<EntityActionOperation, OperationMetadata> OperationToColumnMap { get; set; } = new();
58+
}
59+
60+
/// <summary>
61+
/// Represents the permission metadata of an operation
62+
/// An operation lists both columns that are included and/or excluded
63+
/// for that operation.
64+
/// </summary>
65+
public class OperationMetadata
66+
{
67+
public string? DatabasePolicy { get; set; }
68+
public HashSet<string> Included { get; set; } = new();
69+
public HashSet<string> Excluded { get; set; } = new();
70+
public HashSet<string> AllowedExposedColumns { get; set; } = new();
7271
}

src/Auth/IAuthorizationResolver.cs

Lines changed: 102 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -4,123 +4,122 @@
44
using Azure.DataApiBuilder.Config.ObjectModel;
55
using Microsoft.AspNetCore.Http;
66

7-
namespace Azure.DataApiBuilder.Auth
7+
namespace Azure.DataApiBuilder.Auth;
8+
9+
/// <summary>
10+
/// Interface for authorization decision-making. Each method performs lookups within a
11+
/// structure representing permissions defined in the runtime config.
12+
/// </summary>
13+
public interface IAuthorizationResolver
814
{
915
/// <summary>
10-
/// Interface for authorization decision-making. Each method performs lookups within a
11-
/// structure representing permissions defined in the runtime config.
16+
/// Representation of authorization permissions for each entity in the runtime config.
1217
/// </summary>
13-
public interface IAuthorizationResolver
14-
{
15-
/// <summary>
16-
/// Representation of authorization permissions for each entity in the runtime config.
17-
/// </summary>
18-
public Dictionary<string, EntityMetadata> EntityPermissionsMap { get; }
18+
public Dictionary<string, EntityMetadata> EntityPermissionsMap { get; }
1919

20-
/// <summary>
21-
/// Checks for the existence of the client role header in httpContext.Request.Headers
22-
/// and evaluates that header against the authenticated (httpContext.User)'s roles
23-
/// </summary>
24-
/// <param name="httpContext">Contains request headers and metadata of the authenticated user.</param>
25-
/// <returns>True, if client role header exists and matches authenticated user's roles.</returns>
26-
public bool IsValidRoleContext(HttpContext httpContext);
20+
/// <summary>
21+
/// Checks for the existence of the client role header in httpContext.Request.Headers
22+
/// and evaluates that header against the authenticated (httpContext.User)'s roles
23+
/// </summary>
24+
/// <param name="httpContext">Contains request headers and metadata of the authenticated user.</param>
25+
/// <returns>True, if client role header exists and matches authenticated user's roles.</returns>
26+
public bool IsValidRoleContext(HttpContext httpContext);
2727

28-
/// <summary>
29-
/// Checks if the permissions collection of the requested entity
30-
/// contains an entry for the role defined in the client role header.
31-
/// </summary>
32-
/// <param name="entityIdentifier">Entity from request. This could be the name of the entity or it could be the GraphQL type name, depending on the entry point.</param>
33-
/// <param name="roleName">Role defined in client role header</param>
34-
/// <param name="operation">Operation type: Create, Read, Update, Delete</param>
35-
/// <returns>True, if a matching permission entry is found.</returns>
36-
public bool AreRoleAndOperationDefinedForEntity(string entityIdentifier, string roleName, EntityActionOperation operation);
28+
/// <summary>
29+
/// Checks if the permissions collection of the requested entity
30+
/// contains an entry for the role defined in the client role header.
31+
/// </summary>
32+
/// <param name="entityIdentifier">Entity from request. This could be the name of the entity or it could be the GraphQL type name, depending on the entry point.</param>
33+
/// <param name="roleName">Role defined in client role header</param>
34+
/// <param name="operation">Operation type: Create, Read, Update, Delete</param>
35+
/// <returns>True, if a matching permission entry is found.</returns>
36+
public bool AreRoleAndOperationDefinedForEntity(string entityIdentifier, string roleName, EntityActionOperation operation);
3737

38-
/// <summary>
39-
/// Any columns referenced in a request's headers, URL(filter/orderby/routes), and/or body
40-
/// are compared against the include/excluded column permission defined for the entityName->roleName->operation
41-
/// </summary>
42-
/// <param name="entityIdentifier">Entity from request</param>
43-
/// <param name="roleName">Role defined in client role header</param>
44-
/// <param name="operation">Operation type: Create, Read, Update, Delete</param>
45-
/// <param name="columns">Compiled list of any column referenced in a request</param>
46-
/// <returns></returns>
47-
public bool AreColumnsAllowedForOperation(string entityIdentifier, string roleName, EntityActionOperation operation, IEnumerable<string> columns);
38+
/// <summary>
39+
/// Any columns referenced in a request's headers, URL(filter/orderby/routes), and/or body
40+
/// are compared against the include/excluded column permission defined for the entityName->roleName->operation
41+
/// </summary>
42+
/// <param name="entityIdentifier">Entity from request</param>
43+
/// <param name="roleName">Role defined in client role header</param>
44+
/// <param name="operation">Operation type: Create, Read, Update, Delete</param>
45+
/// <param name="columns">Compiled list of any column referenced in a request</param>
46+
/// <returns></returns>
47+
public bool AreColumnsAllowedForOperation(string entityIdentifier, string roleName, EntityActionOperation operation, IEnumerable<string> columns);
4848

49-
/// <summary>
50-
/// Method to return the list of exposed columns for the given combination of
51-
/// entityName, roleName, operation.
52-
/// </summary>
53-
/// <param name="entityName">Entity from request</param>
54-
/// <param name="roleName">Role defined in client role header</param>
55-
/// <param name="operation">Operation type: Create, Read, Update, Delete</param>
56-
/// <returns></returns>
57-
public IEnumerable<string> GetAllowedExposedColumns(string entityName, string roleName, EntityActionOperation operation);
49+
/// <summary>
50+
/// Method to return the list of exposed columns for the given combination of
51+
/// entityName, roleName, operation.
52+
/// </summary>
53+
/// <param name="entityName">Entity from request</param>
54+
/// <param name="roleName">Role defined in client role header</param>
55+
/// <param name="operation">Operation type: Create, Read, Update, Delete</param>
56+
/// <returns></returns>
57+
public IEnumerable<string> GetAllowedExposedColumns(string entityName, string roleName, EntityActionOperation operation);
5858

59-
/// <summary>
60-
/// Retrieves the policy of an operation within an entity's role entry
61-
/// within the permissions section of the runtime config, and tries to process
62-
/// the policy.
63-
/// </summary>
64-
/// <param name="entityName">Entity from request.</param>
65-
/// <param name="roleName">Role defined in client role header.</param>
66-
/// <param name="operation">Operation type: Create, Read, Update, Delete.</param>
67-
/// <param name="httpContext">Contains token claims of the authenticated user used in policy evaluation.</param>
68-
/// <returns>Returns the parsed policy, if successfully processed, or an exception otherwise.</returns>
69-
public string ProcessDBPolicy(string entityName, string roleName, EntityActionOperation operation, HttpContext httpContext);
59+
/// <summary>
60+
/// Retrieves the policy of an operation within an entity's role entry
61+
/// within the permissions section of the runtime config, and tries to process
62+
/// the policy.
63+
/// </summary>
64+
/// <param name="entityName">Entity from request.</param>
65+
/// <param name="roleName">Role defined in client role header.</param>
66+
/// <param name="operation">Operation type: Create, Read, Update, Delete.</param>
67+
/// <param name="httpContext">Contains token claims of the authenticated user used in policy evaluation.</param>
68+
/// <returns>Returns the parsed policy, if successfully processed, or an exception otherwise.</returns>
69+
public string ProcessDBPolicy(string entityName, string roleName, EntityActionOperation operation, HttpContext httpContext);
7070

71-
/// <summary>
72-
/// Get list of roles defined for entity within runtime configuration.. This is applicable for GraphQL when creating authorization
73-
/// directive on Object type.
74-
/// </summary>
75-
/// <param name="entityName">Name of entity.</param>
76-
/// <returns>Collection of role names.</returns>
77-
public IEnumerable<string> GetRolesForEntity(string entityName);
71+
/// <summary>
72+
/// Get list of roles defined for entity within runtime configuration.. This is applicable for GraphQL when creating authorization
73+
/// directive on Object type.
74+
/// </summary>
75+
/// <param name="entityName">Name of entity.</param>
76+
/// <returns>Collection of role names.</returns>
77+
public IEnumerable<string> GetRolesForEntity(string entityName);
7878

79-
/// <summary>
80-
/// Returns the collection of roles which can perform {operation} the provided field.
81-
/// Applicable to GraphQL field directive @authorize on ObjectType fields.
82-
/// </summary>
83-
/// <param name="entityName">EntityName whose operationMetadata will be searched.</param>
84-
/// <param name="field">Field to lookup operation permissions</param>
85-
/// <param name="operation">Specific operation to get collection of roles</param>
86-
/// <returns>Collection of role names allowed to perform operation on Entity's field.</returns>
87-
public IEnumerable<string> GetRolesForField(string entityName, string field, EntityActionOperation operation);
79+
/// <summary>
80+
/// Returns the collection of roles which can perform {operation} the provided field.
81+
/// Applicable to GraphQL field directive @authorize on ObjectType fields.
82+
/// </summary>
83+
/// <param name="entityName">EntityName whose operationMetadata will be searched.</param>
84+
/// <param name="field">Field to lookup operation permissions</param>
85+
/// <param name="operation">Specific operation to get collection of roles</param>
86+
/// <returns>Collection of role names allowed to perform operation on Entity's field.</returns>
87+
public IEnumerable<string> GetRolesForField(string entityName, string field, EntityActionOperation operation);
8888

89-
/// <summary>
90-
/// Returns whether the httpVerb (GET, POST, PUT, PATCH, DELETE) is allowed to be performed
91-
/// on the stored procedure (represented by entityName) for the role: roleName.
92-
/// </summary>
93-
/// <param name="entityName"></param>
94-
/// <param name="roleName"></param>
95-
/// <param name="httpVerb"></param>
96-
/// <returns>True if the execution of the stored procedure is permitted. Otherwise, false.</returns>
97-
public bool IsStoredProcedureExecutionPermitted(string entityName, string roleName, SupportedHttpVerb httpVerb);
89+
/// <summary>
90+
/// Returns whether the httpVerb (GET, POST, PUT, PATCH, DELETE) is allowed to be performed
91+
/// on the stored procedure (represented by entityName) for the role: roleName.
92+
/// </summary>
93+
/// <param name="entityName"></param>
94+
/// <param name="roleName"></param>
95+
/// <param name="httpVerb"></param>
96+
/// <returns>True if the execution of the stored procedure is permitted. Otherwise, false.</returns>
97+
public bool IsStoredProcedureExecutionPermitted(string entityName, string roleName, SupportedHttpVerb httpVerb);
9898

99-
/// <summary>
100-
/// Returns a list of roles which define permissions for the provided operation.
101-
/// i.e. list of roles which allow the operation 'Read' on entityName.
102-
/// </summary>
103-
/// <param name="entityName">Entity to lookup permissions</param>
104-
/// <param name="operation">Operation to lookup applicable roles</param>
105-
/// <returns>Collection of roles. Empty list if entityPermissionsMap is null.</returns>
106-
public static IEnumerable<string> GetRolesForOperation(
107-
string entityName,
108-
EntityActionOperation operation,
109-
Dictionary<string, EntityMetadata>? entityPermissionsMap)
99+
/// <summary>
100+
/// Returns a list of roles which define permissions for the provided operation.
101+
/// i.e. list of roles which allow the operation 'Read' on entityName.
102+
/// </summary>
103+
/// <param name="entityName">Entity to lookup permissions</param>
104+
/// <param name="operation">Operation to lookup applicable roles</param>
105+
/// <returns>Collection of roles. Empty list if entityPermissionsMap is null.</returns>
106+
public static IEnumerable<string> GetRolesForOperation(
107+
string entityName,
108+
EntityActionOperation operation,
109+
Dictionary<string, EntityMetadata>? entityPermissionsMap)
110+
{
111+
if (entityName is null)
110112
{
111-
if (entityName is null)
112-
{
113-
throw new ArgumentNullException(paramName: nameof(entityName));
114-
}
115-
116-
if (entityPermissionsMap is not null &&
117-
entityPermissionsMap[entityName].OperationToRolesMap.TryGetValue(operation, out List<string>? roleList) &&
118-
roleList is not null)
119-
{
120-
return roleList;
121-
}
113+
throw new ArgumentNullException(paramName: nameof(entityName));
114+
}
122115

123-
return new List<string>();
116+
if (entityPermissionsMap is not null &&
117+
entityPermissionsMap[entityName].OperationToRolesMap.TryGetValue(operation, out List<string>? roleList) &&
118+
roleList is not null)
119+
{
120+
return roleList;
124121
}
122+
123+
return new List<string>();
125124
}
126125
}

0 commit comments

Comments
 (0)