Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rodauth: Clean Authentication

Rodauth: Clean Authentication

Have you had any concerns with a selection of tool for authentication? This talk about Rodauth, awesome authentication framework. We will see how Rodauth can compete with existing libraries and which advantages and disadvantages do they have.

Valentine Ostakh

February 14, 2017
Tweet

More Decks by Valentine Ostakh

Other Decks in Programming

Transcript

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

    View Slide

  2. Rodauth
    Clean Authentication

    View Slide

  3. What is the most
    necessary feature for
    interaction with users?

    View Slide

  4. Authentication

    View Slide

  5. Authentication is the act of
    identification of user that going
    to interact with your product

    View Slide

  6. I want authentication
    for my application

    View Slide

  7. View Slide

  8. Ruby-toolbox

    View Slide

  9. Awesome-ruby

    View Slide

  10. Authentication
    • Authlogic
    • Devise
    • Clearance
    • Sorcery
    • Warden
    • Rodauth

    View Slide

  11. What about custom
    solution?

    View Slide

  12. View Slide

  13. View Slide

  14. Custom Solution vs
    Authentication Libraries
    Library 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

    View Slide

  15. I want flexible
    authentication that can be
    used with any framework

    View Slide

  16. View Slide

  17. View Slide

  18. View Slide

  19. How to choose a library
    for my application?

    View Slide

  20. Dependencies

    View Slide

  21. • Authlogic - activerecord, activesupport
    • Devise - rails, warden
    • Clearance - rails, rack
    • Sorcery - rails
    • Warden - rack
    • Rodauth - roda, rack

    View Slide

  22. Clearance

    View Slide

  23. Features

    View Slide

  24. Registration
    • Authlogic
    • Devise
    • Clearance
    • Sorcery
    • Warden
    • Rodauth

    View Slide

  25. Login
    • Authlogic
    • Devise
    • Clearance
    • Sorcery
    • Warden
    • Rodauth

    View Slide

  26. Logout
    • Authlogic
    • Devise
    • Clearance
    • Sorcery
    • Warden
    • Rodauth

    View Slide

  27. Would be great to have
    token authentication

    View Slide

  28. Token Authentication
    • Authlogic
    • Devise
    • Clearance
    • Sorcery
    • Warden
    • Rodauth

    View Slide

  29. 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

    View Slide

  30. Token Authentication

    Gems
    • jwt_authentication
    • simple_token_authentication
    • devise_token_auth

    View Slide

  31. Token Authentication

    Gems
    • jwt_authentication (based on devise)
    • simple_token_authentication (based on devise)
    • devise_token_auth (based on devise)

    View Slide

  32. Token Authentication

    View Slide

  33. Popularity

    View Slide

  34. 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

    View Slide

  35. Summary

    View Slide

  36. Library Dependencies Features
    Token

    Authentication
    Devise
    Warden
    Authlogic
    Sorcery
    Clearance
    Rodauth

    View Slide

  37. Rodauth

    View Slide

  38. Rodauth

    View Slide

  39. Jeremy Evans
    Twitter: @jeremyevans0

    View Slide

  40. Roda
    Sequel

    View Slide

  41. Rodauth Goals
    • Security
    • Simplicity
    • Flexibility

    View Slide

  42. Features first

    View Slide

  43. Rodauth Features
    Login

    View Slide

  44. Rodauth Features
    Login
    Logout

    View Slide

  45. Rodauth Features
    Login
    Logout
    Change Password

    View Slide

  46. Rodauth Features
    Login
    Logout
    Change Password
    Change Login

    View Slide

  47. Rodauth Features
    Login
    Logout
    Change Password
    Change Login
    Reset Password

    View Slide

  48. Rodauth Features
    Login
    Logout
    Change Password
    Change Login
    Reset Password
    Create Account

    View Slide

  49. Rodauth Features
    Login
    Logout
    Change Password
    Change Login
    Reset Password
    Create Account
    Close Account

    View Slide

  50. Rodauth Features
    Login
    Logout
    Change Password
    Change Login
    Reset Password
    Create Account
    Close Account
    Verify Account

    View Slide

  51. Rodauth Features
    Login
    Logout
    Change Password
    Change Login
    Reset Password
    Create Account
    Close Account
    Verify Account
    Confirm Password

    View Slide

  52. Rodauth Features
    Login
    Logout
    Change Password
    Change Login
    Reset Password
    Create Account
    Close Account
    Verify Account
    Confirm Password
    Remember (Autologin via token)

    View Slide

  53. Rodauth Features
    Login
    Logout
    Change Password
    Change Login
    Reset Password
    Create Account
    Close Account
    Verify Account
    Confirm Password
    Remember (Autologin via token)
    Lockout (Bruteforce protection)

    View Slide

  54. Rodauth Features
    Login
    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)

    View Slide

  55. Rodauth Features
    Login
    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)

    View Slide

  56. Rodauth Features
    Login
    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)

    View Slide

  57. Rodauth Features
    Login
    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

    View Slide

  58. Rodauth Features
    Login
    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

    View Slide

  59. Rodauth Features
    Login
    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

    View Slide

  60. Rodauth Features
    Login
    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

    View Slide

  61. Rodauth Features
    Login
    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

    View Slide

  62. Rodauth Features
    Login
    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

    View Slide

  63. Rodauth Features
    Login
    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

    View Slide

  64. Rodauth Features
    Login
    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

    View Slide

  65. Rodauth Features
    Login
    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

    View Slide

  66. Rodauth Features
    Login
    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

    View Slide

  67. Rodauth Features
    Login
    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

    View Slide

  68. Rodauth Features
    Login
    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
    HTTP Basic Auth

    View Slide

  69. View Slide

  70. Security

    View Slide

  71. • Uses database functions to access password
    hashes
    • Two database accounts are used

    View Slide

  72. • Uses database functions to access password
    hashes (optional)
    • Two database accounts are used (optional)

    View Slide

  73. Flexibility

    View Slide

  74. Can be used with the
    any rack framework

    View Slide

  75. 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

    View Slide

  76. 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

    View Slide

  77. Rodauth uses a simple
    configuration DSL

    View Slide

  78. 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

    View Slide

  79. Simplicity

    View Slide

  80. Rodauth allows for
    overriding any part of the
    framework

    View Slide

  81. 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

    View Slide

  82. How to start use
    Rodauth?

    View Slide

  83. • Resolve database dependencies
    • Define Rodauth features

    View Slide

  84. Database
    dependencies

    View Slide

  85. • Setup database
    • Create tables

    View Slide

  86. 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

    View Slide

  87. Setup With Postgresql
    create_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

    View Slide

  88. Define Rodauth Features
    plugin :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

    View Slide

  89. Summary

    View Slide

  90. Rodauth Advantages
    • Integration with any rack application
    • Minimun dependencies
    • Features
    • Security
    • Simplicity

    View Slide

  91. Rodauth Disadvantages
    • Doesn’t work with OAuth
    • Routes design: can mismatch with your design

    View Slide

  92. My own experience

    View Slide

  93. Registration
    module 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

    View Slide

  94. Token Authentication
    module 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

    View Slide

  95. 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

    View Slide

  96. Rodauth
    Clean Authentication

    View Slide

  97. Thanks!

    View Slide

  98. Questions?

    View Slide

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

    View Slide