Skip to content

Commit

Permalink
Add infinity expiration option
Browse files Browse the repository at this point in the history
  • Loading branch information
nsweeting committed Jul 24, 2019
1 parent 2481bfc commit 25abd66
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lib/authex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions lib/authex/token.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Authex.Mixfile do
use Mix.Project

@version "2.0.0"
@version "2.1.0"

def project do
[
Expand Down
5 changes: 5 additions & 0 deletions test/authex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 25abd66

Please sign in to comment.