View Source Charon.SessionPlugs (Charon v3.1.1)

Plugs to create, update/refresh and delete sessions. When creating or updating a session, new tokens are created as well.

Link to this section Summary

Functions

Delete the persistent session identified by the session_id in the token claims.

Create or update a session. If a session exists in the conn, the session is updated / refreshed, otherwise a new one is created. Refresh an existing session by putting it on the connection with Charon.TokenPlugs.load_session/2.

Link to this section Types

@type upsert_session_opts() :: [
  user_id: any(),
  token_transport: binary() | :cookie | :bearer | :cookie_only,
  access_claim_overrides: %{required(String.t()) => any()},
  refresh_claim_overrides: %{required(String.t()) => any()},
  extra_session_payload: map(),
  session_type: atom()
]

Link to this section Functions

Link to this function

delete_session(conn, config)

View Source
@spec delete_session(Plug.Conn.t(), Charon.Config.t()) :: Plug.Conn.t()

Delete the persistent session identified by the session_id in the token claims.

Note that the token remains valid until it expires, it is left up to the client to drop the access token. It will no longer be possible to refresh the session, however.

examples-doctests

Examples / doctests

# instructs browsers to clear signature cookies
iex> conn()
...> |> Plug.Test.put_req_cookie(@config.access_cookie_name, "anything")
...> |> Plug.Test.put_req_cookie(@config.refresh_cookie_name, "anything")
...> |> delete_session(@config)
...> |> Conn.fetch_cookies()
...> |> Map.get(:cookies)
%{}
Link to this function

upsert_session(conn, config, opts \\ [])

View Source
@spec upsert_session(Plug.Conn.t(), Charon.Config.t(), upsert_session_opts()) ::
  Plug.Conn.t()

Create or update a session. If a session exists in the conn, the session is updated / refreshed, otherwise a new one is created. Refresh an existing session by putting it on the connection with Charon.TokenPlugs.load_session/2.

In both cases, new access / refresh tokens are created and stored in the conn's private map. The server-side session stored in the session store is created / updated as well.

If a new session is created, options :user_id and :token_transport must be provided or an error will be raised.

The token transport can be:

  • :cookie_only - tokens are returned to the client as cookies
  • :cookie - tokens' signatures are split from the tokens and sent as http-only strictly-same-site secure cookies
  • :bearer - tokens are left as-is for the implementing application to return to the client

If config option :enforce_browser_cookies is enabled, browser clients will be attempted to be detected by the presence of (forbidden) header "Sec-Fetch-Mode", in which case only cookie-based token transports will be allowed. This feature is experimental and disabled by default, although this may change in a future release.

Optionally, it is possible to add extra claims to the access- and refresh tokens or to store extra payload in the server-side session.

Session stores may return an optimistic locking error, meaning there are concurrent updates to a session. In this case, upsert/3 will raise a Charon.SessionPlugs.SessionUpdateConflictError, which should result in an HTTP 409 Conflict error. If the session store returns another error, a Charon.SessionPlugs.SessionStorageError is raised, which is an unrecoverable state that should result in an HTTP 500 Internal Server Error.

claims

Claims

