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

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54260 488
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New

We're in Beta

About us Mission Statement