|
| 1 | +--- |
| 2 | +title: "Authorization" |
| 3 | +description: "Adds the integration with authorization middleware and system components to validate and grant the access to the resources." |
| 4 | +lead: "" |
| 5 | +date: 2023-05-13T15:40:19+02:00 |
| 6 | +lastmod: 2025-10-11T16:18:19Z |
| 7 | +draft: false |
| 8 | +images: [] |
| 9 | +menu: |
| 10 | + library: |
| 11 | + identifier: "authorization" |
| 12 | + name: "Authorization" |
| 13 | + parent: "library" |
| 14 | +weight: 2 |
| 15 | +toc: true |
| 16 | +--- |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +```bash |
| 21 | +dotnet add package Genocs.Auth |
| 22 | +``` |
| 23 | + |
| 24 | +## Dependencies |
| 25 | + |
| 26 | +- Genocs.Core |
| 27 | +- Genocs.Security |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +This module provides the integration with authorization middleware and system components to validate and grant the access to the resources. |
| 32 | + |
| 33 | +The Genocs Authorization module provides the following features: |
| 34 | + |
| 35 | +- Authorization middleware |
| 36 | +- Authorization system components |
| 37 | +- Authorization policies |
| 38 | +- Authorization handlers |
| 39 | +- Authorization attributes |
| 40 | + |
| 41 | + |
| 42 | +In order to provide multiple authorization strategies and to be able to authorize the access to the resource for different purposes like FrontEnd Application and/or MCP services, the Genocs Authorization module provides the following features: |
| 43 | + |
| 44 | +- Authorization for FrontEnd Application |
| 45 | +- Authorization for MCP services |
| 46 | + |
| 47 | + |
| 48 | +The Authorization is made by using endpoint decorators and policies. |
| 49 | + |
| 50 | +## How to enable the authorization |
| 51 | +Extend `IGenocsBuilder` with `AddOpenIdJwt()` that will register the required services. |
| 52 | + |
| 53 | +```cs |
| 54 | +public static IGenocsBuilder RegisterGenocs(this IGenocsBuilder builder) |
| 55 | +{ |
| 56 | + builder.AddOpenIdJwt() |
| 57 | + // Other services. |
| 58 | + |
| 59 | + |
| 60 | + // Return the Genocs builder to be used to chain other services |
| 61 | + return builder; |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +public static IApplicationBuilder UseCustomAuthentication(this IApplicationBuilder builder) |
| 66 | +{ |
| 67 | + // Custom middleware to allow both Jwt and ApiKey authentication |
| 68 | + builder.UseMiddleware<JwtOrApiKeyAuthenticationMiddleware>(); |
| 69 | + |
| 70 | + // Return the Application builder to be used to chain other services |
| 71 | + return builder; |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +### Endpoint decorators |
| 76 | + |
| 77 | +Upone the registration of the authorization services, you can use the following decorators to authorize the access to the resources: |
| 78 | + |
| 79 | +```cs |
| 80 | +[ApiKeyOrJwtAuthorize] |
| 81 | +``` |
| 82 | + |
| 83 | +```cs |
| 84 | +[Authorize(Roles = "Editor")] |
| 85 | +``` |
| 86 | + |
| 87 | +### Example |
| 88 | + |
| 89 | +The example below shows how to authorize the access to the resources using the `Authorize` decorator, you can also use the `ApiKeyOrJwtAuthorize` decorator to authorize the access to the resources using the `ApiKey` or `Jwt` authentication. |
| 90 | + |
| 91 | + |
| 92 | +```cs |
| 93 | + /// <summary> |
| 94 | + /// Retrieves authorization information for the authenticated user. |
| 95 | + /// </summary> |
| 96 | + /// <remarks> |
| 97 | + /// <para> |
| 98 | + /// This endpoint demonstrates JWT authorization by returning the authorization header |
| 99 | + /// from the authenticated request. It serves as a simple test to verify that JWT |
| 100 | + /// authentication is working correctly. |
| 101 | + /// </para> |
| 102 | + /// <para>**Important:** This endpoint requires the user to have the 'Editor' role.</para> |
| 103 | + /// <para>Sample request:</para> |
| 104 | + /// <para> |
| 105 | + /// GET /JwtAuthorized |
| 106 | + /// Authorization: Bearer your-jwt-token-here |
| 107 | + /// </para> |
| 108 | + /// |
| 109 | + /// </remarks> |
| 110 | + /// <returns>A string containing the authorization header value from the request.</returns> |
| 111 | + /// <response code="200">Returns the authorization header information successfully.</response> |
| 112 | + /// <response code="401">Unauthorized - Invalid or missing JWT token.</response> |
| 113 | + /// <response code="403">Forbidden - Valid token but user lacks the required 'Editor' role.</response> |
| 114 | + [HttpGet("")] |
| 115 | + [Authorize(Roles = "Editor")] |
| 116 | + [ProducesResponseType(typeof(string), StatusCodes.Status200OK)] |
| 117 | + [ProducesResponseType(StatusCodes.Status401Unauthorized)] |
| 118 | + [ProducesResponseType(StatusCodes.Status403Forbidden)] |
| 119 | + public async Task<IActionResult> GetAuthorizedAsync() |
| 120 | + { |
| 121 | + _logger.LogInformation("Processing authorized request for user"); |
| 122 | + |
| 123 | + return await Task.Run(() => Ok($"Done! Authorization is: {HttpContext.Request.Headers.Authorization}")); |
| 124 | + } |
| 125 | +``` |
| 126 | + |
| 127 | + |
| 128 | +## Options |
| 129 | + |
| 130 | +The default section name for the authorization settings is `authorization`. The following options are available: |
| 131 | + |
| 132 | +- `enabled` - If true then the authorization is enabled. |
| 133 | +- `devApiKey` - The API key used to authorize the client in the development environment. |
| 134 | +- `apiKeys` - A list of API keys that can be used to authorize the client. |
| 135 | + |
| 136 | + |
| 137 | +## Settings |
| 138 | + |
| 139 | +Use the following settings in the `appsettings.json` file according to your needs |
| 140 | + |
| 141 | +```json |
| 142 | + "authorization": { |
| 143 | + "enabled": true, |
| 144 | + "devApiKey": "dev_api_key", |
| 145 | + "apiKeys": [ |
| 146 | + "prod_api_key_1", |
| 147 | + "prod_api_key_2" |
| 148 | + ] |
| 149 | + } |
| 150 | +``` |
| 151 | + |
| 152 | +### Default settings |
| 153 | + |
| 154 | +Default settings for some variable has bee overwritten with the following configuration. |
| 155 | + |
| 156 | +```json |
| 157 | + "authorization": { |
| 158 | + "enabled": true, |
| 159 | + "devApiKey": "abc_123", |
| 160 | + "apiKeys": [ |
| 161 | + "prod_api_key_1", |
| 162 | + "prod_api_key_2" |
| 163 | + ] |
| 164 | + } |
| 165 | +``` |
0 commit comments