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

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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New

We're in Beta

About us Mission Statement