rodauth: clean authentication - valentine ostakh

99
Valentyn Ostakh https://github.com/valikos https://twitter.com/valikos_ost

Upload: ruby-meditation

Post on 11-Apr-2017

39 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Rodauth: Clean Authentication - Valentine Ostakh

Valentyn Ostakh

https://github.com/valikos

https://twitter.com/valikos_ost

Page 2: Rodauth: Clean Authentication - Valentine Ostakh

RodauthClean Authentication

Page 3: Rodauth: Clean Authentication - Valentine Ostakh

What is the most necessary feature for interaction with users?

Page 4: Rodauth: Clean Authentication - Valentine Ostakh

Authentication

Page 5: Rodauth: Clean Authentication - Valentine Ostakh

Authentication is the act of identification of user that going

to interact with your product

Page 6: Rodauth: Clean Authentication - Valentine Ostakh

I want authentication for my application

Page 7: Rodauth: Clean Authentication - Valentine Ostakh
Page 8: Rodauth: Clean Authentication - Valentine Ostakh

Ruby-toolbox

Page 9: Rodauth: Clean Authentication - Valentine Ostakh

Awesome-ruby

Page 10: Rodauth: Clean Authentication - Valentine Ostakh

Authentication• Authlogic

• Devise

• Clearance

• Sorcery

• Warden

• Rodauth

Page 11: Rodauth: Clean Authentication - Valentine Ostakh

What about custom solution?

Page 12: Rodauth: Clean Authentication - Valentine Ostakh
Page 13: Rodauth: Clean Authentication - Valentine Ostakh
Page 14: Rodauth: Clean Authentication - Valentine Ostakh

Custom Solution vs Authentication LibrariesLibrary Issues Pull Requests First Release

Sorcery 64/451 28/306 31 Jan 2011

Clearance 12/374 4/369 1 Sep 2009

Authlogic 124/221 6/186 3 Nov 2008

Devise 39/3353 29/979 21 Oct 2009

Warden 18/74 4/49 26 May 2009

Rodauth 0/8 0/11 12 Aug 2015

Page 15: Rodauth: Clean Authentication - Valentine Ostakh

I want flexible authentication that can be used with any framework

Page 16: Rodauth: Clean Authentication - Valentine Ostakh
Page 17: Rodauth: Clean Authentication - Valentine Ostakh
Page 18: Rodauth: Clean Authentication - Valentine Ostakh
Page 19: Rodauth: Clean Authentication - Valentine Ostakh

How to choose a library for my application?

Page 20: Rodauth: Clean Authentication - Valentine Ostakh

Dependencies

Page 21: Rodauth: Clean Authentication - Valentine Ostakh

• Authlogic - activerecord, activesupport

• Devise - rails, warden

• Clearance - rails, rack

• Sorcery - rails

• Warden - rack

• Rodauth - roda, rack

Page 22: Rodauth: Clean Authentication - Valentine Ostakh

Clearance

Page 23: Rodauth: Clean Authentication - Valentine Ostakh

Features

Page 24: Rodauth: Clean Authentication - Valentine Ostakh

Registration• Authlogic

• Devise

• Clearance

• Sorcery

• Warden

• Rodauth

Page 25: Rodauth: Clean Authentication - Valentine Ostakh

Login• Authlogic

• Devise

• Clearance

• Sorcery

• Warden

• Rodauth

Page 26: Rodauth: Clean Authentication - Valentine Ostakh

Logout• Authlogic

• Devise

• Clearance

• Sorcery

• Warden

• Rodauth

Page 27: Rodauth: Clean Authentication - Valentine Ostakh

Would be great to have token authentication

Page 28: Rodauth: Clean Authentication - Valentine Ostakh

Token Authentication• Authlogic

• Devise

• Clearance

• Sorcery

• Warden

• Rodauth

Page 29: Rodauth: Clean Authentication - Valentine Ostakh

Token Authentication Articles

• An Introduction to Using JWT Authentication in Rails

• Authenticate Your Rails API with JWT from Scratch

• Token-based authentication with Ruby on Rails 5 API

• JWT Auth in Rails, From Scratch

• Implementing JWT in Ruby on Rails-based API

