Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This defines a single method `#credentials` which provides a useful shorthand for accessing rails-auth.credentials from the Rack environment. It wrapps it in a HashWithIndifferentAccess for eash access.
- Loading branch information
Tony Arcieri
committed
Mar 11, 2016
1 parent
78ff735
commit 1e60da2
Showing
5 changed files
with
78 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require "active_support/hash_with_indifferent_access" | ||
|
||
module Rails | ||
module Auth | ||
# Convenience methods designed to be included in an ActionController::Base subclass | ||
# Recommended use: include in ApplicationController | ||
module ControllerMethods | ||
# Obtain credentials for the current request | ||
# | ||
# @return [HashWithIndifferentAccess] credentials extracted from the environment | ||
# | ||
def credentials | ||
@_rails_auth_credentials ||= begin | ||
creds = Rails::Auth.credentials(request.env) | ||
HashWithIndifferentAccess.new(creds).freeze | ||
end | ||
end | ||
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,25 @@ | ||
RSpec.describe Rails::Auth::ControllerMethods do | ||
let(:controller_class) do | ||
Class.new do | ||
attr_reader :request | ||
|
||
def initialize(env) | ||
@request = OpenStruct.new(env: env) | ||
end | ||
|
||
include Rails::Auth::ControllerMethods | ||
end | ||
end | ||
|
||
describe "#credentials" do | ||
let(:example_credential_type) { "x509" } | ||
let(:example_credential_value) { instance_double(Rails::Auth::X509::Certificate) } | ||
|
||
let(:example_env) { Rails::Auth.add_credential({}, example_credential_type, example_credential_value) } | ||
let(:example_controller) { controller_class.new(example_env) } | ||
|
||
it "extracts credentials from the Rack environment" do | ||
expect(example_controller.credentials[example_credential_type.to_sym]).to eq example_credential_value | ||
end | ||
end | ||
end |