The following claims are set by default:

  • "exp" expires at (this value is guaranteed to never outlive the session itself)
  • "iat" time of token creation
  • "iss" issuer, usually a url like "https://myapp.com"
  • "jti" jwt id, random unique id for the token (a refresh token's id is stored in the session as well)
  • "nbf" not before, same value as "iat" but means "token not valid before this time"
  • "sid" session id
  • "sub" subject, the user id of the session owner
  • "type" type, "access" or "refresh"
  • "styp" session type

Additional claims or overrides can be provided with opts.

examples-doctests

Examples / doctests

# error if user id not set for new session
iex> upsert_session(conn(), @config, token_transport: :bearer)
** (RuntimeError) Set user id using upsert_session/3 option :user_id

# error if signature transport not set for new session
iex> upsert_session(conn(), @config, user_id: 1)
** (RuntimeError) Set token transport using upsert_session/3 option :token_transport

# creates session if none present in conn
iex> conn = upsert_session(conn(), @config, user_id: 1, token_transport: :bearer)
iex> %Session{} = Utils.get_session(conn)
iex> %Tokens{} = Utils.get_tokens(conn)

# works with infinite lifespan sessions
iex> conn = upsert_session(conn(), %{@config | session_ttl: :infinite}, user_id: 1, token_transport: :bearer)
iex> %Session{expires_at: :infinite} = Utils.get_session(conn)
iex> %Tokens{} = Utils.get_tokens(conn)

# renews session if present in conn, updating only refresh_tokens, refreshed_at, and refresh_expires_at
# existing session's user id will not change despite attempted override
iex> old_session = test_session(user_id: 43, id: "a", expires_at: :infinite, refresh_expires_at: 0, refreshed_at: 0)
iex> conn = conn()
...> |> Conn.put_private(@session, old_session)
...> |> upsert_session(@config, user_id: 1, token_transport: :bearer)
iex> session = Utils.get_session(conn) |> Map.from_struct()
iex> old_session = Map.from_struct(old_session)
iex> Enum.find(~w(id user_id created_at expires_at)a, & session[&1] != old_session[&1])
nil
iex> Enum.find(~w(refresh_token_id refreshed_at refresh_expires_at)a, & session[&1] == old_session[&1])
nil

# returns token signatures in cookies if token transport is :cookie
# cookie options are merged with defaults
iex> conn = upsert_session(conn(), @config, user_id: 1, token_transport: :cookie)
iex> cookies = conn |> Conn.fetch_cookies() |> Map.get(:cookies)
iex> <<_access_sig::binary>> = Map.get(cookies, @config.access_cookie_name)
iex> <<_refresh_sig::binary>> = Map.get(cookies, @config.refresh_cookie_name)
iex> tokens = Utils.get_tokens(conn)
iex> [_, _] = String.split(tokens.access_token, ".", trim: true)
iex> [_, _] = String.split(tokens.refresh_token, ".", trim: true)
iex> %{http_only: true, path: _, same_site: "Strict", secure: true, max_age: _} = conn.resp_cookies["_refresh_token_signature"]


# returns full tokens in cookies if token transport is :cookie_only
iex> conn = upsert_session(conn(), @config, user_id: 1, token_transport: :cookie_only)
iex> cookies = conn |> Conn.fetch_cookies() |> Map.get(:cookies)
iex> [_, _, _] = cookies |> Map.get(@config.access_cookie_name) |> String.split(".", trim: true)
iex> [_, _, _] = cookies |> Map.get(@config.refresh_cookie_name) |> String.split(".", trim: true)
iex> %{access_token: nil, refresh_token: nil} = Utils.get_tokens(conn)

# tokens get a lot of default claims
iex> conn = upsert_session(conn(), @config, user_id: 1, token_transport: :bearer)
iex> %{"exp" => _, "iat" => _, "iss" => "my_test_app", "jti" => <<_::binary>>, "nbf" => _, "sid" => <<sid::binary>>, "sub" => 1, "type" => "access", "styp" => "full"} = get_private(conn, @access_token_payload)
iex> %{"exp" => _, "iat" => _, "iss" => "my_test_app", "jti" => <<_::binary>>, "nbf" => _, "sid" => ^sid, "sub" => 1, "type" => "refresh", "styp" => "full"} = get_private(conn, @refresh_token_payload)

# allows adding extra claims to tokens
iex> conn = upsert_session(
...>   conn(),
...>   @config,
...>   user_id: 1,
...>   token_transport: :bearer,
...>   access_claim_overrides: %{"much" => :extra},
...>   refresh_claim_overrides: %{"really" => true}
...> )
iex> %{"much" => :extra} = get_private(conn, @access_token_payload)
iex> %{"really" => true} = get_private(conn, @refresh_token_payload)

# allows adding extra payload to session
iex> conn = upsert_session(
...>   conn(),
...>   @config,
...>   user_id: 1,
...>   token_transport: :bearer,
...>   extra_session_payload: %{what?: "that's right!"}
...> )
iex> %Session{extra_payload: %{what?: "that's right!"}} = Utils.get_session(conn)

# allows separating sessions by type (default :full)
iex> conn = upsert_session(conn(), @config, session_type: :oauth2, user_id: 1, token_transport: :bearer)
iex> %Session{type: :oauth2} = Utils.get_session(conn)
iex> %{"styp" => "oauth2"} = get_private(conn, @access_token_payload)