Git Product home page Git Product logo

joken's Introduction

Joken

Documentation

Encodes and decodes JSON Web Tokens.

Currently supports the following algorithms:

  • HS256
  • HS384
  • HS512

Currently supports the following claims:

  • Expiration (exp)
  • Not Before (nbf)
  • Audience (aud)
  • Issuer (iss)
  • Subject (sub)
  • Issued At (iat)
  • JSON Token ID (jti)

Usage:

First, create a module that implements the Joken.Config Behaviour. This Behaviour is responsible for the following:

* encoding and decoding tokens
* adding and validating claims
* secret key used for encoding and decoding
* the algorithm used

If a claim function returns nil then that claim will not be added to the token. Here is a full example of a module that would add and validate the exp claim and not add or validate the others:

  defmodule My.Config.Module do
    @behaviour Joken.Config

    def secret_key() do
      Application.get_env(:app, :secret_key)
    end

    def algorithm() do
      :H256
    end

    def encode(map) do
      Poison.encode!(map)
    end

    def decode(binary) do
      Poison.decode!(binary, keys: :atoms!)
    end

    def claim(:exp, payload) do
      Joken.Config.get_current_time() + 300
    end

    def claim(_, _) do
      nil
    end

    def validate_claim(:exp, payload) do
      Joken.Config.validate_time_claim(payload, :exp, "Token expired", fn(expires_at, now) -> expires_at > now end)
    end

    def validate_claim(_, _) do
      :ok
    end
  end

Joken looks for a joken config with config_module. config_module module being a module that implements the Joken.Config Behaviour.

     config :joken,
       config_module: My.Config.Module

then to encode and decode

{:ok, token} = Joken.encode(%{username: "johndoe"})

{:ok, decoded_payload} = Joken.decode(jwt)

joken's People

Contributors

bryanjos avatar alanpeabody avatar cconstantin avatar cs-victor-nascimento avatar riverrun avatar jhosteny avatar mobileoverlord avatar

Watchers

Evax Software avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.