Added twitter and facebook auth to use app_id/app_secret from database.

This commit is contained in:
Martin Edenhofer 2012-04-18 09:40:37 +02:00
parent 621f8ce0f0
commit 98baefd508
7 changed files with 231 additions and 97 deletions

View file

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

View file

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

View file

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

View file

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

20
lib/facebook_database.rb Normal file
View file

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

20
lib/twitter_database.rb Normal file
View file

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