Code Monkey home page Code Monkey logo

ocaml-jwt's Introduction

OCaml JWT

JSON Web Token (JWT) is a compact claims representation format intended for space constrained environments such as HTTP Authorization headers and URI query parameters. JWTs encode claims to be transmitted as a JSON [RFC7159] object that is used as the payload of a JSON Web Signature (JWS) [JWS] structure or as the plaintext of a JSON Web Encryption (JWE) [JWE] structure, enabling the claims to be digitally signed or integrity protected with a Message Authentication Code (MAC) and/or encrypted. JWTs are always represented using the JWS Compact Serialization or the JWE Compact Serialization.

The suggested pronunciation of JWT is the same as the English word "jot".

RFC7519

How to install?

The last version of nocrypto:

opam pin add nocrypto https://github.com/mirleft/ocaml-nocrypto.git

You can use OPAM to get the last released version:

opam install jwt

If you want the development version, you can pin the repository:

opam pin add jwt https://github.com/besport/ocaml-jwt.git

Documentation

A JWT object is represented by a type Jwt.t containing the header (of type Jwt.header) and the payload (of type Jwt.payload).

Header

You can choose the algorithm you want to sign the token. A header contains the attribute typ and alg. The attribute alg is represented by a sum type Jwt.algorithm. For the moment, only HS256 and HS512 are supported. You need to give the secret key when you create an algorithm value.

You can create a header with Jwt.header_of_algorithm_and_type.

For example:

Jwt.header_of_algorithm_and_typ (Jwt.HS256 "SecretKeyNotReallySecret") "JWT"

Payload

Possible claims are represented by a type Jwt.claim. You can create a new claim with Jwt.claim "claim name" and get the claim name with Jwt.string_of_claim claim. Here a list of predefined claims:

  • iss
  • sub
  • aud
  • exp
  • nbf
  • iat
  • jti
  • typ
  • ctyp
  • alg
  • auth_time
  • nonce
  • acr
  • amr
  • azp

An empty payload can be created with Jwt.empty_payload and you can add claim with Jwt.add_claim claim "claim value" payload

For example:

let payload =
  let open Jwt in
  empty_payload
  |> add_claim iss "https://github.com"
  |> add_claim sub "github"

Get the token representation.

You can get the token representation of a type t with Jwt.token_of_t.

Decode a token.

As JWT data are encoded with B64, we can retrieve the information like the header and the payload from the token.

You can use Jwt.t_of_token to get a type t with the header and the payload encoded in the token.

Development

Build with dune:

dune build

To run the tests:

dune runtest

ocaml-jwt's People

Contributors

dannywillems avatar actionshrimp avatar bronsa avatar b2c2-dannywillems avatar mattjbray avatar ulrikstrid avatar ismith avatar

Watchers

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.