merb auth

42
MerbAuth Darwinian Authentication

Upload: hassox

Post on 18-May-2015

4.626 views

Category:

Technology


3 download

DESCRIPTION

A talk given at merbcamp debuting merb-auth

TRANSCRIPT

Page 1: Merb Auth

MerbAuth Darwinian Authentication

Page 2: Merb Auth

In The Beginning

Conditions for Authentication were ripe

Merb

Plugins

Rubigen with scopes (6th Nov 2007)

Restful Authentication was missing

Page 3: Merb Auth

merbful_authentication

Page 4: Merb Auth

merbful_authentication

Released 4th January 2008

Direct port of Restful Authentication

Supported DataMapper & ActiveRecord

Supported Rspec & Test::Unit

Page 5: Merb Auth

Pros

First plugin with multi-ORM support in Merb

A lot of applications used it

Page 6: Merb Auth

Cons

Generated code

Very complex

Maintenance sucked

Page 7: Merb Auth

The Catalyst

Slices were born

Page 8: Merb Auth

Original merb-auth

Page 9: Merb Auth

merb-auth

Mostly direct port of merbful_authentication

Used brand new slices plugin

Moved to Library Code not Generated

ORM support via mixins

Forgotten Passwords

17 June 2008

Page 10: Merb Auth

merb-auth - Pros

Live code. Not generated

Minimal Application Configuration

Implemented as a slice

Easier to maintain (still sucked)

Page 11: Merb Auth

merb-auth - ConsUser model hidden

Hard to please all through configuration

Unclear how to customize it

Tied to one model type

Dictates user model

Extensions difficult (No OpenID)

Difficult to change logic

Page 12: Merb Auth

Evolutionary Step Required

Page 13: Merb Auth

The Catalyst

Adam French proposed:

Authenticating Sessions

Simple session based api

Using Exceptions to refuse entry

Provides correct status code

Page 14: Merb Auth

ExceptionalAuthentication

Adam created a prototype

ExceptionalAuthentication

Application including his proposals

Page 15: Merb Auth

Session API

session.authenticated?

session.authenticate!

session.user

session.user=

session.abandon!

Page 16: Merb Auth

ExceptionalAuthentication

Originally a DataMapper based system

Decided to allow arbitrary “user” objects

Page 17: Merb Auth

Code Named - Mauth

Page 18: Merb Auth

MerbAuth - What is it?

Authentication Framework

Cascading Strategy Concept

Only what’s needed to support authentication

Supports user objects such as

DM, AR, Sequel, Hash, String, File, IO, or just plain old Object

Page 19: Merb Auth

MerbAuth - What it’s Not

A user management system

Page 20: Merb Auth

Default Merb Stack

Merb stack includes MerbAuth with:

Password Slice

Password based form authentication

Basic Authentication

Salted Users

Routes / Forms

Page 21: Merb Auth

Activating Merb Stack

Comes with a Basic User model (Change it up)

rake db:automigrate

Make something to protect

Page 22: Merb Auth

Protect It

Route Level

authenticate do resources :paychecks end

Controller Level

before :ensure_authenticated

Page 23: Merb Auth

Activating Merb Stack

Add a user to the system

Login “/login”

Page 24: Merb Auth

Protect - Router Based

authenticate(BasicAuth) do resources :api

authenticate do resources :posts do resources :comments end end

end

Page 25: Merb Auth

Controller Based

:ensure_authenticated

before :ensure_authenticated

# OR

before :ensure_authenticated, :with => [OpenID, BasicAuth]

Page 26: Merb Auth

Why Protect Routes?

Page 27: Merb Auth

Why Protect Routes?

RACK

DISPATCHER

ROUTER

AUTH

CONTROLLER

FILTERS

ACTION + RENDER

FILTERSRACK

FAIL

Pass

Page 28: Merb Auth

What is a Strategy?Strategy contains logic for “Authentication”

Implements a #run! method

class PasswordStrategy < Merb::Authentication::Strategy def run! login = request.params[:login] password = request.params[:password] User.authenticate!(login, password) if login endend

Declare many. One for each login type

Page 29: Merb Auth

MerbAuthCascading Strategies

Each strategy is run in order

Success == First Strategy to return object

Failure == No Strategies return object

Stops trying when authenticated

Re-order strategies with

Merb::Authentication.default_strategy_order=

Page 30: Merb Auth

What Happens on Fail?

Raises Unauthenticated exception

Uses Merbs Exception Handling

Exceptions#unauthenticated

Sets correct status code

Page 31: Merb Auth

Display Errors

<%= error_messages_for session.authentication %>

Page 32: Merb Auth

Customize Login Form

Exceptions#unauthenticated (view)

Page 33: Merb Auth

How to Logout?

session.abandon!

Page 34: Merb Auth

What Strategies Are There?

Password based form login

Open ID

Basic Authentication

Require a default strategy to load / define it

Monkey patch parts of it you need to change

Page 35: Merb Auth

Mixins

Use mixins to extend your User or MerbAuth

Salted User mixin

Redirect Mixin (redirect_back_or)

Page 36: Merb Auth

Advanced Strategies

Loaded in order of declaration

Implement a #run! method

Use Inheritance to share code

Arbitrary Object Session Serialization

Return an object from #run! to authenticate

Page 37: Merb Auth

Advanced StrategiesYourStrategy#redirect!

YourStrategy.abstract!

YourStrategy#user_class

YourStrategy#headers

YourStrategy#status

request.params (request params)

params (route params)

Page 38: Merb Auth

Failure Messages

Inside a strategy

session.authentication.errors.add(:label, "message")

Page 39: Merb Auth

Overview of Advanced Setup

require ‘merb-auth-core’

Define <User> model (Maybe)

Setup session storage

Declare strategies

Protect methods

Setup login / logout actions

Page 40: Merb Auth

Where Next?

Implement slices

Implement Strategies & Mixins

Page 42: Merb Auth

Thanks

Adam French

Ben Burket