package sihl-token
Install
    
    dune-project
 Dependency
Authors
Maintainers
Sources
md5=b4d86577876e268da4219d29f0f3207e
    
    
  sha512=e5ad0fffa52dc7dad72b84b49d375316342704e9414afe6da7a3be96cf9595cfbb5f792e9e20a7958f3fad1708f5005964daf29dc81e80923c86750441b82567
    
    
  doc/index.html
Sihl Token
A token is a string that has some values associated with it. Tokens are often used for authentication by associating a user_id to a string.
Backends
sihl-token ships with 4 backend implementations.
JSON Web Token
JSON Web Token (JWT) is a standard for client-side tokens. The associated data is stored in the actual token, which is signed and sent to the client.
JWTs are valid until they expire. If you want to invalidate them before, it is necessary to keep a blacklist on the server. This requires some persistent storage.
Use either Sihl_token.JwtPostgreSql or Sihl_token.JwtMariaDb.
Server-side
Server-side tokens have their data persisted on the server. This is useful for sensitive information.
Use either Sihl_token.PostgreSql or Sihl_token.MariaDb.
Installation
Backend
First, choose a backend in service/service.ml:
module Token = Sihl_token.JwtPostgresqlRegistration
Register the service in run/run.ml:
let services = [ Service.Token.register () ]Migrations
Run make sihl migrate to run pending migrations.
Usage
The API is documented in Sihl.Contract.Token.Sig.
Middleware
The token middleware Sihl.Contract.Token.Sig.Web.Middleware.user fetches the current user based on the provided Bearer Token.
let index req =
  let open Lwt.Syntax in
  match Service.Token.Web.User.find_opt req with
  | None -> Lwt.return @@ Sihl.Web.Response.redirect_to "/login"
  | Some user -> Lwt.return @@ Sihl.Web.Response.of_html (View.Welcome.index user)
;;