Skip to content
This repository has been archived by the owner on Oct 5, 2023. It is now read-only.

clear session before Devise::Oauth2Providable::TokensController#create to work with iPhone #21

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions app/controllers/devise/oauth2_providable/tokens_controller.rb
@@ -1,4 +1,5 @@
class Devise::Oauth2Providable::TokensController < ApplicationController
before_filter :clear_session
before_filter :authenticate_user!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would using force => true accomplish the same thing?

before_filter :authenticate_user!, :force => true

can you add a unit test to verify that either of these solutions work as expected?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will take a look at when i hvae time => wednesday ,

i dont think that

before_filter :authenticate_user!, :force => true

will work,
as I understand devise it will at first look for a session cookie and the
try strategies to authenticate the user, if the session is there devise will re-authenticate the user by the session cookie
or in depending of the ordering of the strategies

regards Luzifer

On Nov 28, 2011, at 6:39 PM, Ryan Sonnek wrote:

@@ -1,4 +1,5 @@
class Devise::Oauth2Providable::TokensController < ApplicationController

  • before_filter :clear_session
    before_filter :authenticate_user!

would using force => true accomplish the same thing?

before_filter :authenticate_user!, :force => true

can you add a unit test to verify that either of these solutions work as expected?


Reply to this email directly or view it on GitHub:
https://github.com/socialcast/devise_oauth2_providable/pull/21/files#r254799

skip_before_filter :verify_authenticity_token, :only => :create

Expand All @@ -7,10 +8,31 @@ def create
@access_token = @refresh_token.access_tokens.create!(:client => oauth2_current_client, :user => current_user)
render :json => @access_token.token_response
end

def destroy
oauth2_current_refresh_token.destroy if oauth2_current_refresh_token

oauth2_current_access_token.destroy if oauth2_current_access_token

head :status => 204
end

private
# clear the session, so devise does not use session cookie based auth in any case
# the iPhone SDK by default has a shared cookie jar for WebViews and NSURL Request's
# and thus will send a cookie to this method
def clear_session
session.clear
end

def oauth2_current_client
env[Devise::Oauth2Providable::CLIENT_ENV_REF]
end

def oauth2_current_access_token
env[Devise::Oauth2Providable::ACCESS_TOKEN_ENV_REF]
end

def oauth2_current_refresh_token
env[Devise::Oauth2Providable::REFRESH_TOKEN_ENV_REF]
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/devise/oauth2_providable/refresh_token.rb
@@ -1,5 +1,5 @@
class Devise::Oauth2Providable::RefreshToken < ActiveRecord::Base
expires_according_to :refresh_token_expires_in

has_many :access_tokens
has_many :access_tokens, :dependent => :delete_all
end
2 changes: 1 addition & 1 deletion config/routes.rb
Expand Up @@ -3,5 +3,5 @@

resources :authorizations, :only => :create
match 'authorize' => 'authorizations#new'
resource :token, :only => :create
resource :token, :only => [:create, :destroy]
end
Expand Up @@ -11,6 +11,7 @@ def authenticate!
@req.setup!
token = Devise::Oauth2Providable::AccessToken.find_by_token @req.access_token
env[Devise::Oauth2Providable::CLIENT_ENV_REF] = token.client if token
env[Devise::Oauth2Providable::ACCESS_TOKEN_ENV_REF] = token
resource = token ? token.user : nil
if validate(resource)
success! resource
Expand Down
1 change: 1 addition & 0 deletions lib/devise_oauth2_providable.rb
Expand Up @@ -15,6 +15,7 @@ module Devise
module Oauth2Providable
CLIENT_ENV_REF = 'oauth2.client'
REFRESH_TOKEN_ENV_REF = "oauth2.refresh_token"
ACCESS_TOKEN_ENV_REF = "oauth2.access_token"

class << self
def random_id
Expand Down
6 changes: 5 additions & 1 deletion spec/routing/tokens_routing_spec.rb
Expand Up @@ -8,5 +8,9 @@
it 'routes POST /oauth2/token' do
post('/oauth2/token').should route_to('devise/oauth2_providable/tokens#create')
end

# it 'routes DELETE /oauth2/token' do
# post('/oauth2/token').should route_to('devise/oauth2_providable/tokens#destroy')
# end
end
end
end