View Source Charon.Models.Session (Charon v2.6.0)
A session.
Link to this section Summary
Functions
deserialize(binary, config)
deprecated
Deserialize a session, without breaking for structural changes in the session struct.
serialize(session)
deprecated
Serialize a session.
Upgrade a session (or map created from a session struct) to the latest struct version (6).
Link to this section Types
@type t() :: %Charon.Models.Session{ created_at: integer(), expires_at: integer() | :infinite, extra_payload: map(), id: String.t(), prev_tokens_fresh_from: integer(), refresh_expires_at: integer(), refresh_token_id: binary(), refreshed_at: integer(), tokens_fresh_from: integer(), type: atom(), user_id: pos_integer() | binary(), version: pos_integer() }
Link to this section Functions
This function is deprecated. Use :erlang.binary_to_term/1.
@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)
This function is deprecated. Use :erlang.term_to_binary/1.
Serialize a session.
@spec upgrade_version(map(), Charon.Config.t()) :: map()
Upgrade a session (or map created from a session struct) to the latest struct version (6).
doctests
DocTests
@charon_config Charon.Config.from_enum(token_issuer: "local")
# old version without the :version, :refesh_expires_at fields but with :__struct__ set
# is updated to latest version (6)
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
...> }
...> |> upgrade_version(@charon_config)
iex> %Session{
...> created_at: 0,
...> expires_at: 1,
...> extra_payload: %{},
...> id: "ab",
...> prev_tokens_fresh_from: 0,
...> refresh_expires_at: 1,
...> refresh_token_id: "cd",
...> refreshed_at: 15,
...> tokens_fresh_from: 15,
...> type: :full,
...> user_id: 9,
...> version: 6
...> } = session
# old version - with :expires_at = nil - is updated 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
...> }
...> |> upgrade_version(@charon_config)
iex> %Session{
...> created_at: 0,
...> expires_at: :infinite,
...> extra_payload: %{},
...> id: "ab",
...> prev_tokens_fresh_from: 0,
...> refresh_expires_at: refresh_exp,
...> refresh_token_id: "cd",
...> refreshed_at: 15,
...> tokens_fresh_from: 15,
...> type: :full,
...> user_id: 9,
...> version: 6
...> } = session
iex> refresh_exp > 100000
true