diff --git a/Gemfile b/Gemfile index 2e107f87c3f2b6f4b06d63c8c501c6957782212a..44dc2e6dba33f323fae753ae7902f7d56d4affc0 100644 --- a/Gemfile +++ b/Gemfile @@ -17,7 +17,7 @@ gem 'devise_invitable', '0.5.0' #Authentication gem 'omniauth', '0.2.6' -gem 'twitter', '1.5.0' +gem 'twitter', '1.5.0' gem 'oauth2-provider', '~> 0.0.0' @@ -93,4 +93,6 @@ group :test do gem 'mongrel', :require => false if RUBY_VERSION.include? '1.8' gem 'rspec-instafail', '>= 0.1.7', :require => false gem 'fuubar' + + gem 'diaspora-client', :path => '~/workspace/diaspora-client' end diff --git a/app/controllers/authorizations_controller.rb b/app/controllers/authorizations_controller.rb index c6537953a193feb5eb751ab433c6a0f8557e9fd7..6f0b0eef4a87c7861c3be38183f4c264df47fb9c 100644 --- a/app/controllers/authorizations_controller.rb +++ b/app/controllers/authorizations_controller.rb @@ -12,7 +12,7 @@ class AuthorizationsController < ApplicationController end def create - if params[:commit] == "Yes" + if params[:commit] == "Authorize" grant_authorization_code(current_user) else deny_authorization_code diff --git a/app/views/authorizations/new.html.haml b/app/views/authorizations/new.html.haml index 9023c487ba66bc1cf4f3bfa9d19501a27571f03b..be6328d3fb140cd2a1688146e7ec3c68f3a1ef76 100644 --- a/app/views/authorizations/new.html.haml +++ b/app/views/authorizations/new.html.haml @@ -15,7 +15,7 @@ %br Cubbies will be able to see your name, profile photo, and other basic profile information. - - elsif scope = "AS_photo:post" + - elsif scope == "AS_photo:post" %li .scope-photo = image_tag('/images/icons/photo.svg') diff --git a/spec/chubbies/Gemfile b/spec/chubbies/Gemfile index dded43bc26b619e6dcd0be596ea41afda418a4ca..4fe4235ac980b3e78fdfc54dff5669eda3efe5b0 100644 --- a/spec/chubbies/Gemfile +++ b/spec/chubbies/Gemfile @@ -3,8 +3,8 @@ source :rubygems gem 'sinatra' gem 'haml' -gem 'httparty' gem 'json' gem 'shotgun' gem 'sqlite3' gem 'activerecord', '3.0.3' +gem 'diaspora-client', :path => '~/workspace/diaspora-client' diff --git a/spec/chubbies/Gemfile.lock b/spec/chubbies/Gemfile.lock index 43ab98117d1a1d857a811b088a3e22d4931cd33e..7012943171118cd44878064da5ce764503d9e622 100644 --- a/spec/chubbies/Gemfile.lock +++ b/spec/chubbies/Gemfile.lock @@ -1,3 +1,12 @@ +PATH + remote: ~/workspace/diaspora-client + specs: + diaspora-client (0.0.0) + activerecord + faraday + oauth2 + sinatra + GEM remote: http://rubygems.org/ specs: @@ -11,14 +20,21 @@ GEM arel (~> 2.0.2) tzinfo (~> 0.3.23) activesupport (3.0.3) + addressable (2.2.6) arel (2.0.10) builder (2.1.2) - crack (0.1.8) + faraday (0.6.1) + addressable (~> 2.2.4) + multipart-post (~> 1.1.0) + rack (>= 1.1.0, < 2) haml (3.0.18) - httparty (0.7.4) - crack (= 0.1.8) i18n (0.6.0) json (1.4.6) + multi_json (1.0.3) + multipart-post (1.1.2) + oauth2 (0.4.1) + faraday (~> 0.6.1) + multi_json (>= 0.0.5) rack (1.2.2) shotgun (0.9) rack (>= 1.0) @@ -34,8 +50,8 @@ PLATFORMS DEPENDENCIES activerecord (= 3.0.3) + diaspora-client! haml - httparty json shotgun sinatra diff --git a/spec/chubbies/app.rb b/spec/chubbies/app.rb index cf1bb1cf9bbf7d1606e19b07cd87f45da1a78a3a..49d8e32313540421992b2059574bb614d2f38035 100644 --- a/spec/chubbies/app.rb +++ b/spec/chubbies/app.rb @@ -1,140 +1,75 @@ -require 'rubygems' -require 'bundler/setup' -require 'sinatra' -require 'haml' -require 'httparty' -require 'json' -require 'active_record' -require 'pp' - -# models ====================================== -`rm -f #{File.expand_path('../chubbies.sqlite3', __FILE__)}` -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 - - create_table :pods do |table| - table.string :host - table.string :client_id - table.string :client_secret - end -end - -class User < ActiveRecord::Base - attr_accessible :diaspora_handle, :access_token - belongs_to :pod -end - -class Pod < ActiveRecord::Base - attr_accessible :host, :client_id, :client_secret - has_many :users - - def authorize_url(redirect_uri) - "http://" + host + "/oauth/authorize?client_id=#{client_id}&client_secret=#{client_secret}&redirect_uri=#{redirect_uri}" +module Chubbies + require 'active_record' + require 'diaspora-client' + `rm -f #{File.expand_path('../chubbies.sqlite3', __FILE__)}` + ActiveRecord::Base.establish_connection( + :adapter => "sqlite3", + :database => "chubbies.sqlite3" + ) + + ActiveRecord::Schema.define do + create_table :resource_servers do |t| + t.string :client_id, :limit => 40, :null => false + t.string :client_secret, :limit => 40, :null => false + t.string :host, :limit => 127, :null => false + t.timestamps + end + add_index :resource_servers, :host, :unique => true + + create_table :access_tokens do |t| + t.integer :user_id, :null => false + t.integer :resource_server_id, :null => false + t.string :access_token, :limit => 40, :null => false + t.string :refresh_token, :limit => 40, :null => false + t.string :uid, :limit => 40, :null => false + t.datetime :expires_at + t.timestamps + end + add_index :access_tokens, :user_id, :unique => true + create_table :users do |t| + t.timestamps + end end - def token_url - "http://" + host + "/oauth/token" + class User < ActiveRecord::Base + has_one :access_token, :class_name => "DiasporaClient::AccessToken", :dependent => :destroy end - def access_token_url - "http://" + host + "/oauth/access_token" - end -end -helpers do - def redirect_uri - "http://" + request.host_with_port + "/callback" << "?diaspora_handle=#{params['diaspora_handle']}" - end - - def get_with_access_token(user, path) - HTTParty.get('http://' + user.pod.host + path, :query => {:oauth_token => user.access_token}) - end -end + class App < DiasporaClient::App + def current_user + User.first + end -get '/' do - @pods = Pod.scoped.includes(:users).all - haml :home -end + def redirect_path + '/callback' + end -get '/callback' do - unless params["error"] - pod = Pod.where(:host => domain_from_handle).first + def after_oauth_redirect_path + '/account?id=1' + end - 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"] ) - redirect "/account?id=#{user.id}" - else - "What is your major malfunction?" - end -end + get '/account' do + if params['id'] && user = User.where(:id => params['id']).first + @resource_response = user.access_token.token.get("/api/v0/me") + haml :response + else + "No user with id #{params['id']}" + end + end -get '/account' do - # have diaspora handle - if params[:diaspora_handle] - host = domain_from_handle - unless pod = Pod.where(:host => host).first - pod = register_with_pod + get '/new' do + @user = User.create + haml :home end - end - if params['id'] && user = User.where(:id => params['id']).first - @resource_response = get_with_access_token(user, "/api/v0/me") - haml :response - else - redirect pod.authorize_url(redirect_uri) + get '/manifest.json' do + { + "name" => "Chubbies", + "description" => "The best way to chub.", + "homepage_url" => "http://localhost:9292/", + "icon_url" => "#" + }.to_json + end end end - -get '/manifest' do - { - :name => "Chubbies", - :description => "Chubbies tests Diaspora's OAuth capabilities.", - :homepage_url => "http://" + request.host_with_port, - :icon_url => "http://" + request.host_with_port + "/chubbies.jpeg" - }.to_json -end - -get '/reset' do - User.delete_all - Pod.delete_all - "reset." -end -#============================= -#helpers -# -def domain_from_handle - m = params['diaspora_handle'].match(/\@(.+)/) - m = m[1] if m -end - -def register_with_pod - 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) - - pod.save! - pod -end - diff --git a/spec/chubbies/config.ru b/spec/chubbies/config.ru index 7ba7d1acbdb4c637e63c6401fe817a01c9184ae4..af1d14c6f9804e4e9ce1939f2bc6c2bba0e6d48c 100644 --- a/spec/chubbies/config.ru +++ b/spec/chubbies/config.ru @@ -1,3 +1,3 @@ require 'app' -run Sinatra::Application \ No newline at end of file +run Chubbies::App diff --git a/spec/chubbies/views/home.haml b/spec/chubbies/views/home.haml index 8417ed875d56b76caf232d84fdf03fee475df543..b97443537753fa2a851e1a3714d78739169ec1ed 100644 --- a/spec/chubbies/views/home.haml +++ b/spec/chubbies/views/home.haml @@ -1,42 +1,8 @@ %html %head %body - %form{:action => '/account', :id => 'login', :method => 'get'} + %form{:action => '/', :id => 'login', :method => 'get'} %label{:for => 'diaspora_handle'} 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 - - + %input{:type => 'submit', :value => "Connect to Diaspora" } diff --git a/spec/chubbies/views/response.haml b/spec/chubbies/views/response.haml index c5237687e326e740e996f931577c9d9f44a758de..dad4edc52105a68a9d34978af20920bf97999c20 100644 --- a/spec/chubbies/views/response.haml +++ b/spec/chubbies/views/response.haml @@ -9,5 +9,5 @@ %h2 Body %pre - =@resource_response.body.inspect + =@resource_response.inspect