Skip to content

Commit dcf3f42

Browse files
MBcomwing328
authored andcommitted
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
1 parent dc35439 commit dcf3f42

File tree

6 files changed

+41
-2
lines changed

6 files changed

+41
-2
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenSecurity.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class CodegenSecurity {
3838
// Oauth specific
3939
public String flow, authorizationUrl, tokenUrl;
4040
public List<Map<String, Object>> scopes;
41-
public Boolean isCode, isPassword, isApplication, isImplicit;
41+
public Boolean isCode, isPassword, isApplication, isImplicit, hasScopes;
4242

4343
@Override
4444
public String toString() {

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java

+34
Original file line numberDiff line numberDiff line change
@@ -1051,13 +1051,47 @@ private void processOperation(String resourcePath, String httpMethod, Operation
10511051
if (securities != null && securities.isEmpty()) {
10521052
continue;
10531053
}
1054+
10541055
Map<String, SecurityScheme> authMethods = getAuthMethods(securities, securitySchemes);
10551056
if (authMethods == null || authMethods.isEmpty()) {
10561057
authMethods = getAuthMethods(globalSecurities, securitySchemes);
10571058
}
10581059

10591060
if (authMethods != null && !authMethods.isEmpty()) {
10601061
codegenOperation.authMethods = config.fromSecurity(authMethods);
1062+
List<Map<String, Object>> scopes = new ArrayList<Map<String, Object>>();
1063+
if (codegenOperation.authMethods != null){
1064+
for (CodegenSecurity security : codegenOperation.authMethods){
1065+
if (security != null && security.isBasicBearer != null && security.isBasicBearer &&
1066+
securities != null){
1067+
for (SecurityRequirement req : securities){
1068+
if (req == null) continue;
1069+
for (String key : req.keySet()){
1070+
if (security.name != null && key.equals(security.name)){
1071+
int count = 0;
1072+
for (String sc : req.get(key)){
1073+
Map<String, Object> scope = new HashMap<String, Object>();
1074+
scope.put("scope", sc);
1075+
scope.put("description", "");
1076+
count++;
1077+
if (req.get(key) != null && count < req.get(key).size()){
1078+
scope.put("hasMore", "true");
1079+
} else {
1080+
scope.put("hasMore", null);
1081+
}
1082+
scopes.add(scope);
1083+
}
1084+
//end this inner for
1085+
break;
1086+
}
1087+
}
1088+
1089+
}
1090+
security.hasScopes = scopes.size() > 0;
1091+
security.scopes = scopes;
1092+
}
1093+
}
1094+
}
10611095
codegenOperation.hasAuthMethods = true;
10621096
}
10631097

modules/openapi-generator/src/main/resources/aspnetcore/2.1/controller.mustache

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ using Newtonsoft.Json;
1414
{{/isLibrary}}
1515
using System.ComponentModel.DataAnnotations;
1616
using {{packageName}}.Attributes;
17+
using Microsoft.AspNetCore.Authorization;
1718
using {{modelPackage}};
1819

1920
namespace {{apiPackage}}
@@ -32,7 +33,8 @@ namespace {{apiPackage}}
3233
/// <param name="{{paramName}}">{{description}}</param>{{/allParams}}{{#responses}}
3334
/// <response code="{{code}}">{{message}}</response>{{/responses}}
3435
[{{httpMethod}}]
35-
[Route("{{{basePathWithoutHost}}}{{{path}}}")]
36+
[Route("{{{basePathWithoutHost}}}{{{path}}}")]{{#hasAuthMethods}}{{#authMethods}}{{#isBasicBearer}}
37+
[Authorize{{#hasScopes}}(Roles = "{{#scopes}}{{scope}}{{#hasMore}},{{/hasMore}}{{/scopes}}"){{/hasScopes}}]{{/isBasicBearer}}{{/authMethods}}{{/hasAuthMethods}}
3638
[ValidateModelState]{{#useSwashbuckle}}
3739
[SwaggerOperation("{{operationId}}")]{{#responses}}{{#dataType}}
3840
[SwaggerResponse(statusCode: {{code}}, type: typeof({{&dataType}}), description: "{{message}}")]{{/dataType}}{{^dataType}}{{/dataType}}{{/responses}}{{/useSwashbuckle}}{{^useSwashbuckle}}{{#responses}}{{#dataType}}

samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/PetApi.cs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.ComponentModel.DataAnnotations;
1818
using Org.OpenAPITools.Attributes;
1919
using Org.OpenAPITools.Models;
20+
using Microsoft.AspNetCore.Authorization;
2021

2122
namespace Org.OpenAPITools.Controllers
2223
{

samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/StoreApi.cs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.ComponentModel.DataAnnotations;
1818
using Org.OpenAPITools.Attributes;
1919
using Org.OpenAPITools.Models;
20+
using Microsoft.AspNetCore.Authorization;
2021

2122
namespace Org.OpenAPITools.Controllers
2223
{

samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/UserApi.cs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.ComponentModel.DataAnnotations;
1818
using Org.OpenAPITools.Attributes;
1919
using Org.OpenAPITools.Models;
20+
using Microsoft.AspNetCore.Authorization;
2021

2122
namespace Org.OpenAPITools.Controllers
2223
{

0 commit comments

Comments
 (0)