tpitale-rack-oauth2-server 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. data/CHANGELOG +202 -0
  2. data/Gemfile +16 -0
  3. data/MIT-LICENSE +21 -0
  4. data/README.rdoc +604 -0
  5. data/Rakefile +90 -0
  6. data/VERSION +1 -0
  7. data/bin/oauth2-server +206 -0
  8. data/lib/rack-oauth2-server.rb +4 -0
  9. data/lib/rack/oauth2/admin/css/screen.css +347 -0
  10. data/lib/rack/oauth2/admin/images/loading.gif +0 -0
  11. data/lib/rack/oauth2/admin/images/oauth-2.png +0 -0
  12. data/lib/rack/oauth2/admin/js/application.coffee +220 -0
  13. data/lib/rack/oauth2/admin/js/jquery.js +166 -0
  14. data/lib/rack/oauth2/admin/js/jquery.tmpl.js +414 -0
  15. data/lib/rack/oauth2/admin/js/protovis-r3.2.js +277 -0
  16. data/lib/rack/oauth2/admin/js/sammy.js +5 -0
  17. data/lib/rack/oauth2/admin/js/sammy.json.js +5 -0
  18. data/lib/rack/oauth2/admin/js/sammy.oauth2.js +142 -0
  19. data/lib/rack/oauth2/admin/js/sammy.storage.js +5 -0
  20. data/lib/rack/oauth2/admin/js/sammy.title.js +5 -0
  21. data/lib/rack/oauth2/admin/js/sammy.tmpl.js +5 -0
  22. data/lib/rack/oauth2/admin/js/underscore.js +722 -0
  23. data/lib/rack/oauth2/admin/views/client.tmpl +58 -0
  24. data/lib/rack/oauth2/admin/views/clients.tmpl +52 -0
  25. data/lib/rack/oauth2/admin/views/edit.tmpl +80 -0
  26. data/lib/rack/oauth2/admin/views/index.html +39 -0
  27. data/lib/rack/oauth2/admin/views/no_access.tmpl +4 -0
  28. data/lib/rack/oauth2/models.rb +27 -0
  29. data/lib/rack/oauth2/models/access_grant.rb +54 -0
  30. data/lib/rack/oauth2/models/access_token.rb +129 -0
  31. data/lib/rack/oauth2/models/auth_request.rb +61 -0
  32. data/lib/rack/oauth2/models/client.rb +93 -0
  33. data/lib/rack/oauth2/rails.rb +105 -0
  34. data/lib/rack/oauth2/server.rb +458 -0
  35. data/lib/rack/oauth2/server/admin.rb +250 -0
  36. data/lib/rack/oauth2/server/errors.rb +104 -0
  37. data/lib/rack/oauth2/server/helper.rb +147 -0
  38. data/lib/rack/oauth2/server/practice.rb +79 -0
  39. data/lib/rack/oauth2/server/railtie.rb +24 -0
  40. data/lib/rack/oauth2/server/utils.rb +30 -0
  41. data/lib/rack/oauth2/sinatra.rb +71 -0
  42. data/rack-oauth2-server.gemspec +24 -0
  43. data/rails/init.rb +11 -0
  44. data/test/admin/api_test.rb +228 -0
  45. data/test/admin/ui_test.rb +38 -0
  46. data/test/oauth/access_grant_test.rb +276 -0
  47. data/test/oauth/access_token_test.rb +311 -0
  48. data/test/oauth/authorization_test.rb +298 -0
  49. data/test/oauth/server_methods_test.rb +292 -0
  50. data/test/rails2/app/controllers/api_controller.rb +40 -0
  51. data/test/rails2/app/controllers/application_controller.rb +2 -0
  52. data/test/rails2/app/controllers/oauth_controller.rb +17 -0
  53. data/test/rails2/config/environment.rb +19 -0
  54. data/test/rails2/config/environments/test.rb +0 -0
  55. data/test/rails2/config/routes.rb +13 -0
  56. data/test/rails3/app/controllers/api_controller.rb +40 -0
  57. data/test/rails3/app/controllers/application_controller.rb +2 -0
  58. data/test/rails3/app/controllers/oauth_controller.rb +17 -0
  59. data/test/rails3/config/application.rb +19 -0
  60. data/test/rails3/config/environment.rb +2 -0
  61. data/test/rails3/config/routes.rb +12 -0
  62. data/test/setup.rb +120 -0
  63. data/test/sinatra/my_app.rb +69 -0
  64. metadata +145 -0
