Code Monkey home page Code Monkey logo

fpauth's Introduction

FPauth - user authentication for Dream

Coverage Status

FPauth is an easy user authentication system for OCaml Dream web-framework. FPauth code example

The main idea behind the system is that user authentication is done via running sets of Strategies, and when one of them succeeds, user is considered to be authenticated. Authentication status is controlled by a middleware standing downstream of session middleware.

The system allows to:

  • Control authentication in web-session;
  • Get authentication status for each request via Dream.field;
  • Check user identity with strategies;
  • Use built-in strategies or custom ones;
  • Add all routes for authentication and strategies at once;
  • Add your own representations of authentication events or use built-in;
  • Use built-in handlers or write your own;
  • Extract params for authentication from requests.

Docs can be found here.

Quick setup

In order to start using FPauth, in your project you should:

  • Initialize the system with a model of user, which suffices FPauth.Auth_sign.MODEL. Basically it requires functions which define, how to put and restore your users in session (serialize and deserialize), how to find users from request params (identificate) and which strategies can be applied to a user (applicable_strats);
module Auth = FPauth.Make_Auth (User)
  • Initialize strategies you are going to use to verify users' identities. There are some strategies in FPauth_strategies. Password can be used for password authentication, passwords are to be hashed with Argon2. OTP is a time-based OTP strategy, it contains routes for setting the strategy up for an already authenticated user. Strategies can have additional requirements for your models, as well as need some other modules.
module Password = FPauth_strategies.Password.Make (User)
  • Add Session_manager middleware after your session middleware;
let () = run
  @@ memory_sessions
  @@ Auth.Session_manager.auth_setup
  • Insert FPauth routes into Dream.router middleware. Here you specify strategies used in the authentication process, the way params are extracted, responses on main authentication events. You can also specify the scope for authentication routes;
  @@ router [
      Auth.Router.call [(module Password)] ~responses:(module Responses) ~extractor:extractor ~scope:"/authentication"
  ]

Strategies and Responses modules are passed as first-class objects which suffice FPauth.Auth_sign.STRATEGY and FPauth.Auth_sign.RESPONSES signatures correspondingly. Extractor is a function which meets FPauth.Static.Params.extractor type.

  • In FPauth_responses you can find some default responses in JSON and HTML format;
  • In FPauth.Static.Params you can find some default extractors from JSON-requests' bodies, forms or from query;
  • Done! Your application can now authenticate users!

Advanced Usage

It is possible to customize many aspects of the system workflow.

  • You can install only the packages you actually need:
    • FPauth-core contains Session_manager, Authenticator, Router, Variables, as well as Static module and signatures. These allow you to build your own workflow almost from the ground;
    • FPauth-strategies contains Password and OTP strategies. If you don't need them - you can choose not to have them 😉;
    • FPauth-responses contains some default responses on main authentication events;
  • You can write your own Strategies, Responses and Params Extractors.

fpauth's People

Contributors

mikegeine avatar

Stargazers

姚文强 avatar Raph avatar ᴍᴜǫɪᴜ ʜᴀɴ (韩暮秋) avatar David.Gao avatar Francesco Serra avatar Christian Sheridan avatar  avatar TJ DeVries avatar Kostarev Kirill avatar Andrejs Agejevs avatar Duncan Holm avatar Gabriel Nogueira avatar David Sancho avatar Ryan Moore avatar Pavel Argentov avatar

Watchers

 avatar

Forkers

davidalphafox

fpauth's Issues

Clarify in documentation that passwords are hashed, not encrypted.

Sorry if this sounds like a nitpick but for me it seemed like a huge red flag when I first saw it:

val encrypted_password: t -> string option

It's really important if you're handling authentication to understand that passwords must be hashed, not encrypted. If I understand this package correctly, it's not actually handling password storage, just password verification, and the Argon2.ID.verify function correctly verifies against a hashed password and not an encrypted password, so I think this is just something misnamed rather than an actual security failure. Still, I worry that it has the potential to misinform users of the library about how they should be securely handling passwords, and makes the library look insecure and dangerous.

I'd recommend renaming encrypted_password to hashed_password or encoded_password (to match the terminology used in the Argon2 documentation).

Doesn't support 5.1.0

FPauth currently conflicts with ocaml-base-compiler.5.1.0. I can't quite tell why; officially, it's because it uses naked pointers in FPauth-core, but I cannot determine where those naked pointers are.
The only other barrier to this package being up-to-date is that it uses twostep which does not support recent versions of mirage-crypto-rng, and thus cannot be installed alongside dream.1.0.0~alpha5.

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.