Module: OAuthCampingPlugin::Controllers

Defined in:
lib/camping-oauth.rb

Overview

Controllers module for the OAuth Camping Plugin. The module will be plugged in to the main app controllers module using:

- extend to add class methods to the app controllers module
  • include_oauth_controllers to dynamically plugin the OAuth and Helpers modules inside each controller class

(this is why the call must be the last statement in the controllers module)

Example:

module CampingOAuthProvider::Controllers

extend OAuthCampingPlugin::Controllers

# …

include_oauth_controllers

end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.common_oauth_controllersObject

Returns the source code for all common OAuth controllers



768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
# File 'lib/camping-oauth.rb', line 768

def self.common_oauth_controllers
	<<-CLASS_DEFS
		
class OAuthRegisterApplication < R '/oauth/register'
	def get
		@application= ClientApplication.new
		render :new_application_registration
	end
	
	def post
		@user = User.find(@state.user_id)
		if !@user
			return "login first"
		end

		@application = ClientApplication.find_by_user_id_and_name(@state.user_id, input.name)
		if @application
			@info = 'You already have an application with this name.'
		else
			@application = ClientApplication.new :user_id => @state.user_id,
				:name => input.name,
				:url => input.url,
				:support_url => input.support_url,
				:callback_url => input.callback_url
				
			@user.client_applications << @application

			@application.save
			if @application
				return(render(:application_registration))
			else
				@info = @application.errors.full_messages unless @application.errors.empty?
			end
		end
		
		render :new_application_registration
	end
end

class OAuthProvideRequestToken < R '/oauth/request_token'
	include OAuthCampingPlugin::OAuth

	def post
		oauth_consumer_key = oauth_header_params['oauth_consumer_key']

		@application = ClientApplication.find_by_key(oauth_consumer_key)
		@token = @application.create_request_token
		log_debug 'OAuthProvideRequestToken> request token for oauth_consumer_key:' + oauth_consumer_key + '=' + @token.inspect
		@token.to_query
	end
end

class OAuthAuthorizeToken < R '/oauth/authorize'
	include OAuthCampingPlugin::OAuth

	def get
		@oauth_token = input.oauth_token
		render :authorize
	end
	
	def post
		@token = RequestToken.find_by_token input.oauth_token
		return(render(:authorize_failure_token_not_found)) if @token.nil?
		
		return(render(:authorize_failure_invalidated)) if @token.invalidated? 
		
			return(render(:authorize_failure)) unless user_authorizes_token?

           @token.authorize!(current_user)
		log_debug 'OAuthAuthorizeToken> request token=' + @token.inspect
		
           if @token.oauth10?
               @redirect_url = input.oauth_callback || @token.client_application.callback_url
           else
               @redirect_url = (@token.oob? || @token.callback_url.nil?) ? @token.client_application.callback_url : @token.callback_url
           end

			return(render(:authorize_success)) unless @redirect_url

		@full_redirect_url = @token.oauth10? ? (@redirect_url + '?oauth_token=' + @token.token) :  (@redirect_url + '?oauth_token=' + @token.token + '&oauth_verifier=' + @token.verifier)
		
		redirect @full_redirect_url
	end
	
	# Override this to match your authorization page form
	def user_authorizes_token?
		input.authorize == '1' || input.authorize == 'on'
	end		
end

class OAuthRevokeToken < R '/oauth/revoke'
	include OAuthCampingPlugin::OAuth

	def get
		@token = OauthToken.find_by_token(input.oauth_token)
		return(render(:authorize_failure_token_not_found)) if @token.nil?

		render :revoke
	end
	
	def post
		@token = OauthToken.find_by_token(input.oauth_token)
		return(render(:authorize_failure_token_not_found)) if @token.nil?

		if input.revoke != 'on'
			@info = "You did not confirm you wanted to revoke this token. Check the checkbox to confirm."
			return(render(:revoke))
		end
		
		@token.invalidate!
		log_debug 'OAuthRevokeToken> access token=' + @token.inspect
		
		render :revoke_success
	end
end

class OAuthProvideAccessToken < R '/oauth/access_token'
	include OAuthCampingPlugin::OAuth

	def post
		log_debug 'OAuthProvideAccessToken> @current_token=' + self.current_token.inspect

		return(r(401,'')) if self.current_token.nil?
		
		@token = self.current_token.exchange!
		log_debug 'OAuthProvideAccessToken> access token=' + @token.inspect

		return(r(401,'')) if self.current_token.nil?
		@token.to_query
		
	end #post
	end

	CLASS_DEFS
end

Instance Method Details

#include_oauth_controllersObject

Includes the OAuth and Helpers modules inside each controller class using class_eval (this is why the call must be the last statement in the controllers module)



906
907
908
909
910
911
912
913
914
915
916
# File 'lib/camping-oauth.rb', line 906

def include_oauth_controllers
	module_eval OAuthCampingPlugin::Controllers.common_oauth_controllers

	# Add Oauth to each controller
	r.each do |x| 
		x.class_eval do
			include OAuthCampingPlugin::OAuth
			include OAuthCampingPlugin::Helpers
		end
	end			
end