collapsed oauth2 migrations; made app factory
This commit is contained in:
parent
052adc3213
commit
74a7c3ab00
9 changed files with 53 additions and 54 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
15
db/schema.rb
15
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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue