Skip to content

Commit

Permalink
Fix credo issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Doridian committed Mar 29, 2024
1 parent d2e3b4f commit eb3ece8
Show file tree
Hide file tree
Showing 15 changed files with 403 additions and 398 deletions.
2 changes: 1 addition & 1 deletion lib/space_age_api/models/player.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule SpaceAgeApi.Models.Player do
timestamps()
end

def public_fields() do
def public_fields do
[:steamid, :name, :score, :playtime, :faction_name, :is_faction_leader]
end

Expand Down
2 changes: 1 addition & 1 deletion lib/space_age_api/models/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule SpaceAgeApi.Models.Server do
field :hidden, :boolean

field :location, :string, virtual: true

timestamps()
end

Expand Down
55 changes: 27 additions & 28 deletions lib/space_age_api/models/server_config.ex
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
defmodule SpaceAgeApi.Models.ServerConfig do
@moduledoc false
use Ecto.Schema
import Ecto.Changeset
alias SpaceAgeApi.Util

@primary_key false
schema "server_configs" do
field :authkey, :string
field :name, :string
field :location, :string
field :rcon_password, :string
field :steam_account_token, :string

timestamps()
end

@doc false
def changeset(server, attrs) do
server
|> cast(attrs, [:name, :authkey, :location, :rcon_password, :steam_account_token])
|> validate_required([:name, :authkey, :location])
|> unique_constraint(:name)
|> unique_constraint(:authkey)
|> put_change(:updated_at, Util.naive_date_time())
end
end

defmodule SpaceAgeApi.Models.ServerConfig do
@moduledoc false
use Ecto.Schema
import Ecto.Changeset
alias SpaceAgeApi.Util

@primary_key false
schema "server_configs" do
field :authkey, :string
field :name, :string
field :location, :string
field :rcon_password, :string
field :steam_account_token, :string

timestamps()
end

@doc false
def changeset(server, attrs) do
server
|> cast(attrs, [:name, :authkey, :location, :rcon_password, :steam_account_token])
|> validate_required([:name, :authkey, :location])
|> unique_constraint(:name)
|> unique_constraint(:authkey)
|> put_change(:updated_at, Util.naive_date_time())
end
end
44 changes: 22 additions & 22 deletions lib/space_age_api/plug/cache.ex
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
defmodule SpaceAgeApi.Plug.Cache do
@moduledoc """
Adds Cache-Control headers when needed
"""
import Plug.Conn

def init(options), do: options

def call(conn, opts) do
conn
|> put_cache_control(opts[:time])
end

defp put_cache_control(conn, nil) do
conn
|> put_cache_control(300)
end
defp put_cache_control(conn, time) do
conn
|> put_resp_header("cache-control", "public, max-age=#{time}")
end
end
defmodule SpaceAgeApi.Plug.Cache do
@moduledoc """
Adds Cache-Control headers when needed
"""
import Plug.Conn

def init(options), do: options

def call(conn, opts) do
conn
|> put_cache_control(opts[:time])
end

defp put_cache_control(conn, nil) do
conn
|> put_cache_control(300)
end
defp put_cache_control(conn, time) do
conn
|> put_resp_header("cache-control", "public, max-age=#{time}")
end
end
100 changes: 50 additions & 50 deletions lib/space_age_api/plug/raw_body_reader.ex
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
defmodule SpaceAgeApi.Plug.RawBodyReader do
@moduledoc """
A body reader that caches raw request body for later use.
This module is intended to be used as the `:body_reader` option of `Plug.Parsers`.
Note that caching is only enabled for specific paths. See `enabled_for?/1`.
"""

@raw_body_key :raw_body

def read_body(%Plug.Conn{} = conn, opts \\ []) do
case Plug.Conn.read_body(conn, opts) do
{:ok, binary, conn} ->
{:ok, binary, maybe_store_body_chunk(conn, binary)}

{:more, binary, conn} ->
{:more, binary, maybe_store_body_chunk(conn, binary)}

{:error, reason} ->
{:error, reason}
end
end

defp enabled_for?(conn) do
case conn.path_info do
["v2" | ["discord" | _rest]] -> true
_ -> false
end
end

