Module: OAuthCampingPlugin::Models

Defined in:
lib/camping-oauth.rb

Overview

Models module for the OAuth Camping Plugin. The module will be plugged in to the main app models module. Example: module CampingOAuthProvider::Models include OAuthCampingPlugin::Models

class User < Base; has_many :client_applications has_many :tokens, :class_name=>“OauthToken”,:order=>“authorized_at desc”,:include=> end # … end

This module requires the oauth-plugin gem to be installed as it will load the following models

- ClientApplication
- OauthToken
- OathNonce
- RequestToken
- AccessToken

Class Method Summary collapse

Class Method Details

.downObject

Down-migrates the schema definition for the 5 OAuth models



740
741
742
743
744
745
746
# File 'lib/camping-oauth.rb', line 740

def self.down
	ActiveRecord::Schema.define do
		drop_table :client_applications
		drop_table :oauth_tokens
		drop_table :oauth_nonces
	end
end

.included(mod) ⇒ Object

Loads the 5 standard OAuth models defined in the oauth-plugin gem



655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
# File 'lib/camping-oauth.rb', line 655

def self.included(mod)
	oauth_plugin_gem = Gem::loaded_specs['oauth-plugin']
	oauth_plugin_path = oauth_plugin_gem.full_gem_path
	provider_template_path = oauth_plugin_path + '/generators/oauth_provider/templates'

	%w(
			client_application.rb
			oauth_token.rb
			oauth_nonce.rb
			request_token.rb
			access_token.rb
	).each { |lib| mod.module_eval(File.read("#{provider_template_path}/#{lib}")) }

	# @techarch : Reset the table names back to pre-Camping
	mod.module_eval do
		mod::ClientApplication.class_eval	{ set_table_name	"client_applications" }
		
		mod::ClientApplication.class_eval	do
			  def self.verify_request(request, options = {}, &block)
				begin
				  signature = OAuth::Signature.build(request, options, &block)
				
					app_module_name = self.to_s.split("::").first	
					nonce_class_name = "#{app_module_name}::Models::OauthNonce"	
					nonce_class = nonce_class_name.constantize

				  return false unless nonce_class.remember(signature.request.nonce, signature.request.timestamp)

				  value = signature.verify
				  value
				rescue OAuth::Signature::UnknownSignatureMethod => e
				  false
				end
			  end
		end

		mod::OauthToken.class_eval 		{ set_table_name	"oauth_tokens" }
		mod::OauthNonce.class_eval 	{ set_table_name	"oauth_nonces" }
	end
end

.upObject

Up-migrates the schema definition for the 5 OAuth models



697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
# File 'lib/camping-oauth.rb', line 697

def self.up
	ActiveRecord::Schema.define do
		create_table :client_applications do |t|
		  t.string :name
		  t.string :url
		  t.string :support_url
		  t.string :callback_url
		  t.string :key, :limit => 20
		  t.string :secret, :limit => 40
		  t.integer :user_id

		  t.timestamps
		end
		
		add_index :client_applications, :key, :unique
		
		create_table :oauth_tokens do |t|
		  t.integer :user_id
		  t.string :type, :limit => 20
		  t.integer :client_application_id
		  t.string :token, :limit => 20
		  t.string :secret, :limit => 40
		  t.string :callback_url
		  t.string :verifier, :limit => 20
		  t.timestamp :authorized_at, :invalidated_at
		  t.timestamps
		end
		
		add_index :oauth_tokens, :token, :unique
		
		create_table :oauth_nonces do |t|
		  t.string :nonce
		  t.integer :timestamp

		  t.timestamps
		end
		
		add_index :oauth_nonces,[:nonce, :timestamp], :unique		
	
	end
end