Skip to content
This repository was archived by the owner on Jan 7, 2019. It is now read-only.

Commit a7ae23d

Browse files
authored
Merge pull request #63 from stormpath/mc-basic-bearer-auth-from-ruby-62
Replace existing code for controller authentication with the Ruby SDK
2 parents 62d2aa4 + 35603c0 commit a7ae23d

File tree

13 files changed

+227
-191
lines changed

13 files changed

+227
-191
lines changed

app/services/stormpath/rails/account_from_access_token.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@ class AccountFromAccessToken
88
DifferentIssuerError = Class.new(ArgumentError)
99

1010
def initialize(access_token)
11-
raise(NoAccessToken) if access_token.nil?
11+
raise(NoAccessToken) if access_token.blank?
1212
@access_token = access_token
1313
end
1414

1515
def account
16-
@account ||= resolution_class.new(access_token).account
16+
@account ||= resolution_instance.verify(access_token).account
1717
end
1818

1919
private
2020

21-
def resolution_class
21+
def resolution_instance
2222
case Stormpath::Rails.config.web.oauth2.password.validation_strategy.to_sym
2323
when :local
24-
LocalAccountResolution
24+
Stormpath::Oauth::VerifyAccessToken.new(Client.application, local: true)
2525
when :stormpath
26-
StormpathAccountResolution
26+
Stormpath::Oauth::VerifyAccessToken.new(Client.application)
2727
else
2828
raise ArgumentError, 'Invalid validation strategy'
2929
end

app/services/stormpath/rails/account_from_access_token/local_account_resolution.rb

Lines changed: 0 additions & 48 deletions
This file was deleted.

app/services/stormpath/rails/account_from_access_token/stormpath_account_resolution.rb

Lines changed: 0 additions & 27 deletions
This file was deleted.

app/services/stormpath/rails/controller_authentication.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,16 @@ def authenticate!
1818
if any_auth_cookie_present?
1919
FromCookies.new(cookies).authenticate!
2020
elsif bearer_authorization_header?
21-
FromBearerAuth.new(authorization_header).authenticate!
21+
Stormpath::Authentication::HttpBearerAuthentication.new(
22+
Stormpath::Rails::Client.application,
23+
authorization_header,
24+
local: validation_strategy
25+
).authenticate!.account
2226
elsif basic_authorization_header?
23-
FromBasicAuth.new(authorization_header).authenticate!
27+
Stormpath::Authentication::HttpBasicAuthentication.new(
28+
Stormpath::Rails::Client.application,
29+
authorization_header
30+
).authenticate!.account
2431
else
2532
raise UnauthenticatedRequest
2633
end
@@ -39,6 +46,14 @@ def any_auth_cookie_present?
3946
def basic_authorization_header?
4047
authorization_header =~ BASIC_PATTERN
4148
end
49+
50+
def validation_strategy
51+
if Stormpath::Rails.config.web.oauth2.password.validation_strategy == 'stormpath'
52+
true
53+
else
54+
false
55+
end
56+
end
4257
end
4358
end
4459
end

app/services/stormpath/rails/controller_authentication/from_basic_auth.rb

Lines changed: 0 additions & 45 deletions
This file was deleted.

app/services/stormpath/rails/controller_authentication/from_bearer_auth.rb

Lines changed: 0 additions & 34 deletions
This file was deleted.

lib/stormpath/rails/controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module Controller
1515
def current_account
1616
@current_account ||= begin
1717
ControllerAuthentication.new(cookies, request.headers['Authorization']).authenticate!
18-
rescue ControllerAuthentication::UnauthenticatedRequest
18+
rescue ControllerAuthentication::UnauthenticatedRequest, Stormpath::Error, JWT::DecodeError
1919
nil
2020
end
2121
end

spec/factories.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
password 'Password1337'
55
given_name { Faker::Name.first_name }
66
surname { Faker::Name.last_name }
7-
username { Faker::Internet.user_name }
7+
username { "#{Faker::Internet.user_name}_#{Faker::Internet.user_name}" }
88
phone_number { Faker::PhoneNumber.cell_phone }
99
end
1010

1111
factory :account_without_username, class: Stormpath::Resource::Account do
12-
sequence(:email) { |n| "dev#{n}@example.com" }
12+
sequence(:email) { |n| "dev#{n}@testmail.stormpath.com" }
1313
password 'Password1337'
1414
given_name { Faker::Name.first_name }
1515
surname { Faker::Name.last_name }
@@ -18,4 +18,14 @@
1818
factory :unverified_account, parent: :account do
1919
status 'UNVERIFIED'
2020
end
21+
22+
factory :directory, class: Stormpath::Resource::Directory do
23+
sequence(:name) { |n| "rails-#{n}-#{Faker::Lorem.word}-directory" }
24+
description 'rails test directory'
25+
end
26+
27+
factory :application, class: Stormpath::Resource::Application do
28+
sequence(:name) { |n| "rails-#{n}-#{Faker::Lorem.word}-application" }
29+
description 'rails test application'
30+
end
2131
end

spec/requests/profile/get_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ def response_body
66
end
77

88
let(:account) { Stormpath::Rails::Client.application.accounts.create(account_attrs) }
9-
109
let(:account_attrs) { FactoryGirl.attributes_for(:account) }
1110

1211
after { account.delete }

0 commit comments

Comments
 (0)