Skip to content

Commit

Permalink
Use mapping's models in the strategies, refs socialcast#23
Browse files Browse the repository at this point in the history
  • Loading branch information
BRMatt committed Dec 12, 2011
1 parent 2999df8 commit f493d60
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions lib/devise/oauth2_providable/strategy_helpers.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions lib/devise_oauth2_providable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down

0 comments on commit f493d60

Please sign in to comment.