@@ -0,0 +1,40 @@
1
+ class ApiController < ApplicationController
2
+
3
+ oauth_required :only=>[:private, :change]
4
+ oauth_required :only=>[:calc], :scope=>"math"
5
+
6
+ def public
7
+ if oauth.authenticated?
8
+ render :text=>"HAI from #{oauth.identity}"
9
+ else
10
+ render :text=>"HAI"
11
+ end
12
+ end
13
+
14
+ def private
15
+ render :text=>"Shhhh"
16
+ end
17
+
18
+ def change
19
+ render :text=>"Woot!"
20
+ end
21
+
22
+ def calc
23
+ render :text=>"2+2=4"
24
+ end
25
+
26
+ def list_tokens
27
+ render :text=>oauth.list_access_tokens("Batman").map(&:token).join(" ")
28
+ end
29
+
30
+ def user
31
+ render :text=>current_user.to_s
32
+ end
33
+
34
+ protected
35
+
36
+ def current_user
37
+ @current_user ||= oauth.identity if oauth.authenticated?
38
+ end
39
+
40
+ end
@@ -0,0 +1,2 @@
1
+ class ApplicationController < ActionController::Base
2
+ end
@@ -0,0 +1,17 @@
1
+ class OauthController < ApplicationController
2
+ before_filter do |c|
3
+ c.send :head, c.oauth.deny! if c.oauth.scope.include?("time-travel") # Only Superman can do that
4
+ end
5
+
6
+ def authorize
7
+ render :text=>"client: #{oauth.client.display_name}\nscope: #{oauth.scope.join(", ")}\nauthorization: #{oauth.authorization}"
8
+ end
9
+
10
+ def grant
11
+ head oauth.grant!(params["authorization"], "Batman")
12
+ end
13
+
14
+ def deny
15
+ head oauth.deny!(params["authorization"])
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ class << Rails
2
+ def vendor_rails?
3
+ false
4
+ end
5
+ end
6
+
7
+ Rails::Initializer.run do |config|
8
+ config.frameworks = [ :action_controller ]
9
+ config.action_controller.session = { :key=>"_myapp_session", :secret=>"Stay hungry. Stay foolish. -- Steve Jobs" }
10
+
11
+ config.after_initialize do
12
+ config.oauth.database = DATABASE
13
+ config.oauth.host = "example.org"
14
+ config.oauth.authenticator = lambda do |username, password|
15
+ "Batman" if username == "cowbell" && password == "more"
16
+ end
17
+ end
18
+ config.middleware.use Rack::OAuth2::Server::Admin.mount
19
+ end
@@ -0,0 +1,13 @@
1
+ ActionController::Routing::Routes.draw do |map|
2
+ # Authorization flow.
3
+ map.with_options :controller=>"oauth" do |oauth|
4
+ oauth.connect "oauth/authorize", :action=>"authorize"
5
+ oauth.connect "oauth/grant", :action=>"grant"
6
+ oauth.connect "oauth/deny", :action=>"deny"
7
+ end
8
+
9
+ # Resources we want to protect
10
+ map.with_options :controller=>"api" do |api|
11
+ api.connection ":action"
12
+ end
13
+ end
@@ -0,0 +1,40 @@
1
+ class ApiController < ApplicationController
2
+
3
+ oauth_required :only=>[:private, :change]
4
+ oauth_required :only=>[:calc], :scope=>"math"
5
+
6
+ def public
7
+ if oauth.authenticated?
8
+ render :text=>"HAI from #{oauth.identity}"
9
+ else
10
+ render :text=>"HAI"
11
+ end
12
+ end
13
+
14
+ def private
15
+ render :text=>"Shhhh"
16
+ end
17
+
18
+ def change
19
+ render :text=>"Woot!"
20
+ end
21
+
22
+ def calc
23
+ render :text=>"2+2=4"
24
+ end
25
+
26
+ def list_tokens
27
+ render :text=>oauth.list_access_tokens("Batman").map(&:token).join(" ")
28
+ end
29
+
30
+ def user
31
+ render :text=>current_user.to_s
32
+ end
33
+
34
+ protected
35
+
36
+ def current_user
37
+ @current_user ||= oauth.identity if oauth.authenticated?
38
+ end
39
+
40
+ end
@@ -0,0 +1,2 @@
1
+ class ApplicationController < ActionController::Base
2
+ end
@@ -0,0 +1,17 @@
1
+ class OauthController < ApplicationController
2
+ before_filter do |c|
3
+ c.send :head, c.oauth.deny! if c.oauth.scope.include?("time-travel") # Only Superman can do that
4
+ end
5
+
6
+ def authorize
7
+ render :text=>"client: #{oauth.client.display_name}\nscope: #{oauth.scope.join(", ")}\nauthorization: #{oauth.authorization}"
8
+ end
9
+
10
+ def grant
11
+ head oauth.grant!(params["authorization"], "Batman")
12
+ end
13
+
14
+ def deny
15
+ head oauth.deny!(params["authorization"])
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ require "action_controller/railtie"
2
+ module MyApp
3
+ class Application < Rails::Application
4
+ config.session_store :cookie_store, :key=>"_my_app_session"
5
+ config.secret_token = "Stay hungry. Stay foolish. -- Steve Jobs"
6
+ config.active_support.deprecation = :stderr
7
+
8
+ config.after_initialize do
9
+ config.oauth.database = DATABASE
10
+ config.oauth.host = "example.org"
11
+ config.oauth.authenticator = lambda do |username, password|
12
+ "Batman" if username == "cowbell" && password == "more"
13
+ end
14
+ config.middleware.use Rack::OAuth2::Server::Admin.mount
15
+ end
16
+ end
17
+ end
18
+ Rails.application.config.root = File.dirname(__FILE__) + "/.."
19
+ require Rails.root + "config/routes"
@@ -0,0 +1,2 @@
1
+ require File.expand_path('../application', __FILE__)
2
+ MyApp::Application.initialize!
@@ -0,0 +1,12 @@
1
+ MyApp::Application.routes.draw do
2
+ # Authorization flow.
3
+ match "oauth/authorize" => "oauth#authorize"
4
+ match "oauth/grant" => "oauth#grant"
5
+ match "oauth/deny" => "oauth#deny"
6
+
7
+ # Resources we want to protect
8
+ match ":action"=>"api"
9
+
10
+ mount Rack::OAuth2::Server::Admin, :at=>"oauth/admin"
11
+
12
+ end
@@ -0,0 +1,120 @@
1
+ require "bundler"
2
+ Bundler.setup
3
+ require "test/unit"
4
+ require "rack/test"
5
+ require "shoulda"
6
+ require "timecop"
7
+ require "ap"
8
+ require "json"
9
+ require "logger"
10
+ $: << File.dirname(__FILE__) + "/../lib"
11
+ $: << File.expand_path(File.dirname(__FILE__) + "/..")
12
+ require "rack/oauth2/server"
13
+ require "rack/oauth2/server/admin"
14
+
15
+
16
+ ENV["RACK_ENV"] = "test"
17
+ DATABASE = Mongo::Connection.new["test"]
18
+ FRAMEWORK = ENV["FRAMEWORK"] || "sinatra"
19
+
20
+
21
+ $logger = Logger.new("test.log")
22
+ $logger.level = Logger::DEBUG
23
+ Rack::OAuth2::Server::Admin.configure do |config|
24
+ config.set :logger, $logger
25
+ config.set :logging, true
26
+ config.set :raise_errors, true
27
+ config.set :dump_errors, true
28
+ config.oauth.logger = $logger
29
+ end
30
+
31
+
32
+ case FRAMEWORK
33
+ when "sinatra", nil
34
+
35
+ require "sinatra/base"
36
+ puts "Testing with Sinatra #{Sinatra::VERSION}"
37
+ require File.dirname(__FILE__) + "/sinatra/my_app"
38
+
39
+ class Test::Unit::TestCase
40
+ def app
41
+ Rack::Builder.new do
42
+ map("/oauth/admin") { run Server::Admin }
43
+ map("/") { run MyApp }
44
+ end
45
+ end
46
+
47
+ def config
48
+ MyApp.oauth
49
+ end
50
+ end
51
+
52
+ when "rails"
53
+
54
+ RAILS_ENV = "test"
55
+ RAILS_ROOT = File.dirname(__FILE__) + "/rails3"
56
+ begin
57
+ require "rails"
58
+ rescue LoadError
59
+ end
60
+
61
+ if defined?(Rails::Railtie)
62
+ # Rails 3.x
63
+ require "rack/oauth2/server/railtie"
64
+ require File.dirname(__FILE__) + "/rails3/config/environment"
65
+ puts "Testing with Rails #{Rails.version}"
66
+
67
+ class Test::Unit::TestCase
68
+ def app
69
+ ::Rails.application
70
+ end
71
+
72
+ def config
73
+ ::Rails.configuration.oauth
74
+ end
75
+ end
76
+
77
+ else
78
+ # Rails 2.x
79
+ RAILS_ROOT = File.dirname(__FILE__) + "/rails2"
80
+ require "initializer"
81
+ require "action_controller"
82
+ require File.dirname(__FILE__) + "/rails2/config/environment"
83
+ puts "Testing with Rails #{Rails.version}"
84
+
85
+ class Test::Unit::TestCase
86
+ def app
87
+ ActionController::Dispatcher.new
88
+ end
89
+
90
+ def config
91
+ ::Rails.configuration.oauth
92
+ end
93
+ end
94
+ end
95
+
96
+ else
97
+ puts "Unknown framework #{FRAMEWORK}"
98
+ exit -1
99
+ end
100
+
101
+
102
+ class Test::Unit::TestCase
103
+ include Rack::Test::Methods
104
+ include Rack::OAuth2
105
+
106
+ def setup
107
+ Server.database = DATABASE
108
+ Server::Admin.scope = %{read write}
109
+ @client = Server.register(:display_name=>"UberClient", :redirect_uri=>"http://uberclient.dot/callback", :scope=>%w{read write oauth-admin})
110
+ end
111
+
112
+ attr_reader :client, :end_user
113
+
114
+ def teardown
115
+ Server::Client.collection.drop
116
+ Server::AuthRequest.collection.drop
117
+ Server::AccessGrant.collection.drop
118
+ Server::AccessToken.collection.drop
119
+ end
120
+ end
@@ -0,0 +1,69 @@
1
+ require "rack/oauth2/sinatra"
2
+
3
+ class MyApp < Sinatra::Base
4
+ use Rack::Logger
5
+ set :sessions, true
6
+
7
+ register Rack::OAuth2::Sinatra
8
+ oauth.authenticator = lambda do |username, password|
9
+ "Batman" if username == "cowbell" && password == "more"
10
+ end
11
+ oauth.host = "example.org"
12
+ oauth.database = DATABASE
13
+
14
+
15
+ # 3. Obtaining End-User Authorization
16
+
17
+ before "/oauth/*" do
18
+ halt oauth.deny! if oauth.scope.include?("time-travel") # Only Superman can do that
19
+ end
20
+
21
+ get "/oauth/authorize" do
22
+ "client: #{oauth.client.display_name}\nscope: #{oauth.scope.join(", ")}\nauthorization: #{oauth.authorization}"
23
+ end
24
+
25
+ post "/oauth/grant" do
26
+ oauth.grant! "Batman"
27
+ end
28
+
29
+ post "/oauth/deny" do
30
+ oauth.deny!
31
+ end
32
+
33
+
34
+ # 5. Accessing a Protected Resource
35
+
36
+ before { @user = oauth.identity if oauth.authenticated? }
37
+
38
+ get "/public" do
39
+ if oauth.authenticated?
40
+ "HAI from #{oauth.identity}"
41
+ else
42
+ "HAI"
43
+ end
44
+ end
45
+
46
+ oauth_required "/private", "/change"
47
+
48
+ get "/private" do
49
+ "Shhhh"
50
+ end
51
+
52
+ post "/change" do
53
+ "Woot!"
54
+ end
55
+
56
+ oauth_required "/calc", :scope=>"math"
57
+
58
+ get "/calc" do
59
+ end
60
+
61
+ get "/user" do
62
+ @user
63
+ end
64
+
65
+ get "/list_tokens" do
66
+ oauth.list_access_tokens("Batman").map(&:token).join(" ")
67
+ end
68
+
69
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tpitale-rack-oauth2-server
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 2.2.1
6
+ platform: ruby
7
+ authors:
8
+ - Assaf Arkin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-05 00:00:00 -04:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rack
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ~>
23
+ - !ruby/object:Gem::Version
24
+ version: "1.1"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 2.3.11
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ description: Because you don't allow strangers into your app, and OAuth 2.0 is the new awesome.
39
+ email: assaf@labnotes.org
40
+ executables:
41
+ - oauth2-server
42
+ extensions: []
43
+
44
+ extra_rdoc_files:
45
+ - README.rdoc
46
+ - CHANGELOG
47
+ files:
48
+ - bin/oauth2-server
49
+ - lib/rack/oauth2/admin/css/screen.css
50
+ - lib/rack/oauth2/admin/images/loading.gif
51
+ - lib/rack/oauth2/admin/images/oauth-2.png
52
+ - lib/rack/oauth2/admin/js/application.coffee
53
+ - lib/rack/oauth2/admin/js/jquery.js
54
+ - lib/rack/oauth2/admin/js/jquery.tmpl.js
55
+ - lib/rack/oauth2/admin/js/protovis-r3.2.js
56
+ - lib/rack/oauth2/admin/js/sammy.js
57
+ - lib/rack/oauth2/admin/js/sammy.json.js
58
+ - lib/rack/oauth2/admin/js/sammy.oauth2.js
59
+ - lib/rack/oauth2/admin/js/sammy.storage.js
60
+ - lib/rack/oauth2/admin/js/sammy.title.js
61
+ - lib/rack/oauth2/admin/js/sammy.tmpl.js
62
+ - lib/rack/oauth2/admin/js/underscore.js
63
+ - lib/rack/oauth2/admin/views/client.tmpl
64
+ - lib/rack/oauth2/admin/views/clients.tmpl
65
+ - lib/rack/oauth2/admin/views/edit.tmpl
66
+ - lib/rack/oauth2/admin/views/index.html
67
+ - lib/rack/oauth2/admin/views/no_access.tmpl
68
+ - lib/rack/oauth2/models/access_grant.rb
69
+ - lib/rack/oauth2/models/access_token.rb
70
+ - lib/rack/oauth2/models/auth_request.rb
71
+ - lib/rack/oauth2/models/client.rb
72
+ - lib/rack/oauth2/models.rb
73
+ - lib/rack/oauth2/rails.rb
74
+ - lib/rack/oauth2/server/admin.rb
75
+ - lib/rack/oauth2/server/errors.rb
76
+ - lib/rack/oauth2/server/helper.rb
77
+ - lib/rack/oauth2/server/practice.rb
78
+ - lib/rack/oauth2/server/railtie.rb
79
+ - lib/rack/oauth2/server/utils.rb
80
+ - lib/rack/oauth2/server.rb
81
+ - lib/rack/oauth2/sinatra.rb
82
+ - lib/rack-oauth2-server.rb
83
+ - rails/init.rb
84
+ - test/admin/api_test.rb
85
+ - test/admin/ui_test.rb
86
+ - test/oauth/access_grant_test.rb
87
+ - test/oauth/access_token_test.rb
88
+ - test/oauth/authorization_test.rb
89
+ - test/oauth/server_methods_test.rb
90
+ - test/rails2/app/controllers/api_controller.rb
91
+ - test/rails2/app/controllers/application_controller.rb
92
+ - test/rails2/app/controllers/oauth_controller.rb
93
+ - test/rails2/config/environment.rb
94
+ - test/rails2/config/environments/test.rb
95
+ - test/rails2/config/routes.rb
96
+ - test/rails3/app/controllers/api_controller.rb
97
+ - test/rails3/app/controllers/application_controller.rb
98
+ - test/rails3/app/controllers/oauth_controller.rb
99
+ - test/rails3/config/application.rb
100
+ - test/rails3/config/environment.rb
101
+ - test/rails3/config/routes.rb
102
+ - test/setup.rb
103
+ - test/sinatra/my_app.rb
104
+ - CHANGELOG
105
+ - VERSION
106
+ - MIT-LICENSE
107
+ - README.rdoc
108
+ - Rakefile
109
+ - Gemfile
110
+ - rack-oauth2-server.gemspec
111
+ has_rdoc: true
112
+ homepage: http://github.com/flowtown/tpitale-rack-oauth2-server
113
+ licenses:
114
+ - MIT
115
+ post_install_message: To get started, run the command oauth2-server
116
+ rdoc_options:
117
+ - --title
118
+ - tpitale-rack-oauth2-server 2.2.1
119
+ - --main
120
+ - README.rdoc
121
+ - --webcvs
122
+ - http://github.com/tpitale/rack-oauth2-server
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: 1.8.7
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: "0"
137
+ requirements: []
138
+
139
+ rubyforge_project:
140
+ rubygems_version: 1.5.3
141
+ signing_key:
142
+ specification_version: 3
143
+ summary: OAuth 2.0 Authorization Server as a Rack module for ActiveRecord
144
+ test_files: []
145
+