|
4 | 4 | using Azure.DataApiBuilder.Config.ObjectModel; |
5 | 5 | using Microsoft.AspNetCore.Http; |
6 | 6 |
|
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 |
8 | 14 | { |
9 | 15 | /// <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. |
12 | 17 | /// </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; } |
19 | 19 |
|
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); |
27 | 27 |
|
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); |
37 | 37 |
|
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); |
48 | 48 |
|
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); |
58 | 58 |
|
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); |
70 | 70 |
|
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); |
78 | 78 |
|
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); |
88 | 88 |
|
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); |
98 | 98 |
|
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) |
110 | 112 | { |
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 | + } |
122 | 115 |
|
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; |
124 | 121 | } |
| 122 | + |
| 123 | + return new List<string>(); |
125 | 124 | } |
126 | 125 | } |
0 commit comments