https://github.com/elonsoft/cookie_to_jwt_plug
Converts cookie header to jwt token authorization header
https://github.com/elonsoft/cookie_to_jwt_plug
Last synced: 8 months ago
JSON representation
Converts cookie header to jwt token authorization header
- Host: GitHub
- URL: https://github.com/elonsoft/cookie_to_jwt_plug
- Owner: Elonsoft
- License: mit
- Created: 2019-11-18T17:23:44.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-06-30T11:05:44.000Z (almost 6 years ago)
- Last Synced: 2025-09-17T16:54:58.972Z (9 months ago)
- Language: Elixir
- Size: 5.86 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# CookieToJwtPlug
Puts authorization token from cookies to Authorization headers and
do refresh auth cookie.
You can put it in your Guardian pipline and think that you're working
with just jwt token.
```elixir
defmodule YourApp.Guardian.Pipeline do
@moduledoc false
use Guardian.Plug.Pipeline,
otp_app: :your_app,
error_handler: YourApp.Guardian.ErrorHandler,
module: YourApp.Guardian
# Extract token from cookies.
plug CookieToJwtPlug
# If there is a session token, restrict it to an access token and validate it
plug Guardian.Plug.VerifySession, claims: %{"typ" => "access"}
# If there is an authorization header, restrict it to an access token
# and validate it
plug Guardian.Plug.VerifyHeader, claims: %{"typ" => "access"}
# Load the user if either of the verifications worked
plug Guardian.Plug.LoadResource, allow_blank: true
end
```
And also you should put your cookies on authorization:
```elixir
defmodule YourAppWeb.SomeController do
@moduledoc false
# The function that authenticate a user and returns a token for her.
import Auth, only: [authenticate_user: 1]
def login(conn, params) do
with {:ok, token} <- authenticate_user(params) do
conn
|> CookieToJwtPlug.put_session_token(token)
|> render("index.html")
end
end
end
```
## Installation
Add this to your deps:
```elixir
def deps do
[
{:cookie_to_jwt_plug, "~> 0.1"}
]
end
```