Jumar.Accounts.UserToken (Jumar v0.1.0)
View SourceAny cryptographicly secure token that is associated with a user. This can be for password resets, logins, email confirmations, or other activities.
Summary
Functions
Builds a token and its hash to be delivered to the user's email.
Generates a token that will be stored in a signed place, such as session or cookie. As they are signed, those tokens do not need to be hashed.
Checks if the token is valid and returns its underlying lookup query.
Checks if the token is valid and returns its underlying lookup query.
Checks if the token is valid and returns its underlying lookup query.
Types
@type context() :: binary()
Opaque binary data stored alongside the token.
@type t() :: %Jumar.Accounts.UserToken{ __meta__: Ecto.Schema.Metadata.t(), authenticated_at: term(), context: context() | nil, id: Jumar.Types.TypeId.t() | nil, inserted_at: DateTime.t() | nil, sent_to: String.t() | nil, token: token() | nil, user: Jumar.Accounts.User.t() | Ecto.Association.NotLoaded.t() | nil, user_id: term() }
@type token() :: binary()
Cryptographically secure binary token.
Functions
Builds a token and its hash to be delivered to the user's email.
The non-hashed token is sent to the user email while the hashed part is stored in the database. The original token cannot be reconstructed, which means anyone with read-only access to the database cannot directly use the token in the application to gain access. Furthermore, if the user changes their email in the system, the tokens sent to the previous email are no longer valid.
Users can easily adapt the existing code to provide other types of delivery methods, for example, by phone numbers.
Generates a token that will be stored in a signed place, such as session or cookie. As they are signed, those tokens do not need to be hashed.
The reason why we store session tokens in the database, even though Phoenix already provides a session cookie, is because Phoenix' default session cookies are not persisted, they are simply signed and potentially encrypted. This means they are valid indefinitely, unless you change the signing/encryption salt.
Therefore, storing them allows individual user sessions to be expired. The token system can also be extended to store additional data, such as the device used for logging in. You could then use this information to display all valid sessions and devices in the UI and allow users to explicitly expire any session they deem invalid.
Checks if the token is valid and returns its underlying lookup query.
The query returns the user_token found by the token, if any.
This is used to validate requests to change the user email. The given token is valid if it matches its hashed counterpart in the database and if it has not expired (after @change_email_validity_in_days). The context must always start with "change:".
Checks if the token is valid and returns its underlying lookup query.
If found, the query returns a tuple of the form {user, token}.
The given token is valid if it matches its hashed counterpart in the database. This function also checks if the token is being used within 15 minutes. The context of a magic link token is always "login".
Checks if the token is valid and returns its underlying lookup query.
The query returns the user found by the token, if any, along with the token's creation time.
The token is valid if it matches the value in the database and it has not expired (after @session_validity_in_days).