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
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

View file

@ -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

View file

@ -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')

View file

@ -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'

View file

@ -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

View file

@ -1,140 +1,75 @@
require 'rubygems'
require 'bundler/setup'
require 'sinatra'
require 'haml'
require 'httparty'
require 'json'
require 'active_record'
require 'pp'
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"
)
# models ======================================
`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
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}"
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
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
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)
class User < ActiveRecord::Base
has_one :access_token, :class_name => "DiasporaClient::AccessToken", :dependent => :destroy
end
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
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'
run Sinatra::Application
run Chubbies::App

View file

@ -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" }

View file

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