Chubbies now uses diaspora-client

This commit is contained in:
Raphael Sofaer 2011-06-09 12:40:30 -07:00
parent d9d66a5c31
commit 775dbda2b2
9 changed files with 98 additions and 179 deletions

View file

@ -93,4 +93,6 @@ group :test do
gem 'mongrel', :require => false if RUBY_VERSION.include? '1.8' gem 'mongrel', :require => false if RUBY_VERSION.include? '1.8'
gem 'rspec-instafail', '>= 0.1.7', :require => false gem 'rspec-instafail', '>= 0.1.7', :require => false
gem 'fuubar' gem 'fuubar'
gem 'diaspora-client', :path => '~/workspace/diaspora-client'
end end

View file

@ -12,7 +12,7 @@ class AuthorizationsController < ApplicationController
end end
def create def create
if params[:commit] == "Yes" if params[:commit] == "Authorize"
grant_authorization_code(current_user) grant_authorization_code(current_user)
else else
deny_authorization_code deny_authorization_code

View file

@ -15,7 +15,7 @@
%br %br
Cubbies will be able to see your name, profile photo, and other basic profile information. 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 %li
.scope-photo .scope-photo
= image_tag('/images/icons/photo.svg') = image_tag('/images/icons/photo.svg')

View file

@ -3,8 +3,8 @@ source :rubygems
gem 'sinatra' gem 'sinatra'
gem 'haml' gem 'haml'
gem 'httparty'
gem 'json' gem 'json'
gem 'shotgun' gem 'shotgun'
gem 'sqlite3' gem 'sqlite3'
gem 'activerecord', '3.0.3' gem 'activerecord', '3.0.3'
gem 'diaspora-client', :path => '~/workspace/diaspora-client'

View file

@ -1,3 +1,12 @@
PATH
remote: ~/workspace/diaspora-client
specs:
diaspora-client (0.0.0)
activerecord
faraday
oauth2
sinatra
GEM GEM
remote: http://rubygems.org/ remote: http://rubygems.org/
specs: specs:
@ -11,14 +20,21 @@ GEM
arel (~> 2.0.2) arel (~> 2.0.2)
tzinfo (~> 0.3.23) tzinfo (~> 0.3.23)
activesupport (3.0.3) activesupport (3.0.3)
addressable (2.2.6)
arel (2.0.10) arel (2.0.10)
builder (2.1.2) 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) haml (3.0.18)
httparty (0.7.4)
crack (= 0.1.8)
i18n (0.6.0) i18n (0.6.0)
json (1.4.6) 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) rack (1.2.2)
shotgun (0.9) shotgun (0.9)
rack (>= 1.0) rack (>= 1.0)
@ -34,8 +50,8 @@ PLATFORMS
DEPENDENCIES DEPENDENCIES
activerecord (= 3.0.3) activerecord (= 3.0.3)
diaspora-client!
haml haml
httparty
json json
shotgun shotgun
sinatra sinatra

View file

@ -1,13 +1,6 @@
require 'rubygems' module Chubbies
require 'bundler/setup'
require 'sinatra'
require 'haml'
require 'httparty'
require 'json'
require 'active_record' require 'active_record'
require 'pp' require 'diaspora-client'
# models ======================================
`rm -f #{File.expand_path('../chubbies.sqlite3', __FILE__)}` `rm -f #{File.expand_path('../chubbies.sqlite3', __FILE__)}`
ActiveRecord::Base.establish_connection( ActiveRecord::Base.establish_connection(
:adapter => "sqlite3", :adapter => "sqlite3",
@ -15,126 +8,68 @@ ActiveRecord::Base.establish_connection(
) )
ActiveRecord::Schema.define do ActiveRecord::Schema.define do
create_table :users do |table| create_table :resource_servers do |t|
table.string :diaspora_handle t.string :client_id, :limit => 40, :null => false
table.string :access_token t.string :client_secret, :limit => 40, :null => false
table.integer :pod_id t.string :host, :limit => 127, :null => false
t.timestamps
end end
add_index :resource_servers, :host, :unique => true
create_table :pods do |table| create_table :access_tokens do |t|
table.string :host t.integer :user_id, :null => false
table.string :client_id t.integer :resource_server_id, :null => false
table.string :client_secret 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
end end
class User < ActiveRecord::Base class User < ActiveRecord::Base
attr_accessible :diaspora_handle, :access_token has_one :access_token, :class_name => "DiasporaClient::AccessToken", :dependent => :destroy
belongs_to :pod
end end
class Pod < ActiveRecord::Base
attr_accessible :host, :client_id, :client_secret
has_many :users
def authorize_url(redirect_uri) class App < DiasporaClient::App
"http://" + host + "/oauth/authorize?client_id=#{client_id}&client_secret=#{client_secret}&redirect_uri=#{redirect_uri}" def current_user
User.first
end end
def token_url def redirect_path
"http://" + host + "/oauth/token" '/callback'
end end
def access_token_url def after_oauth_redirect_path
"http://" + host + "/oauth/access_token" '/account?id=1'
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
get '/' do
@pods = Pod.scoped.includes(:users).all
haml :home
end
get '/callback' do
unless params["error"]
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"] )
redirect "/account?id=#{user.id}"
else
"What is your major malfunction?"
end
end end
get '/account' do 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
end
end
if params['id'] && user = User.where(:id => params['id']).first if params['id'] && user = User.where(:id => params['id']).first
@resource_response = get_with_access_token(user, "/api/v0/me") @resource_response = user.access_token.token.get("/api/v0/me")
haml :response haml :response
else else
redirect pod.authorize_url(redirect_uri) "No user with id #{params['id']}"
end end
end end
get '/manifest' do get '/new' do
@user = User.create
haml :home
end
get '/manifest.json' do
{ {
:name => "Chubbies", "name" => "Chubbies",
:description => "Chubbies tests Diaspora's OAuth capabilities.", "description" => "The best way to chub.",
:homepage_url => "http://" + request.host_with_port, "homepage_url" => "http://localhost:9292/",
:icon_url => "http://" + request.host_with_port + "/chubbies.jpeg" "icon_url" => "#"
}.to_json }.to_json
end end
get '/reset' do
User.delete_all
Pod.delete_all
"reset."
end end
#=============================
#helpers
#
def domain_from_handle
m = params['diaspora_handle'].match(/\@(.+)/)
m = m[1] if m
end 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

View file

@ -1,3 +1,3 @@
require 'app' require 'app'
run Sinatra::Application run Chubbies::App

View file

@ -1,42 +1,8 @@
%html %html
%head %head
%body %body
%form{:action => '/account', :id => 'login', :method => 'get'} %form{:action => '/', :id => 'login', :method => 'get'}
%label{:for => 'diaspora_handle'} %label{:for => 'diaspora_handle'}
Diaspora Handle Diaspora Handle
%input{:type=>'text', :id => 'diaspora_handle', :name => 'diaspora_handle'} %input{:type=>'text', :id => 'diaspora_handle', :name => 'diaspora_handle'}
%input{:type => 'submit', :value => "Log in with Diaspora" } %input{:type => 'submit', :value => "Connect to 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

View file

@ -9,5 +9,5 @@
%h2 %h2
Body Body
%pre %pre
=@resource_response.body.inspect =@resource_response.inspect