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
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'
0
Popular in Questions
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
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
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
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
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Other popular topics
Hi!
In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
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
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









