Resuelve AuthPlug v1.4.3 ResuelveAuth.Helpers.TokenHelper View Source

Token generation and validation module.

Link to this section Summary

Functions

Returns token generated from a map in the following format SECURE_BASE64_JSON.SIGNATURE_HMAC_SHA_256_BASE16

Extrae la fecha de un mapa en el formato {:ok, %{}} esperando que sea un formato de unix y convertirlo a fecha.

Identify if the time is less than the time limit

Get the data from a valid token.

Link to this section Functions

Link to this function

create_token(data, options)

View Source

Specs

create_token(struct(), String.t()) :: String.t()

Returns token generated from a map in the following format SECURE_BASE64_JSON.SIGNATURE_HMAC_SHA_256_BASE16

Examples


   iex> alias ResuelveAuth.TokenData
   iex> timestamp = 1572656155135
   iex> data = %TokenData{    meta: nil,    role: "service",    service: "my-api",    session: nil,    timestamp: timestamp    }
   iex> options = [secret: "secret", limit_time: 4]
   iex> alias ResuelveAuth.Helpers.TokenHelper
   iex> token = TokenHelper.create_token(data, options)
   "eyJ0aW1lc3RhbXAiOjE1NzI2NTYxNTUxMzUsInNlc3Npb24iOm51bGwsInNlcnZpY2UiOiJteS1hcGkiLCJyb2xlIjoic2VydmljZSIsIm1ldGEiOm51bGx9.1E1FA5A03B62DB5E0E5C5627D578E4ABBD1E83EFBFF72907428D0C95DC491394"
   iex> String.length(token)
   185

Extrae la fecha de un mapa en el formato {:ok, %{}} esperando que sea un formato de unix y convertirlo a fecha.

Ejemplos:


iex> timestamp = 1583797948623
iex> data = %{"timestamp" => timestamp, "otra_llave" => "algo"}
iex> parameter = {:ok, data}
iex> {:ok, data} = ResuelveAuth.Helpers.TokenHelper.extract(parameter)
iex> Map.keys(data)
["otra_llave", "time", "timestamp"]
Link to this function

is_expired(error, limit_time)

View Source

Specs

is_expired({:error, any()} | {:ok, binary()}, integer()) ::
  {:ok, binary()} | {:error, binary()}
is_expired(integer(), integer()) :: boolean()

Identify if the time is less than the time limit

Ejemplo:


iex> ResuelveAuth.Helpers.TokenHelper.is_expired(4, 5)
false

iex> ResuelveAuth.Helpers.TokenHelper.is_expired(4, 4)
false

iex> ResuelveAuth.Helpers.TokenHelper.is_expired(5, 4)
true
Link to this function

verify_token(token, options)

View Source

Specs

verify_token(String.t(), list()) :: tuple()

Get the data from a valid token.

Examples


   iex> alias ResuelveAuth.TokenData
   iex> timestamp = DateTime.to_unix(DateTime.utc_now(), :millisecond)
   iex> data = %TokenData{    meta: nil,    role: "service",    service: "my-api",    session: nil,    timestamp: timestamp    }
   iex> options = [secret: "secret", limit_time: 4]
   iex> alias ResuelveAuth.Helpers.TokenHelper
   iex> token = TokenHelper.create_token(data, options)
   iex> {:ok, result} = TokenHelper.verify_token(token, options)
   iex> result["timestamp"] == data.timestamp
   true
   iex> result["service"] == data.service
   true