Skip to content

Commit

Permalink
Refactor scope agnostic auth into a helper
Browse files Browse the repository at this point in the history
Still needs tests, refs socialcast#23
  • Loading branch information
BRMatt committed Nov 29, 2011
1 parent ea71ebe commit fb5509d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Devise
module Oauth2Providable
class AuthorizationsController < ApplicationController
include Devise::Controllers::InternalHelpers
include Devise::Oauth2Providable::Controllers::Helpers
before_filter :authenticate_scope!

rescue_from Rack::OAuth2::Server::Authorize::BadRequest do |e|
Expand Down Expand Up @@ -55,13 +55,6 @@ def authorize_endpoint(allow_approval = false)
end
end
end

# Authenticates the current scope and gets the current resource from the session.
# Taken from devise
def authenticate_scope!
send(:"authenticate_#{resource_name}!", :force => true)
self.resource = send(:"current_#{resource_name}")
end
end
end
end
7 changes: 4 additions & 3 deletions app/controllers/devise/oauth2_providable/tokens_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class Devise::Oauth2Providable::TokensController < ApplicationController
before_filter :authenticate_user!
include Devise::Oauth2Providable::Controllers::Helpers
before_filter :authenticate_scope!
skip_before_filter :verify_authenticity_token, :only => :create

def create
@refresh_token = oauth2_current_refresh_token || oauth2_current_client.refresh_tokens.create!(:user => current_user)
@access_token = @refresh_token.access_tokens.create!(:client => oauth2_current_client, :user => current_user)
@refresh_token = oauth2_current_refresh_token || oauth2_current_client.refresh_tokens.create!(:user => self.resource)
@access_token = @refresh_token.access_tokens.create!(:client => oauth2_current_client, :user => self.resource)
render :json => @access_token.token_response
end
private
Expand Down
24 changes: 24 additions & 0 deletions lib/devise/oauth2_providable/controllers/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

module Devise
module Oauth2Providable
module Controllers
module Helpers
extend ActiveSupport::Concern

module ClassMethods
include Devise::Controllers::InternalHelpers
include LocalInstanceMethods
end

module LocalInstanceMethods
# Authenticates the current scope and gets the current resource from the session.
# Taken from devise
def authenticate_scope!
send(:"authenticate_#{resource_name}!", :force => true)
self.resource = send(:"current_#{resource_name}")
end
end
end
end
end
end

0 comments on commit fb5509d

Please sign in to comment.