Chubbies now uses sqlite to have tables of pods and users
This commit is contained in:
parent
e720fc097d
commit
4b13c83de7
9 changed files with 123 additions and 71 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -33,7 +33,7 @@ public/assets/*
|
|||
public/source.tar*
|
||||
tmp/**/*
|
||||
tmp/*
|
||||
db/*.sqlite3
|
||||
*.sqlite3
|
||||
|
||||
# Temporary files of every sort
|
||||
.sass-cache/*
|
||||
|
|
|
|||
4
Gemfile
4
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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -6,3 +6,5 @@ gem 'haml'
|
|||
gem 'httparty'
|
||||
gem 'json'
|
||||
gem 'shotgun'
|
||||
gem 'sqlite3'
|
||||
gem 'activerecord'
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -4,20 +4,50 @@ require 'sinatra'
|
|||
require 'haml'
|
||||
require 'httparty'
|
||||
require 'json'
|
||||
require 'active_record'
|
||||
|
||||
def resource_host
|
||||
url = "http://localhost:"
|
||||
if ENV["DIASPORA_PORT"]
|
||||
url << ENV["DIASPORA_PORT"]
|
||||
else
|
||||
url << "3000"
|
||||
# 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
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
enable :sessions
|
||||
|
||||
|
|
@ -26,66 +56,47 @@ helpers do
|
|||
"http://" + request.host_with_port + "/callback" << "?diaspora_handle=#{params['diaspora_handle']}"
|
||||
end
|
||||
|
||||
def access_token
|
||||
session[:access_token]
|
||||
end
|
||||
|
||||
def get_with_access_token(path)
|
||||
HTTParty.get('http://' + domain_from_handle + path, :query => {:oauth_token => access_token})
|
||||
end
|
||||
|
||||
def authorize_url
|
||||
"http://" + domain_from_handle + "/oauth/authorize?client_id=#{@@client_id}&client_secret=#{@@client_secret}&redirect_uri=#{redirect_uri}"
|
||||
end
|
||||
|
||||
def token_url
|
||||
"http://" + domain_from_handle + "/oauth/token"
|
||||
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"]
|
||||
pod = Pod.where(:host => domain_from_handle).first
|
||||
|
||||
if(params["client_id"] && params["client_secret"])
|
||||
@@client_id = params["client_id"]
|
||||
@@client_secret = params["client_secret"]
|
||||
redirect '/account'
|
||||
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'}
|
||||
)
|
||||
|
||||
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
|
||||
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 @@ get '/manifest' do
|
|||
}.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
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue