View Source Charon.Models.Session (Charon v2.1.2)

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_refresh_tokens: binary(),
  refresh_expires_at: integer(),
  refresh_tokens: [binary()],
  refresh_tokens_at: integer(),
  refreshed_at: integer(),
  type: atom(),
  user_id: pos_integer() | binary(),
  version: pos_integer()
}

Link to this section Functions

Link to this function

deserialize(binary, config)

View Source
@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, :refresh_tokens, :refresh_tokens_at, :prev_refresh_tokens fields
# but with :__struct__ and :refresh_token_id set
# is deserialized without error, and updated to latest version (2)
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_refresh_tokens: [],
...>   refresh_expires_at: 1,
...>   refresh_tokens_at: 15,
...>   refresh_tokens: ["cd"],
...>   refreshed_at: 15,
...>   type: :full,
...>   user_id: 9,
...>   version: 3
...> } = 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_refresh_tokens: [],
...>   refresh_expires_at: refresh_exp,
...>   refresh_tokens_at: 15,
...>   refresh_tokens: ["cd"],
...>   refreshed_at: 15,
...>   type: :full,
...>   user_id: 9,
...>   version: 3
...> } = session
iex> refresh_exp > 100000
true
@spec serialize(struct()) :: binary()

Serialize a session.