Added twitter and facebook auth to use app_id/app_secret from database.
This commit is contained in:
parent
621f8ce0f0
commit
98baefd508
7 changed files with 231 additions and 97 deletions
|
@ -8,42 +8,41 @@ class Authorization < ApplicationModel
|
||||||
after_destroy :cache_delete
|
after_destroy :cache_delete
|
||||||
|
|
||||||
def self.find_from_hash(hash)
|
def self.find_from_hash(hash)
|
||||||
auth = Authorization.where( :provider => hash['provider'], :uid => hash['uid'] )
|
auth = Authorization.where( :provider => hash['provider'], :uid => hash['uid'] ).first
|
||||||
if auth && auth.first then
|
if auth
|
||||||
# raise auth.first.to_yaml
|
|
||||||
# raise hash.to_yaml
|
|
||||||
|
|
||||||
# update auth tokens
|
# update auth tokens
|
||||||
auth.first.update_attributes(
|
auth.update_attributes(
|
||||||
:token => hash['credentials']['token'],
|
:token => hash['credentials']['token'],
|
||||||
:secret => hash['credentials']['secret']
|
:secret => hash['credentials']['secret']
|
||||||
)
|
)
|
||||||
|
|
||||||
# update image if needed
|
# update image if needed
|
||||||
if hash['info']['image']
|
if hash['info']['image']
|
||||||
user = User.where( :id => auth.first.user_id ).first
|
user = User.find( auth.user_id )
|
||||||
user.update_attributes(
|
user.update_attributes(
|
||||||
:image => hash['info']['image']
|
:image => hash['info']['image']
|
||||||
)
|
)
|
||||||
|
|
||||||
# reset cache
|
|
||||||
user.cache_delete
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
return auth
|
||||||
return auth.first
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.create_from_hash(hash, user = nil)
|
def self.create_from_hash(hash, user = nil)
|
||||||
if user then
|
if user then
|
||||||
user.update_attributes(
|
user.update_attributes(
|
||||||
:username => hash['username'],
|
# :username => hash['username'],
|
||||||
:image => hash['info']['image']
|
:image => hash['info']['image']
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# fillup empty attributes
|
||||||
|
# TODO
|
||||||
|
|
||||||
else
|
else
|
||||||
user = User.create_from_hash!(hash)
|
user = User.create_from_hash!(hash)
|
||||||
end
|
end
|
||||||
Authorization.create(
|
|
||||||
|
auth = Authorization.create(
|
||||||
:user => user,
|
:user => user,
|
||||||
:uid => hash['uid'],
|
:uid => hash['uid'],
|
||||||
:username => hash['username'],
|
:username => hash['username'],
|
||||||
|
@ -51,8 +50,6 @@ class Authorization < ApplicationModel
|
||||||
:token => hash['credentials']['token'],
|
:token => hash['credentials']['token'],
|
||||||
:secret => hash['credentials']['secret']
|
:secret => hash['credentials']['secret']
|
||||||
)
|
)
|
||||||
|
return auth
|
||||||
# reset cache
|
|
||||||
user.cache_delete
|
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -4,5 +4,9 @@ require File.expand_path('../application', __FILE__)
|
||||||
# load module used to get current user for active recorde observer
|
# load module used to get current user for active recorde observer
|
||||||
require 'user_info'
|
require 'user_info'
|
||||||
|
|
||||||
|
# load omniauth strategies with database lookups api keys at runtime
|
||||||
|
require 'twitter_database'
|
||||||
|
require 'facebook_database'
|
||||||
|
|
||||||
# Initialize the rails application
|
# Initialize the rails application
|
||||||
Zammad::Application.initialize!
|
Zammad::Application.initialize!
|
||||||
|
|
13
config/initializers/omniauth.rb
Normal file
13
config/initializers/omniauth.rb
Normal 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
|
102
db/seeds.rb
102
db/seeds.rb
|
@ -246,7 +246,7 @@ Setting.create(
|
||||||
:title => 'Autentication via Twitter',
|
:title => 'Autentication via Twitter',
|
||||||
:name => 'auth_twitter',
|
:name => 'auth_twitter',
|
||||||
:area => 'Security::Authentication',
|
: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 => {
|
:options => {
|
||||||
:form => [
|
:form => [
|
||||||
{
|
{
|
||||||
|
@ -255,22 +255,48 @@ Setting.create(
|
||||||
:name => 'auth_twitter',
|
:name => 'auth_twitter',
|
||||||
:tag => 'select',
|
:tag => 'select',
|
||||||
:options => {
|
:options => {
|
||||||
1 => 'yes',
|
true => 'yes',
|
||||||
0 => 'no',
|
false => 'no',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
:state => {
|
:state => {
|
||||||
:value => 1,
|
: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(
|
Setting.create(
|
||||||
:title => 'Autentication via Facebook',
|
:title => 'Autentication via Facebook',
|
||||||
:name => 'auth_facebook',
|
:name => 'auth_facebook',
|
||||||
:area => 'Security::Authentication',
|
: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 => {
|
:options => {
|
||||||
:form => [
|
:form => [
|
||||||
{
|
{
|
||||||
|
@ -279,17 +305,44 @@ Setting.create(
|
||||||
:name => 'auth_facebook',
|
:name => 'auth_facebook',
|
||||||
:tag => 'select',
|
:tag => 'select',
|
||||||
:options => {
|
:options => {
|
||||||
1 => 'yes',
|
true => 'yes',
|
||||||
0 => 'no',
|
false => 'no',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
:state => {
|
: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(
|
Setting.create(
|
||||||
:title => 'Autentication via LinkedIn',
|
:title => 'Autentication via LinkedIn',
|
||||||
:name => 'auth_linkedin',
|
:name => 'auth_linkedin',
|
||||||
|
@ -303,17 +356,44 @@ Setting.create(
|
||||||
:name => 'auth_linkedin',
|
:name => 'auth_linkedin',
|
||||||
:tag => 'select',
|
:tag => 'select',
|
||||||
:options => {
|
:options => {
|
||||||
1 => 'yes',
|
true => 'yes',
|
||||||
0 => 'no',
|
false => 'no',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
:state => {
|
:state => {
|
||||||
:value => 1,
|
:value => false,
|
||||||
},
|
},
|
||||||
:frontend => true
|
: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(
|
Setting.create(
|
||||||
:title => 'Minimal size',
|
:title => 'Minimal size',
|
||||||
:name => 'password_min_size',
|
:name => 'password_min_size',
|
||||||
|
|
20
lib/facebook_database.rb
Normal file
20
lib/facebook_database.rb
Normal 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
20
lib/twitter_database.rb
Normal 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
|
Loading…
Reference in a new issue