From f493d60f0a7f4a07dbe93c5362d861a7d6403047 Mon Sep 17 00:00:00 2001 From: Matt Button Date: Mon, 12 Dec 2011 23:22:24 +0000 Subject: [PATCH] Use mapping's models in the strategies, refs #23 --- .../strategies/oauth2_grant_type_strategy.rb | 9 ++++++++- .../strategies/oauth2_providable_strategy.rb | 4 +++- lib/devise/oauth2_providable/strategy_helpers.rb | 15 +++++++++++++++ lib/devise_oauth2_providable.rb | 3 +++ 4 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 lib/devise/oauth2_providable/strategy_helpers.rb diff --git a/lib/devise/oauth2_providable/strategies/oauth2_grant_type_strategy.rb b/lib/devise/oauth2_providable/strategies/oauth2_grant_type_strategy.rb index 8241be1..871086e 100644 --- a/lib/devise/oauth2_providable/strategies/oauth2_grant_type_strategy.rb +++ b/lib/devise/oauth2_providable/strategies/oauth2_grant_type_strategy.rb @@ -3,6 +3,8 @@ module Devise module Strategies class Oauth2GrantTypeStrategy < Authenticatable + include Devise::Oauth2Providable::StrategyHelpers + def valid? params[:controller] == 'devise/oauth2_providable/tokens' && request.post? && params[:grant_type] == grant_type end @@ -13,7 +15,7 @@ def grant_type def client return @client if @client - @client = Devise::Oauth2Providable::Client.find_by_identifier params[:client_id] + @client = devise_oauth_mapping.models[:client].find_by_identifier_and_secret(params[:client_id], params[:client_secret]) env[Devise::Oauth2Providable::CLIENT_ENV_REF] = @client @client end @@ -24,6 +26,11 @@ def oauth_error!(error_code = :invalid_request, description = nil) body[:error_description] = description if description custom! [400, {'Content-Type' => 'application/json'}, [body.to_json]] end + + protected + def devise_oauth_mapping + env[Devise::Oauth2Providable::MAPPING_ENV_REF] + end end end end diff --git a/lib/devise/oauth2_providable/strategies/oauth2_providable_strategy.rb b/lib/devise/oauth2_providable/strategies/oauth2_providable_strategy.rb index 113c2fa..efd6329 100644 --- a/lib/devise/oauth2_providable/strategies/oauth2_providable_strategy.rb +++ b/lib/devise/oauth2_providable/strategies/oauth2_providable_strategy.rb @@ -3,13 +3,15 @@ module Devise module Strategies class Oauth2Providable < Authenticatable + include Devise::Oauth2Providable::StrategyHelpers + def valid? @req = Rack::OAuth2::Server::Resource::Bearer::Request.new(env) @req.oauth2? end def authenticate! @req.setup! - token = Devise::Oauth2Providable::AccessToken.find_by_token @req.access_token + token = oauth_models[:access_token].find_by_token @req.access_token env[Devise::Oauth2Providable::CLIENT_ENV_REF] = token.client if token resource = token ? token.user : nil if validate(resource) diff --git a/lib/devise/oauth2_providable/strategy_helpers.rb b/lib/devise/oauth2_providable/strategy_helpers.rb new file mode 100644 index 0000000..bdaa1a4 --- /dev/null +++ b/lib/devise/oauth2_providable/strategy_helpers.rb @@ -0,0 +1,15 @@ + +module Devise + module Oauth2Providable + module StrategyHelpers + protected + def oauth_mapping + Devise::Oauth2Providable.mappings[mapping.name] + end + + def oauth_models + self.oauth_mapping.models + end + end + end +end diff --git a/lib/devise_oauth2_providable.rb b/lib/devise_oauth2_providable.rb index 236ca5b..1c8ad86 100644 --- a/lib/devise_oauth2_providable.rb +++ b/lib/devise_oauth2_providable.rb @@ -2,6 +2,7 @@ require 'rack/oauth2' require 'devise/oauth2_providable/engine' require 'devise/oauth2_providable/expirable_token' +require 'devise/oauth2_providable/strategy_helpers' require 'devise/oauth2_providable/strategies/oauth2_providable_strategy' require 'devise/oauth2_providable/strategies/oauth2_password_grant_type_strategy' require 'devise/oauth2_providable/strategies/oauth2_refresh_token_grant_type_strategy' @@ -14,10 +15,12 @@ require 'devise/oauth2_providable/mapping' require 'devise/oauth2_providable/rails/routes' + module Devise module Oauth2Providable CLIENT_ENV_REF = 'oauth2.client' REFRESH_TOKEN_ENV_REF = "oauth2.refresh_token" + MAPPING_ENV_REF = "devise_oauth2_providable.mapping" mattr_reader :mappings @@mappings = ActiveSupport::OrderedHash.new