# JWT

An implementation of JSON Web Token.

# Install

```bash
$ npm install immersa-jwt
```

# Usage

Instantiate JWT object

```
import os
import jwt

# secret must be 32-character long
secret = os.environ.get("IMMERSA_TOKEN_SECRET")

# expiration denotes the number of seconds the token is valid
expiration = os.environ.get("IMMERSA_TOKEN_EXPIRATION")

# if expiration is ommitted, default is set to 24 hours
JWT = jwt.JWT(secret, expiration)
```

Given a session which represents dictionary, JWT can be generated by:

```
session = {
    "id": "61c24785bd77c10012d58199",
    "displayName": "John Smoth",
    "email": "jsmith@immersa.co",
    "admin": True,
}
token = JWT.create-token(session)
```

The token can then be passed along HTTP request apart of its header. This token can be validated on the server-side by calling:

```
session = JWT.session(token)
```

which, in turn, extract the session which was used to the tokken earlier. In case expired or invalid token,  `JWT.session(token)` will raise `InvalidTokenError`