• Authenticate Your Rails API with JWT

• Rails Api Backed With JWT

• Rails, Devise, JWT and the forgotten Warden

Page 30: Rodauth: Clean Authentication - Valentine Ostakh

Token Authentication Gems

• jwt_authentication

• simple_token_authentication

• devise_token_auth

Page 31: Rodauth: Clean Authentication - Valentine Ostakh

Token Authentication Gems

• jwt_authentication (based on devise)

• simple_token_authentication (based on devise)

• devise_token_auth (based on devise)

Page 32: Rodauth: Clean Authentication - Valentine Ostakh

Token Authentication

Page 33: Rodauth: Clean Authentication - Valentine Ostakh

Popularity

Page 34: Rodauth: Clean Authentication - Valentine Ostakh

Library Total Downloads rubygems.org

Devise 21,407,462

Warden 21,018,495

Authlogic 2,343,678

Sorcery 527,431

Clearance 317,409

Rodauth 6,163

Page 35: Rodauth: Clean Authentication - Valentine Ostakh

Summary

Page 36: Rodauth: Clean Authentication - Valentine Ostakh

Library Dependencies Features TokenAuthentication

Devise

Warden

Authlogic

Sorcery

Clearance

Rodauth

Page 37: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth

Page 38: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth

Page 39: Rodauth: Clean Authentication - Valentine Ostakh

Jeremy EvansTwitter: @jeremyevans0

Page 40: Rodauth: Clean Authentication - Valentine Ostakh

Roda

Sequel

Page 41: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth Goals

• Security

• Simplicity

• Flexibility

Page 42: Rodauth: Clean Authentication - Valentine Ostakh

Features first

Page 43: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth FeaturesLogin

Page 44: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth FeaturesLogin

Logout

Page 45: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth FeaturesLogin

Logout

Change Password

Page 46: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth FeaturesLogin

Logout

Change Password

Change Login

Page 47: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth FeaturesLogin

Logout

Change Password

Change Login

Reset Password

Page 48: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth FeaturesLogin

Logout

Change Password

Change Login

Reset Password

Create Account

Page 49: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth FeaturesLogin

Logout

Change Password

Change Login

Reset Password

Create Account

Close Account

Page 50: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth FeaturesLogin

Logout

Change Password

Change Login

Reset Password

Create Account

Close Account

Verify Account

Page 51: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth FeaturesLogin

Logout

Change Password

Change Login

Reset Password

Create Account

Close Account

Verify Account

Confirm Password

Page 52: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth FeaturesLogin

Logout

Change Password

Change Login

Reset Password

Create Account

Close Account

Verify Account

Confirm Password

Remember (Autologin via token)

Page 53: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth FeaturesLogin

Logout

Change Password

Change Login

Reset Password

Create Account

Close Account

Verify Account

Confirm Password

Remember (Autologin via token)

Lockout (Bruteforce protection)

Page 54: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth FeaturesLogin

Logout

Change Password

Change Login

Reset Password

Create Account

Close Account

Verify Account

Confirm Password

Remember (Autologin via token)

Lockout (Bruteforce protection)

OTP (2 factor authentication via TOTP)

Page 55: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth FeaturesLogin

Logout

Change Password

Change Login

Reset Password

Create Account

Close Account

Verify Account

Confirm Password

Remember (Autologin via token)

Lockout (Bruteforce protection)

OTP (2 factor authentication via TOTP)

Recovery Codes (2 factor authentication via backup codes)

Page 56: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth FeaturesLogin

Logout

Change Password

Change Login

Reset Password

Create Account

Close Account

Verify Account

Confirm Password

Remember (Autologin via token)

Lockout (Bruteforce protection)

OTP (2 factor authentication via TOTP)

Recovery Codes (2 factor authentication via backup codes)

SMS Codes (2 factor authentication via SMS)

Page 57: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth FeaturesLogin

Logout

Change Password

Change Login

Reset Password

Create Account

Close Account

Verify Account

Confirm Password

Remember (Autologin via token)

Lockout (Bruteforce protection)

OTP (2 factor authentication via TOTP)

Recovery Codes (2 factor authentication via backup codes)

