Skip to content

Commit

Permalink
New JWT view
Browse files Browse the repository at this point in the history
  • Loading branch information
Doridian committed Mar 30, 2024
1 parent 03e835d commit a229cc7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
27 changes: 18 additions & 9 deletions lib/space_age_api_web/controllers/players_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,7 @@ defmodule SpaceAgeApiWeb.PlayersController do

def make_discordlink_jwt(conn, params) do
steamid = params["steamid"]
make_jwt_internal(conn, %{
steamid: steamid,
faction_name: "",
is_faction_leader: false,
}, "https://api.spaceage.mp/v2/jwt/discordlink", 5 * 60)
make_jwt_minimal_internal(conn, steamid, "https://api.spaceage.mp/v2/jwt/discordlink", 5 * 60)
end

defp get_single_show(conn, params, template, select \\ nil) do
Expand All @@ -99,18 +95,31 @@ defmodule SpaceAgeApiWeb.PlayersController do
single_or_404(conn, template, player)
end

defp make_jwt_internal(conn, player) do
make_jwt_internal(conn, player, "https://api.spaceage.mp/v2/jwt/clientauth", SpaceAgeApi.Token.default_exp())
defp make_jwt_minimal_internal(conn, steamid, aud, valid_time) do
expiry = System.system_time(:second) + valid_time

jwt = SpaceAgeApi.Token.generate_and_sign!(%{
sub: steamid,
aud: aud,
})

single_or_404(conn, "jwt_minimal.json", %{
token: jwt,
expiry: expiry,
valid_time: valid_time,
steamid: player.steamid,
})
end

defp make_jwt_internal(conn, player, aud, valid_time) do
defp make_jwt_internal(conn, player) do
valid_time = SpaceAgeApi.Token.default_exp()
expiry = System.system_time(:second) + valid_time

server = conn.assigns[:auth_server]

jwt = SpaceAgeApi.Token.generate_and_sign!(%{
sub: player.steamid,
aud: aud,
aud: "https://api.spaceage.mp/v2/jwt/clientauth",
server: server.name,
faction_name: player.faction_name,
is_faction_leader: player.is_faction_leader,
Expand Down
9 changes: 9 additions & 0 deletions lib/space_age_api_web/views/players_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ defmodule SpaceAgeApiWeb.PlayersView do
}
end

def render("jwt_minimal.json", %{data: token}) do
%{
token: token.token,
expiry: token.expiry,
valid_time: token.valid_time,
steamid: token.steamid,
}
end

def player_all(player) do
%{
advancement_level: player.advancement_level,
Expand Down

0 comments on commit a229cc7

Please sign in to comment.