1
votes

Good day,

I have a Symfony API project with FOSRestBundle and NelmioApiDocBundle. I dont know how to add a security annotation with FOS Rest. I am using OAuth v2 so my security is based on the following:

apiKey: accessToken, refreshToken.

This is my nelmio api bundle config in app/config:

nelmio_api_doc:
    areas:
        path_patterns: # an array of regexps
            - ^/api/v1(?!/doc$)
    documentation:
        info:
            title: Ads api documentation
            description: Swagger api documentation
            version: 1.0.0
        securityDefinitions:
            api_key:
                type: apiKey
                description: "Your Json Web Token, dont forget to preprend 'Bearer'"
                name: Authorization
                in: header
        security:
            api_key: []

And an example of a route in one of my controllers:

/**
 * @Rest\View(statusCode=200, serializerGroups={"rentAdList", "time"})
 * @Rest\Get("", name="api_v1_user_ad_list")
 *
 * @SWG\Tag(name="user_ad")
 * @SWG\Response(
 *     response=200,
 *     description="Display ad",
 *     @SWG\Schema(
 *      @Model(type=AdBundle\Entity\RentAd::class, groups={"rentAdList", "time"})
 *     )
 * )
 *
 * @return RentAd[]
 */
public function listAction()
{
    $user           = $this->get('security.token_storage')->getToken()->getUser();
    $rentAdDataProvider = $this->get('ad.data_provider.rent_ad_data_provider');
    $rentAds        = $rentAdDataProvider->getRentAdsByUser($user);

    return $rentAds;
}

So my question is how to use a Swagger security annotation with api_key with role as USER_ROLE

I tried to add:

/**
 * @Rest\View(statusCode=200, serializerGroups={"rentAdList", "time"})
 * @Rest\Get("", name="api_v1_user_ad_list")
 *
 * @SWG\Tag(name="user_ad")
 * @SWG\Response(
 *     response=200,
 *     description="Display ad",
 *     @SWG\Schema(
 *      @Model(type=AdBundle\Entity\RentAd::class, groups={"rentAdList", "time"})
 *     )
 * )
 * @SWG\SecurityScheme(name="apiKey")
 *
 * @return RentAd[]
 */

Bun in this case I've got an exception :

Using the annotation "Swagger\Annotations\SecurityScheme" as a root annotation in "ApiBundle\Controller\Api\V1\UserRentAdController::listAction()" is not allowed.

Please help me.

1

1 Answers

-1
votes

For a moment I found the only one solution which works for me.
I added two annotations in Controller

* @SWG\Parameter(name="Authorization", in="header", required=true, type="string", default="Bearer accessToken", description="Authorization")
* @Security(name="Bearer")`

In this case in api cods you'll have the additional parameter with Authorisation : Bearer {accessToken}