Skip to content

artemeff/jwt

This branch is 77 commits ahead of, 3 commits behind kato-im/ejwt:master.

Folders and files

NameName
Last commit message
Last commit date
Jun 4, 2022
Feb 10, 2019
Jan 28, 2019
Jul 27, 2022
Jul 27, 2022
Feb 2, 2019
Feb 4, 2019
Jul 5, 2017
Feb 14, 2014
Jan 28, 2019
Feb 10, 2019
Feb 1, 2019
Mar 29, 2019
Mar 29, 2019
Oct 6, 2021

Repository files navigation

jwt — Erlang JWT Library


Build Status Coverage Status Hex.pm


JWT is a simple authorization token format based on JSON.

If you use rebar (supports both 2 and 3 versions) or mix (Elixir):

% in rebar.config for rebar3
{deps, [{jwt}]}.

% or for rebar2
{deps, [{jwt, ".*", {git, "https://github.com/artemeff/jwt", {tag, "0.1.0"}}}]}
% mix.exs
def deps do
  [{:jwt, "~> 0.1"}]
end

Or use it as git dependency.

%% Create JWT token
> application:ensure_all_started(jwt).
> Key = <<"supas3cri7">>.
> Claims = [
    {user_id, 42},
    {user_name, <<"Bob">>}
  ].
> {ok, Token} = jwt:encode(<<"HS256">>, Claims, Key).
%% or with expiration
> ExpirationSeconds = 86400.
> {ok, Token} = jwt:encode(<<"HS256">>, Claims, ExpirationSeconds, Key).

%% Parse JWT token
> {ok, Claims} = jwt:decode(Token, Key).
%% Issuer specific keys workflow

%% The encoder just knows about itself
> Issuer = <<"iss1">>.
> IssuerKey = <<"Issuer-1-Key">>.
> Claims2 = [
    {iss, Issuer},
    {user_id, 42},
    {user_name, <<"Bob">>}
  ].
> {ok, Token2} = jwt:encode(<<"HS256">>, Claims, ExpirationSeconds, IssuerKey).

%% Decoder Workflow
%% The decoder knows about all encoder keys (issuer specific)
> IssuerKeyMapping = #{ Issuer => IssuerKey,
                        <<"iss2">> => <<"Issuer2Key">>}.
> {ok, Claims} = jwt:decode(Token, <<"default-key">>, IssuerKeyMapping).

  • Fork it
  • Create your feature branch (git checkout -b my-new-feature)
  • Commit your changes (git commit -am 'add some feature')
  • Push to the branch (git push origin my-new-feature)
  • Create new Pull Request

Modules

jwk
jwt
jwt_ecdsa

About

Erlang JWT library

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Erlang 98.1%
  • Shell 1.1%
  • Makefile 0.8%