From 98baefd508e23e870a927a31ecc8efa8654b524f Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 18 Apr 2012 09:40:37 +0200 Subject: [PATCH] Added twitter and facebook auth to use app_id/app_secret from database. --- .../{ui => }/jquery-ui-1.8.18.custom.min.js | 0 app/models/authorization.rb | 33 ++- config/environment.rb | 4 + config/initializers/omniauth.rb | 13 + db/seeds.rb | 238 ++++++++++++------ lib/facebook_database.rb | 20 ++ lib/twitter_database.rb | 20 ++ 7 files changed, 231 insertions(+), 97 deletions(-) rename app/assets/javascripts/app/lib/{ui => }/jquery-ui-1.8.18.custom.min.js (100%) create mode 100644 config/initializers/omniauth.rb create mode 100644 lib/facebook_database.rb create mode 100644 lib/twitter_database.rb diff --git a/app/assets/javascripts/app/lib/ui/jquery-ui-1.8.18.custom.min.js b/app/assets/javascripts/app/lib/jquery-ui-1.8.18.custom.min.js similarity index 100% rename from app/assets/javascripts/app/lib/ui/jquery-ui-1.8.18.custom.min.js rename to app/assets/javascripts/app/lib/jquery-ui-1.8.18.custom.min.js diff --git a/app/models/authorization.rb b/app/models/authorization.rb index 739eae3a9..309b382a2 100644 --- a/app/models/authorization.rb +++ b/app/models/authorization.rb @@ -8,42 +8,41 @@ class Authorization < ApplicationModel after_destroy :cache_delete def self.find_from_hash(hash) - auth = Authorization.where( :provider => hash['provider'], :uid => hash['uid'] ) - if auth && auth.first then -# raise auth.first.to_yaml -# raise hash.to_yaml + auth = Authorization.where( :provider => hash['provider'], :uid => hash['uid'] ).first + if auth # update auth tokens - auth.first.update_attributes( + auth.update_attributes( :token => hash['credentials']['token'], :secret => hash['credentials']['secret'] ) - + # update image if needed if hash['info']['image'] - user = User.where( :id => auth.first.user_id ).first + user = User.find( auth.user_id ) user.update_attributes( :image => hash['info']['image'] ) - - # reset cache - user.cache_delete end end - - return auth.first + return auth end def self.create_from_hash(hash, user = nil) if user then user.update_attributes( - :username => hash['username'], - :image => hash['info']['image'] +# :username => hash['username'], + :image => hash['info']['image'] ) + + # fillup empty attributes + # TODO + else user = User.create_from_hash!(hash) end - Authorization.create( + + auth = Authorization.create( :user => user, :uid => hash['uid'], :username => hash['username'], @@ -51,8 +50,6 @@ class Authorization < ApplicationModel :token => hash['credentials']['token'], :secret => hash['credentials']['secret'] ) - - # reset cache - user.cache_delete + return auth end end \ No newline at end of file diff --git a/config/environment.rb b/config/environment.rb index 7cfe94f39..03f4adb6b 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -4,5 +4,9 @@ require File.expand_path('../application', __FILE__) # load module used to get current user for active recorde observer require 'user_info' +# load omniauth strategies with database lookups api keys at runtime +require 'twitter_database' +require 'facebook_database' + # Initialize the rails application Zammad::Application.initialize! diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb new file mode 100644 index 000000000..90819a036 --- /dev/null +++ b/config/initializers/omniauth.rb @@ -0,0 +1,13 @@ +Rails.application.config.middleware.use OmniAuth::Builder do + + # twitter database connect + provider :twitter_database, 'xx', 'xx', + :client_options => { :authorize_path => '/oauth/authorize', :site => 'https://api.twitter.com' } + + # facebook database connect + provider :facebook_database, 'xx', 'xx' + + # linkedin database connect +# provider :linked_in_database, 'xx', 'xx' + +end diff --git a/db/seeds.rb b/db/seeds.rb index 2e3f72d26..262829308 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -13,17 +13,17 @@ Setting.create( :options => { :form => [ { - :display => '', - :null => false, - :name => 'product_name', - :tag => 'input', + :display => '', + :null => false, + :name => 'product_name', + :tag => 'input', }, ], }, - :state => { + :state => { :value => 'Zammad', }, - :frontend => true + :frontend => true ) Setting.create( @@ -34,17 +34,17 @@ Setting.create( :options => { :form => [ { - :display => '', - :null => false, - :name => 'organization', - :tag => 'input', + :display => '', + :null => false, + :name => 'organization', + :tag => 'input', }, ], }, - :state => { + :state => { :value => 'Example Inc.', }, - :frontend => true + :frontend => true ) Setting.create( @@ -55,11 +55,11 @@ Setting.create( :options => { :form => [ { - :display => '', - :null => true, - :name => 'system_id', - :tag => 'select', - :options => { + :display => '', + :null => true, + :name => 'system_id', + :tag => 'select', + :options => { '10' => '10', '11' => '11', '12' => '12', @@ -68,10 +68,10 @@ Setting.create( }, ], }, - :state => { + :state => { :value => '10', }, - :frontend => true + :frontend => true ) Setting.create( :title => 'Fully Qualified Domain Name', @@ -81,17 +81,17 @@ Setting.create( :options => { :form => [ { - :display => '', - :null => false, - :name => 'fqdn', - :tag => 'input', + :display => '', + :null => false, + :name => 'fqdn', + :tag => 'input', }, ], }, - :state => { + :state => { :value => 'zammad.example.com', }, - :frontend => true + :frontend => true ) Setting.create( :title => 'http type', @@ -101,11 +101,11 @@ Setting.create( :options => { :form => [ { - :display => '', - :null => true, - :name => 'storage', - :tag => 'select', - :options => { + :display => '', + :null => true, + :name => 'storage', + :tag => 'select', + :options => { 'https' => 'https', 'http' => 'http', }, @@ -128,11 +128,11 @@ Setting.create( :options => { :form => [ { - :display => '', - :null => true, - :name => 'storage', - :tag => 'select', - :options => { + :display => '', + :null => true, + :name => 'storage', + :tag => 'select', + :options => { 'DB' => 'Database', 'FS' => 'Filesystem', }, @@ -154,11 +154,11 @@ Setting.create( :options => { :form => [ { - :display => '', - :null => true, - :name => 'user_create_account', - :tag => 'select', - :options => { + :display => '', + :null => true, + :name => 'user_create_account', + :tag => 'select', + :options => { 1 => 'yes', 0 => 'no', }, @@ -178,11 +178,11 @@ Setting.create( :options => { :form => [ { - :display => '', - :null => true, - :name => 'user_lost_password', - :tag => 'select', - :options => { + :display => '', + :null => true, + :name => 'user_lost_password', + :tag => 'select', + :options => { 1 => 'yes', 0 => 'no', }, @@ -202,11 +202,11 @@ Setting.create( :options => { :form => [ { - :display => '', - :null => true, - :name => 'switch_to_user', - :tag => 'select', - :options => { + :display => '', + :null => true, + :name => 'switch_to_user', + :tag => 'select', + :options => { 1 => 'yes', 0 => 'no', }, @@ -226,11 +226,11 @@ Setting.create( :options => { :form => [ { - :display => '', - :null => true, - :name => 'auth_db', - :tag => 'select', - :options => { + :display => '', + :null => true, + :name => 'auth_db', + :tag => 'select', + :options => { 1 => 'yes', 0 => 'no', }, @@ -246,49 +246,102 @@ Setting.create( :title => 'Autentication via Twitter', :name => 'auth_twitter', :area => 'Security::Authentication', - :description => 'Enables user authentication via twitter.', + :description => 'Enables user authentication via twitter. Register your app first at https://dev.twitter.com/apps', :options => { :form => [ { - :display => '', - :null => true, - :name => 'auth_twitter', - :tag => 'select', - :options => { - 1 => 'yes', - 0 => 'no', + :display => '', + :null => true, + :name => 'auth_twitter', + :tag => 'select', + :options => { + true => 'yes', + false => 'no', }, }, ], }, - :state => { - :value => 1, + :state => { + :value => false, }, - :frontend => true + :frontend => true +) +Setting.create( + :title => 'Twitter App Credentials', + :name => 'auth_twitter_credentials', + :area => 'Security::Authentication', + :description => 'App credentials for Twitter.', + :options => { + :form => [ + { + :display => 'Twitter Key', + :null => true, + :name => 'key', + :tag => 'input', + }, + { + :display => 'Twitter Secret', + :null => true, + :name => 'secret', + :tag => 'input', + }, + ], + }, + :state => { + :value => {} + }, + :frontend => false ) Setting.create( :title => 'Autentication via Facebook', :name => 'auth_facebook', :area => 'Security::Authentication', - :description => 'Enables user authentication via Facebook.', + :description => 'Enables user authentication via Facebook. Register your app first at https://developers.facebook.com/apps/', :options => { :form => [ { - :display => '', - :null => true, - :name => 'auth_facebook', - :tag => 'select', - :options => { - 1 => 'yes', - 0 => 'no', + :display => '', + :null => true, + :name => 'auth_facebook', + :tag => 'select', + :options => { + true => 'yes', + false => 'no', }, }, ], }, :state => { - :value => 1, + :value => false, }, - :frontend => true + :frontend => true +) + +Setting.create( + :title => 'Facebook App Credentials', + :name => 'auth_facebook_credentials', + :area => 'Security::Authentication', + :description => 'App credentials for Facebook.', + :options => { + :form => [ + { + :display => 'App ID', + :null => true, + :name => 'app_id', + :tag => 'input', + }, + { + :display => 'App Secret', + :null => true, + :name => 'app_secret', + :tag => 'input', + }, + ], + }, + :state => { + :value => {}, + }, + :frontend => false ) Setting.create( :title => 'Autentication via LinkedIn', @@ -303,17 +356,44 @@ Setting.create( :name => 'auth_linkedin', :tag => 'select', :options => { - 1 => 'yes', - 0 => 'no', + true => 'yes', + false => 'no', }, }, ], }, :state => { - :value => 1, + :value => false, }, :frontend => true ) +Setting.create( + :title => 'LinkedIn App Credentials', + :name => 'auth_linkedin_credentials', + :area => 'Security::Authentication', + :description => 'Enables user authentication via LinkedIn.', + :options => { + :form => [ + { + :display => 'App ID', + :null => true, + :name => 'app_id', + :tag => 'input', + }, + { + :display => 'App Secret', + :null => true, + :name => 'app_secret', + :tag => 'input', + }, + ], + }, + :state => { + :value => {}, + }, + :frontend => false +) + Setting.create( :title => 'Minimal size', :name => 'password_min_size', diff --git a/lib/facebook_database.rb b/lib/facebook_database.rb new file mode 100644 index 000000000..4ac8c1d9c --- /dev/null +++ b/lib/facebook_database.rb @@ -0,0 +1,20 @@ +module OmniAuth + module Strategies + + class FacebookDatabase < OmniAuth::Strategies::Facebook + option :name, 'facebook' + + def initialize(app, *args, &block) + + # database lookup + puts 'FacebookDatabase -> initialize' + config = Setting.get('auth_facebook_credentials') || {} + *args[0] = config['app_id'] + *args[1] = config['app_secret'] + super + end + + end + + end +end diff --git a/lib/twitter_database.rb b/lib/twitter_database.rb new file mode 100644 index 000000000..dabebbd66 --- /dev/null +++ b/lib/twitter_database.rb @@ -0,0 +1,20 @@ +module OmniAuth + module Strategies + + class TwitterDatabase < OmniAuth::Strategies::Twitter + option :name, 'twitter' + + def initialize(app, *args, &block) + + # database lookup + puts 'TwitterDatabase -> initialize' + config = Setting.get('auth_twitter_credentials') || {} + *args[0] = config['key'] + *args[1] = config['secret'] + super + end + + end + + end +end