devise_oauth 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. data/.gitignore +7 -0
  2. data/.rspec +2 -0
  3. data/Gemfile +17 -0
  4. data/Gemfile.lock +136 -0
  5. data/README.md +77 -0
  6. data/app/controllers/{oauth → devise/oauth}/access_tokens_controller.rb +0 -0
  7. data/app/controllers/{oauth → devise/oauth}/accesses_controller.rb +0 -0
  8. data/app/controllers/{oauth → devise/oauth}/authorizations_controller.rb +0 -0
  9. data/app/controllers/{oauth → devise/oauth}/clients_controller.rb +0 -0
  10. data/app/models/{oauth → devise/oauth}/access.rb +0 -0
  11. data/app/models/{oauth → devise/oauth}/access_token.rb +0 -0
  12. data/app/models/{oauth → devise/oauth}/authorization.rb +0 -0
  13. data/app/models/{oauth → devise/oauth}/client.rb +0 -0
  14. data/devise_oauth.gemspec +27 -0
  15. data/lib/devise/oauth/engine.rb +7 -2
  16. data/lib/devise/oauth/version.rb +1 -1
  17. data/script/rails +8 -0
  18. data/spec/controllers/access_tokens_controller_spec.rb +208 -0
  19. data/spec/controllers/authorizations_controller_spec.rb +53 -0
  20. data/spec/controllers/protected_resources_controller_spec.rb +79 -0
  21. data/spec/dummy/README.rdoc +261 -0
  22. data/spec/dummy/Rakefile +7 -0
  23. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  24. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  25. data/spec/dummy/app/controllers/application_controller.rb +7 -0
  26. data/spec/dummy/app/controllers/protected_resources_controller.rb +14 -0
  27. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  28. data/spec/dummy/app/mailers/.gitkeep +0 -0
  29. data/spec/dummy/app/models/.gitkeep +0 -0
  30. data/spec/dummy/app/models/ability.rb +15 -0
  31. data/spec/dummy/app/models/user.rb +13 -0
  32. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  33. data/spec/dummy/config.ru +4 -0
  34. data/spec/dummy/config/application.rb +67 -0
  35. data/spec/dummy/config/boot.rb +10 -0
  36. data/spec/dummy/config/database.yml +25 -0
  37. data/spec/dummy/config/environment.rb +5 -0
  38. data/spec/dummy/config/environments/development.rb +37 -0
  39. data/spec/dummy/config/environments/production.rb +67 -0
  40. data/spec/dummy/config/environments/test.rb +37 -0
  41. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  42. data/spec/dummy/config/initializers/devise.rb +216 -0
  43. data/spec/dummy/config/initializers/inflections.rb +15 -0
  44. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  45. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  46. data/spec/dummy/config/initializers/session_store.rb +8 -0
  47. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  48. data/spec/dummy/config/locales/devise.en.yml +57 -0
  49. data/spec/dummy/config/locales/en.yml +5 -0
  50. data/spec/dummy/config/routes.rb +7 -0
  51. data/spec/dummy/db/migrate/20120622183848_devise_create_users.rb +46 -0
  52. data/spec/dummy/db/schema.rb +34 -0
  53. data/spec/dummy/lib/assets/.gitkeep +0 -0
  54. data/spec/dummy/log/.gitkeep +0 -0
  55. data/spec/dummy/public/404.html +26 -0
  56. data/spec/dummy/public/422.html +26 -0
  57. data/spec/dummy/public/500.html +25 -0
  58. data/spec/dummy/public/favicon.ico +0 -0
  59. data/spec/dummy/script/rails +6 -0
  60. data/spec/factories.rb +34 -0
  61. data/spec/models/oauth/access_token_spec.rb +25 -0
  62. data/spec/models/oauth/authorization_spec.rb +19 -0
  63. data/spec/models/oauth/client_spec.rb +31 -0
  64. data/spec/models/user_spec.rb +10 -0
  65. data/spec/spec_helper.rb +39 -0
  66. data/spec/support/match_json.rb +6 -0
  67. metadata +134 -30
@@ -0,0 +1,15 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new inflection rules using the following format
4
+ # (all these examples are active by default):
5
+ # ActiveSupport::Inflector.inflections do |inflect|
6
+ # inflect.plural /^(ox)$/i, '\1en'
7
+ # inflect.singular /^(ox)en/i, '\1'
8
+ # inflect.irregular 'person', 'people'
9
+ # inflect.uncountable %w( fish sheep )
10
+ # end
11
+ #
12
+ # These inflection rules are supported but not enabled by default:
13
+ # ActiveSupport::Inflector.inflections do |inflect|
14
+ # inflect.acronym 'RESTful'
15
+ # end
@@ -0,0 +1,5 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new mime types for use in respond_to blocks:
4
+ # Mime::Type.register "text/richtext", :rtf
5
+ # Mime::Type.register_alias "text/html", :iphone
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key for verifying the integrity of signed cookies.
4
+ # If you change this key, all old signed cookies will become invalid!
5
+ # Make sure the secret is at least 30 characters and all random,
6
+ # no regular words or you'll be exposed to dictionary attacks.
7
+ Dummy::Application.config.secret_token = 'a3b9441a0e35db28706fb05e2409e3786e6cbbe3f5a1541046a4cc627ddbd30de79650ab99d9212583d8f2f494cfb88232e675020d415548aaa178fc25fe98c0'
@@ -0,0 +1,8 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ Dummy::Application.config.session_store :cookie_store, key: '_dummy_session'
4
+
5
+ # Use the database for sessions instead of the cookie-based default,
6
+ # which shouldn't be used to store highly confidential information
7
+ # (create the session table with "rails generate session_migration")
8
+ # Dummy::Application.config.session_store :active_record_store
@@ -0,0 +1,14 @@
1
+ # Be sure to restart your server when you modify this file.
2
+ #
3
+ # This file contains settings for ActionController::ParamsWrapper which
4
+ # is enabled by default.
5
+
6
+ # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
7
+ ActiveSupport.on_load(:action_controller) do
8
+ wrap_parameters format: [:json]
9
+ end
10
+
11
+ # Disable root element in JSON by default.
12
+ ActiveSupport.on_load(:active_record) do
13
+ self.include_root_in_json = false
14
+ end
@@ -0,0 +1,57 @@
1
+ # Additional translations at https://github.com/plataformatec/devise/wiki/I18n
2
+
3
+ en:
4
+ errors:
5
+ messages:
6
+ expired: "has expired, please request a new one"
7
+ not_found: "not found"
8
+ already_confirmed: "was already confirmed, please try signing in"
9
+ not_locked: "was not locked"
10
+ not_saved:
11
+ one: "1 error prohibited this %{resource} from being saved:"
12
+ other: "%{count} errors prohibited this %{resource} from being saved:"
13
+
14
+ devise:
15
+ failure:
16
+ already_authenticated: 'You are already signed in.'
17
+ unauthenticated: 'You need to sign in or sign up before continuing.'
18
+ unconfirmed: 'You have to confirm your account before continuing.'
19
+ locked: 'Your account is locked.'
20
+ invalid: 'Invalid email or password.'
21
+ invalid_token: 'Invalid authentication token.'
22
+ timeout: 'Your session expired, please sign in again to continue.'
23
+ inactive: 'Your account was not activated yet.'
24
+ sessions:
25
+ signed_in: 'Signed in successfully.'
26
+ signed_out: 'Signed out successfully.'
27
+ passwords:
28
+ send_instructions: 'You will receive an email with instructions about how to reset your password in a few minutes.'
29
+ updated: 'Your password was changed successfully. You are now signed in.'
30
+ updated_not_active: 'Your password was changed successfully.'
31
+ send_paranoid_instructions: "If your email address exists in our database, you will receive a password recovery link at your email address in a few minutes."
32
+ confirmations:
33
+ send_instructions: 'You will receive an email with instructions about how to confirm your account in a few minutes.'
34
+ send_paranoid_instructions: 'If your email address exists in our database, you will receive an email with instructions about how to confirm your account in a few minutes.'
35
+ confirmed: 'Your account was successfully confirmed. You are now signed in.'
36
+ registrations:
37
+ signed_up: 'Welcome! You have signed up successfully.'
38
+ signed_up_but_unconfirmed: 'A message with a confirmation link has been sent to your email address. Please open the link to activate your account.'
39
+ signed_up_but_inactive: 'You have signed up successfully. However, we could not sign you in because your account is not yet activated.'
40
+ signed_up_but_locked: 'You have signed up successfully. However, we could not sign you in because your account is locked.'
41
+ updated: 'You updated your account successfully.'
42
+ update_needs_confirmation: "You updated your account successfully, but we need to verify your new email address. Please check your email and click on the confirm link to finalize confirming your new email address."
43
+ destroyed: 'Bye! Your account was successfully cancelled. We hope to see you again soon.'
44
+ unlocks:
45
+ send_instructions: 'You will receive an email with instructions about how to unlock your account in a few minutes.'
46
+ unlocked: 'Your account has been unlocked successfully. Please sign in to continue.'
47
+ send_paranoid_instructions: 'If your account exists, you will receive an email with instructions about how to unlock it in a few minutes.'
48
+ omniauth_callbacks:
49
+ success: 'Successfully authenticated from %{kind} account.'
50
+ failure: 'Could not authenticate you from %{kind} because "%{reason}".'
51
+ mailer:
52
+ confirmation_instructions:
53
+ subject: 'Confirmation instructions'
54
+ reset_password_instructions:
55
+ subject: 'Reset password instructions'
56
+ unlock_instructions:
57
+ subject: 'Unlock Instructions'
@@ -0,0 +1,5 @@
1
+ # Sample localization file for English. Add more files in this directory for other locales.
2
+ # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3
+
4
+ en:
5
+ hello: "Hello world"
@@ -0,0 +1,7 @@
1
+ Rails.application.routes.draw do
2
+ devise_for :users
3
+
4
+ mount Devise::Oauth::Engine => "/oauth"
5
+
6
+ resources :protected_resources
7
+ end
@@ -0,0 +1,46 @@
1
+ class DeviseCreateUsers < ActiveRecord::Migration
2
+ def change
3
+ create_table(:users) do |t|
4
+ ## Database authenticatable
5
+ t.string :email, :null => false, :default => ""
6
+ t.string :encrypted_password, :null => false, :default => ""
7
+
8
+ ## Recoverable
9
+ t.string :reset_password_token
10
+ t.datetime :reset_password_sent_at
11
+
12
+ ## Rememberable
13
+ t.datetime :remember_created_at
14
+
15
+ ## Trackable
16
+ t.integer :sign_in_count, :default => 0
17
+ t.datetime :current_sign_in_at
18
+ t.datetime :last_sign_in_at
19
+ t.string :current_sign_in_ip
20
+ t.string :last_sign_in_ip
21
+
22
+ ## Confirmable
23
+ # t.string :confirmation_token
24
+ # t.datetime :confirmed_at
25
+ # t.datetime :confirmation_sent_at
26
+ # t.string :unconfirmed_email # Only if using reconfirmable
27
+
28
+ ## Lockable
29
+ # t.integer :failed_attempts, :default => 0 # Only if lock strategy is :failed_attempts
30
+ # t.string :unlock_token # Only if unlock strategy is :email or :both
31
+ # t.datetime :locked_at
32
+
33
+ ## Token authenticatable
34
+ # t.string :authentication_token
35
+
36
+
37
+ t.timestamps
38
+ end
39
+
40
+ add_index :users, :email, :unique => true
41
+ add_index :users, :reset_password_token, :unique => true
42
+ # add_index :users, :confirmation_token, :unique => true
43
+ # add_index :users, :unlock_token, :unique => true
44
+ # add_index :users, :authentication_token, :unique => true
45
+ end
46
+ end
@@ -0,0 +1,34 @@
1
+ # encoding: UTF-8
2
+ # This file is auto-generated from the current state of the database. Instead
3
+ # of editing this file, please use the migrations feature of Active Record to
4
+ # incrementally modify your database, and then regenerate this schema definition.
5
+ #
6
+ # Note that this schema.rb definition is the authoritative source for your
7
+ # database schema. If you need to create the application database on another
8
+ # system, you should be using db:schema:load, not running all the migrations
9
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
11
+ #
12
+ # It's strongly recommended to check this file into your version control system.
13
+
14
+ ActiveRecord::Schema.define(:version => 20120622183848) do
15
+
16
+ create_table "users", :force => true do |t|
17
+ t.string "email", :default => "", :null => false
18
+ t.string "encrypted_password", :default => "", :null => false
19
+ t.string "reset_password_token"
20
+ t.datetime "reset_password_sent_at"
21
+ t.datetime "remember_created_at"
22
+ t.integer "sign_in_count", :default => 0
23
+ t.datetime "current_sign_in_at"
24
+ t.datetime "last_sign_in_at"
25
+ t.string "current_sign_in_ip"
26
+ t.string "last_sign_in_ip"
27
+ t.datetime "created_at", :null => false
28
+ t.datetime "updated_at", :null => false
29
+ end
30
+
31
+ add_index "users", ["email"], :name => "index_users_on_email", :unique => true
32
+ add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
33
+
34
+ end
File without changes
File without changes
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/404.html -->
21
+ <div class="dialog">
22
+ <h1>The page you were looking for doesn't exist.</h1>
23
+ <p>You may have mistyped the address or the page may have moved.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/422.html -->
21
+ <div class="dialog">
22
+ <h1>The change you wanted was rejected.</h1>
23
+ <p>Maybe you tried to change something you didn't have access to.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,25 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/500.html -->
21
+ <div class="dialog">
22
+ <h1>We're sorry, but something went wrong.</h1>
23
+ </div>
24
+ </body>
25
+ </html>
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
data/spec/factories.rb ADDED
@@ -0,0 +1,34 @@
1
+ FactoryGirl.define do
2
+ factory :user do
3
+ email "alice@example.com"
4
+ password "example"
5
+ end
6
+
7
+ factory :client_owner, parent: :user do
8
+ email "bob@example.com"
9
+ password "example"
10
+ end
11
+
12
+ factory :client, class: Devise::Oauth::Client do
13
+ name "super client"
14
+ site_uri "http://localhost"
15
+ redirect_uris ["http://localhost:3000/callback"]
16
+ association :owner, factory: :client_owner
17
+ end
18
+
19
+ factory :access_token, class: Devise::Oauth::AccessToken do
20
+ association :client, factory: :client
21
+ association :resource_owner, factory: :user
22
+ end
23
+
24
+ factory :authorization, class: Devise::Oauth::Authorization do
25
+ association :client, factory: :client
26
+ association :resource_owner, factory: :user
27
+ redirect_uri "http://localhost:3000/callback"
28
+ end
29
+
30
+ factory :access, class: Devise::Oauth::Access do
31
+ association :client, factory: :client
32
+ association :resource_owner, factory: :user
33
+ end
34
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe Devise::Oauth::AccessToken do
4
+ before { @token = create(:access_token) }
5
+ subject { @token }
6
+
7
+ it { should validate_presence_of(:client_id) }
8
+ it { should validate_presence_of(:resource_owner_id) }
9
+
10
+ it { should belong_to(:client) }
11
+ it { should belong_to(:resource_owner) }
12
+
13
+ it { should have_db_index(:value).unique(true) }
14
+ it { should have_db_index(:refresh_token).unique(true) }
15
+ it { should have_db_index(:client_id).unique(false) }
16
+
17
+ its(:value) { should_not be_nil }
18
+ its(:refresh_token) { should_not be_nil }
19
+ its(:expires_at) { should_not be_nil }
20
+ it { should_not be_blocked }
21
+
22
+ it "refreshes value" do
23
+ expect{ subject.refresh! }.to change { subject.value }
24
+ end
25
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Devise::Oauth::Authorization do
4
+ before { @auth = create(:authorization) }
5
+ subject { @auth }
6
+
7
+ it { should validate_presence_of(:client_id) }
8
+ it { should validate_presence_of(:resource_owner_id) }
9
+
10
+ it { should belong_to(:client) }
11
+ it { should belong_to(:resource_owner) }
12
+
13
+ it { should have_db_index(:code).unique(true) }
14
+ it { should have_db_index(:client_id).unique(false) }
15
+
16
+ its(:code) { should_not be_nil }
17
+ its(:expires_at) { should_not be_nil }
18
+ it { should_not be_blocked }
19
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Devise::Oauth::Client do
4
+ before { @client = create(:client) }
5
+ subject { @client }
6
+
7
+ it { should validate_presence_of(:name) }
8
+ it { should validate_presence_of(:site_uri) }
9
+
10
+ it { should belong_to(:owner) }
11
+
12
+ it { should have_many(:access_tokens) }
13
+ it { should have_many(:authorizations) }
14
+
15
+ it { should have_db_index(:identifier).unique(true) }
16
+ it { should have_db_index(:secret).unique(true) }
17
+ it { should have_db_index(:owner_id).unique(false) }
18
+
19
+ its(:identifier) { should_not be_nil }
20
+ its(:secret) { should_not be_nil }
21
+
22
+ it { should_not be_blocked }
23
+
24
+ it ".granted!" do
25
+ lambda{ subject.granted! }.should change{ subject.reload; subject.granted_times }.by(1)
26
+ end
27
+
28
+ it ".revoked!" do
29
+ lambda{ subject.revoked! }.should change{ subject.reload; subject.revoked_times }.by(1)
30
+ end
31
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe User do
4
+
5
+ it { should have_many(:oauth_clients) }
6
+ it { should have_many(:oauth_access_tokens) }
7
+ it { should have_many(:oauth_authorizations) }
8
+ it { should have_many(:oauth_accesses) }
9
+
10
+ end
@@ -0,0 +1,39 @@
1
+ ENV["RAILS_ENV"] ||= 'test'
2
+ require File.expand_path("../dummy/config/environment", __FILE__)
3
+ require 'rspec/rails'
4
+ require 'devise/test_helpers'
5
+
6
+
7
+ ENGINE_RAILS_ROOT=File.join(File.dirname(__FILE__), '../')
8
+ # Requires supporting ruby files with custom matchers and macros, etc,
9
+ # in spec/support/ and its subdirectories.
10
+ Dir[("#{ENGINE_RAILS_ROOT}/spec/support/**/*.rb")].each {|f| require f}
11
+
12
+ RSpec.configure do |config|
13
+ config.treat_symbols_as_metadata_keys_with_true_values = true
14
+ config.run_all_when_everything_filtered = true
15
+ config.filter_run :focus
16
+
17
+ config.mock_with :rspec
18
+ config.include FactoryGirl::Syntax::Methods
19
+ config.include Devise::TestHelpers, :type => :controller
20
+
21
+ config.before(:suite) do
22
+ DatabaseCleaner.strategy = :transaction
23
+ DatabaseCleaner.clean_with(:truncation)
24
+ end
25
+
26
+ config.before(:each) do
27
+ DatabaseCleaner.start
28
+ end
29
+
30
+ config.after(:each) do
31
+ DatabaseCleaner.clean
32
+ end
33
+ end
34
+
35
+ ["../../db/migrate", "../dummy/db/migrate"].each do |migration_path|
36
+ ActiveRecord::Migrator.migrate(File.expand_path(migration_path, __FILE__))
37
+ end
38
+
39
+ require 'factories'