brycesch-devise_oauth2_providable 1.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +35 -0
  3. data/.ruby_gemset +1 -0
  4. data/.ruby_version +1 -0
  5. data/CONTRIBUTORS.txt +6 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +160 -0
  9. data/Rakefile +8 -0
  10. data/app/controllers/devise/oauth2_providable/authorizations_controller.rb +59 -0
  11. data/app/controllers/devise/oauth2_providable/tokens_controller.rb +35 -0
  12. data/app/models/devise/oauth2_providable/access_token.rb +25 -0
  13. data/app/models/devise/oauth2_providable/authorization_code.rb +3 -0
  14. data/app/models/devise/oauth2_providable/client.rb +26 -0
  15. data/app/models/devise/oauth2_providable/refresh_token.rb +6 -0
  16. data/app/views/devise/oauth2_providable/authorizations/_form.html.erb +7 -0
  17. data/app/views/devise/oauth2_providable/authorizations/error.html.erb +4 -0
  18. data/app/views/devise/oauth2_providable/authorizations/new.html.erb +4 -0
  19. data/config/routes.rb +6 -0
  20. data/db/migrate/20111014160714_create_devise_oauth2_providable_schema.rb +58 -0
  21. data/devise_oauth2_providable.gemspec +33 -0
  22. data/lib/devise/oauth2_providable/engine.rb +16 -0
  23. data/lib/devise/oauth2_providable/expirable_token.rb +56 -0
  24. data/lib/devise/oauth2_providable/models/oauth2_authorization_code_grantable.rb +6 -0
  25. data/lib/devise/oauth2_providable/models/oauth2_password_grantable.rb +6 -0
  26. data/lib/devise/oauth2_providable/models/oauth2_providable.rb +13 -0
  27. data/lib/devise/oauth2_providable/models/oauth2_refresh_token_grantable.rb +6 -0
  28. data/lib/devise/oauth2_providable/strategies/oauth2_authorization_code_grant_type_strategy.rb +21 -0
  29. data/lib/devise/oauth2_providable/strategies/oauth2_grant_type_strategy.rb +44 -0
  30. data/lib/devise/oauth2_providable/strategies/oauth2_password_grant_type_strategy.rb +22 -0
  31. data/lib/devise/oauth2_providable/strategies/oauth2_providable_strategy.rb +31 -0
  32. data/lib/devise/oauth2_providable/strategies/oauth2_refresh_token_grant_type_strategy.rb +22 -0
  33. data/lib/devise/oauth2_providable/version.rb +5 -0
  34. data/lib/devise_oauth2_providable.rb +41 -0
  35. data/script/rails +6 -0
  36. data/spec/controllers/authorizations_controller_spec.rb +32 -0
  37. data/spec/controllers/protected_controller_spec.rb +43 -0
  38. data/spec/controllers/tokens_controller_spec.rb +50 -0
  39. data/spec/dummy/Rakefile +7 -0
  40. data/spec/dummy/app/assets/javascripts/application.js +7 -0
  41. data/spec/dummy/app/assets/stylesheets/application.css +7 -0
  42. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  43. data/spec/dummy/app/controllers/protected_controller.rb +7 -0
  44. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  45. data/spec/dummy/app/mailers/.gitkeep +0 -0
  46. data/spec/dummy/app/models/.gitkeep +0 -0
  47. data/spec/dummy/app/models/user.rb +3 -0
  48. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  49. data/spec/dummy/config.ru +4 -0
  50. data/spec/dummy/config/application.rb +51 -0
  51. data/spec/dummy/config/boot.rb +10 -0
  52. data/spec/dummy/config/database.yml +25 -0
  53. data/spec/dummy/config/environment.rb +5 -0
  54. data/spec/dummy/config/environments/development.rb +32 -0
  55. data/spec/dummy/config/environments/production.rb +62 -0
  56. data/spec/dummy/config/environments/test.rb +42 -0
  57. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  58. data/spec/dummy/config/initializers/devise.rb +259 -0
  59. data/spec/dummy/config/initializers/inflections.rb +10 -0
  60. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  61. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  62. data/spec/dummy/config/initializers/session_store.rb +8 -0
  63. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  64. data/spec/dummy/config/locales/devise.en.yml +58 -0
  65. data/spec/dummy/config/locales/en.yml +5 -0
  66. data/spec/dummy/config/routes.rb +7 -0
  67. data/spec/dummy/db/migrate/20111014142838_create_users.rb +9 -0
  68. data/spec/dummy/db/migrate/20111014161437_create_devise_oauth2_providable_schema.rb +55 -0
  69. data/spec/dummy/db/schema.rb +78 -0
  70. data/spec/dummy/lib/assets/.gitkeep +0 -0
  71. data/spec/dummy/public/404.html +26 -0
  72. data/spec/dummy/public/422.html +26 -0
  73. data/spec/dummy/public/500.html +26 -0
  74. data/spec/dummy/public/favicon.ico +0 -0
  75. data/spec/dummy/script/rails +6 -0
  76. data/spec/factories/client_factory.rb +7 -0
  77. data/spec/factories/user_factory.rb +6 -0
  78. data/spec/integration/oauth2_authorization_token_grant_type_strategy_spec.rb +137 -0
  79. data/spec/integration/oauth2_password_grant_type_strategy_spec.rb +174 -0
  80. data/spec/integration/oauth2_refresh_token_grant_type_strategy_spec.rb +138 -0
  81. data/spec/lib/devise_oauth2_providable_spec.rb +7 -0
  82. data/spec/models/access_token_spec.rb +50 -0
  83. data/spec/models/authorization_code_spec.rb +21 -0
  84. data/spec/models/client_spec.rb +17 -0
  85. data/spec/models/refresh_token_spec.rb +23 -0
  86. data/spec/models/user_spec.rb +6 -0
  87. data/spec/routing/authorizations_routing_spec.rb +16 -0
  88. data/spec/routing/tokens_routing_spec.rb +9 -0
  89. data/spec/spec_helper.rb +33 -0
  90. data/spec/support/match_json.rb +6 -0
  91. metadata +330 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 89087ce6dfc7e93ca6f2dd4188c32ddb3b088c75
