Haytni.Token (Haytni v0.7.0) View Source

This module handles generation of tokens for the use of Haytni's plugins.

Link to this section Summary

Functions

Generates a token, associated to a given user and a given email address (sent_to).

Deletes all tokens associated to the given user and contexts (if not :all)

When the multi will be executed, generates and inserts a token, associated to the user resulting of a previous operation of multi identified by the name user_name.

Generates a token associated to user and add it to the multi for later insertion.

Generates a length long random binary token

Purges all expired tokens.

Deletes all tokens associated to a given user.

Helper to build the query (intended to be composed) to select all tokens associated to a given user and for the specified contexts

Decodes a token previously encoded by url_encode/1

Encodes a token to safely figure in an URL

Returns the user associated to the given non-expired token, nil if none matches. This function checks that the current email address of the found user is the same than the the one associated to the token at its creation.

Returns the user associated to the given non-expired token, nil if none matches but, in opposition to user_from_token_with_mail_match/4, the email address between the user and the token is expected (has) to be different. This behaviour (and function) is primarily intended to change its email address.

Link to this section Types

Link to this section Functions

Link to this function

build_and_assoc_token(user, sent_to, context)

View Source

Specs

build_and_assoc_token(
  user :: Haytni.user(),
  sent_to :: String.t(),
  context :: String.t() | atom()
) :: t()

Generates a token, associated to a given user and a given email address (sent_to).

NOTE: this is a "low level" function for specific needs, the token is NOT persisted (designed to be inserted later by an Ecto.Multi). Use the higher level function insert_token_in_multi/4 if it feet your needs.

Link to this function

delete_tokens_in_multi(multi, name, user, contexts)

View Source

Specs

delete_tokens_in_multi(
  multi :: Ecto.Multi.t(),
  name :: Ecto.Multi.name(),
  user :: Haytni.user(),
  contexts :: String.t() | [String.t(), ...] | :all
) :: Ecto.Multi.t()

Deletes all tokens associated to the given user and contexts (if not :all)

Link to this function

insert_token_in_multi(multi, token_name, user_name, context)

View Source

Specs

insert_token_in_multi(
  multi :: Ecto.Multi.t(),
  token_name :: Ecto.Multi.name(),
  user_name :: Ecto.Multi.name(),
  context :: String.t()
) :: Ecto.Multi.t()

When the multi will be executed, generates and inserts a token, associated to the user resulting of a previous operation of multi identified by the name user_name.

Exemple:

Ecto.Multi.new()
|> Ecto.Multi.update(:user, Ecto.Changeset.change(user, changes))
|> Haytni.Token.insert_token_in_multi(:token, :user, "context")
|> Repo.Transaction()
Link to this function

insert_token_in_multi(multi, name, user, email, context)

View Source

Specs

insert_token_in_multi(
  multi :: Ecto.Multi.t(),
  name :: Ecto.Multi.name(),
  user :: Haytni.user(),
  email :: String.t(),
  context :: String.t()
) :: Ecto.Multi.t()

Generates a token associated to user and add it to the multi for later insertion.

Generates a length long random binary token

Link to this function

purge_expired_tokens(module)

View Source

Specs

purge_expired_tokens(module :: module()) :: non_neg_integer()

Purges all expired tokens.

Returns the number of deleted tokens.

Link to this function

revoke_user_tokens(module, user)

View Source

Specs

revoke_user_tokens(module :: module(), user :: Haytni.user()) ::
  non_neg_integer()

Deletes all tokens associated to a given user.

Returns the number of tokens that were actually deleted (expired tokens included).

Link to this function

tokens_from_user_query(user, contexts)

View Source

Specs

tokens_from_user_query(
  user :: Haytni.user(),
  contexts :: String.t() | [String.t(), ...] | :all
) :: Ecto.Query.t()

Helper to build the query (intended to be composed) to select all tokens associated to a given user and for the specified contexts

Specs

url_decode(token :: t()) :: String.t()
url_decode(token :: String.t()) :: {:ok, String.t()} | :error

Decodes a token previously encoded by url_encode/1

Specs

url_encode(token :: t()) :: String.t()

Encodes a token to safely figure in an URL

Link to this function

user_from_token_with_mail_match(module, token, context, duration)

View Source

Specs

user_from_token_with_mail_match(
  module :: module(),
  token :: String.t(),
  context :: String.t(),
  duration :: pos_integer()
) :: Haytni.nilable(Haytni.user())

Returns the user associated to the given non-expired token, nil if none matches. This function checks that the current email address of the found user is the same than the the one associated to the token at its creation.

Link to this function

user_from_token_without_mail_match(module, user, token, context, duration)

View Source

Specs

user_from_token_without_mail_match(
  module :: module(),
  user :: Haytni.user(),
  token :: String.t(),
  context :: String.t(),
  duration :: pos_integer()
) :: Haytni.nilable(t())

Returns the user associated to the given non-expired token, nil if none matches but, in opposition to user_from_token_with_mail_match/4, the email address between the user and the token is expected (has) to be different. This behaviour (and function) is primarily intended to change its email address.