insrc_devise_oauth2_providable 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. data/.gitignore +35 -0
  2. data/.rvmrc +1 -0
  3. data/CONTRIBUTORS.txt +6 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +158 -0
  7. data/Rakefile +8 -0
  8. data/app/controllers/devise/oauth2_providable/authorizations_controller.rb +59 -0
  9. data/app/controllers/devise/oauth2_providable/tokens_controller.rb +17 -0
  10. data/app/models/devise/oauth2_providable/access_token.rb +24 -0
  11. data/app/models/devise/oauth2_providable/authorization_code.rb +3 -0
  12. data/app/models/devise/oauth2_providable/client.rb +24 -0
  13. data/app/models/devise/oauth2_providable/refresh_token.rb +8 -0
  14. data/app/views/devise/oauth2_providable/authorizations/_form.html.erb +7 -0
  15. data/app/views/devise/oauth2_providable/authorizations/error.html.erb +4 -0
  16. data/app/views/devise/oauth2_providable/authorizations/new.html.erb +4 -0
  17. data/config/routes.rb +7 -0
  18. data/db/migrate/20111014160714_create_devise_oauth2_providable_schema.rb +54 -0
  19. data/devise_oauth2_providable.gemspec +32 -0
  20. data/lib/devise/oauth2_providable/engine.rb +16 -0
  21. data/lib/devise/oauth2_providable/expirable_token.rb +58 -0
  22. data/lib/devise/oauth2_providable/models/oauth2_authorization_code_grantable.rb +6 -0
  23. data/lib/devise/oauth2_providable/models/oauth2_password_grantable.rb +6 -0
  24. data/lib/devise/oauth2_providable/models/oauth2_providable.rb +13 -0
  25. data/lib/devise/oauth2_providable/models/oauth2_refresh_token_grantable.rb +6 -0
  26. data/lib/devise/oauth2_providable/strategies/oauth2_authorization_code_grant_type_strategy.rb +21 -0
  27. data/lib/devise/oauth2_providable/strategies/oauth2_grant_type_strategy.rb +39 -0
  28. data/lib/devise/oauth2_providable/strategies/oauth2_password_grant_type_strategy.rb +22 -0
  29. data/lib/devise/oauth2_providable/strategies/oauth2_providable_strategy.rb +25 -0
  30. data/lib/devise/oauth2_providable/strategies/oauth2_refresh_token_grant_type_strategy.rb +22 -0
  31. data/lib/devise/oauth2_providable/version.rb +5 -0
  32. data/lib/devise_oauth2_providable.rb +41 -0
  33. data/script/rails +6 -0
  34. data/spec/controllers/authorizations_controller_spec.rb +32 -0
  35. data/spec/controllers/protected_controller_spec.rb +42 -0
  36. data/spec/dummy/Rakefile +7 -0
  37. data/spec/dummy/app/assets/javascripts/application.js +7 -0
  38. data/spec/dummy/app/assets/stylesheets/application.css +7 -0
  39. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  40. data/spec/dummy/app/controllers/protected_controller.rb +6 -0
  41. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  42. data/spec/dummy/app/mailers/.gitkeep +0 -0
  43. data/spec/dummy/app/models/.gitkeep +0 -0
  44. data/spec/dummy/app/models/user.rb +3 -0
  45. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  46. data/spec/dummy/config.ru +4 -0
  47. data/spec/dummy/config/application.rb +51 -0
  48. data/spec/dummy/config/boot.rb +10 -0
  49. data/spec/dummy/config/database.yml +25 -0
  50. data/spec/dummy/config/environment.rb +5 -0
  51. data/spec/dummy/config/environments/development.rb +30 -0
  52. data/spec/dummy/config/environments/production.rb +60 -0
  53. data/spec/dummy/config/environments/test.rb +39 -0
  54. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  55. data/spec/dummy/config/initializers/devise.rb +210 -0
  56. data/spec/dummy/config/initializers/inflections.rb +10 -0
  57. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  58. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  59. data/spec/dummy/config/initializers/session_store.rb +8 -0
  60. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  61. data/spec/dummy/config/locales/devise.en.yml +58 -0
  62. data/spec/dummy/config/locales/en.yml +5 -0
  63. data/spec/dummy/config/routes.rb +7 -0
  64. data/spec/dummy/db/migrate/20111014142838_create_users.rb +9 -0
  65. data/spec/dummy/db/migrate/20111014161437_create_devise_oauth2_providable_schema.rb +55 -0
  66. data/spec/dummy/db/schema.rb +78 -0
  67. data/spec/dummy/lib/assets/.gitkeep +0 -0
  68. data/spec/dummy/public/404.html +26 -0
  69. data/spec/dummy/public/422.html +26 -0
  70. data/spec/dummy/public/500.html +26 -0
  71. data/spec/dummy/public/favicon.ico +0 -0
  72. data/spec/dummy/script/rails +6 -0
  73. data/spec/factories/client_factory.rb +5 -0
  74. data/spec/factories/user_factory.rb +4 -0
  75. data/spec/integration/oauth2_authorization_token_grant_type_strategy_spec.rb +136 -0
  76. data/spec/integration/oauth2_password_grant_type_strategy_spec.rb +174 -0
  77. data/spec/integration/oauth2_refresh_token_grant_type_strategy_spec.rb +138 -0
  78. data/spec/lib/devise_oauth2_providable_spec.rb +7 -0
  79. data/spec/models/access_token_spec.rb +53 -0
  80. data/spec/models/authorization_code_spec.rb +23 -0
  81. data/spec/models/client_spec.rb +22 -0
  82. data/spec/models/refresh_token_spec.rb +26 -0
  83. data/spec/models/user_spec.rb +6 -0
  84. data/spec/routing/authorizations_routing_spec.rb +16 -0
  85. data/spec/routing/tokens_routing_spec.rb +9 -0
  86. data/spec/spec_helper.rb +29 -0
  87. data/spec/support/inject_engine_routes_into_application.rb +74 -0
  88. data/spec/support/match_json.rb +6 -0
  89. metadata +374 -0
