Vovchikan

Vovchikan

Open Api Spec for bearer Token

I can’t understand where and what i need to write for adding Bearer Token Auth for my API JSON SPEC.

I’ve added security schema in %OpenApiSpex.OpenApi{}

%OpenApi{
      servers: [%Server{url: url}],
      info: %Info{
        title: to_string(Application.spec(:getmsg_api, :description)),
        version: to_string(Application.spec(:getmsg_api, :vsn))
      },
      components: %Components{
        securitySchemes: %{"authorization" => %OpenApiSpex.SecurityScheme{
          type: "apiKey",
          name: "Autorization",
          in: "header"}
        }
      },
      # Populate the paths from a phoenix router
      paths: Paths.from_router(Router)
    }

I’ve added macro security to my Phoenix.Controller. But i don’t understand, what it does.

security [%{}, %{"api_key" => ["write:message", "read:message"]}]

This is one of method from this controller

operation :index,
    summary: "List messages",
    parameters: [
      token: [
        in: :header,
        name: "Authorization",
        schema: %OpenApiSpex.Schema{type: :string},
        required: true,
        example: "Bearer valid_token"
      ]],
    responses: %{
      200 => {"List of messages", "application/json", OpenApi.MessageListResponse},
      401 => {"Permission denied", "application/json", OpenApi.PermissionDeniedResponse}
    }

  def index(conn, _params) do
    messages = Msgs.list_all()
    render(conn, "index.json", messages: messages)
  end

I’m missing something, but don’t know what.

When i’m trying to test method /GET through swaggerui, there is no req_header 'autorization` with value “Bearer some_token_value”

curl from swaggerui

curl -X 'GET' \
  'http://localhost:4000/getmsg/api/messages' \
  -H 'accept: application/json' \
  -H 'x-csrf-token: TCwFJzMqJhoFKAQLClAYIV9ULgwEQD8taKJRWksmULaZ8iJE7akos9GG'

Marked As Solved

Vovchikan

Vovchikan

Solved!

Security scheme in OpenApi{} struct and in macro OpenApiSpex.ControllerSpecs.security/1 must have same key (in my case it is “bearerAuth”)! And i had spelling mistake in field name in my security scheme.

%OpenApi{
      servers: [%Server{url: url}],
      info: %Info{
        title: to_string(Application.spec(:getmsg_api, :description)),
        version: to_string(Application.spec(:getmsg_api, :vsn))
      },
      components: %Components{
        securitySchemes: %{"bearerAuth" => %OpenApiSpex.SecurityScheme{
          type: "apiKey",
          name: "Authorization",
          in: "header"}
        }
      },
      # Populate the paths from a phoenix router
      paths: Paths.from_router(Router)
    }

and in controller

security [%{"bearerAuth" => []}]

so correct curl generated

curl -X 'GET' \
  'http://localhost:4000/getmsg/api/messages' \
  -H 'accept: application/json' \
  -H 'Authorization: Bearer SFMyNTY.g2gDYQFuBgCHP8PphgFiAAFRgA.HAuxlwCHIsGcRibuYnWuDaLOcjx_ZB44RdcAwPPy3xA' \
  -H 'x-csrf-token: NmQ5ETMwXAAjLSk4ESQYL3E-e3gxdEYgU2siii6NEdvirtMHIG5IaG6T'

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40082 209
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement