-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds Guardian based token authentication for data endpoint
- Loading branch information
Showing
11 changed files
with
139 additions
and
20 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
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 |
---|---|---|
@@ -1,10 +1,12 @@ | ||
defmodule Brood.HTTPRouter do | ||
use Plug.Router | ||
alias Brood.Resource.Account | ||
alias Brood.Resource.Data | ||
|
||
plug :match | ||
plug :dispatch | ||
|
||
forward "/account", to: Account.Router | ||
forward "/data", to: Data.Router | ||
|
||
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
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,17 @@ | ||
defmodule Brood.Resource.Account.GuardianSerializer do | ||
@behaviour Guardian.Serializer | ||
|
||
alias Brood.Resource.Account | ||
|
||
def for_token(account = %Account{}) do | ||
id = BSON.ObjectId.encode!(account._id) | ||
{:ok, "Account:#{id}" } | ||
end | ||
def for_token(_), do: { :error, "Unknown resource type" } | ||
|
||
def from_token("Account:" <> id) do | ||
account = Account.from_id(id) | ||
{ :ok, account } | ||
end | ||
def from_token(_), do: { :error, "Unknown resource type" } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
defmodule Brood.Resource.Data.Query do | ||
use PlugRest.Resource | ||
require Logger | ||
|
||
def allowed_methods(conn, state) do | ||
{["POST"], conn, state} | ||
end | ||
|
||
def content_types_accepted(conn, state) do | ||
{[{{"application", "json", :*}, :from_json}], conn, state} | ||
end | ||
|
||
def from_json(conn, state) do | ||
account = Guardian.Plug.current_resource(conn) | ||
Logger.info("#{inspect account}") | ||
Logger.info("#{inspect conn.params}") | ||
#Query InfluxDB | ||
{true, conn, state} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
defmodule Brood.Resource.Data.Router do | ||
use PlugRest.Router | ||
use Plug.ErrorHandler | ||
alias Brood.Resource.Data | ||
require Logger | ||
|
||
plug Plug.Parsers, parsers: [:json], json_decoder: Poison | ||
plug Guardian.Plug.VerifyHeader, realm: "Bearer" | ||
plug Guardian.Plug.LoadResource | ||
plug Guardian.Plug.EnsureAuthenticated, handler: __MODULE__ | ||
plug :match | ||
plug :dispatch | ||
|
||
resource "/:type/:timeframe", Data.Query | ||
|
||
def unauthenticated(conn, params) do | ||
Logger.error "Unauthenticated: #{inspect conn}" | ||
Logger.error "Unauthenticated: #{inspect params}" | ||
conn |> send_error(401, "{\"error\": \"Unauthorized\"}") | ||
end | ||
|
||
def handle_errors(conn, other) do | ||
Logger.error("#{inspect other}") | ||
send_error(conn, 500, "{\"error\": \"Ruh Roh!\"}") | ||
end | ||
|
||
def send_error(conn, status, data) do | ||
conn | ||
|> Plug.Conn.put_resp_content_type("application/json") | ||
|> send_resp(status, data) | ||
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