defp maybe_store_body_chunk(conn, chunk) do
if enabled_for?(conn) do
store_body_chunk(conn, chunk)
else
conn
end
end

def store_body_chunk(%Plug.Conn{} = conn, chunk) when is_binary(chunk) do
chunks = conn.private[@raw_body_key] || []
Plug.Conn.put_private(conn, @raw_body_key, [chunk | chunks])
end

def get_raw_body(%Plug.Conn{} = conn) do
case conn.private[@raw_body_key] do
nil -> nil
chunks -> chunks |> Enum.reverse() |> Enum.join("")
end
end
end
defmodule SpaceAgeApi.Plug.RawBodyReader do
@moduledoc """
A body reader that caches raw request body for later use.
This module is intended to be used as the `:body_reader` option of `Plug.Parsers`.
Note that caching is only enabled for specific paths. See `enabled_for?/1`.
"""

@raw_body_key :raw_body

def read_body(%Plug.Conn{} = conn, opts \\ []) do
case Plug.Conn.read_body(conn, opts) do
{:ok, binary, conn} ->
{:ok, binary, maybe_store_body_chunk(conn, binary)}

{:more, binary, conn} ->
{:more, binary, maybe_store_body_chunk(conn, binary)}

{:error, reason} ->
{:error, reason}
end
end

defp enabled_for?(conn) do
case conn.path_info do
["v2" | ["discord" | _rest]] -> true
_ -> false
end
end

defp maybe_store_body_chunk(conn, chunk) do
if enabled_for?(conn) do
store_body_chunk(conn, chunk)
else
conn
end
end

def store_body_chunk(%Plug.Conn{} = conn, chunk) when is_binary(chunk) do
chunks = conn.private[@raw_body_key] || []
Plug.Conn.put_private(conn, @raw_body_key, [chunk | chunks])
end

def get_raw_body(%Plug.Conn{} = conn) do
case conn.private[@raw_body_key] do
nil -> nil
chunks -> chunks |> Enum.reverse() |> Enum.join("")
end
end
end
31 changes: 17 additions & 14 deletions lib/space_age_api/token.ex
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
defmodule SpaceAgeApi.Token do
use Joken.Config

@default_exp 30 * 60

def default_exp, do: @default_exp

def token_config do
default_claims(default_exp: @default_exp, skip: [:aud])
|> add_claim("aud", nil, &(&1 in [
"https://api.spaceage.mp/v2/clientauth"
]))
end
end
defmodule SpaceAgeApi.Token do
@moduledoc """
Token configuration for SpaceAge API.
"""
use Joken.Config

@default_exp 30 * 60

def default_exp, do: @default_exp

def token_config do
default_claims(default_exp: @default_exp, skip: [:aud])
|> add_claim("aud", nil, &(&1 in [
"https://api.spaceage.mp/v2/clientauth"
]))
end
end
2 changes: 1 addition & 1 deletion lib/space_age_api/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule SpaceAgeApi.Util do
end)
end

def naive_date_time() do
def naive_date_time do
NaiveDateTime.truncate(NaiveDateTime.utc_now, :second)
end
end
8 changes: 6 additions & 2 deletions lib/space_age_api_web/controllers/applications_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ defmodule SpaceAgeApiWeb.ApplicationsController do
alias SpaceAgeApi.Models.Player
alias SpaceAgeApi.Repo

plug SpaceAgeApi.Plug.Authenticate, [allow_server: true, allow_client: true, require_faction_name: true, require_faction_leader: true] when action in [:list_by_faction, :decline_by_faction_for_player]
plug SpaceAgeApi.Plug.Authenticate, [allow_server: true, allow_client: true, require_steamid: true] when action in [:get_by_player]
plug SpaceAgeApi.Plug.Authenticate,
[allow_server: true, allow_client: true, require_faction_name: true, require_faction_leader: true]
when action in [:list_by_faction, :decline_by_faction_for_player]
plug SpaceAgeApi.Plug.Authenticate,
[allow_server: true, allow_client: true, require_steamid: true]
when action in [:get_by_player]
plug SpaceAgeApi.Plug.Authenticate, [allow_server: true] when action in [:upsert, :accept_by_faction_for_player]

def get_by_player(conn, params) do
Expand Down
Loading

0 comments on commit eb3ece8

Please sign in to comment.