-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for role based security in ATC generator. #26
Comments
The problem with using |
We should just to be sure, that we follow https://swagger.io/docs/specification/authentication/ 4 definitions with sub-schemas for securitySchemes (=> 6 definitions):
And as I read it, is only |
Started working on this. |
The above suggestion is not valid. It will be parsed by the Microsoft.OpenApi.Readers Nuget Package as an invalid reference identifier, since theres no SecurityScheme named x-atc-azure-ad-application-roles and we cant extend with our own custom security scheme. Theres also only the possibility to have 1 Security Scheme pr. type. Also we cant reference the Extensibility point with the roles under the oAuth2Sample SecuritySchema from the OpenApiOperation (PathItem). |
The solution ended up with us defining 3 new extension "tags":
At the root level of the specification we specify the available roles and auth-schemes possible to use in paths (controllers) /path-items (actions/methods). Validation is in place when reading the YAML file to ensure that any path/path-item does not have roles and/or auth-schemes defined, which are not defined globally. Other validations are also in place to ensure that the combination of the 3 new extensions "tags" are set correctly. The 3 extension "tags" can be specified at path/path-item level as can be seen in the below example. If all path-items under a given path all have x-authentication-required set to true, then a [Authorize] header will still be added to the generated controller class. Otherwise [Authorize(Roles=x,y,z)] and [AllowAnonymous] will be applied the necessary places on the actions/methods in the controller. Authentication-Schemes and Authorize-Roles defined at path/controller level is taken into consideration when generating [Authorize] attributes for path-item/action/method level. Example specification using the new extension "tags": info:
title: DEMO API
description: DEMO API
version: v1
contact:
name: ATC
license:
name: MIT
servers:
- url: /api/v1
description: Api version 1.0
x-authorize-roles:
- api.execute.read
- api.execute.write
- admin
- operator
x-authentication-schemes:
- OpenIddict.Validation.AspNetCore
tags:
- name: DEMO
description: ''
paths:
/data-templates:
x-authentication-required: true
x-authorize-roles: [operator]
x-authentication-schemes: [OpenIddict.Validation.AspNetCore]
get:
summary: Returns a list of the groups data templates
operationId: getDataTemplates
x-authorize-roles: [admin,operator]
x-authentication-schemes: [OpenIddict.Validation.AspNetCore]
post:
summary: Create a new data template
operationId: createDataTemplate
x-authentication-required: false
'/data-templates/{dataTemplateId}':
x-authentication-required: true
x-authorize-roles: [operator]
get:
summary: Returns a specific data template
operationId: getDataTemplateById
x-authentication-required: true
x-authorize-roles: [api.execute.read]
delete:
summary: Removes a specific data template
operationId: deleteDataTemplateById
put:
summary: Updates a specific data template
operationId: updateDataTemplateById
'/data-templates/{dataTemplateId}/tags':
post:
summary: Creates a new data template tag
operationId: createDataTemplateTag
x-authorize-roles: [api.execute.read]
delete:
summary: Deletes a data template tag
operationId: deleteDataTemplateTag
x-authentication-schemes: [OpenIddict.Validation.AspNetCore]
'/data-templates/{dataTemplateId}/tags/{dataTemplateTagId}':
x-authentication-required: false
put:
summary: Updates a specific data template tag
operationId: updateDataTemplateTagById
components:
schemas: {}
requestBodies: {}
responses: {}
securitySchemes: {} |
Implemented with PR #168 |
Tasks:
Design
Setup in yaml file will be done using security schemes and the first type to support is OAuth2 with client id + client secret as authetication. Custom roles will be used to support access based on rights.
This can be done like shown below:
Definition of the security schema:
Usage in a operation:
The generated code will affect the controller class(es) and will add an Authorize attribute to each method that is setup with security in its operation definition. The Roles property in the Authorize attribute maps to OAuth2 scopes and these are defined in the securiy scheme.
As the Roles property of the Authorize attribute treats multiple attributes on the same method as AND requirements it will be needed to have only 1 attribute per method to support OR and the roles will have to be comma separated. To make the code a bit nicer a static class with scope definitions are generated for each defined security schema.
Make sure Swagger UI shows the required roles per operation.
Validation:
ATC generator will validate the setup:
The generated code could look something like this:
The text was updated successfully, but these errors were encountered: