danski-ooh-auth 0.1.2

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 (47) hide show
  1. data/LICENSE +20 -0
  2. data/Rakefile +58 -0
  3. data/app/controllers/application.rb +16 -0
  4. data/app/controllers/authenticating_clients.rb +60 -0
  5. data/app/controllers/tokens.rb +94 -0
  6. data/app/helpers/application_helper.rb +64 -0
  7. data/app/helpers/authenticating_clients_helper.rb +5 -0
  8. data/app/helpers/authentications_helper.rb +5 -0
  9. data/app/models/authenticating_client.rb +12 -0
  10. data/app/models/authenticating_client/dm_authenticating_client.rb +71 -0
  11. data/app/models/token.rb +12 -0
  12. data/app/models/token/dm_token.rb +150 -0
  13. data/app/views/authenticating_clients/_help.html.erb +1 -0
  14. data/app/views/authenticating_clients/edit.html.erb +27 -0
  15. data/app/views/authenticating_clients/index.html.erb +24 -0
  16. data/app/views/authenticating_clients/new.html.erb +47 -0
  17. data/app/views/authenticating_clients/show.html.erb +40 -0
  18. data/app/views/layout/ooh_auth.html.erb +23 -0
  19. data/app/views/tokens/create.html.erb +34 -0
  20. data/app/views/tokens/edit.html.erb +4 -0
  21. data/app/views/tokens/new.html.erb +52 -0
  22. data/app/views/tokens/show.html.erb +1 -0
  23. data/lib/ooh-auth.rb +103 -0
  24. data/lib/ooh-auth/authentication_mixin.rb +13 -0
  25. data/lib/ooh-auth/controller_mixin.rb +38 -0
  26. data/lib/ooh-auth/key_generators.rb +57 -0
  27. data/lib/ooh-auth/merbtasks.rb +103 -0
  28. data/lib/ooh-auth/request_verification_mixin.rb +160 -0
  29. data/lib/ooh-auth/slicetasks.rb +18 -0
  30. data/lib/ooh-auth/spectasks.rb +65 -0
  31. data/lib/ooh-auth/strategies/oauth.rb +16 -0
  32. data/public/javascripts/master.js +0 -0
  33. data/public/stylesheets/master.css +2 -0
  34. data/readme.markdown +43 -0
  35. data/spec/controllers/application_spec.rb +35 -0
  36. data/spec/controllers/authenticating_clients_spec.rb +119 -0
  37. data/spec/controllers/tokens_spec.rb +173 -0
  38. data/spec/merb-auth-slice-fullfat_spec.rb +41 -0
  39. data/spec/models/authenticating_client_spec.rb +44 -0
  40. data/spec/models/oauth_strategy_spec.rb +48 -0
  41. data/spec/models/request_verification_mixin_spec.rb +121 -0
  42. data/spec/models/token_spec.rb +139 -0
  43. data/spec/spec_fixtures.rb +19 -0
  44. data/spec/spec_helper.rb +107 -0
  45. data/stubs/app/controllers/application.rb +2 -0
  46. data/stubs/app/controllers/main.rb +2 -0
  47. metadata +133 -0
@@ -0,0 +1,139 @@
1
+ require File.join( File.dirname(__FILE__), '..', "spec_helper" )
2
+
3
+ describe OohAuth::Token do
4
+
5
+ before :each do
6
+ @authenticating_clients = 3.of {OohAuth::AuthenticatingClient.gen}
7
+ @users = 3.of {user_class.gen}
8
+ @date = DateTime.civil(2009)
9
+ end
10
+
11
+ it "should be creatable as a request key for an application" do
12
+ a = OohAuth::Token.create_request_key(@authenticating_clients.first, @date)
13
+ a.expires.should == @date
14
+ a.authenticating_client.should == @authenticating_clients.first
15
+ a.key.should_not be_blank
16
+ a.user.should be_nil
17
+ a.should be_valid
18
+ a.activated?.should be_false
19
+ end
20
+
21
+ it "should generate a unique token_key upon creation" do
22
+ a = OohAuth::Token.create_request_key(@authenticating_clients.first, @date)
23
+ a.token_key.should match(/[a-zA-Z0-9]{10}/)
24
+ end
25
+ it "should generate a unique token and apply the given expiry upon activation" do
26
+ a = OohAuth::Token.create_request_key(@authenticating_clients.first, @date)
27
+ result = a.activate!(@users.first)
28
+ a.should be_valid
29
+ result.should be_true
30
+ a.token_key.should match(/[a-zA-Z0-9]{10}/)
31
+ a.activated?.should be_true
32
+ end
33
+ it "should not activate if no user has been specified" do
34
+ a = OohAuth::Token.create_request_key(@authenticating_clients.first, @date)
35
+ a.activate!(nil).should be_false
36
+ a.activated?.should be_false
37
+ end
38
+
39
+ it "should not change token_key code on save if one was already set" do
40
+ a = OohAuth::Token.create_request_key(@authenticating_clients.first, @date)
41
+ a.new_record?.should be_false
42
+ a.activate!(@users.first).should be_true
43
+ t = a.token_key
44
+ a.save
45
+ a.token_key.should == t
46
+ end
47
+
48
+ it "should be findable with ::get_token" do
49
+ a = OohAuth::Token.create_request_key(@authenticating_clients.first, @date)
50
+ OohAuth::Token.get_token(a.token_key).should == a
51
+ end
52
+
53
+ it "should get a new key when activated" do
54
+ a = OohAuth::Token.create_request_key(@authenticating_clients.first, @date)
55
+ token = a.token_key
56
+ token.should_not be_blank
57
+ a.activate!(@users.first).should be_true
58
+ a.token_key.should_not == token
59
+ a.token_key.should_not be_blank
60
+ end
61
+
62
+ it "should generate a secret on first save" do
63
+ a = OohAuth::Token.create_request_key(@authenticating_clients.first, @date)
64
+ a.secret.should match(/[a-zA-Z0-9]{10}/)
65
+ end
66
+ it "should not change the secret on further saves" do
67
+ a = OohAuth::Token.create_request_key(@authenticating_clients.first, @date)
68
+ secret = a.secret
69
+ a.expires = 1.year.since
70
+ a.save.should be_true
71
+ a.secret.should == secret
72
+ end
73
+
74
+ it "should determine if the object is editable by a given user" do
75
+ a = OohAuth::Token.create_request_key(@authenticating_clients.first, @date)
76
+ a.activate!(@users.first)
77
+ a.editable_by_user?(@users.first).should be_true
78
+ a.editable_by_user?(user_class.gen).should be_false
79
+ end
80
+
81
+ it "should return default permissions if permissions are not set" do
82
+ a = OohAuth::Token.create_request_key(@authenticating_clients.first, @date)
83
+ OohAuth[:default_permissions].should_not be_nil
84
+ a.permissions = nil
85
+ a.permissions.should == OohAuth[:default_permissions]
86
+ a.permissions = "delete"
87
+ a.permissions.should == "delete"
88
+ end
89
+
90
+ describe "#authenticate!" do
91
+ before :each do
92
+ @user = user_class.gen
93
+ @activated = OohAuth::Token.create_request_key(@authenticating_clients.first, @date)
94
+ @activated.activate!(@user).should be_true
95
+ @unactivated = OohAuth::Token.create_request_key(@authenticating_clients[1], @date)
96
+ end
97
+
98
+ it "should not authenticate a user when given an incorrect API key and an activated token_key" do
99
+ OohAuth::Token.authenticate!("DSFARGEG", @activated.token_key).should be_nil
100
+ end
101
+ it "should not authenticate a user when given a correct API key and an unactivated key token_key" do
102
+ OohAuth::Token.authenticate!(@authenticating_clients.first.api_key, @unactivated.token_key).should be_nil
103
+ end
104
+ it "should not authenticate a user when given a correct API key but an incorrect token_key" do
105
+ OohAuth::Token.authenticate!(@authenticating_clients.first.api_key, "DSFARGEG").should be_nil
106
+ end
107
+ it "should authenticate a user when given a correct API key and a correct, activated token_key" do
108
+ #@a.user_id.should == ""
109
+ OohAuth::Token.authenticate!(@authenticating_clients.first.api_key, @activated.token_key).should == @user
110
+ end
111
+ end
112
+
113
+ describe "transformations" do
114
+ before :each do
115
+ @user = user_class.gen
116
+ @a = OohAuth::Token.create_request_key(@authenticating_clients.first, @date)
117
+ @a.new_record?.should be_false
118
+ end
119
+
120
+ it "should transform as a 'request_key' bundle when not activated" do
121
+ @a.activated?.should be_false
122
+ @a.to_xml.should contain(@a.token_key)
123
+ @a.to_json.should contain(@a.token_key)
124
+ @a.to_json.should contain("request_key")
125
+ @a.to_yaml.should contain(@a.token_key)
126
+ @a.to_yaml.should contain("request_key")
127
+ end
128
+ it "should transform as a 'access_key' bundle when activated" do
129
+ @a.activate!(@user).should be_true
130
+ @a.token_key.should match(/[0-9A-Za-z]{10}/)
131
+ @a.to_xml.should contain(@a.token_key)
132
+ @a.to_json.should contain(@a.token_key)
133
+ @a.to_json.should contain("access_key")
134
+ @a.to_yaml.should contain(@a.token_key)
135
+ @a.to_yaml.should contain("access_key")
136
+ end
137
+ end
138
+
139
+ end
@@ -0,0 +1,19 @@
1
+ OohAuth::Mocks::User.fixture{{
2
+ :name => (name = /\w+/.gen),
3
+ :login => name,
4
+ :email => "#{name}@test.com",
5
+ :password => (password = "#{name}_goodpass"),
6
+ :password_confirmation => password
7
+ }}
8
+
9
+ OohAuth::AuthenticatingClient.fixture{{
10
+ :name => /\w+/.gen,
11
+ :web_url => "http://www.#{ /\w+/.gen }.com/client/",
12
+ :api_key => /\w+/.gen,
13
+ :secret => /\w+/.gen,
14
+ :kind => /desktop|web/.gen
15
+ }}
16
+
17
+ OohAuth::Token.fixture{{
18
+
19
+ }}
@@ -0,0 +1,107 @@
1
+ require 'rubygems'
2
+ require 'merb-core'
3
+ require 'merb-slices'
4
+ require 'spec'
5
+
6
+ # Add ooh-auth.rb to the search path
7
+ Merb::Plugins.config[:merb_slices][:auto_register] = true
8
+ Merb::Plugins.config[:merb_slices][:search_path] = File.join(File.dirname(__FILE__), '..', 'lib', 'ooh-auth.rb')
9
+ require Merb::Plugins.config[:merb_slices][:search_path]
10
+
11
+ # Using Merb.root below makes sure that the correct root is set for
12
+ # - testing standalone, without being installed as a gem and no host application
13
+ # - testing from within the host application; its root will be used
14
+ Merb.start_environment(
15
+ :testing => true,
16
+ :adapter => 'runner',
17
+ :environment => ENV['MERB_ENV'] || 'test',
18
+ :session_store => 'memory'
19
+ )
20
+
21
+ module Merb
22
+ module Test
23
+ module SliceHelper
24
+
25
+ # The absolute path to the current slice
26
+ def current_slice_root
27
+ @current_slice_root ||= File.expand_path(File.join(File.dirname(__FILE__), '..'))
28
+ end
29
+
30
+ # Whether the specs are being run from a host application or standalone
31
+ def standalone?
32
+ #raise StandardError, "Merb.root #{Merb.root.inspect} ::OohAuth.root #{::OohAuth.root.inspect}"
33
+ File.join(Merb.root, "") == File.join(::OohAuth.root, "")
34
+ end
35
+
36
+ def user_class
37
+ Merb::Authentication.user_class
38
+ end
39
+
40
+ def noko(document)
41
+ Nokogiri::HTML(document)
42
+ end
43
+
44
+ # Produces a signed FakeRequest ready to be used when testing any action that requires signing.
45
+ def request_signed_by(client, get_params={}, post_params={}, env={}, opts={})
46
+ raise RuntimeError, "client #{client.inspect} is not a saved record, has errors #{client.errors.inspect}" if client.new_record?
47
+ get_params = {
48
+ :oauth_consumer_key=>client.api_key
49
+ }.merge(get_params)
50
+ # Prepare headers
51
+ env = {
52
+ :request_method => "GET",
53
+ :http_host => "test.fullfat.com",
54
+ :request_uri=>"/secrets/"
55
+ }.merge(env)
56
+ env[:query_string] = get_params.collect{|k,v| "#{k}=#{v}"}.join("&")
57
+ # Extras
58
+ opts = {
59
+ :post_body=>post_params.collect{|k,v| "#{k}=#{v}"}.join("&")
60
+ }.merge(opts)
61
+
62
+ unsigned = fake_request(env, opts)
63
+ get_params[:oauth_signature] ||= Merb::Parse.escape(unsigned.build_signature)
64
+ env[:query_string] = get_params.collect{|k,v| "#{k}=#{v}"}.join("&")
65
+
66
+ signed = fake_request(env, opts)
67
+ #raise RuntimeError, "Request not properly signed. Got: #{signed.uri}?#{signed.params.collect{|k,v|"#{k}=#{v}"}.join("&")}, expected: #{signed.signature_base_string} / #{signed.signature_secret}" unless signed.signed?
68
+ #signed
69
+ end
70
+
71
+ # Signs a URL like "/controller/action" with the correct signature to avoid triggering the
72
+ # ensure_signed filter method.
73
+ def sign_url_with(client, url, params={})
74
+ signed = request_signed_by(client, params, {}, {:request_uri=>url, :http_host=>"localhost"})
75
+ return "#{signed.uri}?#{signed.query_string}"
76
+ end
77
+
78
+ # Override for buggy freaking redirect_to assertion in merb 0.9.11.
79
+ # duplicates syntax of old version, so can be safely removed once
80
+ # http://merb.lighthouseapp.com/projects/7433-merb/tickets/949-redirect_to-assertion-errors-on-success-under-some-setups
81
+ # is fixed.
82
+ def redirect_to(url)
83
+ simple_matcher("redirect to #{url.inspect}") do |controller, matcher|
84
+ actual_url = controller.rack_response[1]["Location"]
85
+ matcher.failure_message = "expected to be redirected to #{url.inspect} but instead was redirected to #{actual_url.inspect}"
86
+ actual_url == url
87
+ end
88
+ end
89
+
90
+ end
91
+ end
92
+ end
93
+
94
+ # this loads all plugins required in your init file so don't add them
95
+ # here again, Merb will do it for you
96
+ #Merb.start_environment(:testing => true, :adapter => 'runner', :environment => ENV['MERB_ENV'] || 'test')
97
+ # Migrate that shit
98
+ DataMapper.auto_migrate!
99
+ # Load fixtures
100
+ require File.join(File.dirname(__FILE__), 'spec_fixtures')
101
+
102
+ Spec::Runner.configure do |config|
103
+ config.include(Merb::Test::ViewHelper)
104
+ config.include(Merb::Test::RouteHelper)
105
+ config.include(Merb::Test::ControllerHelper)
106
+ config.include(Merb::Test::SliceHelper)
107
+ end
@@ -0,0 +1,2 @@
1
+ class OohAuth::Application < Merb::Controller
2
+ end
@@ -0,0 +1,2 @@
1
+ class OohAuth::Main < OohAuth::Application
2
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: danski-ooh-auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Dan Glegg
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-10-22 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: ruby-hmac
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.3.2
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: merb-slices
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 0.9.10
32
+ version:
33
+ description: Merb slice that adds OAuth provider capabilities to any merb-auth application.
34
+ email: dan@angryameoba.co.uk
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files:
40
+ - readme.markdown
41
+ - LICENSE
42
+ files:
43
+ - LICENSE
44
+ - readme.markdown
45
+ - Rakefile
46
+ - lib/ooh-auth
47
+ - lib/ooh-auth/authentication_mixin.rb
48
+ - lib/ooh-auth/controller_mixin.rb
49
+ - lib/ooh-auth/key_generators.rb
50
+ - lib/ooh-auth/merbtasks.rb
51
+ - lib/ooh-auth/request_verification_mixin.rb
52
+ - lib/ooh-auth/slicetasks.rb
53
+ - lib/ooh-auth/spectasks.rb
54
+ - lib/ooh-auth/strategies
55
+ - lib/ooh-auth/strategies/oauth.rb
56
+ - lib/ooh-auth.rb
57
+ - spec/controllers
58
+ - spec/controllers/application_spec.rb
59
+ - spec/controllers/authenticating_clients_spec.rb
60
+ - spec/controllers/tokens_spec.rb
61
+ - spec/merb-auth-slice-fullfat_spec.rb
62
+ - spec/models
63
+ - spec/models/authenticating_client_spec.rb
64
+ - spec/models/oauth_strategy_spec.rb
65
+ - spec/models/request_verification_mixin_spec.rb
66
+ - spec/models/token_spec.rb
67
+ - spec/spec_fixtures.rb
68
+ - spec/spec_helper.rb
69
+ - app/controllers
70
+ - app/controllers/application.rb
71
+ - app/controllers/authenticating_clients.rb
72
+ - app/controllers/tokens.rb
73
+ - app/helpers
74
+ - app/helpers/application_helper.rb
75
+ - app/helpers/authenticating_clients_helper.rb
76
+ - app/helpers/authentications_helper.rb
77
+ - app/models
78
+ - app/models/authenticating_client
79
+ - app/models/authenticating_client/dm_authenticating_client.rb
80
+ - app/models/authenticating_client.rb
81
+ - app/models/token
82
+ - app/models/token/dm_token.rb
83
+ - app/models/token.rb
84
+ - app/views
85
+ - app/views/authenticating_clients
86
+ - app/views/authenticating_clients/_help.html.erb
87
+ - app/views/authenticating_clients/edit.html.erb
88
+ - app/views/authenticating_clients/index.html.erb
89
+ - app/views/authenticating_clients/new.html.erb
90
+ - app/views/authenticating_clients/show.html.erb
91
+ - app/views/layout
92
+ - app/views/layout/ooh_auth.html.erb
93
+ - app/views/tokens
94
+ - app/views/tokens/create.html.erb
95
+ - app/views/tokens/edit.html.erb
96
+ - app/views/tokens/new.html.erb
97
+ - app/views/tokens/show.html.erb
98
+ - public/javascripts
99
+ - public/javascripts/master.js
100
+ - public/stylesheets
101
+ - public/stylesheets/master.css
102
+ - stubs/app
103
+ - stubs/app/controllers
104
+ - stubs/app/controllers/application.rb
105
+ - stubs/app/controllers/main.rb
106
+ has_rdoc: true
107
+ homepage: http://github.com/danski/ooh-auth
108
+ post_install_message:
109
+ rdoc_options: []
110
+
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: "0"
118
+ version:
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: "0"
124
+ version:
125
+ requirements: []
126
+
127
+ rubyforge_project: merb
128
+ rubygems_version: 1.2.0
129
+ signing_key:
130
+ specification_version: 2
131
+ summary: Merb Slice that provides RESTful authentication functionality for your application.
132
+ test_files: []
133
+