store.ex (1927B)
1 defmodule Plug.Session.Store do 2 @moduledoc """ 3 Specification for session stores. 4 """ 5 6 @doc """ 7 Gets the store name from an atom or a module. 8 9 iex> Plug.Session.Store.get(CustomStore) 10 CustomStore 11 12 iex> Plug.Session.Store.get(:cookie) 13 Plug.Session.COOKIE 14 15 """ 16 def get(store) do 17 case Atom.to_string(store) do 18 "Elixir." <> _ -> store 19 reference -> Module.concat(Plug.Session, String.upcase(reference)) 20 end 21 end 22 23 @typedoc """ 24 The internal reference to the session in the store. 25 """ 26 @type sid :: term | nil 27 28 @typedoc """ 29 The cookie value that will be sent in cookie headers. This value should be 30 base64 encoded to avoid security issues. 31 """ 32 @type cookie :: binary 33 34 @typedoc """ 35 The session contents, the final data to be stored after it has been built 36 with `Plug.Conn.put_session/3` and the other session manipulating functions. 37 """ 38 @type session :: map 39 40 @doc """ 41 Initializes the store. 42 43 The options returned from this function will be given 44 to `c:get/3`, `c:put/4` and `c:delete/3`. 45 """ 46 @callback init(opts :: Plug.opts()) :: Plug.opts() 47 48 @doc """ 49 Parses the given cookie. 50 51 Returns a session id and the session contents. The session id is any 52 value that can be used to identify the session by the store. 53 54 The session id may be nil in case the cookie does not identify any 55 value in the store. The session contents must be a map. 56 """ 57 @callback get(conn :: Plug.Conn.t(), cookie, opts :: Plug.opts()) :: {sid, session} 58 59 @doc """ 60 Stores the session associated with given session id. 61 62 If `nil` is given as id, a new session id should be 63 generated and returned. 64 """ 65 @callback put(conn :: Plug.Conn.t(), sid, any, opts :: Plug.opts()) :: cookie 66 67 @doc """ 68 Removes the session associated with given session id from the store. 69 """ 70 @callback delete(conn :: Plug.Conn.t(), sid, opts :: Plug.opts()) :: :ok 71 end