mirror of
https://github.com/mgerb/classic-wow-forums
synced 2026-01-12 01:52:49 +00:00
init
This commit is contained in:
31
lib/myapp/application.ex
Normal file
31
lib/myapp/application.ex
Normal file
@@ -0,0 +1,31 @@
|
||||
defmodule MyApp.Application do
|
||||
use Application
|
||||
|
||||
# See https://hexdocs.pm/elixir/Application.html
|
||||
# for more information on OTP Applications
|
||||
def start(_type, _args) do
|
||||
import Supervisor.Spec
|
||||
|
||||
# Define workers and child supervisors to be supervised
|
||||
children = [
|
||||
# Start the Ecto repository
|
||||
supervisor(MyApp.Repo, []),
|
||||
# Start the endpoint when the application starts
|
||||
supervisor(MyAppWeb.Endpoint, []),
|
||||
# Start your own worker by calling: MyApp.Worker.start_link(arg1, arg2, arg3)
|
||||
# worker(MyApp.Worker, [arg1, arg2, arg3]),
|
||||
]
|
||||
|
||||
# See https://hexdocs.pm/elixir/Supervisor.html
|
||||
# for other strategies and supported options
|
||||
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
|
||||
Supervisor.start_link(children, opts)
|
||||
end
|
||||
|
||||
# Tell Phoenix to update the endpoint configuration
|
||||
# whenever the application is updated.
|
||||
def config_change(changed, _new, removed) do
|
||||
MyAppWeb.Endpoint.config_change(changed, removed)
|
||||
:ok
|
||||
end
|
||||
end
|
||||
33
lib/myapp/auth/auth.ex
Normal file
33
lib/myapp/auth/auth.ex
Normal file
@@ -0,0 +1,33 @@
|
||||
defmodule MyApp.Guardian do
|
||||
use Guardian, otp_app: :myapp
|
||||
|
||||
def subject_for_token(resource, _claims) do
|
||||
# You can use any value for the subject of your token but
|
||||
# it should be useful in retrieving the resource later, see
|
||||
# how it being used on `resource_from_claims/1` function.
|
||||
# A unique `id` is a good subject, a non-unique email address
|
||||
# is a poor subject.
|
||||
sub = to_string(resource["id"])
|
||||
{:ok, sub}
|
||||
end
|
||||
|
||||
# def subject_for_token(_, _) do
|
||||
# {:error, :reason_for_error}
|
||||
# end
|
||||
|
||||
def resource_from_claims(claims) do
|
||||
# Here we'll look up our resource from the claims, the subject can be
|
||||
# found in the `"sub"` key. In `above subject_for_token/2` we returned
|
||||
# the resource id so here we'll rely on that to look it up.
|
||||
id = claims["sub"]
|
||||
# resource = MyApp.get_resource_by_id(id)
|
||||
IO.inspect(claims)
|
||||
resource = id
|
||||
{:ok, resource}
|
||||
end
|
||||
|
||||
# def resource_from_claims(_claims) do
|
||||
# {:error, :reason_for_error}
|
||||
# end
|
||||
|
||||
end
|
||||
10
lib/myapp/auth/handler.ex
Normal file
10
lib/myapp/auth/handler.ex
Normal file
@@ -0,0 +1,10 @@
|
||||
defmodule MyApp.Auth.ErrorHandler do
|
||||
import Plug.Conn
|
||||
alias MyAppWeb.Response
|
||||
|
||||
def auth_error(conn, {type, _reason}, _opts) do
|
||||
conn
|
||||
|> put_status(401)
|
||||
|> Response.json(to_string(type))
|
||||
end
|
||||
end
|
||||
9
lib/myapp/auth/pipeline.ex
Normal file
9
lib/myapp/auth/pipeline.ex
Normal file
@@ -0,0 +1,9 @@
|
||||
defmodule MyApp.Guardian.AuthPipeline.JSON do
|
||||
use Guardian.Plug.Pipeline, otp_app: :MyApp,
|
||||
module: MyApp.Guardian,
|
||||
error_handler: MyApp.Auth.ErrorHandler
|
||||
|
||||
plug Guardian.Plug.VerifyHeader, realm: "Bearer"
|
||||
plug Guardian.Plug.EnsureAuthenticated
|
||||
plug Guardian.Plug.LoadResource, allow_blank: true
|
||||
end
|
||||
50
lib/myapp/battle_net/auth.ex
Normal file
50
lib/myapp/battle_net/auth.ex
Normal file
@@ -0,0 +1,50 @@
|
||||
defmodule MyApp.BattleNet.Auth do
|
||||
alias MyApp.BattleNet.User
|
||||
alias MyApp.JWT
|
||||
|
||||
def token_uri, do: "https://us.battle.net/oauth/token"
|
||||
|
||||
def get_token(code) do
|
||||
client_id = Application.get_env(:myapp, :bnet_client_id)
|
||||
client_secret = Application.get_env(:myapp, :bnet_client_secret)
|
||||
redirect_uri = Application.get_env(:myapp, :bnet_redirect_uri)
|
||||
|
||||
req_options = [hackney: [basic_auth: {client_id, client_secret}]]
|
||||
|
||||
HTTPoison.request(:post, token_uri, get_req_body(code), [], req_options)
|
||||
|> parse_body
|
||||
|> parse_token
|
||||
|> validate_user
|
||||
|> generate_jwt
|
||||
end
|
||||
|
||||
defp parse_body({:error, err}), do: {:error, err}
|
||||
defp parse_body({:ok, %HTTPoison.Response{body: body}}), do: Poison.decode(body)
|
||||
|
||||
defp parse_token({:ok, %{"access_token" => token}}), do: {:ok, token}
|
||||
defp parse_token({:ok, %{"error" => err}}), do: {:error, err}
|
||||
defp parse_token({:error, err}), do: {:error, "Authentication error"}
|
||||
|
||||
defp validate_user({:error, err}), do: {:error, err}
|
||||
defp validate_user({:ok, token}), do: User.get_user(token)
|
||||
|
||||
defp generate_jwt({:error, err}), do: {:error, err}
|
||||
defp generate_jwt({:ok, user}) do
|
||||
case JWT.get_jwt(user, user) do
|
||||
{:ok, token} -> {:ok, Map.merge(user, %{"token" => token})}
|
||||
{:error, err} -> {:error, err}
|
||||
end
|
||||
end
|
||||
|
||||
defp get_req_body(code) do
|
||||
redirect_uri = Application.get_env(:myapp, :bnet_redirect_uri)
|
||||
{:form, [
|
||||
grant_type: "authorization_code",
|
||||
scope: "wow.profile",
|
||||
code: code,
|
||||
redirect_uri: redirect_uri,
|
||||
]}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
16
lib/myapp/battle_net/user.ex
Normal file
16
lib/myapp/battle_net/user.ex
Normal file
@@ -0,0 +1,16 @@
|
||||
defmodule MyApp.BattleNet.User do
|
||||
defstruct id: nil, battletag: nil
|
||||
|
||||
def api_url, do: "https://us.api.battle.net"
|
||||
|
||||
def get_user(access_token) do
|
||||
case HTTPoison.get(resource_url("account/user", access_token)) do
|
||||
{:ok, %HTTPoison.Response{body: body}} -> {:ok, Poison.decode!(body, as: Battlenet.User)}
|
||||
{:error, err} -> {:error, err}
|
||||
end
|
||||
end
|
||||
|
||||
defp resource_url(path, access_token) do
|
||||
"#{api_url}/#{path}?access_token=#{access_token}"
|
||||
end
|
||||
end
|
||||
14
lib/myapp/jwt.ex
Normal file
14
lib/myapp/jwt.ex
Normal file
@@ -0,0 +1,14 @@
|
||||
defmodule MyApp.JWT do
|
||||
alias MyApp.Guardian
|
||||
|
||||
# ~1 year
|
||||
defp tokenTTL(), do: {52, :weeks}
|
||||
|
||||
def get_jwt(user, claims) do
|
||||
case Guardian.encode_and_sign(user, claims, ttl: tokenTTL()) do
|
||||
{:ok, token, _claims} -> {:ok, token}
|
||||
{:error, _token, _claims} -> {:error, "JWT error"}
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
11
lib/myapp/repo.ex
Normal file
11
lib/myapp/repo.ex
Normal file
@@ -0,0 +1,11 @@
|
||||
defmodule MyApp.Repo do
|
||||
use Ecto.Repo, otp_app: :myapp
|
||||
|
||||
@doc """
|
||||
Dynamically loads the repository url from the
|
||||
DATABASE_URL environment variable.
|
||||
"""
|
||||
def init(_, opts) do
|
||||
{:ok, Keyword.put(opts, :url, System.get_env("DATABASE_URL"))}
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user