Last active
December 23, 2015 02:59
-
-
Save wegorich/6570841 to your computer and use it in GitHub Desktop.
multioauth authorization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem 'pg' | |
gem 'activerecord-postgres-hstore', '~> 0.7' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lib/hstorable.rb | |
require 'active_record' | |
module Hstorable | |
# Usage | |
# | |
# hstorable ({ | |
# boolean_key: lambda {|v| v == "true" ? true : false }, | |
# string_key: lambda {|v| v.to_s}, | |
# integer_key: lambda {|v| v.to_i}, | |
# float_key: lambda {|v| v.to_f}, | |
# array_key: lambda {|v| v[1..-2].split(',').collect!{|n| n.to_i} } | |
# | |
# or | |
# | |
# key: { block: lambda{..}, default: val} | |
# | |
# }, column_name) | |
# | |
def hstorable (keys = {}, column = :data) | |
serialize column, ActiveRecord::Coders::Hstore | |
keys.each do |key, args| | |
key = key.to_s | |
if args.is_a? Hash | |
block, default = args[:block], args[:default] | |
else | |
block, default = args, nil | |
end | |
attr_accessible key | |
define_method(key) do | |
data = self.send(column) | |
value = data && data[key] | |
value ? block.call(value) : default | |
end | |
define_method("#{key}=") do |value| | |
self.send("#{column.to_s}=", (send(column) || {}).merge(key => value)) | |
end | |
end | |
end | |
end | |
ActiveRecord::Base.send :extend, Hstorable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#config/initializers/omniauth.rb | |
#require 'openid/store/filesystem' | |
Rails.application.config.middleware.use OmniAuth::Builder do | |
Secrets::secret['omniauth'].each do |service, definition| | |
params = {} | |
definition['scope'] and params[:scope] = definition['scope'] | |
provider service.to_sym, definition['key'], definition['secret'], params | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#app/controllers/registrations_controller.rb | |
require 'authorization_builder' | |
class RegistrationsController < Devise::RegistrationsController | |
respond_to :html, :js | |
def success | |
respond_with(resource) do |format| | |
format.html { render :layout => !request.xhr? } | |
end | |
end | |
private | |
def build_resource(*args) | |
super | |
if session[:omniauth_token] and params[:with_provider] == "true" | |
AuthorizationBuilder.set_with_omniauth! session[:omniauth_token], @user | |
@authorization = @user.authorizations.first | |
@user.valid? | |
end | |
end | |
protected | |
def after_sign_up_path_for resource | |
session[:just_signed_up] = true | |
super(resource) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#config/routes.rb | |
match '/auth/:provider/callback' => 'authorizations#create' | |
resources :authorizations, :only => [:index, :create, :destroy] | |
match '/auth/failure' => 'authorizations#auth_failure' | |
devise_for :users, :controllers => {registrations: 'registrations'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE user_authorizations ( | |
id integer NOT NULL, | |
provider character varying(255), | |
uid character varying(255), | |
token character varying(255), | |
secret character varying(255), | |
data hstore, | |
user_id integer, | |
"primary" boolean DEFAULT false, | |
created_at timestamp without time zone NOT NULL, | |
updated_at timestamp without time zone NOT NULL | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#config/initializers/secrets.rb | |
module Secrets | |
def self.secret | |
@secrets ||= YAML::load( File::open(File::expand_path("../../#{keys_file}", __FILE__))) | |
end | |
def self.defined_providers | |
secret['omniauth'].each { |provider, pair|}.map { |provider| provider[0].to_sym } | |
end | |
private | |
def self.keys_file | |
'secrets.yml' | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#config/secrets.yml | |
omniauth: | |
provider: | |
key: | |
secret: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SetupHstore < ActiveRecord::Migration | |
def self.up | |
execute "CREATE EXTENSION IF NOT EXISTS hstore" | |
end | |
def self.down | |
execute "DROP EXTENSION IF EXISTS hstore" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#app/models/user.rb | |
class User < ActiveRecord::Base | |
has_many :authorizations, | |
class_name: "User::Authorization", | |
dependent: :destroy | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment