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

BostonRB - January 2014 - Devise - Bob Breznak

BostonRB - January 2014 - Devise - Bob Breznak

My presentation at the January 2014 BostonRB meetup. I discussed the inner workings of Devise by reviewing Rack and Warden then dove into a problem that we addressed in Devise.

Avatar for Bob Breznak

Bob Breznak

January 14, 2014

Other Decks in Programming

Transcript

  1. Application Kanye Caps JSON Content-Type: application/json { “fact”: “CATS CAN

    ROTATE THEIR EARS 180 DEGREES EACH, INDEPENDENTLY.” }
  2. Application Kanye Caps JSON Content-Type: text { “fact”: “I'ma let

    you finish but CATS CAN ROTATE THEIR EARS 180 DEGREES EACH, INDEPENDENTLY.” }
  3. Application Kanye Caps JSON Auth Location: /facts/premium Params: { token:

    ‘12345’ } env[‘user’] = { name: ‘Bob’ }
  4. Application Kanye Caps JSON Auth Location: /facts/premium Params: { token:

    ‘12345’ } env[‘user’] = { name: ‘Bob’ }
  5. Application Kanye Caps JSON Auth Location: /facts/premium Params: { token:

    ‘12345’ } env[‘user’] = { name: ‘Bob’ }
  6. Application Kanye Caps JSON Auth Location: /facts/premium Params: { token:

    ‘12345’ } env[‘user’] = { name: ‘Bob’ }
  7. Application Kanye Caps JSON Auth Content-Type: text Bob, Cats can

    rotate their ears 180 degrees each, independently.
  8. Application Kanye Caps JSON Auth Content-Type: text BOB, CATS CAN

    ROTATE THEIR EARS 180 DEGREES EACH, INDEPENDENTLY.
  9. Application Kanye Caps JSON Auth Content-Type: text BOB, CATS CAN

    ROTATE THEIR EARS 180 DEGREES EACH, INDEPENDENTLY.
  10. Application Kanye Caps JSON Auth Content-Type: text I’MA LET YOU

    FINISH BUT BOB, CATS CAN ROTATE THEIR EARS 180 DEGREES EACH, INDEPENDENTLY.
  11. Application Kanye Caps JSON Auth Content-Type: text I’MA LET YOU

    FINISH BUT BOB, CATS CAN ROTATE THEIR EARS 180 DEGREES EACH, INDEPENDENTLY.
  12. Application Kanye Caps JSON Auth Content-Type: text Sorry, you do

    not have access to superior feline trivia.
  13. Application Warden Warden Manager Proxy Strategy Session Hooks in Warden

    Location: /facts/premium Params: { token: ‘12345’ }
  14. Application Warden Warden Manager Proxy Strategy Session Hooks in Warden

    Location: /facts/premium Params: { token: ‘12345’ } env[‘warden’] => Warden::Proxy
  15. Application Warden Warden Manager Proxy Strategy Session Hooks in Warden

    Location: /facts/premium Params: { token: ‘12345’ } env[‘warden’] => Warden::Proxy 1
  16. Application Warden Warden Manager Proxy Strategy Session Warden “Client” 1

    def call(env) env[‘warden’].authenticate! ...
  17. Application Warden Warden Manager Proxy Strategy Session Warden “Client” 1

    def call(env) env[‘warden’].authenticate! ... 2
  18. Application Warden Warden Manager Proxy Strategy Session Warden “Client” 2

    1 if user = session.fetch_user return user else run_strategies if winning_strategy && winning_strategy.user return winning_strategy.user end end
  19. Application Warden Warden Manager Proxy Strategy Session Warden “Client” 2

    1 if user = session.fetch_user return user else run_strategies if winning_strategy && winning_strategy.user return winning_strategy.user end end 3
  20. Application Warden Warden Manager Proxy Strategy Session User <=> ID

    ! Requires Session Middleware 2 1 3 def serialize(user) # User to a key (i.e. User.id) ... end ! def deserialize(key) # User from a key (i.e. User.find key ) ... end
  21. Application Warden Warden Manager Proxy Strategy Session User <=> ID

    ! Requires Session Middleware 2 1 3 4 def serialize(user) # User to a key (i.e. User.id) ... end ! def deserialize(key) # User from a key (i.e. User.find key ) ... end
  22. Application Warden Warden Manager Proxy Strategy Session Hooks in Warden

    2 1 3 4 config.failure_app.call(env) # returns [401, {}, [‘Unauthenticated’]] 5
  23. Application Warden Warden Manager Proxy Strategy Session Hooks in Warden

    2 1 3 4 config.failure_app.call(env) # returns [401, {}, [‘Unauthenticated’]] 5 6
  24. Application Warden Warden Manager Proxy Strategy Session Authentication logic 2

    1 3 4 def authenticate! user = ... # some method to find a user success! user end
  25. Application Warden Warden Manager Proxy Strategy Session Authentication logic 2

    1 3 4 def authenticate! user = ... # some method to find a user success! user end 5
  26. Application Warden Warden Manager Proxy Strategy Session 2 1 3

    4 5 6 def call(env) ... [ 200, {}, [“Hello #{ user.name }”] ]
  27. Application Warden Warden Manager Proxy Strategy Session 2 1 3

    4 5 6 def call(env) ... [ 200, {}, [“Hello #{ user.name }”] ] 7
  28. Application Warden Warden Manager Proxy Strategy Session Hooks in Warden

    2 1 3 4 5 6 [ 200, {}, [“Hello #{ user.name }”] ] 7
  29. Simple Rails Auth Configure Rails Middleware Build a Session Serializer

    Build a Strategy Build session controller Add views for users & session management Add helper methods for current_user and authenticate! more… add gem use generators to install run migrations Warden Devise
  30. Devise Warden + Rails + Application • Strategies • Session

    • Configuration • Initialization • Conventions • Generators • Controllers • Views • Mailers
  31. What did Devise keep? • Confirmable, Recoverable, etc.. • Secure

    token generator • Constant-time Compare • Warden
  32. What was our solution? • Store only digested tokens •

    Use constant time compare • Build a Warden Strategy • Bundle the user id & the source token
  33. Checkout EverTrue If you’re in the Boston area, we’re always

    looking for smart people, developers and non-developers alike! ! http://www.evertrue.com/careers http://www.pinterest.com/evertrueco