diff --git a/lib/authex.ex b/lib/authex.ex index 99d4d80..5db9923 100644 --- a/lib/authex.ex +++ b/lib/authex.ex @@ -218,8 +218,9 @@ defmodule Authex do ## Options * `:time` - The base time (timestamp format) in which to use. - * `:ttl` - The time-to-live for the token in seconds. The lifetime is based - on the time provided via the options, or the current time if not provided. + * `:ttl` - The time-to-live for the token in seconds or `:infinity` if no expiration + is required. The lifetime is based on the time provided via the options, + or the current time if not provided. ## Example diff --git a/lib/authex/token.ex b/lib/authex/token.ex index b205e5a..9ddcfc9 100644 --- a/lib/authex/token.ex +++ b/lib/authex/token.ex @@ -39,7 +39,7 @@ defmodule Authex.Token do @type claims :: [claim] @type option :: {:time, integer} - | {:ttl, integer} + | {:ttl, integer | :infinity} @type options :: [option] @type compact :: binary @@ -48,7 +48,7 @@ defmodule Authex.Token do ## Options * `:time` - the base time (timestamp format) in which to use. - * `:ttl` - the TTL for the token. + * `:ttl` - the TTL for the token or `:infinity` if no expiration is required. ## Examples @@ -101,6 +101,10 @@ defmodule Authex.Token do end @doc false + def put_exp(token, _time, :infinity) do + %{token | exp: nil} + end + def put_exp(token, time, ttl) do %{token | exp: time + ttl} end diff --git a/mix.exs b/mix.exs index 2419e0c..3046d01 100644 --- a/mix.exs +++ b/mix.exs @@ -1,7 +1,7 @@ defmodule Authex.Mixfile do use Mix.Project - @version "2.0.0" + @version "2.1.0" def project do [ diff --git a/test/authex_test.exs b/test/authex_test.exs index 4e1ffa9..12c752a 100644 --- a/test/authex_test.exs +++ b/test/authex_test.exs @@ -58,6 +58,11 @@ defmodule AuthexTest do assert %Token{exp: 20} = Authex.token(Auth, [], time: 0) end + test "can set an infinity exp through the ttl option" do + start_supervised(Auth, id: :one) + assert %Token{exp: nil} = Authex.token(Auth, [], ttl: :infinity) + end + test "uses 3600 seconds as the default ttl" do start_supervised(Auth) assert %Token{exp: 3600} = Authex.token(Auth, [], time: 0)