Skip to content

Commit

Permalink
Implement auth metods in Symfony swagger-api#5985
Browse files Browse the repository at this point in the history
  • Loading branch information
ksm2 committed Jul 6, 2017
1 parent dcc93f4 commit 04bdd47
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
operations.put("controllerName", toControllerName((String) operations.get("pathPrefix")));
operations.put("symfonyService", toSymfonyService((String) operations.get("pathPrefix")));

HashSet<CodegenSecurity> authMethods = new HashSet<>();
HashSet<String> imports = new HashSet<>();
List<CodegenOperation> operationList = (List<CodegenOperation>) operations.get("operation");
for (CodegenOperation op : operationList) {
Expand Down Expand Up @@ -310,9 +311,15 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
imports.add(exception);
}
}

// Add operation's authentication methods to whole interface
if (op.authMethods != null) {
authMethods.addAll(op.authMethods);
}
}

operations.put("imports", new ArrayList<>(imports));
operations.put("authMethods", authMethods);

return objs;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ namespace {{apiPackage}};
*/
interface {{classname}}
{
{{#authMethods}}

/**
* Sets authentication method {{name}}
*
* @param string $value Value of the {{name}} authentication method.
*
* @return void
*/
public function set{{name}}($value);
{{/authMethods}}
{{#operation}}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,38 @@ class {{controllerName}} extends Controller
*/
public function {{operationId}}Action(Request $request)
{
{{#authMethods}}
// Authentication '{{name}}' required
{{#isApiKey}}
{{#isKeyInHeader}}
// Set key with prefix in header
$security{{name}} = $request->headers->get('{{keyParamName}}');
{{/isKeyInHeader}}
{{#isKeyInQuery}}
// Set key with prefix in query string
$security{{name}} = $request->query->get('{{keyParamName}}');
{{/isKeyInQuery}}
{{/isApiKey}}
{{#isBasic}}
// HTTP basic authentication required
$security{{name}} = $request->headers->get('authorization');
{{/isBasic}}
{{#isOAuth}}
// Oauth required
$security{{name}} = $request->headers->get('authorization');
{{/isOAuth}}
{{/authMethods}}
{{#queryParams}}
// Handle query params
${{paramName}} = $this->fromQuery($request->query->get('{{paramName}}'), '{{dataType}}');
{{/queryParams}}
{{#headerParams}}
// Handle header params
${{paramName}} = $this->fromHeader($request->headers->get('{{paramName}}'), '{{dataType}}');
${{paramName}} = $this->fromHeader($request->headers->get('{{paramName}}'), '{{dataType}}');
{{/headerParams}}
{{#pathParams}}
// Handle path params
${{paramName}} = $this->fromPath($request->attributes->get('{{paramName}}'), '{{dataType}}');
${{paramName}} = $this->fromPath($request->attributes->get('{{paramName}}'), '{{dataType}}');
{{/pathParams}}
{{#formParams}}
{{#isFile}}
Expand Down Expand Up @@ -131,9 +152,15 @@ class {{controllerName}} extends Controller

// Call the API interface
try {
$handler = $this->getApiHandler();
{{#authMethods}}
// Set authentication method '{{name}}'
$handler->set{{name}}($security{{name}});
{{/authMethods}}
{{#returnType}}
// Expecting a return value (exception otherwise)
$result = $this->getApiHandler()->{{operationId}}({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
$result = $handler->{{operationId}}({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});

{{#responses}}
{{^vendorExtensions.x-symfonyExceptionSimple}}
Expand All @@ -148,7 +175,7 @@ class {{controllerName}} extends Controller
{{/returnType}}
{{^returnType}}
// No return type expected; return empty response
$this->getApiHandler()->{{operationId}}({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
$handler->{{operationId}}({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
return new Response('', 204);
{{/returnType}}
{{#responses}}
Expand Down

0 comments on commit 04bdd47

Please sign in to comment.