oauth_consumer 0.1.0

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.
Files changed (50) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/CHANGELOG +2 -0
  4. data/Gemfile +2 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.md +1 -0
  7. data/Rakefile +2 -0
  8. data/generators/oauth_consumer/USAGE +10 -0
  9. data/generators/oauth_consumer/oauth_consumer_generator.rb +52 -0
  10. data/generators/oauth_consumer/templates/consumer_token.rb +11 -0
  11. data/generators/oauth_consumer/templates/controller.rb +27 -0
  12. data/generators/oauth_consumer/templates/index.html.erb +29 -0
  13. data/generators/oauth_consumer/templates/index.html.haml +18 -0
  14. data/generators/oauth_consumer/templates/index.html.slim +15 -0
  15. data/generators/oauth_consumer/templates/migration.rb +23 -0
  16. data/generators/oauth_consumer/templates/oauth_config.rb +73 -0
  17. data/generators/oauth_consumer/templates/show.html.erb +7 -0
  18. data/generators/oauth_consumer/templates/show.html.haml +8 -0
  19. data/generators/oauth_consumer/templates/show.html.slim +8 -0
  20. data/init.rb +1 -0
  21. data/lib/generators/active_record/oauth_consumer_generator.rb +33 -0
  22. data/lib/generators/active_record/oauth_consumer_templates/consumer_token.rb +11 -0
  23. data/lib/generators/active_record/oauth_consumer_templates/migration.rb +20 -0
  24. data/lib/generators/erb/oauth_consumer_generator.rb +14 -0
  25. data/lib/generators/erb/oauth_consumer_templates/index.html.erb +29 -0
  26. data/lib/generators/erb/oauth_consumer_templates/show.html.erb +7 -0
  27. data/lib/generators/haml/oauth_consumer_generator.rb +21 -0
  28. data/lib/generators/haml/oauth_consumer_templates/index.html.haml +18 -0
  29. data/lib/generators/haml/oauth_consumer_templates/show.html.haml +8 -0
  30. data/lib/generators/mongoid/oauth_consumer_generator.rb +15 -0
  31. data/lib/generators/mongoid/oauth_consumer_templates/consumer_token.rb +41 -0
  32. data/lib/generators/oauth_consumer/USAGE +11 -0
  33. data/lib/generators/oauth_consumer/oauth_consumer_generator.rb +31 -0
  34. data/lib/generators/oauth_consumer/templates/controller.rb +59 -0
  35. data/lib/generators/oauth_consumer/templates/oauth_config.rb +68 -0
  36. data/lib/generators/oauth_inflections.rb +6 -0
  37. data/lib/generators/oauth_plugin.rb +0 -0
  38. data/lib/oauth/controllers/consumer_controller.rb +152 -0
  39. data/lib/oauth/models/consumers/service_loader.rb +28 -0
  40. data/lib/oauth/models/consumers/services/oauth2_token.rb +50 -0
  41. data/lib/oauth/models/consumers/services/twitter_token.rb +24 -0
  42. data/lib/oauth/models/consumers/simple_client.rb +50 -0
  43. data/lib/oauth/models/consumers/token.rb +121 -0
  44. data/lib/oauth_consumer/version.rb +3 -0
  45. data/lib/oauth_consumer.rb +2 -0
  46. data/oauth_consumer.gemspec +24 -0
  47. data/rails/init.rb +1 -0
  48. data/spec/spec_helper.rb +3 -0
  49. data/tasks/oauth_tasks.rake +4 -0
  50. metadata +148 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 722d2b73fe0515ce4cb7f332ff7a20a28ce660a6
4
+ data.tar.gz: f4424beb1696f4fd3733802b5c70b80cb090ee1e
5
+ SHA512:
6
+ metadata.gz: 047ac94d1844c592a1335832d3ccfe6bcf191fa409b3cef26f059b9398f81dc0b61a27767a28a3488deae3e3f6c6c113d162e503b012578ac112e7a4a433c44d
7
+ data.tar.gz: e612eb41d57a5951abaeca8604dbd8dd798128756ddf75adbd832e1826fa295de843e732c22c19a88b44f77db21fc99d0b4b1cefc602185dae5911675bf3e9ce
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ doc
2
+ pkg
3
+ *.log
4
+ .DS_Store
5
+ .svn
6
+ *.gem
7
+ .bundle
8
+ .swp
9
+ .idea
10
+ .rvmrc
11
+
12
+ Gemfile.lock
data/CHANGELOG ADDED
@@ -0,0 +1,2 @@
1
+ 0.1.0
2
+ - Forked from oauth-plugin
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Pelle Braendgaard, Kontextual
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1 @@
1
+ # OAuth Consumer
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,10 @@
1
+ ./script/generate oauth_consumer
2
+
3
+ This creates an OAuth Provider controller as well as requisite models.
4
+
5
+ It requires an authentication framework such as acts_as_authenticated, restful_authentication or restful_open_id_authentication that provides the methods "login_required" and "current_user".
6
+
7
+ If you generated the migration file (true by default), make sure you run
8
+ rake db:migrate
9
+
10
+ See README.rdoc for more.
@@ -0,0 +1,52 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../oauth_provider/lib/insert_routes.rb")
2
+
3
+ class OauthConsumerGenerator < Rails::Generator::Base
4
+ default_options :skip_migration => false
5
+
6
+ def manifest
7
+ record do |m|
8
+
9
+ # Controller, helper, views, and test directories.
10
+ m.directory File.join('app/models')
11
+ m.directory File.join('app/controllers')
12
+ m.directory File.join('app/helpers')
13
+ m.directory File.join('app/views', 'oauth_consumers')
14
+ m.directory File.join('config/initializers')
15
+
16
+ m.template 'oauth_config.rb',File.join('config/initializers', "oauth_consumers.rb")
17
+ m.template 'consumer_token.rb',File.join('app/models',"consumer_token.rb")
18
+
19
+ m.template 'controller.rb',File.join('app/controllers',"oauth_consumers_controller.rb")
20
+ m.route_entry "map.resources :oauth_consumers,:member=>{:callback=>:get}"
21
+
22
+ @template_extension= options[:haml] ? "haml" : ( options[:slim] ? "slim": "erb")
23
+
24
+ m.template "show.html.#{@template_extension}", File.join('app/views', 'oauth_consumers', "show.html.#{@template_extension}")
25
+ m.template "index.html.#{@template_extension}", File.join('app/views', 'oauth_consumers', "index.html.#{@template_extension}")
26
+
27
+ unless options[:skip_migration]
28
+ m.migration_template 'migration.rb', 'db/migrate', :assigns => {
29
+ :migration_name => "CreateOauthConsumerTokens"
30
+ }, :migration_file_name => "create_oauth_consumer_tokens"
31
+ end
32
+ end
33
+ end
34
+
35
+ protected
36
+ def banner
37
+ "Usage: #{$0} #{spec.name}"
38
+ end
39
+
40
+ def add_options!(opt)
41
+ opt.separator ''
42
+ opt.separator 'Options:'
43
+ opt.on("--skip-migration",
44
+ "Don't generate a migration file") { |v| options[:skip_migration] = v }
45
+ # opt.on("--test-unit",
46
+ # "Generate the Test::Unit compatible tests instead of RSpec") { |v| options[:test_unit] = v }
47
+ opt.on("--haml",
48
+ "Templates use haml") { |v| options[:haml] = v }
49
+ opt.on("--slim",
50
+ "Templates use slim") { |v| options[:slim] = v }
51
+ end
52
+ end
@@ -0,0 +1,11 @@
1
+ require 'oauth/models/consumers/token'
2
+ class ConsumerToken < ActiveRecord::Base
3
+ include Oauth::Models::Consumers::Token
4
+
5
+ # You can safely remove this callback if you don't allow login from any of your services
6
+ before_create :create_user
7
+
8
+ # Modify this with class_name etc to match your application
9
+ belongs_to :user
10
+
11
+ end
@@ -0,0 +1,27 @@
1
+ require 'oauth/controllers/consumer_controller'
2
+ class OauthConsumersController < ApplicationController
3
+ include Oauth::Controllers::ConsumerController
4
+
5
+ def index
6
+ @consumer_tokens=ConsumerToken.all :conditions => {:user_id => current_user.id}
7
+ @services=OAUTH_CREDENTIALS.keys-@consumer_tokens.collect{|c| c.class.service_name}
8
+ end
9
+
10
+ def callback
11
+ super
12
+ end
13
+
14
+ def client
15
+ super
16
+ end
17
+
18
+ protected
19
+
20
+ # Change this to decide where you want to redirect user to after callback is finished.
21
+ # params[:id] holds the service name so you could use this to redirect to various parts
22
+ # of your application depending on what service you're connecting to.
23
+ def go_back
24
+ redirect_to root_url
25
+ end
26
+
27
+ end
@@ -0,0 +1,29 @@
1
+ <h1>Services</h1>
2
+
3
+ <%% if @consumer_tokens.empty? %>
4
+ <p>
5
+ You are currently not connected to any external services.
6
+ </p>
7
+ <%% else %>
8
+ <p>
9
+ You are connected to the following services:
10
+ </p>
11
+ <ul>
12
+ <%% @consumer_tokens.each do |token| %>
13
+ <li>
14
+ <%%= link_to token.class.service_name.to_s.humanize, oauth_consumer_path(token.class.service_name) %>
15
+ </li>
16
+ <%% end %>
17
+ </ul>
18
+ <%% end %>
19
+
20
+ <%% unless @services.empty? %>
21
+ <h3>You can connect to the following services:</h3>
22
+ <ul>
23
+ <%% @services.each do |service| %>
24
+ <li>
25
+ <%%= link_to service.to_s.humanize, oauth_consumer_path(service) %>
26
+ </li>
27
+ <%% end %>
28
+ </ul>
29
+ <%% end %>
@@ -0,0 +1,18 @@
1
+ %h1 Services
2
+
3
+ -if @consumer_tokens.empty?
4
+ %p
5
+ You are currently not connected to any external services.
6
+ -else
7
+ %p You are connected to the following services:
8
+ %ul
9
+ -@consumer_tokens.each do |token|
10
+ %li
11
+ =link_to token.class.service_name.to_s.humanize, oauth_consumer_path(token.class.service_name)
12
+
13
+ -unless @services.empty?
14
+ %h3 You can connect to the following services:
15
+ %ul
16
+ -@services.each do |service|
17
+ %li
18
+ =link_to service.to_s.humanize,oauth_consumer_path(service)
@@ -0,0 +1,15 @@
1
+ h1 Services
2
+
3
+ -if @consumer_tokens.empty?
4
+ p You are currently not connected to any external services.
5
+ -else
6
+ p You are connected to the following services:
7
+ ul
8
+ -@consumer_tokens.each do |token|
9
+ li = link_to token.class.service_name.to_s.humanize, oauth_consumer_path(token.class.service_name)
10
+
11
+ -unless @services.empty?
12
+ h3 You can connect to the following services:
13
+ ul
14
+ -@services.each do |service|
15
+ li = link_to service.to_s.humanize,oauth_consumer_path(service)
@@ -0,0 +1,23 @@
1
+ class CreateOauthConsumerTokens < ActiveRecord::Migration
2
+ def self.up
3
+
4
+ create_table :consumer_tokens do |t|
5
+ t.integer :user_id
6
+ t.string :type, :limit => 30
7
+ t.string :token, :limit => 1024 # This has to be huge because of Yahoo's excessively large tokens
8
+ t.string :refresh_token
9
+ t.string :expires_in
10
+ t.string :expires_at
11
+ t.string :secret
12
+ t.timestamps
13
+ end
14
+
15
+ add_index :consumer_tokens, :token, :unique => true, :length => 100
16
+
17
+ end
18
+
19
+ def self.down
20
+ drop_table :consumer_tokens
21
+ end
22
+
23
+ end
@@ -0,0 +1,73 @@
1
+ # edit this file to contain credentials for the OAuth services you support.
2
+ # each entry needs a corresponding token model.
3
+ #
4
+ # eg. :twitter => TwitterToken, :hour_feed => HourFeedToken etc.
5
+ #
6
+ # OAUTH_CREDENTIALS => {
7
+ # :twitter => {
8
+ # :key => "",
9
+ # :secret => "",
10
+ # :client => :twitter_gem, # :twitter_gem or :oauth_gem (defaults to :twitter_gem)
11
+ # :expose => false, # expose client at /oauth_consumers/twitter/client see docs
12
+ # :allow_login => true # Use :allow_login => true to allow user to login to account
13
+ # },
14
+ # :google => {
15
+ # :key => "",
16
+ # :secret => "",
17
+ # :expose => false, # expose client at /oauth_consumers/google/client see docs
18
+ # :scope => "" # see http://code.google.com/apis/gdata/faq.html#AuthScopes
19
+ # },
20
+ # :github => {
21
+ # :key => "",
22
+ # :secret => "",
23
+ # :expose => false, # expose client at /oauth_consumers/twitter/client see docs
24
+ #
25
+ # },
26
+ # :facebook => {
27
+ # :key => "",
28
+ # :secret => "",
29
+ # :oauth_version => 2,
30
+ # :super_class => 'Oauth2Token' # unnecessary if you have an explicit "class FacebookToken < Oauth2Token",
31
+ # :options => {
32
+ # :site => "https://graph.facebook.com"
33
+ # }
34
+ # },
35
+ # :agree2 => {
36
+ # :key => "",
37
+ # :secret => ""
38
+ # },
39
+ # :fireeagle => {
40
+ # :key => "",
41
+ # :secret => ""
42
+ # },
43
+ # :oauth2_server => {
44
+ # :key => "",
45
+ # :secret => "",
46
+ # :oauth_version => 2
47
+ # :options => { # OAuth::Consumer options
48
+ # :site => "http://hourfeed.com" # Remember to add a site for a generic OAuth site
49
+ # }
50
+ # },
51
+ # :hour_feed => {
52
+ # :key => "",
53
+ # :secret => "",
54
+ # :options => { # OAuth::Consumer options
55
+ # :site => "http://hourfeed.com" # Remember to add a site for a generic OAuth site
56
+ # }
57
+ # },
58
+ # :nu_bux => {
59
+ # :key => "",
60
+ # :secret => "",
61
+ # :super_class => "OpenTransactToken", # if a OAuth service follows a particular standard
62
+ # # with a token implementation you can set the superclass
63
+ # # to use
64
+ # :options => { # OAuth::Consumer options
65
+ # :site => "http://nubux.heroku.com"
66
+ # }
67
+ # }
68
+ # }
69
+ #
70
+ OAUTH_CREDENTIALS = {
71
+ } unless defined? OAUTH_CREDENTIALS
72
+
73
+ load 'oauth/models/consumers/service_loader.rb'
@@ -0,0 +1,7 @@
1
+ <h1>You are already Connected to <%%=params[:id].humanize%></h1>
2
+ <%% form_tag oauth_consumer_path(params[:id]),:method=>:delete do %>
3
+ <%%=submit_tag "Disconnect" %>
4
+ or
5
+ <%%=submit_tag "Reconnect" %>
6
+ if you experienced a problem.
7
+ <%% end %>
@@ -0,0 +1,8 @@
1
+ %h1
2
+ You are already Connected to
3
+ =params[:id].humanize
4
+ -form_tag oauth_consumer_path(params[:id]),:method=>:delete do
5
+ =submit_tag "Disconnect"
6
+ or
7
+ =submit_tag "Reconnect"
8
+ if you experienced a problem.
@@ -0,0 +1,8 @@
1
+ h1
2
+ | You are already Connected to
3
+ = params[:id].humanize
4
+ - form_tag oauth_consumer_path(params[:id]),:method=>:delete do
5
+ = submit_tag "Disconnect"
6
+ | or
7
+ = submit_tag "Reconnect"
8
+ |if you experienced a problem.
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/rails/init"
@@ -0,0 +1,33 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ module ActiveRecord
4
+ module Generators
5
+ class OauthConsumerGenerator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+
8
+ source_root File.expand_path('../oauth_consumer_templates', __FILE__)
9
+
10
+ # Implement the required interface for Rails::Generators::Migration.
11
+ def self.next_migration_number(dirname) #:nodoc:
12
+ next_migration_number = current_migration_number(dirname) + 1
13
+ if ActiveRecord::Base.timestamped_migrations
14
+ [Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
15
+ else
16
+ "%.3d" % next_migration_number
17
+ end
18
+ end
19
+
20
+ def check_class_collisions
21
+ class_collisions '', %w(ConsumerToken)
22
+ end
23
+
24
+ def copy_models
25
+ template 'consumer_token.rb', File.join('app/models', 'consumer_token.rb')
26
+ end
27
+
28
+ def copy_migration
29
+ migration_template 'migration.rb', 'db/migrate/create_oauth_consumer_tokens'
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,11 @@
1
+ require 'oauth/models/consumers/token'
2
+ class ConsumerToken < ActiveRecord::Base
3
+ include Oauth::Models::Consumers::Token
4
+
5
+ # You can safely remove this callback if you don't allow login from any of your services
6
+ before_create :create_user
7
+
8
+ # Modify this with class_name etc to match your application
9
+ belongs_to :user
10
+
11
+ end
@@ -0,0 +1,20 @@
1
+ class CreateOauthConsumerTokens < ActiveRecord::Migration
2
+ def self.up
3
+
4
+ create_table :consumer_tokens do |t|
5
+ t.integer :user_id
6
+ t.string :type, :limit => 30
7
+ t.string :token, :limit => 1024 # This has to be huge because of Yahoo's excessively large tokens
8
+ t.string :secret
9
+ t.timestamps
10
+ end
11
+
12
+ add_index :consumer_tokens, :token, :unique => true, :length => 100
13
+
14
+ end
15
+
16
+ def self.down
17
+ drop_table :consumer_tokens
18
+ end
19
+
20
+ end
@@ -0,0 +1,14 @@
1
+ require 'rails/generators/erb'
2
+
3
+ module Erb
4
+ module Generators
5
+ class OauthConsumerGenerator < Erb::Generators::Base
6
+ source_root File.expand_path('../oauth_consumer_templates', __FILE__)
7
+
8
+ def copy_view_files
9
+ template 'index.html.erb', File.join('app/views', class_path, 'oauth_consumers', 'index.html.erb')
10
+ template 'show.html.erb', File.join('app/views', class_path, 'oauth_consumers', 'show.html.erb')
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,29 @@
1
+ <h1>Services</h1>
2
+
3
+ <%% if @consumer_tokens.empty? %>
4
+ <p>
5
+ You are currently not connected to any external services.
6
+ </p>
7
+ <%% else %>
8
+ <p>
9
+ You are connected to the following services:
10
+ </p>
11
+ <ul>
12
+ <%% @consumer_tokens.each do |token| %>
13
+ <li>
14
+ <%%= link_to token.class.service_name.to_s.humanize, oauth_consumer_path(token.class.service_name) %>
15
+ </li>
16
+ <%% end %>
17
+ </ul>
18
+ <%% end %>
19
+
20
+ <%% unless @services.empty? %>
21
+ <h3>You can connect to the following services:</h3>
22
+ <ul>
23
+ <%% @services.each do |service| %>
24
+ <li>
25
+ <%%= link_to service.to_s.humanize, oauth_consumer_path(service) %>
26
+ </li>
27
+ <%% end %>
28
+ </ul>
29
+ <%% end %>
@@ -0,0 +1,7 @@
1
+ <h1>You are already Connected to <%%=params[:id].humanize%></h1>
2
+ <%% form_tag oauth_consumer_path(params[:id]),:method=>:delete do %>
3
+ <%%=submit_tag "Disconnect" %>
4
+ or
5
+ <%%=submit_tag "Reconnect" %>
6
+ if you experienced a problem.
7
+ <%% end %>
@@ -0,0 +1,21 @@
1
+ require 'rails/generators/erb/controller/controller_generator'
2
+
3
+ module Haml
4
+ module Generators
5
+ class OauthConsumerGenerator < Erb::Generators::Base
6
+ source_root File.expand_path('../oauth_consumer_templates', __FILE__)
7
+
8
+ argument :name, :type => :string, :default => 'Oauth'
9
+
10
+ def copy_view_files
11
+ template 'index.html.haml', File.join('app/views', class_path, 'oauth_consumers', 'index.html.haml')
12
+ template 'show.html.haml', File.join('app/views', class_path, 'oauth_consumers', 'show.html.haml')
13
+ end
14
+
15
+ protected
16
+ def handler
17
+ :haml
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ %h1 Services
2
+
3
+ -if @consumer_tokens.empty?
4
+ %p
5
+ You are currently not connected to any external services.
6
+ -else
7
+ %p You are connected to the following services:
8
+ %ul
9
+ -@consumer_tokens.each do |token|
10
+ %li
11
+ =link_to token.class.service_name.to_s.humanize, oauth_consumer_path(token.class.service_name)
12
+
13
+ -unless @services.empty?
14
+ %h3 You can connect to the following services:
15
+ %ul
16
+ -@services.each do |service|
17
+ %li
18
+ =link_to service.to_s.humanize,oauth_consumer_path(service)
@@ -0,0 +1,8 @@
1
+ %h1
2
+ You are already Connected to
3
+ =params[:id].humanize
4
+ -form_tag oauth_consumer_path(params[:id]),:method=>:delete do
5
+ =submit_tag "Disconnect"
6
+ or
7
+ =submit_tag "Reconnect"
8
+ if you experienced a problem.
@@ -0,0 +1,15 @@
1
+ module Mongoid
2
+ module Generators
3
+ class OauthConsumerGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../oauth_consumer_templates', __FILE__)
5
+
6
+ def check_class_collisions
7
+ class_collisions '', %w(ConsumerToken)
8
+ end
9
+
10
+ def copy_models
11
+ template 'consumer_token.rb', File.join('app/models', 'consumer_token.rb')
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,41 @@
1
+ require 'oauth/models/consumers/token'
2
+ class ConsumerToken
3
+ include Mongoid::Document
4
+ include Mongoid::Timestamps
5
+ include Oauth::Models::Consumers::Token
6
+
7
+ # You can safely remove this callback if you don't allow login from any of your services
8
+ before_create :create_user
9
+
10
+ field :token, :type => String
11
+ field :secret, :type => String
12
+
13
+ index :token
14
+
15
+ # Add the following to your user model:
16
+ #
17
+ # embeds_many :consumer_tokens
18
+ # index "consumer_tokens.token"
19
+ #
20
+ embedded_in :user, :inverse_of => :consumer_tokens
21
+
22
+ def self.find_or_create_from_access_token(user,access_token)
23
+ secret = access_token.respond_to?(:secret) ? access_token.secret : nil
24
+
25
+ if user
26
+ user.consumer_tokens.where(:_type=>self.to_s,:token=>access_token.token).first ||
27
+ self.create!(:_type=>self.to_s,:token=>access_token.token, :secret=>secret, :user=>user)
28
+ else
29
+ user = User.where("consumer_tokens._type"=>self.to_s,"consumer_tokens.token"=>access_token.token).first
30
+ if user
31
+ user.consumer_tokens.detect{|t| t.token==access_token.token && t.is_a?(self)}
32
+ else
33
+ user = User.new
34
+ self.create!(:_type=>self.to_s,:token=>access_token.token, :secret=>secret, :user=>user)
35
+ user.save!
36
+ user.consumer_tokens.last
37
+ end
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,11 @@
1
+ This creates an OAuth Consumer controller as well as requisite models.
2
+
3
+ It requires an authentication framework such as acts_as_authenticated, restful_authentication or restful_open_id_authentication that provides the methods "login_required" and "current_user".
4
+
5
+ See comments in generated controller for more info about overriding these.
6
+
7
+ If you generated the migration file (true by default), make sure you run
8
+
9
+ rake db:migrate
10
+
11
+ See README.rdoc for more.
@@ -0,0 +1,31 @@
1
+ require 'rails/generators/migration'
2
+ require 'rails/generators/active_record'
3
+
4
+ class OauthConsumerGenerator < Rails::Generators::Base
5
+ source_root File.expand_path("../templates", __FILE__)
6
+
7
+ hook_for :orm
8
+
9
+ def copy_models
10
+ template 'oauth_config.rb', File.join('config', 'initializers', 'oauth_consumers.rb')
11
+ end
12
+
13
+ def copy_controller
14
+ template 'controller.rb', File.join('app', 'controllers', 'oauth_consumers_controller.rb')
15
+ end
16
+
17
+ hook_for :template_engine
18
+
19
+ def add_route
20
+ route <<-ROUTE.strip
21
+ resources :oauth_consumers do
22
+ member do
23
+ get :callback
24
+ get :callback2
25
+ match 'client/*endpoint' => 'oauth_consumers#client', :via => [:get, :post, :put, :delete]
26
+ end
27
+ end
28
+ ROUTE
29
+ end
30
+
31
+ end