@@ -0,0 +1,10 @@
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
@@ -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 = 'ede0a0440c0b53d6589668e54cf525f27305242a2b32b5dbbfc9e50dd7cb7af8da2b7d7c386b7d675283c0ecc4bb522ab4cc5b53edee8ed60f7482d4c22d0e22'
@@ -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,58 @@
1
+ # Additional translations at http://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 e-mail exists on our database, you will receive a password recovery link on your e-mail"
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 e-mail exists on 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
+ inactive_signed_up: 'You have signed up successfully. However, we could not sign you in because your account is %{reason}.'
39
+ updated: 'You updated your account successfully.'
40
+ destroyed: 'Bye! Your account was successfully cancelled. We hope to see you again soon.'
41
+ reasons:
42
+ inactive: 'inactive'
43
+ unconfirmed: 'unconfirmed'
44
+ locked: 'locked'
45
+ unlocks:
46
+ send_instructions: 'You will receive an email with instructions about how to unlock your account in a few minutes.'
47
+ unlocked: 'Your account was successfully unlocked. You are now signed in.'
48
+ send_paranoid_instructions: 'If your account exists, you will receive an email with instructions about how to unlock it in a few minutes.'
49
+ omniauth_callbacks:
50
+ success: 'Successfully authorized from %{kind} account.'
51
+ failure: 'Could not authorize you from %{kind} because "%{reason}".'
52
+ mailer:
53
+ confirmation_instructions:
54
+ subject: 'Confirmation instructions'
55
+ reset_password_instructions:
56
+ subject: 'Reset password instructions'
57
+ unlock_instructions:
58
+ 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
+ resources :protected
5
+
6
+ mount Devise::Oauth2Providable::Engine => '/oauth2'
7
+ end
@@ -0,0 +1,9 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+ def change
3
+ create_table :users do |t|
4
+ t.string :email
5
+ t.string :encrypted_password
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,55 @@
1
+ class CreateDeviseOauth2ProvidableSchema < ActiveRecord::Migration
2
+ def change
3
+ create_table :oauth2_clients do |t|
4
+ t.string :name
5
+ t.string :redirect_uri
6
+ t.string :website
7
+ t.string :identifier
8
+ t.string :secret
9
+ t.timestamps
10
+ end
11
+ change_table :oauth2_clients do |t|
12
+ t.index :identifier, :unique => true
13
+ end
14
+
15
+ create_table :oauth2_access_tokens do |t|
16
+ t.belongs_to :user, :client, :refresh_token
17
+ t.string :token
18
+ t.datetime :expires_at
19
+ t.timestamps
20
+ end
21
+ change_table :oauth2_access_tokens do |t|
22
+ t.index :token, :unique => true
23
+ t.index :expires_at
24
+ t.index :user_id
25
+ t.index :client_id
26
+ end
27
+
28
+ create_table :oauth2_refresh_tokens do |t|
29
+ t.belongs_to :user, :client
30
+ t.string :token
31
+ t.datetime :expires_at
32
+ t.timestamps
33
+ end
34
+ change_table :oauth2_refresh_tokens do |t|
35
+ t.index :token, :unique => true
36
+ t.index :expires_at
37
+ t.index :user_id
38
+ t.index :client_id
39
+ end
40
+
41
+ create_table :oauth2_authorization_codes do |t|
42
+ t.belongs_to :user, :client
43
+ t.string :token
44
+ t.datetime :expires_at
45
+ t.string :redirect_uri
46
+ t.timestamps
47
+ end
48
+ change_table :oauth2_authorization_codes do |t|
49
+ t.index :token, :unique => true
50
+ t.index :expires_at
51
+ t.index :user_id
52
+ t.index :client_id
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,78 @@
1
+ # This file is auto-generated from the current state of the database. Instead
2
+ # of editing this file, please use the migrations feature of Active Record to
3
+ # incrementally modify your database, and then regenerate this schema definition.
4
+ #
5
+ # Note that this schema.rb definition is the authoritative source for your
6
+ # database schema. If you need to create the application database on another
7
+ # system, you should be using db:schema:load, not running all the migrations
8
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
9
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
10
+ #
11
+ # It's strongly recommended to check this file into your version control system.
12
+
13
+ ActiveRecord::Schema.define(:version => 20111014161437) do
14
+
15
+ create_table "oauth2_access_tokens", :force => true do |t|
16
+ t.integer "user_id"
17
+ t.integer "client_id"
18
+ t.integer "refresh_token_id"
19
+ t.string "token"
20
+ t.datetime "expires_at"
21
+ t.datetime "created_at"
22
+ t.datetime "updated_at"
23
+ end
24
+
25
+ add_index "oauth2_access_tokens", ["client_id"], :name => "index_oauth2_access_tokens_on_client_id"
26
+ add_index "oauth2_access_tokens", ["expires_at"], :name => "index_oauth2_access_tokens_on_expires_at"
27
+ add_index "oauth2_access_tokens", ["token"], :name => "index_oauth2_access_tokens_on_token", :unique => true
28
+ add_index "oauth2_access_tokens", ["user_id"], :name => "index_oauth2_access_tokens_on_user_id"
29
+
30
+ create_table "oauth2_authorization_codes", :force => true do |t|
31
+ t.integer "user_id"
32
+ t.integer "client_id"
33
+ t.string "token"
34
+ t.datetime "expires_at"
35
+ t.string "redirect_uri"
36
+ t.datetime "created_at"
37
+ t.datetime "updated_at"
38
+ end
39
+
40
+ add_index "oauth2_authorization_codes", ["client_id"], :name => "index_oauth2_authorization_codes_on_client_id"
41
+ add_index "oauth2_authorization_codes", ["expires_at"], :name => "index_oauth2_authorization_codes_on_expires_at"
42
+ add_index "oauth2_authorization_codes", ["token"], :name => "index_oauth2_authorization_codes_on_token", :unique => true
43
+ add_index "oauth2_authorization_codes", ["user_id"], :name => "index_oauth2_authorization_codes_on_user_id"
44
+
45
+ create_table "oauth2_clients", :force => true do |t|
46
+ t.string "name"
47
+ t.string "redirect_uri"
48
+ t.string "website"
49
+ t.string "identifier"
50
+ t.string "secret"
51
+ t.datetime "created_at"
52
+ t.datetime "updated_at"
53
+ end
54
+
55
+ add_index "oauth2_clients", ["identifier"], :name => "index_oauth2_clients_on_identifier", :unique => true
56
+
57
+ create_table "oauth2_refresh_tokens", :force => true do |t|
58
+ t.integer "user_id"
59
+ t.integer "client_id"
60
+ t.string "token"
61
+ t.datetime "expires_at"
62
+ t.datetime "created_at"
63
+ t.datetime "updated_at"
64
+ end
65
+
66
+ add_index "oauth2_refresh_tokens", ["client_id"], :name => "index_oauth2_refresh_tokens_on_client_id"
67
+ add_index "oauth2_refresh_tokens", ["expires_at"], :name => "index_oauth2_refresh_tokens_on_expires_at"
68
+ add_index "oauth2_refresh_tokens", ["token"], :name => "index_oauth2_refresh_tokens_on_token", :unique => true
69
+ add_index "oauth2_refresh_tokens", ["user_id"], :name => "index_oauth2_refresh_tokens_on_user_id"
70
+
71
+ create_table "users", :force => true do |t|
72
+ t.string "email", :default => "", :null => false
73
+ t.string "encrypted_password", :limit => 128, :default => "", :null => false
74
+ t.datetime "created_at"
75
+ t.datetime "updated_at"
76
+ end
77
+
78
+ end
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,26 @@
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
+ <p>We've been notified about this issue and we'll take a look at it shortly.</p>
24
+ </div>
25
+ </body>
26
+ </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'
@@ -0,0 +1,5 @@
1
+ Factory.define :client, :class => 'Devise::Oauth2Providable::Client' do |f|
2
+ f.name 'test'
3
+ f.website 'http://localhost'
4
+ f.redirect_uri 'http://localhost:3000'
5
+ end
@@ -0,0 +1,4 @@
1
+ Factory.define :user do |f|
2
+ f.email 'ryan@socialcast.com'
3
+ f.password 'test'
4
+ end
@@ -0,0 +1,136 @@
1
+ require 'spec_helper'
2
+
3
+ describe Devise::Strategies::Oauth2AuthorizationCodeGrantTypeStrategy do
4
+ describe 'POST /oauth2/token' do
5
+ describe 'with grant_type=authorization_code' do
6
+ context 'with valid params' do
7
+ with :client
8
+ with :user
9
+ before do
10
+ @authorization_code = user.authorization_codes.create!(:client => client, :redirect_uri => client.redirect_uri)
11
+ params = {
12
+ :grant_type => 'authorization_code',
13
+ :client_id => client.identifier,
14
+ :client_secret => client.secret,
15
+ :code => @authorization_code.token
16
+ }
17
+
18
+ post '/oauth2/token', params
19
+ end
20
+ it { response.code.to_i.should == 200 }
21
+ it { response.content_type.should == 'application/json' }
22
+ it 'returns json' do
23
+ token = Devise::Oauth2Providable::AccessToken.last
24
+ refresh_token = Devise::Oauth2Providable::RefreshToken.last
25
+ expected = {
26
+ :token_type => 'bearer',
27
+ :expires_in => 899,
28
+ :refresh_token => refresh_token.token,
29
+ :access_token => token.token
30
+ }
31
+ response.body.should match_json(expected)
32
+ end
33
+ end
34
+ context 'with expired authorization_code' do
35
+ with :client
36
+ with :user
37
+ before do
38
+ timenow = 2.days.from_now
39
+ Time.stub!(:now).and_return(timenow)
40
+ @authorization_code = user.authorization_codes.create(:client_id => client, :redirect_uri => client.redirect_uri)
41
+ params = {
42
+ :grant_type => 'authorization_code',
43
+ :client_id => client.identifier,
44
+ :client_secret => client.secret,
45
+ :code => @authorization_code.token
46
+ }
47
+ Time.stub!(:now).and_return(timenow + 10.minutes)
48
+
49
+ post '/oauth2/token', params
50
+ end
51
+ it { response.code.to_i.should == 400 }
52
+ it { response.content_type.should == 'application/json' }
53
+ it 'returns json' do
54
+ expected = {
55
+ :error => 'invalid_grant',
56
+ :error_description => 'invalid authorization code request'
57
+ }
58
+ response.body.should match_json(expected)
59
+ end
60
+ end
61
+ context 'with invalid authorization_code' do
62
+ with :client
63
+ with :user
64
+ before do
65
+ @authorization_code = user.authorization_codes.create(:client_id => client, :redirect_uri => client.redirect_uri)
66
+ params = {
67
+ :grant_type => 'authorization_code',
68
+ :client_id => client.identifier,
69
+ :client_secret => client.secret,
70
+ :code => 'invalid'
71
+ }
72
+
73
+ post '/oauth2/token', params
74
+ end
75
+ it { response.code.to_i.should == 400 }
76
+ it { response.content_type.should == 'application/json' }
77
+ it 'returns json' do
78
+ expected = {
79
+ :error => 'invalid_grant',
80
+ :error_description => 'invalid authorization code request'
81
+ }
82
+ response.body.should match_json(expected)
83
+ end
84
+ end
85
+ context 'with invalid client_secret' do
86
+ with :user
87
+ with :client
88
+ before do
89
+ @authorization_code = user.authorization_codes.create(:client_id => client, :redirect_uri => client.redirect_uri)
90
+ params = {
91
+ :grant_type => 'authorization_code',
92
+ :client_id => client.identifier,
93
+ :client_secret => 'invalid',
94
+ :code => @authorization_code.token
95
+ }
96
+
97
+ post '/oauth2/token', params
98
+ end
99
+ it { response.code.to_i.should == 400 }
100
+ it { response.content_type.should == 'application/json' }
101
+ it 'returns json' do
102
+ expected = {
103
+ :error => 'invalid_client',
104
+ :error_description => 'invalid client credentials'
105
+ }
106
+ response.body.should match_json(expected)
107
+ end
108
+ end
109
+ context 'with invalid client_id' do
110
+ with :user
111
+ with :client
112
+ before do
113
+ @authorization_code = user.authorization_codes.create(:client_id => client, :redirect_uri => client.redirect_uri)
114
+ params = {
115
+ :grant_type => 'authorization_code',
116
+ :client_id => 'invalid',
117
+ :client_secret => client.secret,
118
+ :code => @authorization_code.token
119
+ }
120
+
121
+ post '/oauth2/token', params
122
+ end
123
+ it { response.code.to_i.should == 400 }
124
+ it { response.content_type.should == 'application/json' }
125
+ it 'returns json' do
126
+ expected = {
127
+ :error => 'invalid_client',
128
+ :error_description => 'invalid client credentials'
129
+ }
130
+ response.body.should match_json(expected)
131
+ end
132
+ end
133
+ end
134
+ end
135
+ end
136
+