oauth-plugin 0.3.14 → 0.4.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. data/CHANGELOG +10 -0
  2. data/README.rdoc +44 -9
  3. data/Rakefile +2 -2
  4. data/VERSION +1 -1
  5. data/generators/oauth_consumer/templates/migration.rb +1 -1
  6. data/generators/oauth_provider/oauth_provider_generator.rb +6 -0
  7. data/generators/oauth_provider/templates/access_token.rb +2 -2
  8. data/generators/oauth_provider/templates/client_application.rb +8 -6
  9. data/generators/oauth_provider/templates/client_applications.yml +6 -6
  10. data/generators/oauth_provider/templates/clients_controller_spec.rb +130 -193
  11. data/generators/oauth_provider/templates/controller.rb +12 -0
  12. data/generators/oauth_provider/templates/controller_spec.rb +762 -291
  13. data/generators/oauth_provider/templates/controller_spec_helper.rb +49 -63
  14. data/generators/oauth_provider/templates/migration.rb +6 -5
  15. data/generators/oauth_provider/templates/oauth2_authorize.html.erb +16 -0
  16. data/generators/oauth_provider/templates/oauth2_authorize.html.haml +17 -0
  17. data/generators/oauth_provider/templates/oauth2_token.rb +6 -0
  18. data/generators/oauth_provider/templates/oauth2_token_spec.rb +29 -0
  19. data/generators/oauth_provider/templates/oauth2_verifier.rb +28 -0
  20. data/generators/oauth_provider/templates/oauth2_verifier_spec.rb +54 -0
  21. data/generators/oauth_provider/templates/oauth_nonce_test.rb +1 -1
  22. data/generators/oauth_provider/templates/oauth_token.rb +3 -4
  23. data/generators/oauth_provider/templates/request_token.rb +1 -1
  24. data/lib/generators/active_record/oauth_consumer_generator.rb +33 -0
  25. data/lib/generators/active_record/oauth_consumer_templates/consumer_token.rb +5 -0
  26. data/lib/generators/active_record/oauth_consumer_templates/migration.rb +20 -0
  27. data/lib/generators/active_record/oauth_provider_generator.rb +39 -0
  28. data/lib/generators/active_record/oauth_provider_templates/access_token.rb +16 -0
  29. data/lib/generators/active_record/oauth_provider_templates/client_application.rb +57 -0
  30. data/lib/generators/active_record/oauth_provider_templates/migration.rb +47 -0
  31. data/lib/generators/active_record/oauth_provider_templates/oauth2_token.rb +5 -0
  32. data/lib/generators/active_record/oauth_provider_templates/oauth2_verifier.rb +28 -0
  33. data/lib/generators/active_record/oauth_provider_templates/oauth_nonce.rb +13 -0
  34. data/lib/generators/active_record/oauth_provider_templates/oauth_token.rb +30 -0
  35. data/lib/generators/active_record/oauth_provider_templates/request_token.rb +40 -0
  36. data/lib/generators/erb/oauth_consumer_generator.rb +14 -0
  37. data/lib/generators/erb/oauth_consumer_templates/index.html.erb +29 -0
  38. data/lib/generators/erb/oauth_consumer_templates/show.html.erb +7 -0
  39. data/lib/generators/erb/oauth_provider_generator.rb +21 -0
  40. data/lib/generators/erb/oauth_provider_templates/_form.html.erb +17 -0
  41. data/lib/generators/erb/oauth_provider_templates/authorize.html.erb +14 -0
  42. data/lib/generators/erb/oauth_provider_templates/authorize_failure.html.erb +1 -0
  43. data/lib/generators/erb/oauth_provider_templates/authorize_success.html.erb +1 -0
  44. data/lib/generators/erb/oauth_provider_templates/edit.html.erb +7 -0
  45. data/lib/generators/erb/oauth_provider_templates/index.html.erb +43 -0
  46. data/lib/generators/erb/oauth_provider_templates/new.html.erb +5 -0
  47. data/lib/generators/erb/oauth_provider_templates/oauth2_authorize.html.erb +16 -0
  48. data/lib/generators/erb/oauth_provider_templates/show.html.erb +27 -0
  49. data/lib/generators/haml/oauth_consumer_generator.rb +21 -0
  50. data/lib/generators/haml/oauth_consumer_templates/index.html.haml +18 -0
  51. data/lib/generators/haml/oauth_consumer_templates/show.html.haml +8 -0
  52. data/lib/generators/haml/oauth_provider_generator.rb +28 -0
  53. data/lib/generators/haml/oauth_provider_templates/_form.html.haml +21 -0
  54. data/lib/generators/haml/oauth_provider_templates/authorize.html.haml +16 -0
  55. data/lib/generators/haml/oauth_provider_templates/authorize_failure.html.haml +1 -0
  56. data/lib/generators/haml/oauth_provider_templates/authorize_success.html.haml +1 -0
  57. data/lib/generators/haml/oauth_provider_templates/edit.html.haml +4 -0
  58. data/lib/generators/haml/oauth_provider_templates/index.html.haml +39 -0
  59. data/lib/generators/haml/oauth_provider_templates/new.html.haml +5 -0
  60. data/lib/generators/haml/oauth_provider_templates/oauth2_authorize.html.haml +17 -0
  61. data/lib/generators/haml/oauth_provider_templates/show.html.haml +30 -0
  62. data/lib/generators/mongoid/oauth_consumer_generator.rb +15 -0
  63. data/lib/generators/mongoid/oauth_consumer_templates/consumer_token.rb +14 -0
  64. data/lib/generators/mongoid/oauth_provider_generator.rb +21 -0
  65. data/lib/generators/mongoid/oauth_provider_templates/access_token.rb +16 -0
  66. data/lib/generators/mongoid/oauth_provider_templates/client_application.rb +71 -0
  67. data/lib/generators/mongoid/oauth_provider_templates/oauth2_token.rb +5 -0
  68. data/lib/generators/mongoid/oauth_provider_templates/oauth2_verifier.rb +25 -0
  69. data/lib/generators/mongoid/oauth_provider_templates/oauth_nonce.rb +24 -0
  70. data/lib/generators/mongoid/oauth_provider_templates/oauth_token.rb +44 -0
  71. data/lib/generators/mongoid/oauth_provider_templates/request_token.rb +36 -0
  72. data/lib/generators/oauth_consumer/USAGE +8 -0
  73. data/lib/generators/oauth_consumer/oauth_consumer_generator.rb +27 -0
  74. data/lib/generators/oauth_consumer/templates/controller.rb +19 -0
  75. data/lib/generators/oauth_consumer/templates/oauth_config.rb +46 -0
  76. data/lib/generators/oauth_inflections.rb +6 -0
  77. data/lib/generators/oauth_plugin.rb +0 -0
  78. data/lib/generators/oauth_provider/USAGE +18 -0
  79. data/lib/generators/oauth_provider/oauth_provider_generator.rb +47 -0
  80. data/lib/generators/oauth_provider/templates/clients_controller.rb +52 -0
  81. data/lib/generators/oauth_provider/templates/controller.rb +23 -0
  82. data/lib/generators/rspec/oauth_provider_generator.rb +39 -0
  83. data/lib/generators/rspec/templates/client_application_spec.rb +29 -0
  84. data/lib/generators/rspec/templates/client_applications.yml +23 -0
  85. data/lib/generators/rspec/templates/clients_controller_spec.rb +176 -0
  86. data/lib/generators/rspec/templates/controller_spec.rb +838 -0
  87. data/lib/generators/rspec/templates/controller_spec_helper.rb +66 -0
  88. data/lib/generators/rspec/templates/oauth2_token_spec.rb +29 -0
  89. data/lib/generators/rspec/templates/oauth2_verifier_spec.rb +54 -0
  90. data/lib/generators/rspec/templates/oauth_nonce_spec.rb +24 -0
  91. data/lib/generators/rspec/templates/oauth_nonces.yml +13 -0
  92. data/lib/generators/rspec/templates/oauth_token_spec.rb +309 -0
  93. data/lib/generators/rspec/templates/oauth_tokens.yml +17 -0
  94. data/lib/generators/test_unit/oauth_provider_generator.rb +37 -0
  95. data/lib/generators/test_unit/templates/client_application_test.rb +42 -0
  96. data/lib/generators/test_unit/templates/client_applications.yml +23 -0
  97. data/lib/generators/test_unit/templates/clients_controller_test.rb +280 -0
  98. data/lib/generators/test_unit/templates/controller_test.rb +310 -0
  99. data/lib/generators/test_unit/templates/controller_test_helper.rb +115 -0
  100. data/lib/generators/test_unit/templates/oauth_nonce_test.rb +26 -0
  101. data/lib/generators/test_unit/templates/oauth_nonces.yml +13 -0
  102. data/lib/generators/test_unit/templates/oauth_token_test.rb +57 -0
  103. data/lib/generators/test_unit/templates/oauth_tokens.yml +17 -0
  104. data/lib/oauth/controllers/application_controller_methods.rb +169 -66
  105. data/lib/oauth/controllers/provider_controller.rb +154 -38
  106. data/lib/oauth/models/consumers/service_loader.rb +2 -0
  107. data/lib/oauth/models/consumers/token.rb +0 -1
  108. data/oauth-plugin.gemspec +97 -10
  109. data/rails/init.rb +6 -2
  110. metadata +119 -12
@@ -0,0 +1,40 @@
1
+ class RequestToken < OauthToken
2
+
3
+ attr_accessor :provided_oauth_verifier
4
+
5
+ def authorize!(user)
6
+ return false if authorized?
7
+ self.user = user
8
+ self.authorized_at = Time.now
9
+ self.verifier=OAuth::Helper.generate_key(20)[0,20] unless oauth10?
10
+ self.save
11
+ end
12
+
13
+ def exchange!
14
+ return false unless authorized?
15
+ return false unless oauth10? || verifier==provided_oauth_verifier
16
+
17
+ RequestToken.transaction do
18
+ access_token = AccessToken.create(:user => user, :client_application => client_application)
19
+ invalidate!
20
+ access_token
21
+ end
22
+ end
23
+
24
+ def to_query
25
+ if oauth10?
26
+ super
27
+ else
28
+ "#{super}&oauth_callback_confirmed=true"
29
+ end
30
+ end
31
+
32
+ def oob?
33
+ self.callback_url=='oob'
34
+ end
35
+
36
+ def oauth10?
37
+ (defined? OAUTH_10_SUPPORT) && OAUTH_10_SUPPORT && self.callback_url.blank?
38
+ end
39
+
40
+ end
@@ -0,0 +1,14 @@
1
+ require 'rails/generators/erb'
2
+
3
+ module Erb
4
+ module Generators
5
+ class OauthConsumerGenerator < Erb::Generators::Base
6
+ source_root File.expand_path('../oauth_consumer_templates', __FILE__)
7
+
8
+ def copy_view_files
9
+ template 'index.html.erb', File.join('app/views', class_path, 'oauth_consumers', 'index.html.erb')
10
+ template 'show.html.erb', File.join('app/views', class_path, 'oauth_consumers', 'show.html.erb')
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,29 @@
1
+ <h1>Services</h1>
2
+
3
+ <%% if @consumer_tokens.empty? %>
4
+ <p>
5
+ You are currently not connected to any external services.
6
+ </p>
7
+ <%% else %>
8
+ <p>
9
+ You are connected to the following services:
10
+ </p>
11
+ <ul>
12
+ <%% @consumer_tokens.each do |token| %>
13
+ <li>
14
+ <%%= link_to token.class.service_name.to_s.humanize, oauth_consumer_path(token.class.service_name) %>
15
+ </li>
16
+ <%% end %>
17
+ </ul>
18
+ <%% end %>
19
+
20
+ <%% unless @services.empty? %>
21
+ <h3>You can connect to the following services:</h3>
22
+ <ul>
23
+ <%% @services.each do |service| %>
24
+ <li>
25
+ <%%= link_to service.to_s.humanize, oauth_consumer_path(service) %>
26
+ </li>
27
+ <%% end %>
28
+ </ul>
29
+ <%% end %>
@@ -0,0 +1,7 @@
1
+ <h1>You are already Connected to <%%=params[:id].humanize%></h1>
2
+ <%% form_tag oauth_consumer_path(params[:id]),:method=>:delete do %>
3
+ <%%=submit_tag "Disconnect" %>
4
+ or
5
+ <%%=submit_tag "Reconnect" %>
6
+ if you experienced a problem.
7
+ <%% end %>
@@ -0,0 +1,21 @@
1
+ require 'rails/generators/erb'
2
+
3
+ module Erb
4
+ module Generators
5
+ class OauthProviderGenerator < Erb::Generators::Base
6
+ source_root File.expand_path('../oauth_provider_templates', __FILE__)
7
+
8
+ def copy_view_files
9
+ template '_form.html.erb', File.join('app/views', class_path, 'oauth_clients', '_form.html.erb')
10
+ template 'new.html.erb', File.join('app/views', class_path, 'oauth_clients', 'new.html.erb')
11
+ template 'index.html.erb', File.join('app/views', class_path, 'oauth_clients', 'index.html.erb')
12
+ template 'show.html.erb', File.join('app/views', class_path, 'oauth_clients', 'show.html.erb')
13
+ template 'edit.html.erb', File.join('app/views', class_path, 'oauth_clients', 'edit.html.erb')
14
+ template 'authorize.html.erb', File.join('app/views', class_path, file_name, 'authorize.html.erb')
15
+ template 'oauth2_authorize.html.erb', File.join('app/views', class_path, file_name, 'oauth2_authorize.html.erb')
16
+ template 'authorize_success.html.erb', File.join('app/views', class_path, file_name, 'authorize_success.html.erb')
17
+ template 'authorize_failure.html.erb', File.join('app/views', class_path, file_name, 'authorize_failure.html.erb')
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,17 @@
1
+ <%%= error_messages_for :client_application %>
2
+ <div class="field">
3
+ <label for="client_application_name">Name*</label><br/>
4
+ <%%= f.text_field :name %>
5
+ </div>
6
+ <div class="field">
7
+ <label for="client_application_url">Main Application URL*</label><br/>
8
+ <%%= f.text_field :url %>
9
+ </div>
10
+ <div class="field">
11
+ <label for="client_application_callback_url">Callback URL*</label><br/>
12
+ <%%= f.text_field :callback_url %>
13
+ </div>
14
+ <div class="field">
15
+ <label for="client_application_support_url">Support URL</label><br/>
16
+ <%%= f.text_field :support_url %>
17
+ </div>
@@ -0,0 +1,14 @@
1
+ <h1>Authorize access to your account</h1>
2
+ <p>Would you like to authorize <%%= link_to @token.client_application.name,@token.client_application.url %> (<%%= link_to @token.client_application.url,@token.client_application.url %>) to access your account?</p>
3
+ <%% form_tag authorize_url do %>
4
+ <%%= hidden_field_tag "oauth_token", @token.token %>
5
+ <%%- if params[:oauth_callback] -%>
6
+ <%%= hidden_field_tag "oauth_callback", params[:oauth_callback] %>
7
+ <%%- end -%>
8
+ <p>
9
+ <%%= check_box_tag 'authorize' %> authorize access
10
+ </p>
11
+ <p>
12
+ <%%= submit_tag %>
13
+ </p>
14
+ <%% end %>
@@ -0,0 +1 @@
1
+ <h1>You have disallowed this request</h1>
@@ -0,0 +1 @@
1
+ <h1>You have allowed this request</h1>
@@ -0,0 +1,7 @@
1
+ <h1>Edit your application</h1>
2
+ <%% form_for :client_application, @client_application, :url => oauth_client_path(@client_application), :html => {:method => :put} do |f| %>
3
+ <%%= render :partial => "form", :locals => { :f => f } %>
4
+ <%%= submit_tag "Edit" %>
5
+ <%% end %>
6
+ <%%= link_to 'Show', oauth_client_path(@client_application) %> |
7
+ <%%= link_to 'Back', oauth_clients_path %>
@@ -0,0 +1,43 @@
1
+ <div class="flash"><%%= flash[:notice] %></div>
2
+ <h1>OAuth Client Applications</h1>
3
+ <%% unless @tokens.empty? %>
4
+ <p>The following tokens have been issued to applications in your name</p>
5
+ <table>
6
+ <tr><th>Application</th><th>Issued</th><th>&nbsp;</th></tr>
7
+ <%% @tokens.each do |token|%>
8
+ <%% content_tag_for :tr, token do %>
9
+ <td><%%= link_to token.client_application.name, token.client_application.url %></td>
10
+ <td><%%= token.authorized_at %></td>
11
+ <td>
12
+ <%% form_tag :controller => 'oauth', :action => 'revoke' do %>
13
+ <%%= hidden_field_tag 'token', token.token %>
14
+ <%%= submit_tag "Revoke!" %>
15
+ <%% end %>
16
+ </td>
17
+ <%% end %>
18
+ <%% end %>
19
+
20
+ </table>
21
+ <%% end %>
22
+ <h3>Application Developers</h3>
23
+ <%% if @client_applications.empty? %>
24
+ <p>
25
+ Do you have an application you would like to register for use with us using the <a href="http://oauth.net">OAuth</a> standard?
26
+ </p>
27
+ <p>
28
+ You must register your web application before it can make OAuth requests to this service
29
+ </p>
30
+ <%% else %>
31
+ <p>
32
+ You have the following client applications registered:
33
+ </p>
34
+ <%% @client_applications.each do |client|%>
35
+ <%% div_for client do %>
36
+ <%%= link_to client.name, oauth_client_path(client) %>-
37
+ <%%= link_to 'Edit', edit_oauth_client_path(client) %>
38
+ <%%= link_to 'Delete', oauth_client_path(client), :confirm => "Are you sure?", :method => :delete %>
39
+ <%% end %>
40
+ <%% end %>
41
+ <%% end %>
42
+ <br />
43
+ <h3><%%= link_to "Register your application", :action => :new %></h3>
@@ -0,0 +1,5 @@
1
+ <h1>Register a new application</h1>
2
+ <%% form_for :client_application, :url => { :action => :create } do |f| %>
3
+ <%%= render :partial => "form", :locals => { :f => f } %>
4
+ <%%= submit_tag "Register" %>
5
+ <%% end %>
@@ -0,0 +1,16 @@
1
+ <h1>Authorize access to your account</h1>
2
+ <p>Would you like to authorize <%%= link_to @token.client_application.name,@token.client_application.url %> (<%%= link_to @token.client_application.url,@token.client_application.url %>) to access your account?</p>
3
+ <%% form_tag authorize_url do %>
4
+ <%%= hidden_field_tag "response_type", params[:response_type]%>
5
+ <%%= hidden_field_tag "client_id", params[:client_id]%>
6
+ <%%= hidden_field_tag "redirect_url", params[:redirect_url]%>
7
+ <%%= hidden_field_tag "state", params[:state]%>
8
+ <%%= hidden_field_tag "scope", params[:scope]%>
9
+
10
+ <p>
11
+ <%%= check_box_tag 'authorize' %> authorize access
12
+ </p>
13
+ <p>
14
+ <%%= submit_tag %>
15
+ </p>
16
+ <%% end %>
@@ -0,0 +1,27 @@
1
+ <h1>OAuth details for <%%=@client_application.name %></h1>
2
+ <p>
3
+ <strong>Consumer Key:</strong>
4
+ <code><%%=@client_application.key %></code>
5
+ </p>
6
+ <p>
7
+ <strong>Consumer Secret:</strong>
8
+ <code><%%=@client_application.secret %></code>
9
+ </p>
10
+ <p>
11
+ <strong>Request Token URL</strong>
12
+ <code>http<%%='s' if request.ssl? %>://<%%= request.host_with_port %><%%=@client_application.oauth_server.request_token_path %></code>
13
+ </p>
14
+ <p>
15
+ <strong>Access Token URL</strong>
16
+ <code>http<%%='s' if request.ssl? %>://<%%= request.host_with_port %><%%=@client_application.oauth_server.access_token_path %></code>
17
+ </p>
18
+ <p>
19
+ <strong>Authorize URL</strong>
20
+ <code>http<%%='s' if request.ssl? %>://<%%= request.host_with_port %><%%=@client_application.oauth_server.authorize_path %></code>
21
+ </p>
22
+
23
+ <p>
24
+ We support hmac-sha1 (recommended) as well as plain text in ssl mode.
25
+ </p>
26
+ <%%= link_to 'Edit', edit_oauth_client_path(@client_application) %> |
27
+ <%%= link_to 'Back', oauth_clients_path %>
@@ -0,0 +1,21 @@
1
+ require 'rails/generators/erb/controller/controller_generator'
2
+
3
+ module Haml
4
+ module Generators
5
+ class OauthConsumerGenerator < Erb::Generators::Base
6
+ source_root File.expand_path('../oauth_consumer_templates', __FILE__)
7
+
8
+ argument :name, :type => :string, :default => 'Oauth'
9
+
10
+ def copy_view_files
11
+ template 'index.html.haml', File.join('app/views', class_path, 'oauth_consumers', 'index.html.haml')
12
+ template 'show.html.haml', File.join('app/views', class_path, 'oauth_consumers', 'show.html.haml')
13
+ end
14
+
15
+ protected
16
+ def handler
17
+ :haml
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ %h1 Services
2
+
3
+ -if @consumer_tokens.empty?
4
+ %p
5
+ You are currently not connected to any external services.
6
+ -else
7
+ %p You are connected to the following services:
8
+ %ul
9
+ -@consumer_tokens.each do |token|
10
+ %li
11
+ =link_to token.class.service_name.to_s.humanize, oauth_consumer_path(token.class.service_name)
12
+
13
+ -unless @services.empty?
14
+ %h3 You can connect to the following services:
15
+ %ul
16
+ -@services.each do |service|
17
+ %li
18
+ =link_to service.to_s.humanize,oauth_consumer_path(service)
@@ -0,0 +1,8 @@
1
+ %h1
2
+ You are already Connected to
3
+ =params[:id].humanize
4
+ -form_tag oauth_consumer_path(params[:id]),:method=>:delete do
5
+ =submit_tag "Disconnect"
6
+ or
7
+ =submit_tag "Reconnect"
8
+ if you experienced a problem.
@@ -0,0 +1,28 @@
1
+ require 'rails/generators/erb/controller/controller_generator'
2
+
3
+ module Haml
4
+ module Generators
5
+ class OauthProviderGenerator < Erb::Generators::Base
6
+ source_root File.expand_path('../oauth_provider_templates', __FILE__)
7
+
8
+ argument :name, :type => :string, :default => 'Oauth'
9
+
10
+ def copy_view_files
11
+ template '_form.html.haml', File.join('app/views', class_path, 'oauth_clients', '_form.html.haml')
12
+ template 'new.html.haml', File.join('app/views', class_path, 'oauth_clients', 'new.html.haml')
13
+ template 'index.html.haml', File.join('app/views', class_path, 'oauth_clients', 'index.html.haml')
14
+ template 'show.html.haml', File.join('app/views', class_path, 'oauth_clients', 'show.html.haml')
15
+ template 'edit.html.haml', File.join('app/views', class_path, 'oauth_clients', 'edit.html.haml')
16
+ template 'authorize.html.haml', File.join('app/views', class_path, file_name, 'authorize.html.haml')
17
+ template 'oauth2_authorize.html.haml', File.join('app/views', class_path, file_name, 'oauth2_authorize.html.haml')
18
+ template 'authorize_success.html.haml', File.join('app/views', class_path, file_name, 'authorize_success.html.haml')
19
+ template 'authorize_failure.html.haml', File.join('app/views', class_path, file_name, 'authorize_failure.html.haml')
20
+ end
21
+
22
+ protected
23
+ def handler
24
+ :haml
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,21 @@
1
+ =error_messages_for :client_application
2
+ .field
3
+ %label{:for=>"client_application_name"} Name*
4
+ %br
5
+ = f.text_field :name
6
+
7
+ .field
8
+ %label{:for=>"client_application_url"} Main Application URL*
9
+ %br
10
+ = f.text_field :url
11
+
12
+ .field
13
+ %label{:for=>"client_application_callback_url"} Callback URL*
14
+ %br
15
+ = f.text_field :callback_url
16
+
17
+ .field
18
+ %label{:for=>"client_application_support_url"} Support URL
19
+ %br
20
+ = f.text_field :support_url
21
+
@@ -0,0 +1,16 @@
1
+ %h1 Authorize access to your account
2
+ %p
3
+ Would you like to authorize
4
+ = link_to @token.client_application.name,@token.client_application.url
5
+ (
6
+ = link_to @token.client_application.url,@token.client_application.url
7
+ ) to access your account?
8
+ - form_tag authorize_url do
9
+ = hidden_field_tag "oauth_token", @token.token
10
+ - if params[:oauth_callback]
11
+ = hidden_field_tag "oauth_callback", params[:oauth_callback]
12
+ %p
13
+ = check_box_tag 'authorize'
14
+ authorize access
15
+ %p
16
+ = submit_tag
@@ -0,0 +1 @@
1
+ %h1 You have disallowed this request
@@ -0,0 +1 @@
1
+ %h1 You have allowed this request
@@ -0,0 +1,4 @@
1
+ %h1 Edit your application
2
+ - form_for :client_application do |f|
3
+ = render :partial => "form", :locals => { :f => f }
4
+ = submit_tag "Edit"
@@ -0,0 +1,39 @@
1
+ .flash= flash[:notice]
2
+ %h1 OAuth Client Applications
3
+ - unless @tokens.empty?
4
+
5
+ %p
6
+ The following tokens have been issued to applications in your name
7
+
8
+ %table
9
+ %tr
10
+ %th Application
11
+ %th Issued
12
+ %th &nbsp;
13
+ - @tokens.each do |token|
14
+ - content_tag_for :tr, token do
15
+ %td= link_to token.client_application.name, token.client_application.url
16
+ %td= token.authorized_at
17
+ %td
18
+ - form_tag :controller => 'oauth', :action => 'revoke' do
19
+ = hidden_field_tag 'token', token.token
20
+ = submit_tag "Revoke!"
21
+
22
+ %h3 Application Developers
23
+
24
+ - if @client_applications.empty?
25
+ %p
26
+ Do you have an application you would like to register for use with us using the <a href="http://oauth.net">OAuth</a> standard?
27
+ You must register your web application before it can make OAuth requests to this service
28
+ - else
29
+ %p
30
+ You have the following client applications registered:
31
+
32
+ - @client_applications.each do |client|
33
+ - div_for client do
34
+ = link_to client.name, :action => :show, :id => client.id
35
+ = link_to 'Edit', edit_oauth_client_path(client)
36
+ = link_to 'Delete', oauth_client_path(client), :confirm => "Are you sure?", :method => :delete
37
+ %br
38
+ %h3
39
+ = link_to "Register your application", :action => :new
@@ -0,0 +1,5 @@
1
+ %h1 Register a new application
2
+
3
+ - form_for :client_application, :url => { :action => :create } do |f|
4
+ = render :partial => "form", :locals => { :f => f }
5
+ = submit_tag "Register"
@@ -0,0 +1,17 @@
1
+ %h1 Authorize access to your account
2
+ %p
3
+ Would you like to authorize
4
+ = link_to @client_application.name,@client_application.url
5
+ (
6
+ = link_to @client_application.url,@client_application.url
7
+ ) to access your account?
8
+ - form_tag authorize_url do
9
+ = hidden_field_tag "response_type", params[:response_type]
10
+ = hidden_field_tag "client_id", params[:client_id]
11
+ = hidden_field_tag "redirect_url", params[:redirect_url]
12
+ = hidden_field_tag "state", params[:state]
13
+ = hidden_field_tag "scope", params[:scope]
14
+ = check_box_tag 'authorize'
15
+ authorize access
16
+ %p
17
+ = submit_tag
@@ -0,0 +1,30 @@
1
+ %h1
2
+ OAuth details for
3
+ =@client_application.name
4
+ %p
5
+ %strong Consumer Key:
6
+ %code=@client_application.key
7
+ %p
8
+ %strong Consumer Secret:
9
+ %code=@client_application.secret
10
+
11
+ %p
12
+ %strong Request Token URL
13
+ %code
14
+ ="http#{'s' if request.ssl?}://#{request.host_with_port}#{@client_application.oauth_server.request_token_path}"
15
+
16
+ %p
17
+ %strong Access Token URL
18
+ %code
19
+ ="http#{'s' if request.ssl?}://#{request.host_with_port}#{@client_application.oauth_server.access_token_path}"
20
+
21
+ %p
22
+ %strong Authorize URL
23
+ %code
24
+ ="http#{'s' if request.ssl?}://#{request.host_with_port}#{@client_application.oauth_server.authorize_path}"
25
+
26
+ %p
27
+ We support hmac-sha1 (recommended) as well as plain text in ssl mode.
28
+
29
+ = link_to 'Edit', edit_oauth_client_path(@client_application)
30
+ = link_to 'Back', oauth_clients_path
@@ -0,0 +1,15 @@
1
+ module Mongoid
2
+ module Generators
3
+ class OauthConsumerGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../oauth_consumer_templates', __FILE__)
5
+
6
+ def check_class_collisions
7
+ class_collisions '', %w(ConsumerToken)
8
+ end
9
+
10
+ def copy_models
11
+ template 'consumer_token.rb', File.join('app/models', 'consumer_token.rb')
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,14 @@
1
+ require 'oauth/models/consumers/token'
2
+ class ConsumerToken
3
+ include Mongoid::Document
4
+ include Mongoid::Timestamps
5
+ include Oauth::Models::Consumers::Token
6
+
7
+ field :token, :type => String
8
+ field :secret, :type => String
9
+
10
+ index :token, :unique => true
11
+
12
+ referenced_in :user
13
+
14
+ end
@@ -0,0 +1,21 @@
1
+ module Mongoid
2
+ module Generators
3
+ class OauthProviderGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../oauth_provider_templates', __FILE__)
5
+
6
+ def check_class_collisions
7
+ class_collisions '', %w(ClientApplication OauthNonce RequestToken AccessToken OauthToken)
8
+ end
9
+
10
+ def copy_models
11
+ template 'client_application.rb', File.join('app/models', 'client_application.rb')
12
+ template 'oauth_token.rb', File.join('app/models', 'oauth_token.rb')
13
+ template 'request_token.rb', File.join('app/models', 'request_token.rb')
14
+ template 'access_token.rb', File.join('app/models', 'access_token.rb')
15
+ template 'oauth2_token.rb', File.join('app/models', 'oauth2_token.rb')
16
+ template 'oauth2_verifier.rb', File.join('app/models', 'oauth2_verifier.rb')
17
+ template 'oauth_nonce.rb', File.join('app/models', 'oauth_nonce.rb')
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,16 @@
1
+ class AccessToken < OauthToken
2
+ validates_presence_of :user, :secret
3
+ before_create :set_authorized_at
4
+
5
+ # Implement this to return a hash or array of the capabilities the access token has
6
+ # This is particularly useful if you have implemented user defined permissions.
7
+ # def capabilities
8
+ # {:invalidate=>"/oauth/invalidate",:capabilities=>"/oauth/capabilities"}
9
+ # end
10
+
11
+ protected
12
+
13
+ def set_authorized_at
14
+ self.authorized_at = Time.now
15
+ end
16
+ end
@@ -0,0 +1,71 @@
1
+ require 'oauth'
2
+
3
+ class ClientApplication
4
+ include Mongoid::Document
5
+ include Mongoid::Timestamps
6
+
7
+ field :name, :type => String
8
+ field :url, :type => String
9
+ field :support_url, :type => String
10
+ field :callback_url, :type => String
11
+ field :key, :type => String
12
+ field :secret, :type => String
13
+ field :secret, :type => String
14
+
15
+ index :key, :unique => true
16
+
17
+ referenced_in :user
18
+ references_many :tokens, :class_name => 'OauthToken'
19
+ references_many :access_tokens
20
+ references_many :oauth2_verifiers
21
+ references_many :oauth_tokens
22
+
23
+ validates_presence_of :name, :url, :key, :secret
24
+ validates_uniqueness_of :key
25
+ before_validation :generate_keys, :on => :create
26
+
27
+ validates_format_of :url, :with => /\Ahttp(s?):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i
28
+ validates_format_of :support_url, :with => /\Ahttp(s?):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i, :allow_blank=>true
29
+ validates_format_of :callback_url, :with => /\Ahttp(s?):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i, :allow_blank=>true
30
+
31
+ attr_accessor :token_callback_url
32
+
33
+ def self.find_token(token_key)
34
+ token = OauthToken.where(:token => token_key)
35
+ if token && token.authorized?
36
+ token
37
+ else
38
+ nil
39
+ end
40
+ end
41
+
42
+ def self.verify_request(request, options = {}, &block)
43
+ begin
44
+ signature = OAuth::Signature.build(request, options, &block)
45
+ return false unless OauthNonce.remember(signature.request.nonce, signature.request.timestamp)
46
+ value = signature.verify
47
+ value
48
+ rescue OAuth::Signature::UnknownSignatureMethod => e
49
+ false
50
+ end
51
+ end
52
+
53
+ def oauth_server
54
+ @oauth_server ||= OAuth::Server.new("http://your.site")
55
+ end
56
+
57
+ def credentials
58
+ @oauth_client ||= OAuth::Consumer.new(key, secret)
59
+ end
60
+
61
+ # If your application requires passing in extra parameters handle it here
62
+ def create_request_token(params={})
63
+ RequestToken.create :client_application => self, :callback_url=>self.token_callback_url
64
+ end
65
+
66
+ protected
67
+ def generate_keys
68
+ self.key = OAuth::Helper.generate_key(40)[0,40]
69
+ self.secret = OAuth::Helper.generate_key(40)[0,40]
70
+ end
71
+ end
@@ -0,0 +1,5 @@
1
+ class Oauth2Token < AccessToken
2
+ def as_json(options={})
3
+ {:access_token=>token}
4
+ end
5
+ end