diff --git a/.gitignore b/.gitignore index 9947f3f6576..f76a27a5937 100644 --- a/.gitignore +++ b/.gitignore @@ -33,7 +33,7 @@ public/assets/* public/source.tar* tmp/**/* tmp/* -db/*.sqlite3 +*.sqlite3 # Temporary files of every sort .sass-cache/* diff --git a/Gemfile b/Gemfile index bcafd174336..2e107f87c3f 100644 --- a/Gemfile +++ b/Gemfile @@ -93,8 +93,4 @@ group :test do gem 'mongrel', :require => false if RUBY_VERSION.include? '1.8' gem 'rspec-instafail', '>= 0.1.7', :require => false gem 'fuubar' - - #For Chubbies - gem 'sinatra', :require => false - gem 'httparty', :require => false end diff --git a/Gemfile.lock b/Gemfile.lock index e3d15cfb897..bd597abff1c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -183,8 +183,6 @@ GEM hashie (1.0.0) highline (1.6.2) http_connection (1.4.1) - httparty (0.7.4) - crack (= 0.1.8) i18n (0.6.0) i18n-inflector (2.5.1) i18n (>= 0.4.1) @@ -426,7 +424,6 @@ DEPENDENCIES fuubar haml (= 3.0.25) http_accept_language! - httparty i18n-inflector-rails (~> 1.0) jammit (= 0.5.4) jasmine (= 1.0.2.1) @@ -452,7 +449,6 @@ DEPENDENCIES ruby-debug selenium-webdriver (= 0.1.3) settingslogic (= 2.0.6) - sinatra sod! thin (= 1.2.11) twitter (= 1.5.0) diff --git a/app/views/authorizations/new.html.haml b/app/views/authorizations/new.html.haml index 4c47a7baf26..4a31aed9ba6 100644 --- a/app/views/authorizations/new.html.haml +++ b/app/views/authorizations/new.html.haml @@ -17,6 +17,9 @@ .description = @client.description + %p + = "You are currently logged in as #{current_user.name}(#{current_user.diaspora_handle})." + = link_to("Not You?", destroy_user_session_path) %br %p diff --git a/features/step_definitions/oauth_steps.rb b/features/step_definitions/oauth_steps.rb index cef6dfb6957..9092b1adf89 100644 --- a/features/step_definitions/oauth_steps.rb +++ b/features/step_definitions/oauth_steps.rb @@ -47,7 +47,7 @@ class Chubbies def self.run @pid = fork do - Process.exec "cd #{Rails.root}/spec/support/chubbies/ && DIASPORA_PORT=9887 bundle exec rackup -p #{PORT} 2> /dev/null" + Process.exec "cd #{Rails.root}/spec/support/chubbies/ && BUNDLE_GEMFILE=Gemfile DIASPORA_PORT=9887 bundle exec rackup -p #{PORT} 2> /dev/null" end while(!running?) do sleep(1) diff --git a/spec/support/chubbies/Gemfile b/spec/support/chubbies/Gemfile index cae954b3301..b0a9d933cd2 100644 --- a/spec/support/chubbies/Gemfile +++ b/spec/support/chubbies/Gemfile @@ -6,3 +6,5 @@ gem 'haml' gem 'httparty' gem 'json' gem 'shotgun' +gem 'sqlite3' +gem 'activerecord' diff --git a/spec/support/chubbies/Gemfile.lock b/spec/support/chubbies/Gemfile.lock index 636e24e358c..cbe76139acd 100644 --- a/spec/support/chubbies/Gemfile.lock +++ b/spec/support/chubbies/Gemfile.lock @@ -1,10 +1,23 @@ GEM remote: http://rubygems.org/ specs: + activemodel (3.0.7) + activesupport (= 3.0.7) + builder (~> 2.1.2) + i18n (~> 0.5.0) + activerecord (3.0.7) + activemodel (= 3.0.7) + activesupport (= 3.0.7) + arel (~> 2.0.2) + tzinfo (~> 0.3.23) + activesupport (3.0.7) + arel (2.0.10) + builder (2.1.2) crack (0.1.8) haml (3.0.18) httparty (0.7.4) crack (= 0.1.8) + i18n (0.5.0) json (1.4.6) rack (1.2.2) shotgun (0.9) @@ -12,14 +25,18 @@ GEM sinatra (1.2.6) rack (~> 1.1) tilt (>= 1.2.2, < 2.0) + sqlite3 (1.3.3) tilt (1.3) + tzinfo (0.3.27) PLATFORMS ruby DEPENDENCIES + activerecord haml httparty json shotgun sinatra + sqlite3 diff --git a/spec/support/chubbies/app.rb b/spec/support/chubbies/app.rb index b30733db92e..0f86e231f1c 100644 --- a/spec/support/chubbies/app.rb +++ b/spec/support/chubbies/app.rb @@ -4,88 +4,99 @@ require 'haml' require 'httparty' require 'json' +require 'active_record' + +# models ====================================== +`rm -f chubbies.sqlite3` +ActiveRecord::Base.establish_connection( + :adapter => "sqlite3", + :database => "chubbies.sqlite3" +) + +ActiveRecord::Schema.define do + create_table :users do |table| + table.string :diaspora_handle + table.string :access_token + table.integer :pod_id + end -def resource_host - url = "http://localhost:" - if ENV["DIASPORA_PORT"] - url << ENV["DIASPORA_PORT"] - else - url << "3000" + create_table :pods do |table| + table.string :host + table.string :client_id + table.string :client_secret end - url end -@@client_id = nil -@@client_secret = nil -RESOURCE_HOST = resource_host +class User < ActiveRecord::Base + attr_accessible :diaspora_handle, :access_token + belongs_to :pod +end -enable :sessions +class Pod < ActiveRecord::Base + attr_accessible :host, :client_id, :client_secret + has_many :users -helpers do - def redirect_uri - "http://" + request.host_with_port + "/callback" << "?diaspora_handle=#{params['diaspora_handle']}" + def authorize_url(redirect_uri) + "http://" + host + "/oauth/authorize?client_id=#{client_id}&client_secret=#{client_secret}&redirect_uri=#{redirect_uri}" end - def access_token - session[:access_token] + def token_url + "http://" + host + "/oauth/token" end - def get_with_access_token(path) - HTTParty.get('http://' + domain_from_handle + path, :query => {:oauth_token => access_token}) + def access_token_url + "http://" + host + "/oauth/access_token" end +end - def authorize_url - "http://" + domain_from_handle + "/oauth/authorize?client_id=#{@@client_id}&client_secret=#{@@client_secret}&redirect_uri=#{redirect_uri}" - end +enable :sessions - def token_url - "http://" + domain_from_handle + "/oauth/token" +helpers do + def redirect_uri + "http://" + request.host_with_port + "/callback" << "?diaspora_handle=#{params['diaspora_handle']}" end - def access_token_url - "http://" + domain_from_handle + "/oauth/access_token" + def get_with_access_token(user, path) + HTTParty.get('http://' + user.pod.host + path, :query => {:oauth_token => user.access_token}) end end get '/' do + @pods = Pod.scoped.includes(:users).all haml :home end get '/callback' do unless params["error"] - - if(params["client_id"] && params["client_secret"]) - @@client_id = params["client_id"] - @@client_secret = params["client_secret"] - redirect '/account' - - else - response = HTTParty.post(access_token_url, :body => { - :client_id => @@client_id, - :client_secret => @@client_secret, - :redirect_uri => redirect_uri, - :code => params["code"], - :grant_type => 'authorization_code'} - ) - - session[:access_token] = response["access_token"] - redirect "/account?diaspora_handle=#{params['diaspora_handle']}" - end + pod = Pod.where(:host => domain_from_handle).first + + response = HTTParty.post(pod.access_token_url, :body => { + :client_id => pod.client_id, + :client_secret => pod.client_secret, + :redirect_uri => redirect_uri, + :code => params["code"], + :grant_type => 'authorization_code'} + ) + + user = pod.users.create!(:access_token => response["access_token"], :diaspora_handle => params['diaspora_handle']) + redirect "/account?diaspora_handle=#{user.diaspora_handle}" else "What is your major malfunction?" end end get '/account' do - if !@@client_id && !@@client_secret - register_with_pod + # have diaspora handle + host = domain_from_handle + unless pod = Pod.where(:host => host).first + pod = register_with_pod end - if access_token - @resource_response = get_with_access_token("/api/v0/me") + if user = pod.users.where(:diaspora_handle => params['diaspora_handle']).first + @resource_response = get_with_access_token(user, "/api/v0/me") haml :response else - redirect authorize_url + redirect pod.authorize_url(redirect_uri) end end @@ -98,12 +109,6 @@ def access_token_url }.to_json end -get '/reset' do - @@client_id = nil - @@client_secret = nil -end - - #============================= #helpers # @@ -113,15 +118,17 @@ def domain_from_handle end def register_with_pod - response = HTTParty.post(token_url, :body => { + pod = Pod.new(:host => domain_from_handle) + + response = HTTParty.post(pod.token_url, :body => { :type => :client_associate, :manifest_url => "http://" + request.host_with_port + "/manifest" }) json = JSON.parse(response.body) + pod.update_attributes(json) - @@client_id = json["client_id"] - @@client_secret = json["client_secret"] + pod.save! + pod end - diff --git a/spec/support/chubbies/views/home.haml b/spec/support/chubbies/views/home.haml index 547f814a068..8417ed875d5 100644 --- a/spec/support/chubbies/views/home.haml +++ b/spec/support/chubbies/views/home.haml @@ -6,6 +6,37 @@ Diaspora Handle %input{:type=>'text', :id => 'diaspora_handle', :name => 'diaspora_handle'} %input{:type => 'submit', :value => "Log in with Diaspora" } + %br + %br + %br + %table + %th + Host + %th + Client ID + %th + Client Secret + %th + Users + - @pods.each do |pod| + %tr + %td + = pod.host + %td + = pod.client_id + %td + = pod.client_secret + %td + - pod.users.each do |user| + %table + %th + Diaspora Handle + %th + Access Token + %tr + %td + = user.diaspora_handle + %td + = user.access_token - - +