SMS Codes (2 factor authentication via SMS)

Verify Change Login

Page 58: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth FeaturesLogin

Logout

Change Password

Change Login

Reset Password

Create Account

Close Account

Verify Account

Confirm Password

Remember (Autologin via token)

Lockout (Bruteforce protection)

OTP (2 factor authentication via TOTP)

Recovery Codes (2 factor authentication via backup codes)

SMS Codes (2 factor authentication via SMS)

Verify Change Login

Verify Account Grace Period

Page 59: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth FeaturesLogin

Logout

Change Password

Change Login

Reset Password

Create Account

Close Account

Verify Account

Confirm Password

Remember (Autologin via token)

Lockout (Bruteforce protection)

OTP (2 factor authentication via TOTP)

Recovery Codes (2 factor authentication via backup codes)

SMS Codes (2 factor authentication via SMS)

Verify Change Login

Verify Account Grace Period

Password Grace Period

Page 60: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth FeaturesLogin

Logout

Change Password

Change Login

Reset Password

Create Account

Close Account

Verify Account

Confirm Password

Remember (Autologin via token)

Lockout (Bruteforce protection)

OTP (2 factor authentication via TOTP)

Recovery Codes (2 factor authentication via backup codes)

SMS Codes (2 factor authentication via SMS)

Verify Change Login

Verify Account Grace Period

Password Grace Period

Password Complexity

Page 61: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth FeaturesLogin

Logout

Change Password

Change Login

Reset Password

Create Account

Close Account

Verify Account

Confirm Password

Remember (Autologin via token)

Lockout (Bruteforce protection)

OTP (2 factor authentication via TOTP)

Recovery Codes (2 factor authentication via backup codes)

SMS Codes (2 factor authentication via SMS)

Verify Change Login

Verify Account Grace Period

Password Grace Period

Password Complexity

Disallow Password Reuse

Page 62: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth FeaturesLogin

Logout

Change Password

Change Login

Reset Password

Create Account

Close Account

Verify Account

Confirm Password

Remember (Autologin via token)

Lockout (Bruteforce protection)

OTP (2 factor authentication via TOTP)

Recovery Codes (2 factor authentication via backup codes)

SMS Codes (2 factor authentication via SMS)

Verify Change Login

Verify Account Grace Period

Password Grace Period

Password Complexity

Disallow Password Reuse

Password Expiration

Page 63: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth FeaturesLogin

Logout

Change Password

Change Login

Reset Password

Create Account

Close Account

Verify Account

Confirm Password

Remember (Autologin via token)

Lockout (Bruteforce protection)

OTP (2 factor authentication via TOTP)

Recovery Codes (2 factor authentication via backup codes)

SMS Codes (2 factor authentication via SMS)

Verify Change Login

Verify Account Grace Period

Password Grace Period

Password Complexity

Disallow Password Reuse

Password Expiration

Account Expiration

Page 64: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth FeaturesLogin

Logout

Change Password

Change Login

Reset Password

Create Account

Close Account

Verify Account

Confirm Password

Remember (Autologin via token)

Lockout (Bruteforce protection)

OTP (2 factor authentication via TOTP)

Recovery Codes (2 factor authentication via backup codes)

SMS Codes (2 factor authentication via SMS)

Verify Change Login

Verify Account Grace Period

Password Grace Period

Password Complexity

Disallow Password Reuse

Password Expiration

Account Expiration

Session Expiration

Page 65: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth FeaturesLogin

Logout

Change Password

Change Login

Reset Password

Create Account

Close Account

Verify Account

Confirm Password

Remember (Autologin via token)

Lockout (Bruteforce protection)

OTP (2 factor authentication via TOTP)

Recovery Codes (2 factor authentication via backup codes)

SMS Codes (2 factor authentication via SMS)

Verify Change Login

Verify Account Grace Period

Password Grace Period

Password Complexity

Disallow Password Reuse

Password Expiration

Account Expiration

Session Expiration

Single Session

Page 66: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth FeaturesLogin

Logout

Change Password

Change Login

Reset Password

Create Account

Close Account

Verify Account

Confirm Password

Remember (Autologin via token)

Lockout (Bruteforce protection)

OTP (2 factor authentication via TOTP)

Recovery Codes (2 factor authentication via backup codes)

SMS Codes (2 factor authentication via SMS)

Verify Change Login

Verify Account Grace Period

Password Grace Period

Password Complexity

Disallow Password Reuse

Password Expiration

Account Expiration

Session Expiration

Single Session

JWT

Page 67: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth FeaturesLogin

Logout

Change Password

Change Login

Reset Password

Create Account

Close Account

Verify Account

Confirm Password

Remember (Autologin via token)

Lockout (Bruteforce protection)

OTP (2 factor authentication via TOTP)

Recovery Codes (2 factor authentication via backup codes)

SMS Codes (2 factor authentication via SMS)

Verify Change Login

Verify Account Grace Period

Password Grace Period

Password Complexity

Disallow Password Reuse

Password Expiration

Account Expiration

Session Expiration

Single Session

JWT

Update Password Hash

Page 68: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth FeaturesLogin

Logout

Change Password

Change Login

Reset Password

Create Account

Close Account

Verify Account

Confirm Password

Remember (Autologin via token)

Lockout (Bruteforce protection)

OTP (2 factor authentication via TOTP)

Recovery Codes (2 factor authentication via backup codes)

SMS Codes (2 factor authentication via SMS)

Verify Change Login

Verify Account Grace Period

Password Grace Period

Password Complexity

Disallow Password Reuse

Password Expiration

Account Expiration

Session Expiration

Single Session

JWT

Update Password HashHTTP Basic Auth

Page 69: Rodauth: Clean Authentication - Valentine Ostakh
Page 70: Rodauth: Clean Authentication - Valentine Ostakh

Security

Page 71: Rodauth: Clean Authentication - Valentine Ostakh

• Uses database functions to access password hashes

• Two database accounts are used

Page 72: Rodauth: Clean Authentication - Valentine Ostakh

• Uses database functions to access password hashes (optional)

• Two database accounts are used (optional)

Page 73: Rodauth: Clean Authentication - Valentine Ostakh

Flexibility

Page 74: Rodauth: Clean Authentication - Valentine Ostakh

Can be used with the any rack framework

Page 75: Rodauth: Clean Authentication - Valentine Ostakh

require "roda"

class RodauthApp < Roda # If using Rodauth in a non-Roda application # plugin :middleware

plugin :rodauth do enable :login, :logout, :change_password end

route do |r| r.rodauth

rodauth.require_authentication

# If using Rodauth in a Roda application # Your app code here end end

# If using Rodauth in a non-Roda application # use RodauthApp

# If using Rodauth in a Roda application run RodauthApp

Page 76: Rodauth: Clean Authentication - Valentine Ostakh

require "roda"

class RodauthApp < Roda # If using Rodauth in a non-Roda application # plugin :middleware

plugin :rodauth do enable :login, :logout, :change_password end

route do |r| r.rodauth

rodauth.require_authentication

# If using Rodauth in a Roda application # Your app code here end end

# If using Rodauth in a non-Roda application # use RodauthApp

# If using Rodauth in a Roda application run RodauthApp

Page 77: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth uses a simple configuration DSL

Page 78: Rodauth: Clean Authentication - Valentine Ostakh

require 'simple_ldap_authenticator' plugin :rodauth do enable :login, :logout

# Don't require the bcrypt library, since using LDAP for auth require_bcrypt? false

# Treat the login itself as the account account_from_login{|l| l.to_s}

# Use the login provided as the session value account_session_value{account}

# Store session value in :login key, since the :account_id # default wouldn't make sense session_key :login

password_match? do |password| SimpleLdapAuthenticator.valid?(account, password) end end

Page 79: Rodauth: Clean Authentication - Valentine Ostakh

Simplicity

Page 80: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth allows for overriding any part of the

framework

Page 81: Rodauth: Clean Authentication - Valentine Ostakh

module Auth class Rodauth < Roda plugin :rodauth do enable :login end

route do |r| r.post 'login' do # Custom POST /login handling here end

r.rodauth end end end

Page 82: Rodauth: Clean Authentication - Valentine Ostakh

How to start use Rodauth?

Page 83: Rodauth: Clean Authentication - Valentine Ostakh

• Resolve database dependencies

• Define Rodauth features

Page 84: Rodauth: Clean Authentication - Valentine Ostakh

Database dependencies

Page 85: Rodauth: Clean Authentication - Valentine Ostakh

• Setup database

• Create tables

Page 86: Rodauth: Clean Authentication - Valentine Ostakh

Setup With Postgresql

# Load extentions psql -U postgres -c "CREATE EXTENSION citext" ${DATABASE_NAME}

# Create database accounts createuser -U postgres ${DATABASE_NAME} createuser -U postgres ${DATABASE_NAME}_password

Page 87: Rodauth: Clean Authentication - Valentine Ostakh

Setup With Postgresqlcreate_table(:accounts) do primary_key :id, :type=>:Bignum foreign_key :status_id, :account_statuses, :null=>false, :default=>1 if db.database_type == :postgres citext :email, :null=>false constraint :valid_email, :email=>/^[^,;@ \r\n]+@[^,@; \r\n]+\.[^,@; \r\n]+$/ index :email, :unique=>true, :where=>{:status_id=>[1, 2]} else String :email, :null=>false index :email, :unique=>true end end

case database_type when :postgres user = get{Sequel.lit('current_user')} + '_password' run "GRANT REFERENCES ON accounts TO #{user}" end

Page 88: Rodauth: Clean Authentication - Valentine Ostakh

Define Rodauth Featuresplugin :rodauth, :json=>true, :csrf=>false, :flash=>false do enable :change_password, :close_account, :create_account, :login, :logout, :remember, :reset_password, :verify_account, :otp, :recovery_codes, :sms_codes, :password_complexity, :disallow_password_reuse, :password_grace_period, :account_expiration, :single_session, :jwt, :session_expiration, max_invalid_logins 2 allow_password_change_after 60 verify_account_grace_period 300 jwt_secret secret sms_send do |phone_number, message| MUTEX.synchronize{SMS[session_value] = "..."} end end

Page 89: Rodauth: Clean Authentication - Valentine Ostakh

Summary

Page 90: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth Advantages• Integration with any rack application

• Minimun dependencies

• Features

• Security

• Simplicity

Page 91: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth Disadvantages

• Doesn’t work with OAuth

• Routes design: can mismatch with your design

Page 92: Rodauth: Clean Authentication - Valentine Ostakh

My own experience

Page 93: Rodauth: Clean Authentication - Valentine Ostakh

Registrationmodule Auth class Rodauth < Roda DB = Sequel.connect(ENV['DATABASE_URL'])

plugin :middleware

plugin :rodauth, json: :only do enable :login, :logout, :jwt, :create_account

jwt_session_hash do super().merge(exp: SmartTaskApi::Utils.jwt_expiration) end

jwt_secret ENV['JWT_SECRET'] end

route do |r| r.rodauth

env['rodauth'] = rodauth end end end

Page 94: Rodauth: Clean Authentication - Valentine Ostakh

Token Authenticationmodule Api class Rodauth < Roda DB = Sequel.connect(ENV['DATABASE_URL'])

plugin :middleware

plugin :rodauth, json: :only do enable :jwt

jwt_secret ENV['JWT_SECRET'] end

route do |r| r.rodauth rodauth.require_authentication env['rodauth'] = rodauth end end end

Page 95: Rodauth: Clean Authentication - Valentine Ostakh

Rodauth Examples• https://github.com/jeremyevans/ginatra

• https://github.com/jeremyevans/rodauth-demo-rails

• https://github.com/davydovanton/rodauth_hanami

• https://github.com/davydovanton/grape-rodauth

• https://github.com/valikos/smart-task-api-hanami

Page 96: Rodauth: Clean Authentication - Valentine Ostakh

RodauthClean Authentication

Page 97: Rodauth: Clean Authentication - Valentine Ostakh

Thanks!

Page 98: Rodauth: Clean Authentication - Valentine Ostakh

Questions?

Page 99: Rodauth: Clean Authentication - Valentine Ostakh

Valentyn Ostakh

https://github.com/valikos

https://twitter.com/valikos_ost