-
-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #177 from thoughtbot/rack
Perform authentication in Rack middleware
- Loading branch information
Showing
17 changed files
with
296 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
require 'clearance/configuration' | ||
require 'clearance/session' | ||
require 'clearance/rack_session' | ||
require 'clearance/authentication' | ||
require 'clearance/user' | ||
require 'clearance/engine' | ||
require 'clearance/password_strategies' | ||
require 'clearance/password_strategies' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Clearance | ||
class RackSession | ||
def initialize(app) | ||
@app = app | ||
end | ||
|
||
def call(env) | ||
session = Clearance::Session.new(env) | ||
env_with_clearance = env.merge(:clearance => session) | ||
response = @app.call(env_with_clearance) | ||
session.add_cookie_to_headers response[1] | ||
response | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
module Clearance | ||
class Session | ||
REMEMBER_TOKEN_COOKIE = "remember_token".freeze | ||
|
||
def initialize(env) | ||
@env = env | ||
end | ||
|
||
def signed_in? | ||
current_user.present? | ||
end | ||
|
||
def current_user | ||
@current_user ||= with_remember_token do |token| | ||
::User.find_by_remember_token(token) | ||
end | ||
end | ||
|
||
def sign_in(user) | ||
@current_user = user | ||
end | ||
|
||
def sign_out | ||
current_user.reset_remember_token! if signed_in? | ||
@current_user = nil | ||
cookies.delete(REMEMBER_TOKEN_COOKIE) | ||
end | ||
|
||
def add_cookie_to_headers(headers) | ||
if signed_in? | ||
Rack::Utils.set_cookie_header!(headers, | ||
REMEMBER_TOKEN_COOKIE, | ||
:value => current_user.remember_token, | ||
:expires => Clearance.configuration.cookie_expiration.call, | ||
:path => "/") | ||
end | ||
end | ||
|
||
private | ||
|
||
def with_remember_token | ||
if token = remember_token | ||
yield token | ||
end | ||
end | ||
|
||
def remember_token | ||
cookies[REMEMBER_TOKEN_COOKIE] | ||
end | ||
|
||
def cookies | ||
@cookies ||= Rack::Request.new(@env).cookies | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'spec_helper' | ||
|
||
describe Clearance::RackSession do | ||
it "injects a clearance session into the environment" do | ||
expected_session = "the session" | ||
expected_session.stubs(:add_cookie_to_headers) | ||
Clearance::Session.stubs(:new => expected_session) | ||
headers = { "X-Roaring-Lobster" => "Red" } | ||
|
||
app = Rack::Builder.new do | ||
use Clearance::RackSession | ||
run lambda { |env| Rack::Response.new(env[:clearance], 200, headers).finish } | ||
end | ||
|
||
env = Rack::MockRequest.env_for("/") | ||
|
||
response = Rack::MockResponse.new(*app.call(env)) | ||
|
||
Clearance::Session.should have_received(:new).with(env) | ||
response.body.should == expected_session | ||
expected_session.should have_received(:add_cookie_to_headers).with(has_entries(headers)) | ||
end | ||
end |
Oops, something went wrong.