oauned 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2010 Damien MATHIEU
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.
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # Oauned
2
+
3
+ Rails Engine that lets you become an OAuth Provider.
4
+
5
+ ## Installation
6
+
7
+ You can use the latest gem which is on rubygems.
8
+
9
+ gem install oauned
10
+
11
+ Don't forget to add it to your Gemfile !
12
+
13
+ Then, you must create the local models.
14
+
15
+ rails g oauned:install
16
+
17
+ This will create three models in your application : `Application`, `Authorization`, `Connection`
18
+ You'll need a fourth one, which is not created : `User`. The authentication management gem you're using should be creating it, not us.
19
+
20
+ You can easily rename those models. Just change their name and the relations names between them.
21
+
22
+ A route is also created.
23
+
24
+ scope '/oauth' { oauned_routing }
25
+
26
+ What's important is the call to `oauned_routing`. Wherever you call this method in your routes, the oauth routes will be created.
27
+ They can be created several times but it's not really advised.
28
+ In the above case, they'll be created in the `/oauth` path.
29
+
30
+ You can now start your rails server, and start connecting to your application using OAuth.
31
+
32
+ ## Controller Helpers
33
+
34
+ There are several controller helpers intended to allow you to manage your oauth connections.
35
+
36
+ - deny_auth - This is a class method. When called, the specified actions won't be accessible while using OAuth.
37
+ `deny_oauth, :only => :index`
38
+ The `:only` and `:except` options are available.
39
+
40
+ - oauth_user - This represents the oauth_user. You almost never need to use it.
41
+ - current_user - This method usually represents your user, whether he's connected "normally" or via OAuth.
42
+ - oauth_allowed? - Has oauth been allowed or denied for the current action ?
43
+
44
+ ## Personnalize the view
45
+
46
+ When a user tries to connect via OAuth, he'll see a page asking for acceptation. You can personalize this page.
47
+ Create the file `app/views/oauned/oauth/index.html.erb` (or any rendering engine you wish to use other than erb) and put your content in it.
48
+ You can find the default view at [app/views/oauned/oauth/index.html.erb](https://github.com/dmathieu/oauned/blob/master/app/views/oauned/oauth/index.html.erb).
49
+
50
+ ## Let the users create and manage applications
51
+
52
+ OAuned manages only user authentication. It's your task to allow your users to create new applications and provide them an interface to see what applications they've accepted.
53
+ To do that, you can manipulate the models directly.
54
+
55
+ * **Application** - Represents any application created.
56
+ * **Connection** - Represents a connection between a user and an application.
57
+
58
+ The **Authorization** model is used only to authorize the user when establishing the connection. You shouldn't use it.
59
+
60
+ ## Contributing
61
+
62
+ We're open to any contribution. It has to be tested properly though.
63
+
64
+ * [Fork](http://help.github.com/forking/) the project
65
+ * Do your changes and commit them to your repository
66
+ * Test your changes. We won't accept any untested contributions (except if they're not testable).
67
+ * Create an [issue](https://github.com/dmathieu/oauned/issues) with a link to your commits.
68
+
69
+ ## Maintainers
70
+
71
+ * Damien MATHIEU (http://github.com/dmathieu)
72
+
73
+ ## License
74
+ MIT License. Copyright 2010 Damien MATHIEU. http://dmathieu.com
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ # encoding: UTF-8
2
+ require 'rubygems'
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ require 'rake'
10
+ require 'rake/rdoctask'
11
+
12
+
13
+ require 'rspec/core'
14
+ require 'rspec/core/rake_task'
15
+ task :default => :spec
16
+ RSpec::Core::RakeTask.new(:spec)
17
+
18
+ Rake::RDocTask.new(:rdoc) do |rdoc|
19
+ rdoc.rdoc_dir = 'rdoc'
20
+ rdoc.title = 'Oauned'
21
+ rdoc.options << '--line-numbers' << '--inline-source'
22
+ rdoc.rdoc_files.include('README.rdoc')
23
+ rdoc.rdoc_files.include('lib/**/*.rb')
24
+ end
@@ -0,0 +1,14 @@
1
+ module Oauned
2
+ module Generators
3
+ module Helpers
4
+
5
+ def model_exists?(model)
6
+ File.exists?(File.join(destination_root, model_path(model)))
7
+ end
8
+
9
+ def model_path(model)
10
+ File.join("app", "models", "#{model}.rb")
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,49 @@
1
+ require 'rails/generators/active_record'
2
+ require 'generators/oauned/helpers'
3
+
4
+ module Oauned
5
+ module Generators
6
+ class InstallGenerator < Rails::Generators::Base
7
+ source_root File.expand_path("../templates", __FILE__)
8
+ include Oauned::Generators::Helpers
9
+
10
+ desc "Creates the oauned routes and models"
11
+ def add_oauned_routes
12
+ route "scope '/scoped' { oauned_routing }"
13
+ end
14
+
15
+ def create_models
16
+ [:application, :authorization, :connection].each do |model|
17
+ invoke "active_record:model", [model], :migration => false unless model_exists?(model) && behavior == :invoke
18
+ end
19
+ end
20
+
21
+ def inject_application_content
22
+ inject_into_class model_path(:application), Application, <<EOS if model_exists?(:authorization)
23
+ include Oauned::Models::Application
24
+
25
+ has_many :authorizations
26
+ has_many :connections
27
+ EOS
28
+ end
29
+
30
+ def inject_authorization_content
31
+ inject_into_class model_path(:authorization), Application, <<EOS if model_exists?(:authorization)
32
+ include Oauned::Models::Authorization
33
+
34
+ belongs_to :user
35
+ belongs_to :application
36
+ EOS
37
+ end
38
+
39
+ def inject_connection_content
40
+ inject_into_class model_path(:connection), Application, <<EOS if model_exists?(:authorization)
41
+ include Oauned::Models::Connection
42
+
43
+ belongs_to :user
44
+ belongs_to :application
45
+ EOS
46
+ end
47
+ end
48
+ end
49
+ end
data/lib/oauned.rb ADDED
@@ -0,0 +1,7 @@
1
+ module Oauned
2
+ autoload :ControllerMethods, 'oauned/controller_methods'
3
+ autoload :Models, 'oauned/models'
4
+
5
+ end
6
+
7
+ require 'oauned/rails'
@@ -0,0 +1,45 @@
1
+ module Oauned
2
+ module ControllerMethods
3
+ def self.included(klass)
4
+ klass.class_eval do
5
+ cattr_accessor :oauth_options, :oauth_options_proc
6
+
7
+ protected
8
+ def self.deny_oauth(options = {}, &block)
9
+ raise 'options cannot contain both :only and :except' if options[:only] && options[:except]
10
+
11
+ [:only, :except].each do |k|
12
+ if values = options[k]
13
+ options[k] = Array(values).map(&:to_s).to_set
14
+ end
15
+ end
16
+ self.oauth_options = options
17
+ self.oauth_options_proc = block
18
+ end
19
+
20
+ def oauth_user
21
+ @oauth_user ||= oauth_allowed? ? user_from_oauth : nil
22
+ end
23
+
24
+ alias :normal_user :current_user
25
+ def current_user
26
+ normal_user || oauth_user
27
+ end
28
+
29
+ private
30
+ def user_from_oauth
31
+ token = Connection.where(:access_token => params[:access_token]).first
32
+ token.user if (token && !token.expired?)
33
+ end
34
+
35
+ def oauth_allowed?
36
+ return true if (oauth_options_proc && !oauth_options_proc.call(self)) || oauth_options.nil?
37
+ return false if oauth_options.empty?
38
+ return true if oauth_options[:only] && !oauth_options[:only].include?(action_name)
39
+ return true if oauth_options[:except] && oauth_options[:except].include?(action_name)
40
+ false
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,5 @@
1
+ module Oauned::Models
2
+ autoload :Application, 'oauned/models/application'
3
+ autoload :Authorization, 'oauned/models/authorization'
4
+ autoload :Connection, 'oauned/models/connection'
5
+ end
@@ -0,0 +1,16 @@
1
+ module Oauned::Models::Application
2
+ def self.included(klass)
3
+ klass.class_eval do
4
+ before_create :set_default
5
+
6
+ def authorize!(user)
7
+ Authorization.create!(:user_id => user.id, :application_id => id)
8
+ end
9
+
10
+ private
11
+ def set_default
12
+ self.consumer_secret = SecureRandom.hex(40)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,25 @@
1
+ module Oauned::Models::Authorization
2
+ def self.included(klass)
3
+ klass.class_eval do
4
+ before_create :set_default
5
+
6
+ def expires_in
7
+ (expires_at - Time.now).to_i
8
+ end
9
+ def expired?
10
+ expires_in <= 0
11
+ end
12
+
13
+ def tokenize!
14
+ self.destroy
15
+ Connection.create!(:user_id => user.id, :application_id => application.id)
16
+ end
17
+
18
+ private
19
+ def set_default
20
+ self.code = SecureRandom.hex(20)
21
+ self.expires_at = 1.hour.from_now
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ module Oauned::Models::Connection
2
+ def self.included(klass)
3
+ klass.class_eval do
4
+ before_create :set_default
5
+
6
+ def expires_in
7
+ (expires_at - Time.now).to_i
8
+ end
9
+ def expired?
10
+ expires_in <= 0
11
+ end
12
+
13
+ def refresh
14
+ self.destroy
15
+ Connection.create!(:user_id => user.id, :application_id => application.id)
16
+ end
17
+
18
+ private
19
+ def set_default
20
+ self.access_token = SecureRandom.hex(20)
21
+ self.expires_at = 1.hour.from_now
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,15 @@
1
+ require 'oauned/rails/routing'
2
+
3
+ module Oauned
4
+ class Engine < ::Rails::Engine
5
+ # Force routes to be loaded if we are doing any eager load.
6
+ config.before_eager_load { |app| app.reload_routes! }
7
+
8
+ initializer "oauned.controller_helpers", :after=> :disable_dependency_loading do
9
+ ActiveSupport.on_load(:action_controller) do
10
+ include Oauned::ControllerMethods
11
+
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,10 @@
1
+ module ActionDispatch::Routing
2
+ class Mapper
3
+
4
+ def oauned_routing
5
+ get '/' => 'oauned/oauth#index', :as => 'oauth'
6
+ post '/' => 'oauned/oauth#authorize'
7
+ post '/token' => 'oauned/oauth#token'
8
+ end
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oauned
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors: []
12
+
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-21 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Rails Engine to be an Oauth Provider
22
+ email:
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/generators/oauned/helpers.rb
31
+ - lib/generators/oauned/install_generator.rb
32
+ - lib/oauned/controller_methods.rb
33
+ - lib/oauned/models/application.rb
34
+ - lib/oauned/models/authorization.rb
35
+ - lib/oauned/models/connection.rb
36
+ - lib/oauned/models.rb
37
+ - lib/oauned/rails/routing.rb
38
+ - lib/oauned/rails.rb
39
+ - lib/oauned.rb
40
+ - MIT-LICENSE
41
+ - Rakefile
42
+ - README.md
43
+ has_rdoc: true
44
+ homepage:
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options: []
49
+
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.7
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Oauth Provider
75
+ test_files: []
76
+