View Source Charon.Models.Session (Charon v2.3.0)
A session.
Link to this section Summary
Functions
Deserialize a session, without breaking for structural changes in the session struct.
Serialize a session.
Link to this section Types
@type t() :: %Charon.Models.Session{ created_at: integer(), expires_at: integer() | :infinite, extra_payload: map(), id: String.t(), prev_t_gen_fresh_at: integer(), refresh_expires_at: integer(), refresh_token_id: binary(), refreshed_at: integer(), t_gen_fresh_at: integer(), type: atom(), user_id: pos_integer() | binary(), version: pos_integer() }
Link to this section Functions
@spec deserialize(binary(), Charon.Config.t()) :: struct()
Deserialize a session, without breaking for structural changes in the session struct.
doctests
DocTests
@charon_config Charon.Config.from_enum(token_issuer: "local")
# serialization is reversible
iex> %Session{} = test_session() |> serialize() |> deserialize(@charon_config)
# old version without the :version, :refesh_expires_at fields but with :__struct__ set
# is deserialized without error, and updated to latest version (5)
iex> session = %{
...> __struct__: Session,
...> created_at: 0,
...> expires_at: 1,
...> extra_payload: %{},
...> id: "ab",
...> refresh_token_id: "cd",
...> refreshed_at: 15,
...> type: :full,
...> user_id: 9
...> }
...> |> :erlang.term_to_binary()
...> |> deserialize(@charon_config)
iex> %Session{
...> created_at: 0,
...> expires_at: 1,
...> extra_payload: %{},
...> id: "ab",
...> prev_t_gen_fresh_at: 0,
...> refresh_expires_at: 1,
...> refresh_token_id: "cd",
...> refreshed_at: 15,
...> t_gen_fresh_at: 15,
...> type: :full,
...> user_id: 9,
...> version: 5
...> } = session
# old version - with :expires_at = nil - is deserialized without error
iex> session = %{
...> __struct__: Session,
...> created_at: 0,
...> expires_at: nil,
...> extra_payload: %{},
...> id: "ab",
...> refresh_token_id: "cd",
...> refreshed_at: 15,
...> type: :full,
...> user_id: 9
...> }
...> |> :erlang.term_to_binary()
...> |> deserialize(@charon_config)
iex> %Session{
...> created_at: 0,
...> expires_at: :infinite,
...> extra_payload: %{},
...> id: "ab",
...> prev_t_gen_fresh_at: 0,
...> refresh_expires_at: refresh_exp,
...> refresh_token_id: "cd",
...> refreshed_at: 15,
...> t_gen_fresh_at: 15,
...> type: :full,
...> user_id: 9,
...> version: 5
...> } = session
iex> refresh_exp > 100000
true
Serialize a session.