general support to add scopes for bearer auth too (#1984)

* general support to add scopes for bearer auth too
implemented authorize workflow in aspnet core too

* petstore update

* fix missing )

* multi roles fix

* null pointer error prevention

* null point exception fixes

* null pointer fixes

* npe fix

* solved line break issue
This commit is contained in:
MBcom 2019-06-04 17:03:15 +02:00 committed by William Cheng
parent dc3543981e
commit dcf3f420f9
6 changed files with 41 additions and 2 deletions

View File

@ -38,7 +38,7 @@ public class CodegenSecurity {
// Oauth specific
public String flow, authorizationUrl, tokenUrl;
public List<Map<String, Object>> scopes;
public Boolean isCode, isPassword, isApplication, isImplicit;
public Boolean isCode, isPassword, isApplication, isImplicit, hasScopes;
@Override
public String toString() {

View File

@ -1051,6 +1051,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
if (securities != null && securities.isEmpty()) {
continue;
}
Map<String, SecurityScheme> authMethods = getAuthMethods(securities, securitySchemes);
if (authMethods == null || authMethods.isEmpty()) {
authMethods = getAuthMethods(globalSecurities, securitySchemes);
@ -1058,6 +1059,39 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
if (authMethods != null && !authMethods.isEmpty()) {
codegenOperation.authMethods = config.fromSecurity(authMethods);
List<Map<String, Object>> scopes = new ArrayList<Map<String, Object>>();
if (codegenOperation.authMethods != null){
for (CodegenSecurity security : codegenOperation.authMethods){
if (security != null && security.isBasicBearer != null && security.isBasicBearer &&
securities != null){
for (SecurityRequirement req : securities){
if (req == null) continue;
for (String key : req.keySet()){
if (security.name != null && key.equals(security.name)){
int count = 0;
for (String sc : req.get(key)){
Map<String, Object> scope = new HashMap<String, Object>();
scope.put("scope", sc);
scope.put("description", "");
count++;
if (req.get(key) != null && count < req.get(key).size()){
scope.put("hasMore", "true");
} else {
scope.put("hasMore", null);
}
scopes.add(scope);
}
//end this inner for
break;
}
}
}
security.hasScopes = scopes.size() > 0;
security.scopes = scopes;
}
}
}
codegenOperation.hasAuthMethods = true;
}

View File

@ -14,6 +14,7 @@ using Newtonsoft.Json;
{{/isLibrary}}
using System.ComponentModel.DataAnnotations;
using {{packageName}}.Attributes;
using Microsoft.AspNetCore.Authorization;
using {{modelPackage}};
namespace {{apiPackage}}
@ -32,7 +33,8 @@ namespace {{apiPackage}}
/// <param name="{{paramName}}">{{description}}</param>{{/allParams}}{{#responses}}
/// <response code="{{code}}">{{message}}</response>{{/responses}}
[{{httpMethod}}]
[Route("{{{basePathWithoutHost}}}{{{path}}}")]
[Route("{{{basePathWithoutHost}}}{{{path}}}")]{{#hasAuthMethods}}{{#authMethods}}{{#isBasicBearer}}
[Authorize{{#hasScopes}}(Roles = "{{#scopes}}{{scope}}{{#hasMore}},{{/hasMore}}{{/scopes}}"){{/hasScopes}}]{{/isBasicBearer}}{{/authMethods}}{{/hasAuthMethods}}
[ValidateModelState]{{#useSwashbuckle}}
[SwaggerOperation("{{operationId}}")]{{#responses}}{{#dataType}}
[SwaggerResponse(statusCode: {{code}}, type: typeof({{&dataType}}), description: "{{message}}")]{{/dataType}}{{^dataType}}{{/dataType}}{{/responses}}{{/useSwashbuckle}}{{^useSwashbuckle}}{{#responses}}{{#dataType}}

View File

@ -17,6 +17,7 @@ using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
using Org.OpenAPITools.Attributes;
using Org.OpenAPITools.Models;
using Microsoft.AspNetCore.Authorization;
namespace Org.OpenAPITools.Controllers
{

View File

@ -17,6 +17,7 @@ using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
using Org.OpenAPITools.Attributes;
using Org.OpenAPITools.Models;
using Microsoft.AspNetCore.Authorization;
namespace Org.OpenAPITools.Controllers
{

View File

@ -17,6 +17,7 @@ using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
using Org.OpenAPITools.Attributes;
using Org.OpenAPITools.Models;
using Microsoft.AspNetCore.Authorization;
namespace Org.OpenAPITools.Controllers
{