forked from epochtalk/epochtalk_server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from slickage/api-key
Api key
- Loading branch information
Showing
5 changed files
with
39 additions
and
1 deletion.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule EpochtalkServerWeb.Plugs.CheckAPIKey do | ||
@moduledoc """ | ||
Plug that tracks user IP address for PUT POST or PATCH operations | ||
""" | ||
use Plug.Builder | ||
import Plug.Conn | ||
|
||
@env Mix.env() | ||
@methods ~w(GET POST PUT PATCH) | ||
|
||
plug(:check_api_key) | ||
|
||
@doc """ | ||
Validates and checks API key sent from frontend against one stored on backend | ||
""" | ||
def check_api_key(conn, _opts) do | ||
%{method: method} = conn | ||
|
||
if method in @methods and @env != :test do | ||
try_verify(conn) | ||
else | ||
conn | ||
end | ||
end | ||
|
||
defp try_verify(conn) do | ||
config = Application.get_env(:epochtalk_server, :frontend_config) | ||
api_key = config[:api_key] | ||
[req_api_key] = get_req_header(conn, "api-key") | ||
|
||
if api_key == req_api_key, | ||
do: conn, | ||
else: raise(Plug.BadRequestError) | ||
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