4
+ data.tar.gz: 21f7d31bfc441e0eb30ad55fec67f7c952a3448c
5
+ SHA512:
6
+ metadata.gz: 970b338d028b599b04666f7818febca2b0e7ed1feefc7bf165f2a2bf6eb6fe43d0607b1a095cec6fe3c2c2ca4aea27a6e5a0bb5957ca1a4a5d52c9ab26c0eb12
7
+ data.tar.gz: c2975a80ad4c94df699f70957dc7d5172a5e88623a739a43b91dec70af7941ca577b83f04d038121bf5b0962c044b4850b5f6c5009a3ac425c72d1983d64662a
data/.gitignore ADDED
@@ -0,0 +1,35 @@
1
+ # rcov generated
2
+ coverage
3
+
4
+ # rdoc generated
5
+ rdoc
6
+
7
+ # yard generated
8
+ doc
9
+ .yardoc
10
+
11
+ # bundler
12
+ .bundle
13
+ Gemfile.lock
14
+
15
+ # jeweler generated
16
+ pkg
17
+
18
+ # test files
19
+ test/*.log
20
+ test/*.sqlite3
21
+
22
+ # For vim:
23
+ *.swp
24
+
25
+ # For MacOS:
26
+ .DS_Store
27
+
28
+ # git files
29
+ *.orig
30
+
31
+ # rails files
32
+ tmp
33
+ log
34
+ *.log
35
+ *.sqlite3
data/.ruby_gemset ADDED
@@ -0,0 +1 @@
1
+ devise_oauth2_providable
data/.ruby_version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.1
data/CONTRIBUTORS.txt ADDED
@@ -0,0 +1,6 @@
1
+ Ryan Sonnek - Original Author
2
+
3
+
4
+ Complete list of contributors:
5
+ https://github.com/socialcast/devise_oauth2_providable/contributors
6
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in the .gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2011 Socialcast, Inc
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,160 @@
1
+ # THIS GEM IS NO LONGER MAINTAINED AND IS CONSIDERED DEPRECATED.
2
+
3
+ # devise_oauth2_providable
4
+
5
+ Rails3 engine that brings OAuth2 Provider support to your application.
6
+
7
+ Current OAuth2 Specification Draft:
8
+ http://tools.ietf.org/html/draft-ietf-oauth-v2-22
9
+
10
+ ## Features
11
+
12
+ * integrate OAuth2 authentication with Devise authenthentication stack
13
+ * one-stop-shop includes all Models, Controllers and Views to get up and
14
+ running quickly
15
+ * All server requests support authentication via bearer token included in
16
+ the request. http://tools.ietf.org/html/draft-ietf-oauth-v2-bearer-04
17
+ * customizable mount point for oauth2 routes (ex: /oauth2 vs /oauth)
18
+
19
+
20
+ ## Requirements
21
+
22
+ * Devise authentication library
23
+ * Rails 3.1 or higher
24
+
25
+ ## Installation
26
+
27
+ #### Install gem
28
+ ```ruby
29
+ # Gemfile
30
+ gem 'devise_oauth2_providable'
31
+ ```
32
+
33
+ #### Migrate database for Oauth2 models
34
+ ```
35
+ $ rake devise_oauth2_providable:install:migrations
36
+ $ rake db:migrate
37
+ ```
38
+
39
+ #### Add Oauth2 Routes
40
+ ```ruby
41
+ # config/routes.rb
42
+ Rails.application.routes.draw do
43
+ # oauth routes can be mounted to any path (ex: /oauth2 or /oauth)
44
+ mount Devise::Oauth2Providable::Engine => '/oauth2'
45
+ end
46
+ ```
47
+
48
+ #### Configure User for supported Oauth2 flows
49
+ ```ruby
50
+ class User
51
+ # NOTE: include :database_authenticatable configuration
52
+ # if supporting Resource Owner Password Credentials Grant Type
53
+ devise :oauth2_providable,
54
+ :oauth2_password_grantable,
55
+ :oauth2_refresh_token_grantable,
56
+ :oauth2_authorization_code_grantable
57
+ end
58
+ ```
59
+
60
+ #### (optional) Configure token expiration settings
61
+ ```ruby
62
+ # config/application.rb
63
+ config.devise_oauth2_providable.access_token_expires_in = 1.second # 15.minute default
64
+ config.devise_oauth2_providable.refresh_token_expires_in = 1.minute # 1.month default
65
+ config.devise_oauth2_providable.authorization_token_expires_in = 5.seconds # 1.minute default
66
+ ```
67
+
68
+ ## Models
69
+
70
+ ### Client
71
+ registered OAuth2 client for storing the unique client_id and
72
+ client_secret.
73
+
74
+ ### AccessToken
75
+ http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-1.3
76
+
77
+ Short lived token used by clients to perform subsequent requests (see
78
+ bearer token spec)
79
+
80
+ expires after 15min by default. to customize the duration of the access token:
81
+
82
+ ```ruby
83
+ Devise::Oauth2Providable::AccessToken.default_lifetime = 1.minute
84
+ ```
85
+
86
+ ### RefreshToken
87
+ http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-1.5
88
+
89
+ Long lived token used by clients to request new access tokens without
90
+ requiring user intervention to re-authorize.
91
+
92
+ expires after 1 month by default. to customize the duration of refresh token:
93
+
94
+ ```ruby
95
+ Devise::Oauth2Providable::RefreshToken.default_lifetime = 1.year
96
+ ```
97
+
98
+ ### AuthorizationCode
99
+ http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-1.4.1
100
+
101
+ *Very* short lived token created to allow a client to request an access
102
+ token after a user has gone through the authorization flow.
103
+
104
+ expires after 1min by default. to customize the duration of the
105
+ authorization code:
106
+
107
+ ```ruby
108
+ Devise::Oauth2Providable::AuthorizationCode.default_lifetime = 5.minutes
109
+ ```
110
+
111
+ ## Routes
112
+
113
+ ### /oauth2/authorize
114
+ http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-2.1
115
+
116
+ Endpoint to start client authorization flow. Models, controllers and
117
+ views are included for out of the box deployment.
118
+
119
+ Supports the Authorization Code and Implicit grant types.
120
+
121
+ ### /oauth2/token
122
+ http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-2.2
123
+
124
+ Endpoint to request access token. See grant type documentation for
125
+ supported flows.
126
+
127
+ ## Grant Types
128
+
129
+ ### Resource Owner Password Credentials Grant Type
130
+ http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-4.3
131
+
132
+ in order to use the Resource Owner Password Credentials Grant Type, your
133
+ Devise model *must* be configured with the :database_authenticatable option
134
+
135
+ ### Client Credentials Grant Type
136
+ http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-4.4
137
+
138
+ ### Authorization Code Grant Type
139
+ http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-4.1
140
+
141
+ ### Implicit Grant Type
142
+ http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-4.2
143
+
144
+ ### Refresh Token Grant Type
145
+ http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-6
146
+
147
+ ## Contributing
148
+
149
+ * Fork the project
150
+ * Fix the issue
151
+ * Add unit tests
152
+ * Submit pull request on github
153
+
154
+ See CONTRIBUTORS.txt for list of project contributors
155
+
156
+ ## Copyright
157
+
158
+ Copyright (c) 2011 Socialcast, Inc.
159
+ See LICENSE.txt for further details.
160
+
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
4
+ load 'rails/tasks/engine.rake'
5
+
6
+ require 'rspec/core/rake_task'
7
+ RSpec::Core::RakeTask.new('spec')
8
+ task :default => :spec
@@ -0,0 +1,59 @@
1
+ module Devise
2
+ module Oauth2Providable
3
+ class AuthorizationsController < ApplicationController
4
+ before_filter :authenticate_user!
5
+
6
+ rescue_from Rack::OAuth2::Server::Authorize::BadRequest do |e|
7
+ @error = e
8
+ render :error, :status => e.status
9
+ end
10
+
11
+ def new
12
+ respond *authorize_endpoint.call(request.env)
13
+ end
14
+
15
+ def create
16
+ respond *authorize_endpoint(:allow_approval).call(request.env)
17
+ end
18
+
19
+ private
20
+
21
+ def respond(status, header, response)
22
+ ["WWW-Authenticate"].each do |key|
23
+ headers[key] = header[key] if header[key].present?
24
+ end
25
+ if response.redirect?
26
+ redirect_to header['Location']
27
+ else
28
+ render :new
29
+ end
30
+ end
31
+
32
+ def authorize_endpoint(allow_approval = false)
33
+ Rack::OAuth2::Server::Authorize.new do |req, res|
34
+ @client = Client.find_by_identifier(req.client_id) || req.bad_request!
35
+ res.redirect_uri = @redirect_uri = req.verify_redirect_uri!(@client.redirect_uri)
36
+ if allow_approval
37
+ if params[:approve].present?
38
+ case req.response_type
39
+ when :code
40
+ authorization_code = current_user.authorization_codes.create!(:client => @client)
41
+ res.code = authorization_code.token
42
+ when :token
43
+ access_token = current_user.access_tokens.create!(:client => @client).token
44
+ bearer_token = Rack::OAuth2::AccessToken::Bearer.new(:access_token => access_token)
45
+ res.access_token = bearer_token
46
+ res.uid = current_user.id
47
+ end
48
+ res.approve!
49
+ else
50
+ req.access_denied!
51
+ end
52
+ else
53
+ @response_type = req.response_type
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,35 @@
1
+ class Devise::Oauth2Providable::TokensController < ApplicationController
2
+ before_filter :reset_session!
3
+ before_filter :authenticate_user!
4
+ skip_before_filter :verify_authenticity_token, :only => :create
5
+
6
+ rescue_from Rack::OAuth2::Server::Authorize::BadRequest do |e|
7
+ head :bad_request
8
+ end
9
+
10
+ def create
11
+ @refresh_token = oauth2_current_refresh_token || oauth2_current_client.refresh_tokens.create!(:user => current_user)
12
+ @access_token = @refresh_token.access_tokens.create!(:client => oauth2_current_client, :user => current_user)
13
+ render :json => @access_token.token_response
14
+ end
15
+
16
+ def destroy
17
+ raise Rack::OAuth2::Server::Authorize::BadRequest unless current_user && oauth2_current_client
18
+ oauth2_current_client.expire_tokens_for_user(current_user)
19
+ head :no_content
20
+ end
21
+
22
+ private
23
+
24
+ def oauth2_current_client
25
+ request.headers.env[Devise::Oauth2Providable::CLIENT_ENV_REF]
26
+ end
27
+
28
+ def oauth2_current_refresh_token
29
+ request.headers.env[Devise::Oauth2Providable::REFRESH_TOKEN_ENV_REF]
30
+ end
31
+
32
+ def reset_session!
33
+ request.reset_session
34
+ end
35
+ end
@@ -0,0 +1,25 @@
1
+ class Devise::Oauth2Providable::AccessToken < ActiveRecord::Base
2
+ expires_according_to :access_token_expires_in
3
+
4
+ before_validation :restrict_expires_at, :on => :create, :if => :refresh_token
5
+ belongs_to :refresh_token
6
+
7
+ scope :unexpired, -> { where('expires_at > ?', Time.zone.now) }
8
+ scope :user_id, ->(user_id){ where(user_id: user_id) }
9
+
10
+ def token_response
11
+ response = {
12
+ :access_token => token,
13
+ :token_type => 'bearer',
14
+ :expires_in => expires_in
15
+ }
16
+ response[:refresh_token] = refresh_token.token if refresh_token
17
+ response
18
+ end
19
+
20
+ private
21
+
22
+ def restrict_expires_at
23
+ self.expires_at = [self.expires_at, refresh_token.expires_at].compact.min
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ class Devise::Oauth2Providable::AuthorizationCode < ActiveRecord::Base
2
+ expires_according_to :authorization_code_expires_in
3
+ end
@@ -0,0 +1,26 @@
1
+ class Devise::Oauth2Providable::Client < ActiveRecord::Base
2
+ has_many :access_tokens
3
+ has_many :refresh_tokens
4
+ has_many :authorization_codes
5
+
6
+ before_validation :init_identifier, :on => :create, :unless => :identifier?
7
+ before_validation :init_secret, :on => :create, :unless => :secret?
8
+ validates :website, :secret, :presence => true
9
+ validates :name, :presence => true, :uniqueness => true
10
+ validates :identifier, :presence => true, :uniqueness => true
11
+
12
+ def expire_tokens_for_user(user)
13
+ access_tokens.unexpired.user_id(user.id).update_all(expires_at: Time.zone.now)
14
+ end
15
+
16
+ private
17
+
18
+ def init_identifier
19
+ self.identifier = Devise::Oauth2Providable.random_id
20
+ end
21
+
22
+ def init_secret
23
+ self.secret = Devise::Oauth2Providable.random_id
24
+ end
25
+
26
+ end
@@ -0,0 +1,6 @@
1
+ class Devise::Oauth2Providable::RefreshToken < ActiveRecord::Base
2
+ expires_according_to :refresh_token_expires_in
3
+
4
+ has_many :access_tokens
5
+
6
+ end
@@ -0,0 +1,7 @@
1
+ <%= form_tag authorizations_path, :class => action do %>
2
+ <%= hidden_field_tag :client_id, client.identifier %>
3
+ <%= hidden_field_tag :response_type, response_type %>
4
+ <%= hidden_field_tag :redirect_uri, redirect_uri %>
5
+ <%= submit_tag action.to_s.capitalize %>
6
+ <%= hidden_field_tag action, true %>
7
+ <% end %>
@@ -0,0 +1,4 @@
1
+ <h2>Invalid Authorization Request</h2>
2
+ <h3><%= @error.error %></h3>
3
+ <p><%= @error.description %></p>
4
+
@@ -0,0 +1,4 @@
1
+ <h2><%= link_to @client.name, @client.website %> is requesting permission to access your resources.</h2>
2
+
3
+ <%= render 'devise/oauth2_providable/authorizations/form', :client => @client, :response_type => @response_type, :redirect_uri => @redirect_uri, :action => :approve %>
4
+ <%= render 'devise/oauth2_providable/authorizations/form', :client => @client, :response_type => @response_type, :redirect_uri => @redirect_uri, :action => :deny %>