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

@ -17,7 +17,7 @@ gem 'devise_invitable', '0.5.0'
#Authentication #Authentication
gem 'omniauth', '0.2.6' gem 'omniauth', '0.2.6'
gem 'twitter', '1.5.0' gem 'twitter', '1.5.0'
gem 'oauth2-provider', '~> 0.0.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 '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,140 +1,75 @@
require 'rubygems' module Chubbies
require 'bundler/setup' require 'active_record'
require 'sinatra' require 'diaspora-client'
require 'haml' `rm -f #{File.expand_path('../chubbies.sqlite3', __FILE__)}`
require 'httparty' ActiveRecord::Base.establish_connection(
require 'json' :adapter => "sqlite3",
require 'active_record' :database => "chubbies.sqlite3"
require 'pp' )
# models ====================================== ActiveRecord::Schema.define do
`rm -f #{File.expand_path('../chubbies.sqlite3', __FILE__)}` create_table :resource_servers do |t|
ActiveRecord::Base.establish_connection( t.string :client_id, :limit => 40, :null => false
:adapter => "sqlite3", t.string :client_secret, :limit => 40, :null => false
:database => "chubbies.sqlite3" t.string :host, :limit => 127, :null => false
) t.timestamps
end
add_index :resource_servers, :host, :unique => true
ActiveRecord::Schema.define do create_table :access_tokens do |t|
create_table :users do |table| t.integer :user_id, :null => false
table.string :diaspora_handle t.integer :resource_server_id, :null => false
table.string :access_token t.string :access_token, :limit => 40, :null => false
table.integer :pod_id t.string :refresh_token, :limit => 40, :null => false
end t.string :uid, :limit => 40, :null => false
t.datetime :expires_at
create_table :pods do |table| t.timestamps
table.string :host end
table.string :client_id add_index :access_tokens, :user_id, :unique => true
table.string :client_secret create_table :users do |t|
end t.timestamps
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}"
end
def token_url
"http://" + host + "/oauth/token"
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
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
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
end end
if params['id'] && user = User.where(:id => params['id']).first class User < ActiveRecord::Base
@resource_response = get_with_access_token(user, "/api/v0/me") has_one :access_token, :class_name => "DiasporaClient::AccessToken", :dependent => :destroy
haml :response end
else
redirect pod.authorize_url(redirect_uri)
class App < DiasporaClient::App
def current_user
User.first
end
def redirect_path
'/callback'
end
def after_oauth_redirect_path
'/account?id=1'
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 '/new' do
@user = User.create
haml :home
end
get '/manifest.json' do
{
"name" => "Chubbies",
"description" => "The best way to chub.",
"homepage_url" => "http://localhost:9292/",
"icon_url" => "#"
}.to_json
end
end 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

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