From 74a7c3ab00e90dbd5d6feecf707dc9da1abd13ee Mon Sep 17 00:00:00 2001 From: danielgrippi <danielgrippi@gmail.com> Date: Thu, 23 Jun 2011 14:37:26 -0700 Subject: [PATCH] collapsed oauth2 migrations; made app factory --- app/controllers/authorizations_controller.rb | 2 +- ...th2_provider_models_activerecord_client.rb | 4 ++-- ...602224152_diaspora_o_auth_client_fields.rb | 13 ---------- ...d_nonce_and_public_key_to_oauth_clients.rb | 13 ---------- ... => 20110623210918_add_o_auth2_support.rb} | 20 ++++++++++++++-- db/schema.rb | 15 ++++++------ .../authorizations_controller_spec.rb | 24 +++++++++---------- spec/factories.rb | 8 +++++++ ..._models_activerecord_authorization_spec.rb | 8 +++---- 9 files changed, 53 insertions(+), 54 deletions(-) delete mode 100644 db/migrate/20110602224152_diaspora_o_auth_client_fields.rb delete mode 100644 db/migrate/20110614005205_add_nonce_and_public_key_to_oauth_clients.rb rename db/migrate/{20110526184644_add_oauth2_tables.rb => 20110623210918_add_o_auth2_support.rb} (68%) diff --git a/app/controllers/authorizations_controller.rb b/app/controllers/authorizations_controller.rb index 71d86631d1..6da21ff8f9 100644 --- a/app/controllers/authorizations_controller.rb +++ b/app/controllers/authorizations_controller.rb @@ -37,7 +37,7 @@ class AuthorizationsController < ApplicationController unless message =='ok' render :text => message, :status => 403 else - client = OAuth2::Provider.client_class.create_or_reset_from_manifest!(manifest) + client = OAuth2::Provider.client_class.create_or_reset_from_manifest!(manifest, public_key) render :json => {:client_id => client.oauth_identifier, :client_secret => client.oauth_secret, diff --git a/app/models/oauth2_provider_models_activerecord_client.rb b/app/models/oauth2_provider_models_activerecord_client.rb index 9063e89c5f..0d440cf6e3 100644 --- a/app/models/oauth2_provider_models_activerecord_client.rb +++ b/app/models/oauth2_provider_models_activerecord_client.rb @@ -1,5 +1,5 @@ class OAuth2::Provider::Models::ActiveRecord::Client - def self.create_or_reset_from_manifest! manifest + def self.create_or_reset_from_manifest!(manifest, pub_key) if obj = find_by_name(manifest['name']) obj.oauth_identifier = OAuth2::Provider::Random.base62(16) obj.oauth_secret = OAuth2::Provider::Random.base62(32) @@ -12,7 +12,7 @@ class OAuth2::Provider::Models::ActiveRecord::Client :description => manifest["description"], :homepage_url => manifest["homepage_url"], :icon_url => manifest["icon_url"], - :public_key => manifest["public_key"] + :public_key => pub_key.export ) end end diff --git a/db/migrate/20110602224152_diaspora_o_auth_client_fields.rb b/db/migrate/20110602224152_diaspora_o_auth_client_fields.rb deleted file mode 100644 index 184c1bd0c7..0000000000 --- a/db/migrate/20110602224152_diaspora_o_auth_client_fields.rb +++ /dev/null @@ -1,13 +0,0 @@ -class DiasporaOAuthClientFields < ActiveRecord::Migration - def self.up - add_column :oauth_clients, :description, :text - add_column :oauth_clients, :homepage_url, :string - add_column :oauth_clients, :icon_url, :string - end - - def self.down - remove_column :oauth_clients, :icon_url - remove_column :oauth_clients, :homepage_url - remove_column :oauth_clients, :description - end -end diff --git a/db/migrate/20110614005205_add_nonce_and_public_key_to_oauth_clients.rb b/db/migrate/20110614005205_add_nonce_and_public_key_to_oauth_clients.rb deleted file mode 100644 index 7623502c6f..0000000000 --- a/db/migrate/20110614005205_add_nonce_and_public_key_to_oauth_clients.rb +++ /dev/null @@ -1,13 +0,0 @@ -class AddNonceAndPublicKeyToOauthClients < ActiveRecord::Migration - def self.up - add_column :oauth_clients, :nonce, :string, :limit => 64 - add_column :oauth_clients, :public_key, :text - add_index :oauth_clients, :nonce - end - - def self.down - remove_column :oauth_clients, :nonce - remove_column :oauth_clients, :public_key - remove_index :oauth_clients, :nonce - end -end diff --git a/db/migrate/20110526184644_add_oauth2_tables.rb b/db/migrate/20110623210918_add_o_auth2_support.rb similarity index 68% rename from db/migrate/20110526184644_add_oauth2_tables.rb rename to db/migrate/20110623210918_add_o_auth2_support.rb index 8b389511f7..14b3541894 100644 --- a/db/migrate/20110526184644_add_oauth2_tables.rb +++ b/db/migrate/20110623210918_add_o_auth2_support.rb @@ -1,13 +1,21 @@ -class AddOauth2Tables < ActiveRecord::Migration +class AddOAuth2Support < ActiveRecord::Migration def self.up create_table 'oauth_clients', :force => true do |t| t.string 'name', :limit => 127, :null => false + t.text 'description', :null => false + t.string 'homepage_url', :limit => 127, :null => false + t.string 'icon_url', :limit => 127, :null => false + t.string 'oauth_identifier', :limit => 32, :null => false t.string 'oauth_secret', :limit => 32, :null => false + t.string 'nonce', :limit => 64 + t.text 'public_key', :null => false t.text 'permissions_overview', :null => false end add_index :oauth_clients, :name, :unique => true + add_index :oauth_clients, :homepage_url, :unique => true + add_index :oauth_clients, :nonce, :unique => true create_table 'oauth_authorization_codes', :force => true do |t| t.integer 'authorization_id', :null => false @@ -39,11 +47,19 @@ class AddOauth2Tables < ActiveRecord::Migration end def self.down - remove_index "oauth_authorizations", ["resource_owner_id", "resource_owner_type", "client_id"] + remove_index "oauth_authorizations", :name => "index_oauth_authorizations_on_resource_owner_and_client_id" drop_table 'oauth_access_tokens' + drop_table 'oauth_authorizations' + drop_table 'oauth_authorization_codes' + + remove_index :oauth_clients, :column => :nonce + remove_index :oauth_clients, :column => :homepage_url + remove_index :oauth_clients, :column => :name + drop_table 'oauth_clients' end + end diff --git a/db/schema.rb b/db/schema.rb index 2d29c5dd7d..69f37ce1df 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110614005205) do +ActiveRecord::Schema.define(:version => 20110623210918) do create_table "aspect_memberships", :force => true do |t| t.integer "aspect_id", :null => false @@ -225,18 +225,19 @@ ActiveRecord::Schema.define(:version => 20110614005205) do create_table "oauth_clients", :force => true do |t| t.string "name", :limit => 127, :null => false + t.text "description", :null => false + t.string "homepage_url", :limit => 127, :null => false + t.string "icon_url", :limit => 127, :null => false t.string "oauth_identifier", :limit => 32, :null => false t.string "oauth_secret", :limit => 32, :null => false - t.text "permissions_overview", :null => false - t.text "description" - t.string "homepage_url" - t.string "icon_url" t.string "nonce", :limit => 64 - t.text "public_key" + t.text "public_key", :null => false + t.text "permissions_overview", :null => false end + add_index "oauth_clients", ["homepage_url"], :name => "index_oauth_clients_on_homepage_url", :unique => true add_index "oauth_clients", ["name"], :name => "index_oauth_clients_on_name", :unique => true - add_index "oauth_clients", ["nonce"], :name => "index_oauth_clients_on_nonce" + add_index "oauth_clients", ["nonce"], :name => "index_oauth_clients_on_nonce", :unique => true create_table "people", :force => true do |t| t.string "guid", :null => false diff --git a/spec/controllers/authorizations_controller_spec.rb b/spec/controllers/authorizations_controller_spec.rb index a8f1102b3a..761f04a413 100644 --- a/spec/controllers/authorizations_controller_spec.rb +++ b/spec/controllers/authorizations_controller_spec.rb @@ -29,7 +29,7 @@ describe AuthorizationsController do "description" => "The best way to chub.", "homepage_url" => "http://chubbi.es/", "icon_url" => "#", - "permissions_overview" => "I will use the permissions this way!", + "permissions_overview" => "I will use the permissions this way!", } packaged_manifest = {:public_key => @public_key.export, :jwt => JWT.encode(manifest, @private_key, "RS256")}.to_json @@ -76,21 +76,22 @@ describe AuthorizationsController do end it 'assigns the auth. & apps for the current user' do - app1 = OAuth2::Provider.client_class.create(:name => "Authorized App") - app2 = OAuth2::Provider.client_class.create(:name => "Unauthorized App") - auth1 = OAuth2::Provider.authorization_class.create(:client => app1, :resource_owner => alice) - auth2 = OAuth2::Provider.authorization_class.create(:client => app1, :resource_owner => bob) - auth3 = OAuth2::Provider.authorization_class.create(:client => app2, :resource_owner => bob) + app1 = Factory.create(:app, :name => "Authorized App") + app2 = Factory.create(:app, :name => "Unauthorized App") + auth = OAuth2::Provider.authorization_class.create(:client => app1, :resource_owner => alice) + + OAuth2::Provider.authorization_class.create(:client => app1, :resource_owner => bob) + OAuth2::Provider.authorization_class.create(:client => app2, :resource_owner => bob) get :index - assigns[:authorizations].should == [auth1] + assigns[:authorizations].should == [auth] assigns[:applications].should == [app1] end end describe "#destroy" do before do - @app1 = OAuth2::Provider.client_class.create(:name => "Authorized App") + @app1 = Factory.create(:app) @auth1 = OAuth2::Provider.authorization_class.create(:client => @app1, :resource_owner => alice) @auth2 = OAuth2::Provider.authorization_class.create(:client => @app1, :resource_owner => bob) end @@ -151,8 +152,6 @@ describe AuthorizationsController do end describe "valid_time?" do - - it "returns true if time is within the last 5 minutes" do @controller.valid_time?(@time - 4.minutes - 59.seconds).should be_true end @@ -164,7 +163,8 @@ describe AuthorizationsController do describe 'valid_nonce' do before do - @app1 = OAuth2::Provider.client_class.create(:name => "Authorized App", :nonce => "abc123") + @nonce = "abc123" + Factory.create(:app, :nonce => @nonce) end it 'returns true if its a new nonce' do @@ -172,7 +172,7 @@ describe AuthorizationsController do end it 'returns false if the nonce was already used' do - @controller.valid_nonce?("abc123").should be_false + @controller.valid_nonce?(@nonce).should be_false end end end diff --git a/spec/factories.rb b/spec/factories.rb index 9dc44722c2..58d634c75e 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -111,3 +111,11 @@ Factory.define(:activity_streams_photo, :class => ActivityStreams::Photo) do |p| p.actor_url "http://notcubbi.es/cubber" p.provider_display_name "not cubbies" end + +Factory.define(:app, :class => OAuth2::Provider.client_class) do |a| + a.sequence(:name) { |token| "Chubbies#{token}" } + a.sequence(:homepage_url) { |token| "http://chubbi#{token}.es/" } + + a.description "The best way to chub on the net." + a.icon_url "/images/chubbies48.png" +end diff --git a/spec/models/oauth2_provider_models_activerecord_authorization_spec.rb b/spec/models/oauth2_provider_models_activerecord_authorization_spec.rb index 7c69f83b91..2eb2c4ca39 100644 --- a/spec/models/oauth2_provider_models_activerecord_authorization_spec.rb +++ b/spec/models/oauth2_provider_models_activerecord_authorization_spec.rb @@ -1,22 +1,22 @@ # Copyright (c) 2010, Diaspora Inc. This file is # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. -# + require 'spec_helper' describe OAuth2::Provider::Models::ActiveRecord::Authorization do describe 'validations'do before do - @client = OAuth2::Provider::Models::ActiveRecord::Client.create!(:name => "APP!!!") + @client = Factory.create(:app) end it 'validates uniqueness on resource owner and client' do OAuth2::Provider::Models::ActiveRecord::Authorization.create!(:client => @client, :resource_owner => alice) - OAuth2::Provider::Models::ActiveRecord::Authorization.new(:client => @client, :resource_owner => alice).valid?.should be_false + OAuth2::Provider::Models::ActiveRecord::Authorization.new(:client => @client, :resource_owner => alice).should_not be_valid end it 'requires a resource owner for an authorization' do - OAuth2::Provider::Models::ActiveRecord::Authorization.new(:client => @client).valid?.should be_false + OAuth2::Provider::Models::ActiveRecord::Authorization.new(:client => @client).should_not be_valid end end end -- GitLab