KeenAuth.Storage behaviour (KeenAuth v1.0.1)

Copy Markdown View Source

Defines the storage behavior for persisting authentication data.

Storage is the final stage in KeenAuth's pipeline architecture. It handles persisting user data, tokens, and session information. Storage implementations can be as simple as session-based or as sophisticated as multi-layer persistence.

Pipeline Stage

The storage is the final stage in the authentication flow:

Strategy  Mapper  Processor  **Storage**

Flexibility

Storage implementations can vary based on your needs:

  • Session Storage (default): Simple session-based storage
  • Database Storage: Persistent user sessions in database
  • JWT Storage: Stateless authentication with JWT tokens
  • Multi-layer: Combine database + session + JWT for different purposes

Example Implementation

defmodule MyApp.Auth.DatabaseStorage do
  @behaviour KeenAuth.Storage

  def store(conn, provider, user, oauth_response) do
    {:ok, session} = create_user_session(user, provider)
    conn = put_session(conn, :session_id, session.id)
    {:ok, conn}
  end

  def current_user(conn) do
    with session_id when not is_nil(session_id) <- get_session(conn, :session_id),
         session when not is_nil(session) <- get_session_from_db(session_id) do
      session.user
    else
      _ -> nil
    end
  end

  # ... implement other callbacks
end

Configuration

Configure a custom storage implementation:

config :keen_auth,
  storage: MyApp.Auth.CustomStorage

Summary

Callbacks

authenticated?(conn)

@callback authenticated?(conn :: Plug.Conn.t()) :: boolean()

current_user(conn)

@callback current_user(conn :: Plug.Conn.t()) :: any() | nil

delete(conn)

@callback delete(conn :: Plug.Conn.t()) :: Plug.Conn.t()

get_access_token(conn)

@callback get_access_token(conn :: Plug.Conn.t()) :: binary() | nil

get_id_token(conn)

@callback get_id_token(conn :: Plug.Conn.t()) :: binary() | nil

get_provider(conn)

@callback get_provider(conn :: Plug.Conn.t()) :: binary() | nil

get_refresh_token(conn)

@callback get_refresh_token(conn :: Plug.Conn.t()) :: binary() | nil

put_current_user(conn, provider, arg3)

@callback put_current_user(
  conn :: Plug.Conn.t(),
  provider :: atom(),
  KeenAuth.User.t() | map()
) ::
  Plug.Conn.t()

put_provider(conn, provider)

@callback put_provider(conn :: Plug.Conn.t(), provider :: atom()) :: Plug.Conn.t()

put_tokens(conn, provider, tokens_map)

@callback put_tokens(
  conn :: Plug.Conn.t(),
  provider :: atom(),
  KeenAuth.AuthenticationController.tokens_map()
) :: Plug.Conn.t()

store(conn, provider, mapped_user, oauth_response)

@callback store(
  conn :: Plug.Conn.t(),
  provider :: atom(),
  mapped_user :: KeenAuth.User.t() | map(),
  oauth_response ::
    KeenAuth.AuthenticationController.oauth_callback_response() | nil
) :: {:ok, Plug.Conn.t()}

Functions

authenticated?(conn)

current_storage(conn)

current_user(conn)

delete(conn)

get_access_token(conn)

get_id_token(conn)

get_provider(conn)

get_refresh_token(conn)

get_storage(config)

put_current_user(conn, provider, user)

put_provider(conn, provider)

put_tokens(conn, provider, tokens)

store(conn, provider, mapped_user, oauth_response)