-
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a8b34d4
commit a596d16
Showing
5 changed files
with
77 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
defmodule PlexusWeb.APIAuthPlug do | ||
@behaviour Plug | ||
|
||
import Plug.Conn | ||
|
||
@endpoint PlexusWeb.Endpoint | ||
@salt "device auth" | ||
|
||
@impl Plug | ||
def init(opts) do | ||
opts | ||
end | ||
|
||
@impl Plug | ||
def call(conn, _opts) do | ||
with {:ok, token} <- fetch_authorization_token(conn) do | ||
case Phoenix.Token.verify(@endpoint, @salt, token) do | ||
{:ok, %{device_id: _device_id, email: _email}} -> | ||
conn | ||
|
||
{:error, :expired} -> | ||
conn | ||
|> send_resp(401, "Token is expired") | ||
|> halt() | ||
|
||
{:error, :invalid} -> | ||
conn | ||
|> send_resp(401, "Invalid token") | ||
|> halt() | ||
|
||
{:error, :missing} -> | ||
conn | ||
|> send_resp(401, "Token is missing") | ||
|> halt() | ||
end | ||
end | ||
end | ||
|
||
defp fetch_authorization_token(conn) do | ||
case get_req_header(conn, "authorization") do | ||
["Bearer " <> token] -> | ||
{:ok, token} | ||
|
||
_ -> | ||
send_resp(conn, 401, "Authorization header is missing") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters