Packages
potionx
0.8.1
0.8.15
0.8.14
0.8.13
0.8.12
0.8.11
0.8.10
0.8.9
0.8.8
0.8.7
0.8.6
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.2
0.7.1
0.7.0
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.3
0.5.2
0.5.1
0.5.0
0.4.16
0.4.15
0.4.14
0.4.13
0.4.12
0.4.11
0.4.10
0.4.9
0.4.8
0.4.7
0.4.6
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.0
0.2.18
0.2.17
0.2.16
0.2.15
0.2.14
0.2.13
0.2.12
0.2.11
0.2.10
0.2.9
0.2.8
0.2.7
0.2.6
0.2.5
0.2.4
0.2.3
0.2.2
0.2.1
0.1.1
0.1.0
Potionx is a set of generators and functions that speeds up the process of setting up and deploying a full-stack application that uses Elixir with GraphQL for the server-side component and Vue for the frontend component.
Current section
Files
Jump to
Current section
Files
lib/potionx/auth/auth.ex
defmodule Potionx.Auth do
use TypedStruct
def cookie_options(%Plug.Conn{} = conn, config, max_age) do
Keyword.merge(
[
http_only: true,
domain: conn.host,
max_age: max_age,
secure: true
],
(config || [])
)
end
def delete_cookie(conn, %{name: name, ttl_seconds: ttl_seconds}) do
conn
|> Plug.Conn.delete_resp_cookie(
name,
cookie_options(
conn,
[sign: true],
ttl_seconds
)
)
end
@doc """
Handles setting sign up cookies used only during social login and general setting cookies.
"""
@spec handle_user_session_cookies(struct(), Plug.Conn.t()) :: any
def handle_user_session_cookies(%{uuid_renewal: nil, uuid_access: uuid_access} = session, conn) when not is_nil(uuid_access) do
conn
|> Potionx.Auth.set_cookie(%{
name: Potionx.Auth.token_config().access_token.name,
same_site: "none",
token: uuid_access,
ttl_seconds: session.ttl_access_seconds
})
end
def handle_user_session_cookies(%{uuid_renewal: renewal} = session, conn) when not is_nil(renewal) do
conn
|> Potionx.Auth.set_cookie(%{
http_only: false,
name: Potionx.Auth.token_config().frontend.name,
token: "1",
ttl_seconds: session.ttl_access_seconds
})
|> Potionx.Auth.set_cookie(%{
name: Potionx.Auth.token_config().access_token.name,
token: session.uuid_access,
ttl_seconds: session.ttl_access_seconds
})
|> Potionx.Auth.set_cookie(%{
name: Potionx.Auth.token_config().renewal_token.name,
token: session.uuid_renewal,
ttl_seconds: session.ttl_renewal_seconds
})
end
def handle_user_session_cookies(err, _conn), do: err
def set_cookie(conn, %{name: name, token: token, ttl_seconds: ttl_seconds} = config) do
conn
|> Plug.Conn.put_resp_cookie(
name,
token,
cookie_options(
conn,
[
http_only: Map.get(config, :http_only, true),
same_site: Map.get(config, :same_site) || "strict",
sign: true
],
ttl_seconds
)
)
end
def token_config(config \\ []) do
Map.merge(
%{
access_token: %{
name: "a_app",
ttl_key: :ttl_access_seconds,
ttl_seconds: 60 * 30, # 30 minutes,
uuid_key: :uuid_access
},
frontend: %{
name: "frontend",
ttl_key: nil,
ttl_seconds: 60 * 30, # 30 minutes
uuid_key: nil
},
renewal_token: %{
name: "r_app",
ttl_key: :ttl_renewal_seconds,
ttl_seconds: 60 * 60 * 24 * 30, # 30 days
uuid_key: :uuid_renewal
},
sign_in_token: %{
name: "a_app",
ttl_key: :ttl_access_seconds,
ttl_seconds: 60 * 5, # 5 minutes
uuid_key: :uuid_access
}
},
Keyword.get(config, :token_config, %{})
)
end
end