oauth_multi_db 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in oauth_multi_db.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Dorren Chen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # OauthMultiDb
2
+
3
+ Switch rails app's db based on incoming oauth client
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'oauth_multi_db'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install oauth_multi_db
18
+
19
+ ## Usage
20
+
21
+ For each request, if oauth client is found. it retrieves client.client_symbol value (from notes field),
22
+ and uses it to switch to corresponding db.
23
+
24
+ To set client_symbol value on Oauth2 client, do it when your register it:
25
+ ``` ruby
26
+ Rack::OAuth2::Server.register(
27
+ :display_name => 'oauth client 1',
28
+ :notes => {'client_symbol' => 'client_1'}.to_json
29
+ )
30
+ ```
31
+
32
+ Setup
33
+ 1. In rails app, create config/initializers/oauth_multi_db_config.rb, with code below:
34
+ ``` ruby
35
+ require 'oauth_multi_db'
36
+
37
+ OauthMultiDb.configure do |config|
38
+ config.domain_models = [User, Article] # models that will have db changed.
39
+
40
+ # your custom way to change db, or anything based on client_symbol
41
+ config.db_switcher = lambda {|client_symbol|
42
+ new_db = "db_" + client_symbol.to_s
43
+
44
+ OauthMultiDb.config.domain_models.each do |klass|
45
+ # change klass's db to new_db
46
+ end
47
+ }
48
+ end
49
+ ```
50
+
51
+ 2. in your controller, use the macro
52
+
53
+ ``` ruby
54
+ class ApplicationController
55
+ oauth_db_switch
56
+ # or
57
+ oauth_db_swith, :only => [:create, :update] # or any before_filter options
58
+ end
59
+ ```
60
+
61
+ This gem requires rack-oauth2-server gem. see https://github.com/assaf/rack-oauth2-server for more info.
62
+
63
+
64
+ ## Contributing
65
+
66
+ 1. Fork it
67
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
68
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
69
+ 4. Push to the branch (`git push origin my-new-feature`)
70
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,41 @@
1
+ class OauthClientsController < ApplicationController
2
+ #oauth_required :only => [:create, :destroy]
3
+
4
+ def index
5
+ clients = Rack::OAuth2::Server::Client.all.collect{|x| x.parse_notes; x}
6
+ render :json => clients.to_json
7
+ end
8
+
9
+ def show
10
+ client = Rack::OAuth2::Server.get_client(params[:id])
11
+
12
+ if client
13
+ client.parse_notes
14
+ render :json => client.to_json
15
+ else
16
+ render :nothing => true, :status => :not_found
17
+ end
18
+ end
19
+
20
+ def create
21
+ attrs = {:id => params[:id],
22
+ :secret => params[:secret],
23
+ :display_name => params[:display_name],
24
+ :link => params[:link]}
25
+
26
+ client = Rack::OAuth2::Server.register(attrs)
27
+ render :json => client.to_json
28
+ end
29
+
30
+ def destroy
31
+ client = Rack::OAuth2::Server.get_client(params[:id])
32
+
33
+ if client
34
+ client.destroy
35
+ render :nothing => true, :status => :accepted
36
+ else
37
+ render :nothing => true, :status => :not_found
38
+ end
39
+ end
40
+ end
41
+
@@ -0,0 +1,39 @@
1
+ class OauthIssuersController < ApplicationController
2
+ #oauth_required :only => [:create, :destroy]
3
+
4
+ def index
5
+ issuers = Rack::OAuth2::Server::Issuer.collection.find.entries
6
+ render :json => issuers.to_json
7
+ end
8
+
9
+ def show
10
+ issuer = Rack::OAuth2::Server.get_issuer(params[:id])
11
+
12
+ if issuer
13
+ render :json => issuer.to_json
14
+ else
15
+ render :nothing => true, :status => :not_found
16
+ end
17
+ end
18
+
19
+ def create
20
+ attrs = {:identifier => params[:issuer_id],
21
+ :hmac_secret => params[:issuer_secret]}
22
+
23
+ issuer = Rack::OAuth2::Server.register_issuer(attrs)
24
+ render :json => issuer.to_json
25
+ end
26
+
27
+ def destroy
28
+ issuer = Rack::OAuth2::Server.get_issuer(params[:id])
29
+
30
+ if issuer
31
+ issuer.destroy
32
+ render :nothing => true, :status => :ok
33
+ else
34
+ render :nothing => true, :status => :not_found
35
+ end
36
+ end
37
+ end
38
+
39
+
data/config/routes.rb ADDED
@@ -0,0 +1,4 @@
1
+ Rails.application.routes.draw do
2
+ resources :oauth_clients
3
+ resources :oauth_issuers
4
+ end
@@ -0,0 +1,10 @@
1
+ require "oauth_multi_db/version"
2
+ require "oauth_multi_db/oauth_client_ext"
3
+ require "oauth_multi_db/action_controller"
4
+ require "oauth_multi_db/engine"
5
+
6
+ module OauthMultiDb
7
+ include ActiveSupport::Configurable
8
+ end
9
+
10
+ Rack::OAuth2::Server::Client.send :include, OauthClientExt
@@ -0,0 +1,65 @@
1
+ require 'active_support/configurable'
2
+
3
+ # perform DB switch based on incoming oauth request's client_id
4
+ #
5
+ # For each request, if oauth client is found. it retrieves client.client_symbol value (from notes field),
6
+ # and uses it to switch to corresponding db.
7
+ #
8
+ # To set client_symbol on Oauth2 client, do it when your register it:
9
+ # Rack::OAuth2::Server.register(
10
+ # :display_name => 'oauth client 1',
11
+ # :notes => {'client_symbol' => 'abc'}.to_json
12
+ # )
13
+ #
14
+ #
15
+ # Setup
16
+ # 1. create config/initializers/oauth_multi_db_config.rb, with code below:
17
+ # require 'oauth_multi_db'
18
+ #
19
+ # OauthMultiDb.configure do |config|
20
+ # config.domain_models = [User, Article] # models that will have db changed.
21
+ #
22
+ # # your custom way to change db, or anything based on client_symbol
23
+ # config.switcher = lambda {|client_symbol|
24
+ # new_db = "db_" + client_symbol.to_s
25
+ # # change to new db
26
+ # }
27
+ # end
28
+ #
29
+ # 2. in your controller, use the macro
30
+ # class ApplicationController
31
+ # oauth_db_switch
32
+ # # or
33
+ # oauth_db_switch, :only => [:create, :update] # or any before_filter options
34
+ # end
35
+ #
36
+ #
37
+ # This module requires rack-oauth2-server gem. see https://github.com/assaf/rack-oauth2-server for more info.
38
+ module OauthMultiDb
39
+ module ActionController
40
+ extend ActiveSupport::Concern
41
+
42
+ included do
43
+ end
44
+
45
+ module ClassMethods
46
+ def oauth_db_switch(options={})
47
+ before_filter :oauth_db_switch, options
48
+ end
49
+ end
50
+
51
+ # switch db based on incoming oauth client_id
52
+ def oauth_db_switch
53
+ if client_symbol = get_client_symbol
54
+ OauthMultiDb.config.db_switcher.call(client_symbol)
55
+ end
56
+ end
57
+
58
+ def get_client_symbol
59
+ if client = oauth.client
60
+ client.parse_notes
61
+ symbol = client.client_symbol
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,10 @@
1
+ require 'oauth_multi_db/action_controller'
2
+
3
+ module OauthMultiDb
4
+ class Engine < Rails::Engine
5
+ config.after_initialize do
6
+ end
7
+ end
8
+ end
9
+
10
+ ActionController::Base.send :include, OauthMultiDb::ActionController
@@ -0,0 +1,15 @@
1
+ module OauthClientExt
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ attr_reader :client_symbol
6
+ end
7
+
8
+ module ClassMethods
9
+ end
10
+
11
+ def parse_notes
12
+ hash = JSON.parse(self.notes) rescue {}
13
+ @client_symbol = hash['client_symbol']
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module OauthMultiDb
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/oauth_multi_db/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Dorren Chen"]
6
+ gem.email = ["dorrenchen@gmail.com"]
7
+ gem.description = %q{oauth client based db switching}
8
+ gem.summary = %q{switch db based on incoming oauth client}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "oauth_multi_db"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = OauthMultiDb::VERSION
17
+
18
+ gem.add_dependency "rails"
19
+ gem.add_dependency "rack-oauth2-server"
20
+ gem.add_development_dependency "rspec"
21
+ gem.add_development_dependency "rspec-rails"
22
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oauth_multi_db
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dorren Chen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rack-oauth2-server
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec-rails
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: oauth client based db switching
79
+ email:
80
+ - dorrenchen@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE
88
+ - README.md
89
+ - Rakefile
90
+ - app/controllers/oauth_clients_controller.rb
91
+ - app/controllers/oauth_issuers_controller.rb
92
+ - config/routes.rb
93
+ - lib/oauth_multi_db.rb
94
+ - lib/oauth_multi_db/action_controller.rb
95
+ - lib/oauth_multi_db/engine.rb
96
+ - lib/oauth_multi_db/oauth_client_ext.rb
97
+ - lib/oauth_multi_db/version.rb
98
+ - oauth_multi_db.gemspec
99
+ homepage: ''
100
+ licenses: []
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 1.8.24
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: switch db based on incoming oauth client
123
+ test_files: []