Skip to content
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

[ASP.Net Core] General support to add scopes for bearer authentication #1984

Merged
merged 11 commits into from
Jun 4, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1051,13 +1051,47 @@ private void processOperation(String resourcePath, String httpMethod, Operation
if (securities != null && securities.isEmpty()) {
continue;
}

Map<String, SecurityScheme> authMethods = getAuthMethods(securities, securitySchemes);
if (authMethods == null || authMethods.isEmpty()) {
authMethods = getAuthMethods(globalSecurities, securitySchemes);
}

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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ using Newtonsoft.Json;
{{/isLibrary}}
using System.ComponentModel.DataAnnotations;
using {{packageName}}.Attributes;
using Microsoft.AspNetCore.Authorization;
using {{modelPackage}};

namespace {{apiPackage}}
Expand All @@ -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}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.ComponentModel.DataAnnotations;
using Org.OpenAPITools.Attributes;
using Org.OpenAPITools.Models;
using Microsoft.AspNetCore.Authorization;

namespace Org.OpenAPITools.Controllers
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.ComponentModel.DataAnnotations;
using Org.OpenAPITools.Attributes;
using Org.OpenAPITools.Models;
using Microsoft.AspNetCore.Authorization;

namespace Org.OpenAPITools.Controllers
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.ComponentModel.DataAnnotations;
using Org.OpenAPITools.Attributes;
using Org.OpenAPITools.Models;
using Microsoft.AspNetCore.Authorization;

namespace Org.OpenAPITools.Controllers
{
Expand Down