Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'feature/application_owner_support' of github.com:Tout/d…

…oorkeeper into feature/application_owner_support

Conflicts:
	spec/lib/config_spec.rb
	spec/models/doorkeeper/application_spec.rb
  • Loading branch information...
commit d959d2f725b121de2b44fb366fb994c418432f36 2 parents af53f7f + 42c046a
@petergoldstein petergoldstein authored
View
14 lib/doorkeeper/config.rb
@@ -28,6 +28,16 @@ def build
@config
end
+ def enable_application_owner(opts={})
+ require File.join(File.dirname(__FILE__), 'models', 'ownership')
+ Doorkeeper::Application.send :include, Doorkeeper::Models::Ownership
+ confirm_application_owner if opts[:confirmation].present? && opts[:confirmation]
+ end
+
+ def confirm_application_owner
+ @config.instance_variable_set("@confirm_application_owner", true)
+ end
+
def default_scopes(*scopes)
@config.instance_variable_set("@default_scopes", Doorkeeper::OAuth::Scopes.from_array(scopes))
end
@@ -124,6 +134,10 @@ def refresh_token_enabled?
!!@refresh_token_enabled
end
+ def confirm_application_owner?
+ !!@confirm_application_owner
+ end
+
def default_scopes
@default_scopes ||= Doorkeeper::OAuth::Scopes.new
end
View
16 lib/doorkeeper/models/ownership.rb
@@ -0,0 +1,16 @@
+module Doorkeeper
+ module Models
+ module Ownership
+ def validate_owner?
+ Doorkeeper.configuration.confirm_application_owner?
+ end
+
+ def self.included(base)
+ base.class_eval do
+ belongs_to :owner, :polymorphic => true
+ validates :owner, :presence => true, :if => :validate_owner?
+ end
+ end
+ end
+ end
+end
View
15 lib/generators/doorkeeper/application_owner_generator.rb
@@ -0,0 +1,15 @@
+require 'rails/generators/active_record'
+
+class Doorkeeper::ApplicationOwnerGenerator < Rails::Generators::Base
+ include Rails::Generators::Migration
+ source_root File.expand_path('../templates', __FILE__)
+ desc "Provide support for client application ownership."
+
+ def application_owner
+ migration_template 'add_owner_to_application_migration.rb', 'db/migrate/add_owner_to_application.rb'
+ end
+
+ def self.next_migration_number(dirname)
+ ActiveRecord::Generators::Base.next_migration_number(dirname)
+ end
+end
View
7 lib/generators/doorkeeper/templates/add_owner_to_application_migration.rb
@@ -0,0 +1,7 @@
+class AddOwnerToApplication < ActiveRecord::Migration
+ def change
+ add_column :oauth_applications, :owner_id, :integer, :null => true
+ add_column :oauth_applications, :owner_type, :string, :null => true
+ add_index :oauth_applications, [:owner_id, :owner_type]
+ end
+end
View
6 lib/generators/doorkeeper/templates/initializer.rb
@@ -32,6 +32,12 @@
# Issue access tokens with refresh token (disabled by default)
# use_refresh_token
+ # Provide support for an owner to be assigned to each registered application (disabled by default)
+ # Optional parameter :confirmation => true (default false) if you want to enforce ownership of
+ # a registered application
+ # Note: you must also run the rails g doorkeeper:application_owner generator to provide the necessary support
+ # enable_application_owner :confirmation => false
+
# Define access token scopes for your provider
# For more information go to https://github.com/applicake/doorkeeper/wiki/Using-Scopes
# default_scopes :public
View
9 lib/generators/doorkeeper/templates/migration.rb
@@ -1,14 +1,15 @@
class CreateDoorkeeperTables < ActiveRecord::Migration
def change
create_table :oauth_applications do |t|
- t.string :name, :null => false
- t.string :uid, :null => false
- t.string :secret, :null => false
- t.string :redirect_uri, :null => false
+ t.string :name, :null => false
+ t.string :uid, :null => false
+ t.string :secret, :null => false
+ t.string :redirect_uri, :null => false
t.timestamps
end
add_index :oauth_applications, :uid, :unique => true
+ add_index :oauth_applications, [:owner_id, :owner_type]
create_table :oauth_access_grants do |t|
t.integer :resource_owner_id, :null => false
View
10 spec/dummy/db/migrate/20120524202412_create_doorkeeper_tables.rb
@@ -1,10 +1,12 @@
class CreateDoorkeeperTables < ActiveRecord::Migration
def change
create_table :oauth_applications do |t|
- t.string :name, :null => false
- t.string :uid, :null => false
- t.string :secret, :null => false
- t.string :redirect_uri, :null => false
+ t.string :name, :null => false
+ t.string :uid, :null => false
+ t.string :secret, :null => false
+ t.string :redirect_uri, :null => false
+ t.integer :owner_id, :null => true
+ t.string :owner_type, :null => true
t.timestamps
end
View
2  spec/dummy/db/schema.rb
@@ -46,6 +46,8 @@
t.string "uid", :null => false
t.string "secret", :null => false
t.string "redirect_uri", :null => false
+ t.string "owner_type", :null => true, :default => "User"
+ t.integer "owner_id", :null => true
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
View
23 spec/generators/application_owner_generator_spec.rb
@@ -0,0 +1,23 @@
+require 'spec_helper_integration'
+require 'generators/doorkeeper/application_owner_generator'
+
+
+describe 'Doorkeeper::ApplicationOwnerGenerator' do
+ include GeneratorSpec::TestCase
+
+ tests Doorkeeper::ApplicationOwnerGenerator
+ destination ::File.expand_path("../tmp/dummy", __FILE__)
+
+ describe "after running the generator" do
+ before :each do
+ prepare_destination
+ FileUtils.mkdir(::File.expand_path("config", Pathname(destination_root)))
+ FileUtils.copy_file(::File.expand_path("../templates/routes.rb", __FILE__), ::File.expand_path("config/routes.rb", Pathname.new(destination_root)))
+ run_generator
+ end
+
+ it "creates a migration" do
+ assert_migration "db/migrate/add_owner_to_application.rb"
+ end
+ end
+end
View
35 spec/lib/config_spec.rb
@@ -95,4 +95,39 @@
subject.access_token_methods.should == [:from_access_token_param, :from_bearer_param]
end
end
+
+ describe "enable_application_owner" do
+ it "is disabled by default" do
+ Doorkeeper::Application.new.should_not respond_to :owner
+ end
+
+ context "when enabled without confirmation" do
+ before do
+ Doorkeeper.configure do
+ enable_application_owner
+ end
+ end
+ it "adds support for application owner" do
+ Doorkeeper::Application.new.should respond_to :owner
+ end
+ it "Doorkeeper.configuration.confirm_application_owner? returns false" do
+ Doorkeeper.configuration.confirm_application_owner?.should_not be_true
+ end
+ end
+
+ context "when enabled with confirmation set to true" do
+ before do
+ Doorkeeper.configure do
+ enable_application_owner :confirmation => true
+ end
+ end
+ it "adds support for application owner" do
+ Doorkeeper::Application.new.should respond_to :owner
+ end
+ it "Doorkeeper.configuration.confirm_application_owner? returns true" do
+ Doorkeeper.configuration.confirm_application_owner?.should be_true
+ end
+ end
+
+ end
end
View
1  spec/lib/oauth/authorization_request_spec.rb
@@ -13,6 +13,7 @@ module Doorkeeper::OAuth
end
before :each do
+ Doorkeeper.stub_chain(:configuration, :confirm_application_owner?).and_return(false)
Doorkeeper.configuration.stub(:default_scopes).and_return(Doorkeeper::OAuth::Scopes.from_string('public write'))
end
View
40 spec/models/doorkeeper/application_spec.rb
@@ -2,10 +2,42 @@
module Doorkeeper
describe Application do
+ let(:require_owner) { Doorkeeper.configuration.instance_variable_set("@confirm_application_owner", true) }
+ let(:unset_require_owner) { Doorkeeper.configuration.instance_variable_set("@confirm_application_owner", false) }
let(:new_application) { FactoryGirl.build(:application) }
- it 'is valid given valid attributes' do
- new_application.should be_valid
+ context "application_owner is enabled" do
+ before do
+ Doorkeeper.configure do
+ enable_application_owner
+ end
+ end
+
+ context 'application owner is not required' do
+ before(:each) do
+ unset_require_owner
+ end
+
+ it 'is valid given valid attributes' do
+ new_application.should be_valid
+ end
+ end
+
+ context "application owner is required" do
+ before(:each) do
+ require_owner
+ @owner = mock_model 'User', :id => 1234
+ end
+
+ it 'is invalid without an owner' do
+ new_application.should_not be_valid
+ end
+
+ it 'is valid with an owner' do
+ new_application.owner = @owner
+ new_application.should be_valid
+ end
+ end
end
it 'is invalid without a name' do
@@ -32,8 +64,8 @@ module Doorkeeper
end
it 'checks uniqueness of uid' do
- app1 = FactoryGirl.create(:application)
- app2 = FactoryGirl.create(:application)
+ app1 = Factory(:application)
+ app2 = Factory(:application)
app2.uid = app1.uid
app2.should_not be_valid
end
Please sign in to comment.
Something went wrong with that request. Please try again.