oauth-provider 0.5.0rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (139) hide show
  1. data/.gitignore +12 -0
  2. data/CHANGELOG +180 -0
  3. data/Gemfile +27 -0
  4. data/Guardfile +8 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.rdoc +393 -0
  7. data/Rakefile +2 -0
  8. data/UPGRADE.rdoc +21 -0
  9. data/generators/oauth_consumer/USAGE +10 -0
  10. data/generators/oauth_consumer/oauth_consumer_generator.rb +50 -0
  11. data/generators/oauth_consumer/templates/consumer_token.rb +11 -0
  12. data/generators/oauth_consumer/templates/controller.rb +27 -0
  13. data/generators/oauth_consumer/templates/index.html.erb +29 -0
  14. data/generators/oauth_consumer/templates/index.html.haml +18 -0
  15. data/generators/oauth_consumer/templates/migration.rb +20 -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_provider/USAGE +20 -0
  20. data/generators/oauth_provider/lib/insert_routes.rb +67 -0
  21. data/generators/oauth_provider/oauth_provider_generator.rb +127 -0
  22. data/generators/oauth_provider/templates/_form.html.erb +17 -0
  23. data/generators/oauth_provider/templates/_form.html.haml +21 -0
  24. data/generators/oauth_provider/templates/access_token.rb +16 -0
  25. data/generators/oauth_provider/templates/authorize.html.erb +14 -0
  26. data/generators/oauth_provider/templates/authorize.html.haml +16 -0
  27. data/generators/oauth_provider/templates/authorize_failure.html.erb +1 -0
  28. data/generators/oauth_provider/templates/authorize_failure.html.haml +1 -0
  29. data/generators/oauth_provider/templates/authorize_success.html.erb +1 -0
  30. data/generators/oauth_provider/templates/authorize_success.html.haml +1 -0
  31. data/generators/oauth_provider/templates/client_application.rb +57 -0
  32. data/generators/oauth_provider/templates/client_application_spec.rb +29 -0
  33. data/generators/oauth_provider/templates/client_application_test.rb +42 -0
  34. data/generators/oauth_provider/templates/client_applications.yml +23 -0
  35. data/generators/oauth_provider/templates/clients_controller.rb +52 -0
  36. data/generators/oauth_provider/templates/clients_controller_spec.rb +176 -0
  37. data/generators/oauth_provider/templates/clients_controller_test.rb +280 -0
  38. data/generators/oauth_provider/templates/controller.rb +23 -0
  39. data/generators/oauth_provider/templates/edit.html.erb +7 -0
  40. data/generators/oauth_provider/templates/edit.html.haml +4 -0
  41. data/generators/oauth_provider/templates/index.html.erb +43 -0
  42. data/generators/oauth_provider/templates/index.html.haml +39 -0
  43. data/generators/oauth_provider/templates/migration.rb +47 -0
  44. data/generators/oauth_provider/templates/new.html.erb +5 -0
  45. data/generators/oauth_provider/templates/new.html.haml +5 -0
  46. data/generators/oauth_provider/templates/oauth2_authorize.html.erb +16 -0
  47. data/generators/oauth_provider/templates/oauth2_authorize.html.haml +17 -0
  48. data/generators/oauth_provider/templates/oauth2_token.rb +20 -0
  49. data/generators/oauth_provider/templates/oauth2_token_spec.rb +52 -0
  50. data/generators/oauth_provider/templates/oauth2_verifier.rb +35 -0
  51. data/generators/oauth_provider/templates/oauth2_verifier_spec.rb +44 -0
  52. data/generators/oauth_provider/templates/oauth_nonce.rb +13 -0
  53. data/generators/oauth_provider/templates/oauth_nonce_spec.rb +24 -0
  54. data/generators/oauth_provider/templates/oauth_nonce_test.rb +26 -0
  55. data/generators/oauth_provider/templates/oauth_nonces.yml +13 -0
  56. data/generators/oauth_provider/templates/oauth_token.rb +30 -0
  57. data/generators/oauth_provider/templates/oauth_token_spec.rb +309 -0
  58. data/generators/oauth_provider/templates/oauth_token_test.rb +57 -0
  59. data/generators/oauth_provider/templates/oauth_tokens.yml +17 -0
  60. data/generators/oauth_provider/templates/request_token.rb +40 -0
  61. data/generators/oauth_provider/templates/show.html.erb +27 -0
  62. data/generators/oauth_provider/templates/show.html.haml +30 -0
  63. data/init.rb +1 -0
  64. data/install.rb +2 -0
  65. data/lib/generators/active_record/oauth_provider_generator.rb +39 -0
  66. data/lib/generators/active_record/oauth_provider_templates/access_token.rb +16 -0
  67. data/lib/generators/active_record/oauth_provider_templates/client_application.rb +57 -0
  68. data/lib/generators/active_record/oauth_provider_templates/migration.rb +47 -0
  69. data/lib/generators/active_record/oauth_provider_templates/oauth2_token.rb +20 -0
  70. data/lib/generators/active_record/oauth_provider_templates/oauth2_verifier.rb +35 -0
  71. data/lib/generators/active_record/oauth_provider_templates/oauth_nonce.rb +13 -0
  72. data/lib/generators/active_record/oauth_provider_templates/oauth_token.rb +30 -0
  73. data/lib/generators/active_record/oauth_provider_templates/request_token.rb +40 -0
  74. data/lib/generators/erb/oauth_provider_generator.rb +23 -0
  75. data/lib/generators/erb/oauth_provider_templates/_form.html.erb +17 -0
  76. data/lib/generators/erb/oauth_provider_templates/authorize.html.erb +14 -0
  77. data/lib/generators/erb/oauth_provider_templates/authorize_failure.html.erb +1 -0
  78. data/lib/generators/erb/oauth_provider_templates/authorize_success.html.erb +1 -0
  79. data/lib/generators/erb/oauth_provider_templates/edit.html.erb +7 -0
  80. data/lib/generators/erb/oauth_provider_templates/index.html.erb +43 -0
  81. data/lib/generators/erb/oauth_provider_templates/new.html.erb +5 -0
  82. data/lib/generators/erb/oauth_provider_templates/oauth2_authorize.html.erb +16 -0
  83. data/lib/generators/erb/oauth_provider_templates/show.html.erb +27 -0
  84. data/lib/generators/haml/oauth_provider_generator.rb +28 -0
  85. data/lib/generators/haml/oauth_provider_templates/_form.html.haml +21 -0
  86. data/lib/generators/haml/oauth_provider_templates/authorize.html.haml +16 -0
  87. data/lib/generators/haml/oauth_provider_templates/authorize_failure.html.haml +1 -0
  88. data/lib/generators/haml/oauth_provider_templates/authorize_success.html.haml +1 -0
  89. data/lib/generators/haml/oauth_provider_templates/edit.html.haml +4 -0
  90. data/lib/generators/haml/oauth_provider_templates/index.html.haml +39 -0
  91. data/lib/generators/haml/oauth_provider_templates/new.html.haml +5 -0
  92. data/lib/generators/haml/oauth_provider_templates/oauth2_authorize.html.haml +17 -0
  93. data/lib/generators/haml/oauth_provider_templates/show.html.haml +30 -0
  94. data/lib/generators/mongoid/oauth_provider_generator.rb +21 -0
  95. data/lib/generators/mongoid/oauth_provider_templates/access_token.rb +16 -0
  96. data/lib/generators/mongoid/oauth_provider_templates/client_application.rb +71 -0
  97. data/lib/generators/mongoid/oauth_provider_templates/oauth2_token.rb +20 -0
  98. data/lib/generators/mongoid/oauth_provider_templates/oauth2_verifier.rb +35 -0
  99. data/lib/generators/mongoid/oauth_provider_templates/oauth_nonce.rb +24 -0
  100. data/lib/generators/mongoid/oauth_provider_templates/oauth_token.rb +44 -0
  101. data/lib/generators/mongoid/oauth_provider_templates/request_token.rb +36 -0
  102. data/lib/generators/oauth_inflections.rb +6 -0
  103. data/lib/generators/oauth_provider/USAGE +18 -0
  104. data/lib/generators/oauth_provider/oauth_provider_generator.rb +37 -0
  105. data/lib/generators/oauth_provider/templates/clients_controller.rb +52 -0
  106. data/lib/generators/oauth_provider/templates/controller.rb +23 -0
  107. data/lib/generators/rspec/oauth_provider_generator.rb +35 -0
  108. data/lib/generators/rspec/templates/client_application_spec.rb +29 -0
  109. data/lib/generators/rspec/templates/client_applications.yml +23 -0
  110. data/lib/generators/rspec/templates/clients_controller_spec.rb +176 -0
  111. data/lib/generators/rspec/templates/oauth2_token_spec.rb +52 -0
  112. data/lib/generators/rspec/templates/oauth2_verifier_spec.rb +44 -0
  113. data/lib/generators/rspec/templates/oauth_nonce_spec.rb +24 -0
  114. data/lib/generators/rspec/templates/oauth_nonces.yml +13 -0
  115. data/lib/generators/rspec/templates/oauth_token_spec.rb +309 -0
  116. data/lib/generators/rspec/templates/oauth_tokens.yml +17 -0
  117. data/lib/generators/test_unit/oauth_provider_generator.rb +33 -0
  118. data/lib/generators/test_unit/templates/client_application_test.rb +42 -0
  119. data/lib/generators/test_unit/templates/client_applications.yml +23 -0
  120. data/lib/generators/test_unit/templates/clients_controller_test.rb +280 -0
  121. data/lib/generators/test_unit/templates/oauth_nonce_test.rb +26 -0
  122. data/lib/generators/test_unit/templates/oauth_nonces.yml +13 -0
  123. data/lib/generators/test_unit/templates/oauth_token_test.rb +57 -0
  124. data/lib/generators/test_unit/templates/oauth_tokens.yml +17 -0
  125. data/lib/oauth-provider.rb +24 -0
  126. data/lib/oauth-provider/version.rb +5 -0
  127. data/lib/oauth/controllers/application_controller_methods.rb +136 -0
  128. data/lib/oauth/controllers/provider_controller.rb +181 -0
  129. data/lib/oauth/provider/authorizer.rb +83 -0
  130. data/lib/oauth/rack/oauth_filter.rb +93 -0
  131. data/oauth-provider.gemspec +37 -0
  132. data/rails/init.rb +1 -0
  133. data/spec/dummy_provider_models.rb +53 -0
  134. data/spec/oauth/provider/authorizer_spec.rb +202 -0
  135. data/spec/rack/oauth_filter_spec.rb +244 -0
  136. data/spec/spec_helper.rb +3 -0
  137. data/tasks/oauth_tasks.rake +4 -0
  138. data/uninstall.rb +1 -0
  139. metadata +286 -0
@@ -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
@@ -0,0 +1,180 @@
1
+ 0.5.0-rc1
2
+ - First step at seperating consumer and provider. This branch contains provider
3
+ 0.4.1
4
+ - Security fix for OAuth1 provider. Please upgrade if you're using any 0.4 version. Thanks to [tomhughes]
5
+ - Limit index size in ConsumerToken migration to deal with very large tokens [devainandor]
6
+ - Accept ancient OAuth2 Token token=asfavasd like tokens. Just for complete compatibility. [pelle]
7
+ - oauth2 errors should return http 400 [pelle]
8
+ 0.4.0
9
+ - fix migrations to use expires_at instead of valid_to [pelle]
10
+ - add force parameter for forcing token refresh [afeld]
11
+ - make it work in rails 2x [Kimtaro]
12
+ - Use 0.5+ OAuth2 gem [kookster]
13
+ - prevent addition of ? marks to callback url when not needed [kookster]
14
+ - make .credentials accessible to TwitterToken [afeld]
15
+ 0.4.0-rc2
16
+ - Better OAuth2 support.
17
+ - Refactored authorizer into a Authorizer object which is now better tested
18
+ 0.4.0-rc1
19
+ PLEASE help test this so we can mark it as final
20
+ - Update to OAuth2 draft 22 which is likely the final release. [pelle] ACTION REQUIRED BY YOU, see README.
21
+ - Update forms to work with rails 3.1 [morgz]
22
+ - Allow nil callbacks for oob in OAuth 1.0a [Shaliko Usubov]
23
+ - OAuthFilter:oauth2_token to rejects headers that explicitly state oauth_version="1.0" [KentonWhite]
24
+ 0.4.0-pre7
25
+ - OAuth 1 requests using query or form encoded parameters where being interpreted as OAuth2 [pelleb]
26
+ - OAuth 2 requests were not checking for invalidated tokens. Please upgrade for this if you offer OAuth 2 [rymai]
27
+ - Handle case where credentials[:options] in consumer plugin was nil [marnen]
28
+ - Better facebook example [marnen]
29
+ 0.4.0-pre6
30
+ - fixes issue with erb generator in rails 3 [pelleb]
31
+ - various cleanups in generators [akonan]
32
+ 0.4.0-pre5
33
+ - protect oauth consumer relay with :expose configuration option. It's off by default. [pelle]
34
+ - Reenable twitter client. It is now configurable for twitter tokens. :client=>:twitter_gem or :oauth_gem [pelle]
35
+ - fix issues with new consumer tokens [afeld]
36
+ - More forgiving about oob callback values[chrisrhoden]
37
+ - Update Twitter consumer to use latest Twitter Gem [afeld]
38
+ - removed portable_contacts client from Google Token [p8]
39
+ - Fixes various mongoid issues [3en]
40
+ - Adds oauth2 consumer support
41
+ - Fixes oauth2 provider. parameter should be redirect_uri not redirect_url [Kimtaro]
42
+ - Most core OAuth functionality is being put into OAuthFilter. This means Rails 2.3 and above only
43
+ 0.4.0-pre4
44
+ - Fixed bug when creating a new user from a new consumer token
45
+ - Fix typo in consumer token [krasio]
46
+ - Fix issue with mongoid not supporting find_by_x style queries. [3en]
47
+ 0.4.0-pre3
48
+ - Experimental rack filter for OAuth 1.0a:
49
+ see lib/oauth/rack/oauth_filter.rb for details
50
+ 0.4.0-pre2
51
+ - mongoid defaults to being embedded
52
+ - new :auto_login option in oauth_config.rb which lets you use eg. twitter as a primary authentication method
53
+ 0.4.0-pre1
54
+ - mongoid support in rails 3 [Alexander Semyonov]
55
+ - OAUTH 2.0 authorization_code and password grant types
56
+ - Supports OAuth 2.0 draft 10 (Note this is incompatible with previous drafts)
57
+ - Refactored application_controller_methods to be a lot less intrusive
58
+ - Increased default token and key size in anticipation of OAuth2 support
59
+ - Rails 3 support
60
+ - Rails 3 generators [Paul Rosiana] and patches by [Alexander Flatter]
61
+ - Modularized Rails 3 generators [Alexander Semyonov]
62
+ - Callback urls now allow query parameters. Multiple patches but I used [Unk]'s.
63
+ 10/08/2009
64
+ 0.3.14
65
+ - Fixed the class generation when you have a custom token defined. [Brian Morearty]
66
+ 10/05/2009
67
+ 0.3.13
68
+ - Got rid of yahoo token. To support it correctly requires way too much work. Let them suffer the consequences of their decissions.
69
+ - GoogleToken now uses portablecontacts gem
70
+ 9/30/2009
71
+ 0.3.12
72
+ - Added a simple PortableContacts adapter for GoogleToken
73
+ - Added a SimpleClient wrapper to provide really simple wrapper for OAuth based json web services
74
+ - Increased token size in consumer_tokens table because of Yahoo's oversized tokens
75
+ - Added support for Yahoo
76
+ - Added support for Google (Boon Low)
77
+ 9/26/2009
78
+ 0.3.11
79
+ - Moved twitter tokens dependency back to regular twitter gem
80
+ 7/29/2009
81
+ 0.3.10
82
+ - Closed blocks in erb template (jcrosby) while pelle is hiding under his desk
83
+ - Handled error case on authorize with non existent token
84
+ - Fixed Agree2 token
85
+ - Security Fix: Only skip verify_authenticity_token for specific oauth token requests in provider controller
86
+ 7/25/2009
87
+ 0.3.9
88
+ - Added an Index to oauth consumers controller. Rerun generator to create index template
89
+ - Added invalidate action to provider, which allows a token to invalidate itself /oauth/invalidate
90
+ - Added capabilities action to provider. Lets you expand to allow auto discovery of permissions and services that token provides.
91
+ - Can override how authorize form indicates an authorization. To get around ugly checkbox
92
+
93
+ def user_authorizes_token?
94
+ params[:commit] == 'Authorize'
95
+ end
96
+
97
+ 7/23/2009
98
+ 0.3.8
99
+ - Fixed Gem Plugins Loading
100
+ 7/21/2009
101
+ 0.3.7
102
+ - A blushing Pelle adds a missing file
103
+ 0.3.6
104
+ - Twitter, Agree2 and FireEagle tokens are working in consumer.
105
+ 0.3.5
106
+ - made it a gem
107
+ - more thorough tests of OAuth 1.0 consumer
108
+ - Add support for a OAUTH_10_SUPPORT constant to switch on support for OAuth 1.0 in provider
109
+ 7/19/2009
110
+ - Added support for OAuth 1.0 consumers (nov)
111
+ 7/17/2009
112
+ - Added back support for OAuth 1.0 for providers (nov)
113
+ 7/14/2009
114
+ - Added OAuth Consumer generator
115
+ - Moved oauth controller code to a module to make it easier to upgrade in the future
116
+ 7/11/2009
117
+ - Added support for OAuth version 1.0a
118
+ - Added haml support
119
+ - Improved OAuth Client Controller gui (alec-c4)
120
+ 2/11/2009
121
+ - Fixed escaping error and file path error in the generator simultaneously reported and fixed by Ivan Valdes and Mike Demers thanks
122
+
123
+ 2/9/2009
124
+ - Fixed compatibility issue with OAuth Gem 3.1 (wr0ngway and aeden)
125
+ - Added Test:Unit tests to generator (Ed Hickey)
126
+ - added missing oauth_clients/edit.html.erb view template (Ed Hickey)
127
+ - added missing :oauth_clients resource route in USAGE (Ed Hickey)
128
+ - Don't throw NPE it token is not in db (Haruska)
129
+ - Cleaned up whitespace (bricolage, Nicholas Nam)
130
+ - Fixed bug in default verify_oauth_signature (igrigorik)
131
+ - Doc fixes (skippy)
132
+
133
+ 6/23/2008
134
+
135
+ - Split OAuth controller into two parts: OAuth and OAuth clients. [jcrosby]
136
+
137
+ revision 31
138
+
139
+ - patch that fixes a problem in oauth_required from Hannes Tyden and Sean Treadway from SoundCloud. Thanks.
140
+
141
+ revision 30
142
+
143
+ - updated to use oauth gem 0.2.1
144
+
145
+
146
+ revision 23
147
+
148
+ - removed all core libraries from plugin. They are now in the oauth gem.
149
+
150
+ # oauth-plugin-pre-gem Branch created
151
+
152
+ revision 18
153
+ - added a generator for creation oauth_providers
154
+
155
+ revision 12
156
+ - the bug with post and put has now been fixed.
157
+ - better documentation
158
+
159
+ revision 9
160
+ - added a test helper. Include OAuth::TestHelper in your tests or specs to mock incoming requests
161
+
162
+ revision: 8
163
+ - moved tests into oauth folder and renamed them to make them work with autotest by default
164
+ - Refactored the request methods to make them more flexible and ready for integrating with ActiveResource
165
+ - There are a few tests that fail. All of them to do with put and post requests with payload data. I decided to commit anyway, to get the new api out.
166
+
167
+ revision: 7
168
+
169
+ - Done a lot of work on the Server side of things. The Server class has changed a lot and is likely to be incompatible with previous versions
170
+
171
+ revision: 6
172
+
173
+ - Throws InsecureSignatureMethod exception if attempting to use straight sha1 or md5.
174
+ - Disables plaintext signature over http (throws an InsecureSignatureMethod)
175
+ - Better testing of signature methods - the prior tests were seriously flawed.
176
+
177
+ revision: 5
178
+
179
+ - Removed support for sha1 and md5
180
+ - Implemented draft 6 support of OAuth removing secrets from base string
data/Gemfile ADDED
@@ -0,0 +1,27 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in oauth-plugin.gemspec
4
+ gemspec
5
+
6
+ require 'rbconfig'
7
+
8
+ platforms :ruby do
9
+ if Config::CONFIG['target_os'] =~ /darwin/i
10
+ gem 'rb-fsevent'
11
+ gem 'growl'
12
+ end
13
+ if Config::CONFIG['target_os'] =~ /linux/i
14
+ gem 'rb-inotify', '>= 0.5.1'
15
+ gem 'libnotify', '~> 0.1.3'
16
+ end
17
+ end
18
+
19
+ platforms :jruby do
20
+ if Config::CONFIG['target_os'] =~ /darwin/i
21
+ gem 'growl'
22
+ end
23
+ if Config::CONFIG['target_os'] =~ /linux/i
24
+ gem 'rb-inotify', '>= 0.5.1'
25
+ gem 'libnotify', '~> 0.1.3'
26
+ end
27
+ end
@@ -0,0 +1,8 @@
1
+ # A sample Guardfile
2
+ # More info at http://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2, :cli => '-c' do
5
+ watch(%r{^spec/(.*)_spec.rb})
6
+ watch(%r{^lib/oauth/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Pelle Braendgaard
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.
@@ -0,0 +1,393 @@
1
+ = OAuth Provider
2
+
3
+ This is a plugin for implementing OAuth Providers in Rails applications.
4
+
5
+ This is a branch off of http://github.com/pelle/oauth-plugin where the consumer aspect is removed..
6
+
7
+ This is not yet complete.
8
+
9
+ We support the revised OAuth 1.0a specs at:
10
+
11
+ http://tools.ietf.org/html/rfc5849
12
+
13
+ As well as support for OAuth 2.0:
14
+
15
+ http://tools.ietf.org/html/draft-ietf-oauth-v2-22
16
+
17
+ Find out more on the OAuth site at:
18
+
19
+ http://oauth.net
20
+
21
+ == IMPORTANT note for people upgrading the provider
22
+
23
+ There are several changes to the latest OAuth 2.0 spec which requires a couple of changes to 2 models which you are REQUIRED to update manually if you are supporting OAuth2.
24
+
25
+ https://github.com/pelle/oauth-plugin/blob/master/lib/generators/active_record/oauth_provider_templates/oauth2_token.rb
26
+
27
+ class Oauth2Token < AccessToken
28
+ attr_accessor :state
29
+ def as_json(options={})
30
+ d = {:access_token=>token, :token_type => 'bearer'}
31
+ d[:expires_in] = expires_in if expires_at
32
+ d
33
+ end
34
+
35
+ def to_query
36
+ q = "access_token=#{token}&token_type=bearer"
37
+ q << "&state=#{URI.escape(state)}" if @state
38
+ q << "&expires_in=#{expires_in}" if expires_at
39
+ q << "&scope=#{URI.escape(scope)}" if scope
40
+ q
41
+ end
42
+
43
+ def expires_in
44
+ expires_at.to_i - Time.now.to_i
45
+ end
46
+ end
47
+
48
+
49
+ https://github.com/pelle/oauth-plugin/blob/master/lib/generators/active_record/oauth_provider_templates/oauth2_verifier.rb
50
+
51
+ class Oauth2Verifier < OauthToken
52
+ validates_presence_of :user
53
+ attr_accessor :state
54
+
55
+ def exchange!(params={})
56
+ OauthToken.transaction do
57
+ token = Oauth2Token.create! :user=>user,:client_application=>client_application, :scope => scope
58
+ invalidate!
59
+ token
60
+ end
61
+ end
62
+
63
+ def code
64
+ token
65
+ end
66
+
67
+ def redirect_url
68
+ callback_url
69
+ end
70
+
71
+ def to_query
72
+ q = "code=#{token}"
73
+ q << "&state=#{URI.escape(state)}" if @state
74
+ q
75
+ end
76
+
77
+ protected
78
+
79
+ def generate_keys
80
+ self.token = OAuth::Helper.generate_key(20)[0,20]
81
+ self.expires_at = 10.minutes.from_now
82
+ self.authorized_at = Time.now
83
+ end
84
+
85
+ end
86
+
87
+ There are matching specs for these which you may want to move into your project as well.
88
+
89
+ == Requirements
90
+
91
+ You need to install the oauth gem (0.4.4) which is the core OAuth ruby library. It will likely NOT work on any previous version of the gem.
92
+
93
+ gem install oauth
94
+
95
+ == Installation (Rails 3.0)
96
+
97
+ Add the plugin to your Gemfile:
98
+
99
+ gem "oauth-provider", "~> 0.5.0rc1"
100
+
101
+ And install it:
102
+
103
+ bundle install
104
+
105
+ == Installation (Rails 2.x)
106
+
107
+ The plugin can now be installed as an gem from github, which is the easiest way to keep it up to date.
108
+
109
+ gem install oauth-provider --pre
110
+
111
+ You should add the following in the gem dependency section of environment.rb
112
+
113
+ config.gem "oauth"
114
+ config.gem "oauth-provider"
115
+
116
+ Alternatively you can install it in vendors/plugin:
117
+
118
+ script/plugin install git://github.com/pelle/oauth-plugin.git
119
+
120
+ The Generator currently creates code (in particular views) that only work in Rails 2 and 3.
121
+
122
+ It should not be difficult to manually modify the code to work on Rails 1.2.x
123
+
124
+ I think the only real issue is that the views have .html.erb extensions. So these could theoretically just be renamed to .rhtml.
125
+
126
+ Please let me know if this works and I will see if I can make the generator conditionally create .rhtml for pre 2.0 versions of RAILS.
127
+
128
+ == OAuth Provider generator (Rails 3)
129
+
130
+ This currently supports rspec, test_unit, haml, erb, active_record and mongoid:
131
+
132
+ rails g oauth_provider
133
+
134
+ This generates OAuth and OAuth client controllers as well as the required models.
135
+
136
+ It requires an authentication framework such as acts_as_authenticated, restful_authentication or restful_open_id_authentication. It also requires Rails 2.0.
137
+
138
+ === INSTALL RACK FILTER (NEW)
139
+
140
+ A big change over previous versions is that we now use a rack filter. You have to install this in your application.rb file:
141
+
142
+ require 'oauth/rack/oauth_filter'
143
+ config.middleware.use OAuth::Rack::OAuthFilter
144
+
145
+
146
+ === Generator Options
147
+
148
+ The generator supports the defaults you have created in your application.rb file. eg:
149
+
150
+ config.generators do |g|
151
+ g.orm :mongoid
152
+ g.template_engine :haml
153
+ g.test_framework :rspec
154
+ end
155
+
156
+ === User Model
157
+
158
+ Add the following lines to your user model:
159
+
160
+ has_many :client_applications
161
+ has_many :tokens, :class_name => "OauthToken", :order => "authorized_at desc", :include => [:client_application]
162
+
163
+ == OAuth Provider generator (Rails 2)
164
+
165
+ While it isn't very flexible at the moment there is an oauth_provider generator which you can use like this:
166
+
167
+ ./script/generate oauth_provider
168
+
169
+ This generates OAuth and OAuth client controllers as well as the required models.
170
+
171
+ It requires an authentication framework such as acts_as_authenticated, restful_authentication or restful_open_id_authentication. It also requires Rails 2.0.
172
+
173
+ === Generator Options
174
+
175
+ By default the generator generates RSpec and ERB templates. The generator can instead create Test::Unit and/or HAML templates. To do this use the following options:
176
+
177
+ ./script/generate oauth_provider --test-unit --haml
178
+
179
+ These can of course be used individually as well.
180
+
181
+ === User Model
182
+
183
+ Add the following lines to your user model:
184
+
185
+ has_many :client_applications
186
+ has_many :tokens, :class_name => "OauthToken", :order => "authorized_at desc", :include => [:client_application]
187
+
188
+ === Migrate database
189
+
190
+ The database is defined in:
191
+
192
+ db/migrate/XXX_create_oauth_tables.rb
193
+
194
+ Run them as any other normal migration in rails with:
195
+
196
+ rake db:migrate
197
+
198
+ == Upgrading from OAuth 1.0 to OAuth 1.0a
199
+
200
+ As the flow has changed slightly and there are a couple of database changes it isn't as simple as just updating the plugin. Please follow these steps closely:
201
+
202
+ === Add a migration
203
+
204
+ You need to add a migration:
205
+
206
+ script/generate migration upgrade_oauth
207
+
208
+ Make it look like this:
209
+
210
+ class UpgradeOauth < ActiveRecord::Migration
211
+ def self.up
212
+ add_column :oauth_tokens, :callback_url, :string
213
+ add_column :oauth_tokens, :verifier, :string, :limit => 20
214
+ end
215
+
216
+ def self.down
217
+ remove_column :oauth_tokens, :callback_url
218
+ remove_column :oauth_tokens, :verifier
219
+ end
220
+ end
221
+
222
+ === Change code
223
+
224
+ There are changes to the following files:
225
+
226
+ app/models/client_application.rb
227
+ app/models/request_token.rb
228
+ app/controllers/oauth_controller.rb
229
+
230
+ === Changes in client_application.rb
231
+
232
+ Add the following towards the top of the model class
233
+
234
+ attr_accessor :token_callback_url
235
+
236
+ Then change the create_request_token method to the following:
237
+
238
+ def create_request_token
239
+ RequestToken.create :client_application => self, :callback_url => token_callback_url
240
+ end
241
+
242
+ === Changes in request_token.rb
243
+
244
+ The RequestToken contains the bulk of the changes so it's easiest to list it in it's entirety. Mainly we need to add support for the oauth_verifier parameter and also tell the client that we support OAuth 1.0a.
245
+
246
+ Make sure it looks like this:
247
+
248
+ class RequestToken < OauthToken
249
+
250
+ attr_accessor :provided_oauth_verifier
251
+
252
+ def authorize!(user)
253
+ return false if authorized?
254
+ self.user = user
255
+ self.authorized_at = Time.now
256
+ self.verifier=OAuth::Helper.generate_key(16)[0,20] unless oauth10?
257
+ self.save
258
+ end
259
+
260
+ def exchange!
261
+ return false unless authorized?
262
+ return false unless oauth10? || verifier == provided_oauth_verifier
263
+
264
+ RequestToken.transaction do
265
+ access_token = AccessToken.create(:user => user, :client_application => client_application)
266
+ invalidate!
267
+ access_token
268
+ end
269
+ end
270
+
271
+ def to_query
272
+ if oauth10?
273
+ super
274
+ else
275
+ "#{super}&oauth_callback_confirmed = true"
276
+ end
277
+ end
278
+
279
+ def oob?
280
+ self.callback_url == 'oob'
281
+ end
282
+
283
+ def oauth10?
284
+ (defined? OAUTH_10_SUPPORT) && OAUTH_10_SUPPORT && self.callback_url.blank?
285
+ end
286
+
287
+ end
288
+
289
+ === Changes in oauth_controller
290
+
291
+ All you need to do here is the change the authorize action to use the request_token callback url and add the oauth_verifier to the callback url.
292
+
293
+ def authorize
294
+ @token = ::RequestToken.find_by_token params[:oauth_token]
295
+ unless @token.invalidated?
296
+ if request.post?
297
+ if params[:authorize] == '1'
298
+ @token.authorize!(current_user)
299
+ if @token.oauth10?
300
+ @redirect_url = params[:oauth_callback] || @token.client_application.callback_url
301
+ else
302
+ @redirect_url = @token.oob? ? @token.client_application.callback_url : @token.callback_url
303
+ end
304
+
305
+ if @redirect_url
306
+ if @token.oauth10?
307
+ redirect_to "#{@redirect_url}?oauth_token=#{@token.token}"
308
+ else
309
+ redirect_to "#{@redirect_url}?oauth_token=#{@token.token}&oauth_verifier=#{@token.verifier}"
310
+ end
311
+ else
312
+ render :action => "authorize_success"
313
+ end
314
+ elsif params[:authorize] == "0"
315
+ @token.invalidate!
316
+ render :action => "authorize_failure"
317
+ end
318
+ end
319
+ else
320
+ render :action => "authorize_failure"
321
+ end
322
+ end
323
+
324
+ Alternatively if you haven't customized your controller you can replace the full controller with this:
325
+
326
+ require 'oauth/controllers/provider_controller'
327
+ class OauthController < ApplicationController
328
+ include OAuth::Controllers::ProviderController
329
+ end
330
+
331
+ This way the controller will automatically include bug fixes in future versions of the plugin.
332
+
333
+ The rest of the changes are in the plugin and will be automatically be included.
334
+
335
+ *Note* OAuth 1.0a removes support for callback url's passed to the authorize page, clients must either define a callback url in their client application or pass one on the token request page.
336
+
337
+ === Supporting old OAuth 1.0 clients
338
+
339
+ If you absolutely have to support older OAuth 1.0 clients on an optional basis, we now include a switch to turn it back on.
340
+
341
+ For legacy OAUTH 1.0 support add the following constant in your environment.rb
342
+
343
+ OAUTH_10_SUPPORT = true
344
+
345
+ Note, you should only do this if you really positively require to support old OAuth1.0 clients. There is a serious security issue with this.
346
+
347
+ == Protecting your actions
348
+
349
+ I recommend that you think about what your users would want to provide access to and limit oauth for those only. For example in a CRUD controller you may think about if you want to let consumer applications do the create, update or delete actions. For your application this might make sense, but for others maybe not.
350
+
351
+ If you want to give oauth access to everything a registered user can do, just replace the filter you have in your controllers with:
352
+
353
+ before_filter :login_or_oauth_required
354
+
355
+ If you want to restrict consumers to the index and show methods of your controller do the following:
356
+
357
+ before_filter :login_required, :except => [:show,:index]
358
+ before_filter :login_or_oauth_required, :only => [:show,:index]
359
+
360
+ If you have an action you only want used via oauth:
361
+
362
+ before_filter :oauth_required
363
+
364
+ All of these places the tokens user in current_user as you would expect. It also exposes the following methods:
365
+
366
+ * current_token - for accessing the token used to authorize the current request
367
+ * current_client_application - for accessing information about which consumer is currently accessing your request
368
+
369
+ You could add application specific information to the OauthToken and ClientApplication model for such things as object level access control, billing, expiry etc. Be creative and you can create some really cool applications here.
370
+
371
+
372
+ == Contribute and earn OAuth Karma
373
+
374
+ Anyone who has a commit accepted into the official oauth-plugin git repo is awarded OAuthKarma:
375
+
376
+ https://picomoney.com/oauth-karma/accounts
377
+
378
+
379
+ == More
380
+
381
+ The Mailing List for all things OAuth in Ruby is:
382
+
383
+ http://groups.google.com/group/oauth-ruby
384
+
385
+ The Mailing list for everything else OAuth is:
386
+
387
+ http://groups.google.com/group/oauth
388
+
389
+ The OAuth Ruby Gem home page is http://oauth.rubyforge.org
390
+
391
+ Please help documentation, patches and testing.
392
+
393
+ Copyright (c) 2007-2012 Pelle Braendgaard and contributors, released under the MIT license