From c4ff07b09a35ec2dd5e44c8afae135b19f86f229 Mon Sep 17 00:00:00 2001 From: maxwell Date: Sat, 23 Oct 2010 16:09:58 -0700 Subject: [PATCH] twitter oauth dance complete --- Gemfile | 4 ++ Gemfile.lock | 64 +++++++++++++++++++ .../omniauth_services_controller.rb | 30 +++++++++ app/models/omniauth_service.rb | 13 ++++ app/models/user.rb | 2 + app/views/omniauth_services/index.html.haml | 17 +++++ config/initializers/_load_services.rb | 15 +++++ config/initializers/omniauth.rb | 9 +++ config/oauth_keys.yml | 3 + config/oauth_keys.yml.example | 3 + config/routes.rb | 5 +- 11 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 app/controllers/omniauth_services_controller.rb create mode 100644 app/models/omniauth_service.rb create mode 100644 app/views/omniauth_services/index.html.haml create mode 100644 config/initializers/_load_services.rb create mode 100644 config/initializers/omniauth.rb create mode 100644 config/oauth_keys.yml create mode 100644 config/oauth_keys.yml.example diff --git a/Gemfile b/Gemfile index bd90c0a46..766e3fd69 100644 --- a/Gemfile +++ b/Gemfile @@ -9,6 +9,10 @@ gem "chef" gem 'devise', '1.1.3' gem 'devise-mongo_mapper', :git => 'git://github.com/collectiveidea/devise-mongo_mapper' gem 'devise_invitable', '~> 0.3.4' + +#Authentication +gem 'omniauth' + #Mongo gem 'mongo_mapper', :branch => 'rails3', :git => 'git://github.com/jnunemaker/mongomapper.git' gem 'bson_ext', '1.1' diff --git a/Gemfile.lock b/Gemfile.lock index 20a7f252c..7a1caf8ce 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -170,6 +170,9 @@ GEM factory_girl_rails (1.0) factory_girl (~> 1.3) rails (>= 3.0.0.beta4) + faraday (0.4.6) + addressable (>= 2.1.1) + rack (>= 1.0.1) ffi (0.6.3) rake (>= 0.8.7) gherkin (2.2.9) @@ -182,11 +185,20 @@ GEM i18n (0.4.1) json (1.4.6) json_pure (1.4.6) +<<<<<<< HEAD launchy (0.3.7) configuration (>= 0.0.5) rake (>= 0.8.1) +<<<<<<< HEAD linecache19 (0.5.11) ruby_core_source (>= 0.1.4) +======= + linecache (0.43) +======= + linecache19 (0.5.11) + ruby_core_source (>= 0.1.4) +>>>>>>> twitter oauth dance complete +>>>>>>> twitter oauth dance complete mail (2.2.7) activesupport (>= 2.3.6) mime-types @@ -207,6 +219,8 @@ GEM moneta (0.6.0) mongo (1.1) bson (>= 1.0.5) + multi_json (0.0.4) + net-ldap (0.1.1) net-scp (1.0.4) net-ssh (>= 1.99.1) net-sftp (2.0.5) @@ -215,6 +229,7 @@ GEM net-ssh-gateway (1.0.1) net-ssh (>= 1.99.1) nokogiri (1.4.3.1) +<<<<<<< HEAD ohai (0.5.8) extlib json (>= 1.4.4, <= 1.4.6) @@ -222,15 +237,54 @@ GEM mixlib-config mixlib-log systemu +======= + oa-basic (0.1.5) + multi_json (~> 0.0.2) + nokogiri (~> 1.4.2) + oa-core (= 0.1.5) + rest-client (~> 1.6.0) + oa-core (0.1.5) + rack (~> 1.1) + oa-enterprise (0.1.5) + net-ldap (~> 0.1.1) + nokogiri (~> 1.4.2) + oa-core (= 0.1.5) + pyu-ruby-sasl (~> 0.0.3.1) + rubyntlm (~> 0.1.1) + oa-oauth (0.1.5) + multi_json (~> 0.0.2) + nokogiri (~> 1.4.2) + oa-core (= 0.1.5) + oauth (~> 0.4.0) + oauth2 (~> 0.0.10) + oa-openid (0.1.5) + oa-core (= 0.1.5) + rack-openid (~> 1.1.1) + ruby-openid-apps-discovery + oauth (0.4.3) + oauth2 (0.0.13) + faraday (~> 0.4.1) + multi_json (>= 0.0.4) + omniauth (0.1.5) + oa-basic (= 0.1.5) + oa-core (= 0.1.5) + oa-enterprise (= 0.1.5) + oa-oauth (= 0.1.5) + oa-openid (= 0.1.5) +>>>>>>> twitter oauth dance complete plucky (0.3.6) mongo (~> 1.1) polyglot (0.3.1) pubsubhubbub (0.1.1) em-http-request (>= 0.1.5) eventmachine (>= 0.12.9) + pyu-ruby-sasl (0.0.3.2) rack (1.2.1) rack-mount (0.6.13) rack (>= 1.0.0) + rack-openid (1.1.2) + rack (>= 0.4) + ruby-openid (>= 2.0.3) rack-test (0.5.6) rack (>= 1.0) rails (3.0.1) @@ -269,8 +323,17 @@ GEM columnize (>= 0.3.1) linecache19 (>= 0.5.11) ruby-debug-base19 (>= 0.11.19) +<<<<<<< HEAD ruby_core_source (0.1.4) archive-tar-minitar (>= 0.5.2) +======= + ruby-openid (2.1.8) + ruby-openid-apps-discovery (1.2.0) + ruby-openid (>= 2.1.7) + ruby_core_source (0.1.4) + archive-tar-minitar (>= 0.5.2) + rubyntlm (0.1.1) +>>>>>>> twitter oauth dance complete rubyzip (0.9.4) selenium-webdriver (0.0.29) childprocess (>= 0.0.7) @@ -325,6 +388,7 @@ DEPENDENCIES mini_magick mocha mongo_mapper! + omniauth pubsubhubbub rails (>= 3.0.0) redfinger! diff --git a/app/controllers/omniauth_services_controller.rb b/app/controllers/omniauth_services_controller.rb new file mode 100644 index 000000000..cbc59be69 --- /dev/null +++ b/app/controllers/omniauth_services_controller.rb @@ -0,0 +1,30 @@ +# Copyright (c) 2010, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + + +class OmniauthServicesController < ApplicationController + before_filter :authenticate_user! + + def index + @services = current_user.services + end + + def create + auth = request.env['omniauth.auth'] + + + puts auth['extra']['access_token'].inspect + + current_user.services.create(:provider => auth['provider'], :uid => auth['uid']) + flash[:notice] = "Authentication successful." + redirect_to omniauth_services_url + end + + def destroy + @service = current_user.services.find(params[:id]) + @service.destroy + flash[:notice] = "Successfully destroyed authentication." + redirect_to omniauth_services_url + end +end diff --git a/app/models/omniauth_service.rb b/app/models/omniauth_service.rb new file mode 100644 index 000000000..1fe6ece32 --- /dev/null +++ b/app/models/omniauth_service.rb @@ -0,0 +1,13 @@ +# Copyright (c) 2010, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + +class OmniauthService + include MongoMapper::Document + + belongs_to :user + + key :provider, String + key :uid, String + +end diff --git a/app/models/user.rb b/app/models/user.rb index 343cc2d76..8d876f0e6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -61,6 +61,8 @@ class User many :raw_visible_posts, :in => :visible_post_ids, :class_name => 'Post' many :aspects, :class_name => 'Aspect', :dependent => :destroy + many :services, :class_name => "OmniauthService" + #after_create :seed_aspects before_destroy :unfriend_everyone, :remove_person diff --git a/app/views/omniauth_services/index.html.haml b/app/views/omniauth_services/index.html.haml new file mode 100644 index 000000000..6e77ade58 --- /dev/null +++ b/app/views/omniauth_services/index.html.haml @@ -0,0 +1,17 @@ +-# Copyright (c) 2010, Diaspora Inc. This file is +-# licensed under the Affero General Public License version 3 or later. See +-# the COPYRIGHT file. + +%h2 + Services + +%ul#stream + - for service in @services + %h3 + = service.provider + %b + =service.uid + = link_to "disconnect", service, :confirm => "disconnect #{service.provider}?", :method => :delete + += link_to "Connect to twitter", "/auth/twitter" + diff --git a/config/initializers/_load_services.rb b/config/initializers/_load_services.rb new file mode 100644 index 000000000..d65f2a695 --- /dev/null +++ b/config/initializers/_load_services.rb @@ -0,0 +1,15 @@ +# Copyright (c) 2010, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + +def load_config_yaml filename + YAML.load(File.read(filename)) +end + +oauth_keys_file = "#{Rails.root}/config/oauth_keys.yml" + +if File.exist? oauth_keys_file + SERVICES = load_config_yaml(oauth_keys_file) + SERVICES ||= load_config_yaml("#{oauth_keys_file}.example") +end + diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb new file mode 100644 index 000000000..876d3f3f2 --- /dev/null +++ b/config/initializers/omniauth.rb @@ -0,0 +1,9 @@ +# Copyright (c) 2010, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + +Rails.application.config.middleware.use OmniAuth::Builder do + provider :twitter, SERVICES['twitter']['consumer_key'], SERVICES['twitter']['consumer_secret'] + #provider :facebook, 'APP_ID', 'APP_SECRET' +end + diff --git a/config/oauth_keys.yml b/config/oauth_keys.yml new file mode 100644 index 000000000..1460b861f --- /dev/null +++ b/config/oauth_keys.yml @@ -0,0 +1,3 @@ +twitter: + consumer_key: no69jTnL5SCOnWgk5XWqbQ + consumer_secret: 2DZyu8DA43AIeFsRVdz8OtAHLvCtPjdRKdNO3UZGqWM diff --git a/config/oauth_keys.yml.example b/config/oauth_keys.yml.example new file mode 100644 index 000000000..3397b2832 --- /dev/null +++ b/config/oauth_keys.yml.example @@ -0,0 +1,3 @@ +twitter: + consumer_key: "" + consumer_secret: "" diff --git a/config/routes.rb b/config/routes.rb index 7364f9016..470193a74 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,6 +9,9 @@ Diaspora::Application.routes.draw do resources :requests, :except => [:edit, :update] resources :photos, :except => [:index] resources :albums + resources :omniauth_services + + match '/auth/:provider/callback' => 'omniauth_services#create' devise_for :users, :controllers => {:registrations => "registrations", :password => "devise/passwords", @@ -18,7 +21,7 @@ Diaspora::Application.routes.draw do match 'users/export', :to => 'users#export' match 'users/import', :to => 'users#import' match 'users/export_photos', :to => 'users#export_photos' - resources :users, :except => [:create, :new, :show] + resources :users, :except => [:create, :new, :show] match 'aspects/move_friend', :to => 'aspects#move_friend', :as => 'move_friend' match 'aspects/add_to_aspect',:to => 'aspects#add_to_aspect', :as => 'add_to_aspect'