vouch 0.0.3.pre1

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.
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Will automatically pull in this gem and all its
4
+ # dependencies specified in the gemspec
5
+ gem "vouch", :path => File.expand_path("..", __FILE__)
6
+
7
+ # These are development dependencies
8
+ gem "jeweler"
9
+ gem "rake"
10
+ gem "oauth2"
11
+
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2010 Daniel McNevin
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.
21
+
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ Vouch
2
+ =====
3
+
4
+ *Please Note*
5
+
6
+ This is very alpha code at the moment, I just extracted it from a project I am working on. Once it's at a more usable stage, I will be releasing it as a gem.
7
+
8
+ About
9
+ -----
10
+
11
+ Rails 3 engines to use OAuth2 for authentication
12
+
13
+ Client
14
+ ------
15
+
16
+ To use, include in your `Gemfile`
17
+
18
+ gem "vouch", :git => "git://github.com/dpmcnevin/vouch.git"
19
+
20
+ Run the generator
21
+
22
+ rails generate vouch:client
23
+
24
+
25
+ Provider
26
+ --------
27
+
28
+ Coming in future releases.
29
+
30
+ Maintainer
31
+ ----------
32
+ Dan McNevin - dpmcnevin@gmail.com
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require "bundler"
2
+ require 'jeweler'
3
+ Bundler.setup
4
+
5
+ begin
6
+ Jeweler::Tasks.new do |gemspec|
7
+ gemspec.name = "vouch"
8
+ gemspec.summary = "OAuth2 Server and Client Engines"
9
+ gemspec.description = "OAuth2 Server and Client Engines"
10
+ gemspec.email = "dpmcnevin@gmail.com"
11
+ gemspec.homepage = "http://github.com/dpmcnevin/vouch"
12
+ gemspec.authors = ["Daniel McNevin"]
13
+ end
14
+ rescue LoadError
15
+ puts "Jeweler not available. Install it with: gem install jeweler"
16
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.3.pre1
@@ -0,0 +1,73 @@
1
+ module Vouch
2
+ class AuthController < ApplicationController
3
+
4
+ unloadable
5
+
6
+ skip_before_filter :require_user
7
+ skip_before_filter :set_user
8
+ skip_before_filter :cache_tags
9
+
10
+ before_filter :read_config
11
+
12
+ def show
13
+ redirect_to client.web_server.authorize_url(
14
+ :redirect_uri => redirect_uri
15
+ )
16
+ end
17
+
18
+ def callback
19
+ access_token = client.web_server.get_access_token(params[:code], :redirect_uri => redirect_uri)
20
+ user = JSON.parse(access_token.get(@oauth_config["user_path"]))
21
+
22
+ if user
23
+ if user.is_a?(Hash) && has_roles?(user)
24
+ @user = User.find_or_create_by_email(user)
25
+ session[:user_id] = @user.id
26
+ session[:expires_at] = access_token.expires_at
27
+ redirect_to session[:return_to] || root_path
28
+ else
29
+ render :text => "Unauthorized", :status => 401
30
+ end
31
+ else
32
+ render :text => "Can't authenticate", :status => 401
33
+ end
34
+ end
35
+
36
+ def destroy
37
+ session[:user_id] = nil
38
+ redirect_to @oauth_config["logout_url"]
39
+ end
40
+
41
+ private
42
+
43
+ def read_config
44
+ @oauth_config = YAML.load_file("#{Rails.root}/config/oauth.yml")[Rails.env]
45
+
46
+ @oauth_config["authorize_path"] ||= '/oauth/authorize'
47
+ @oauth_config["access_token_path"] ||= '/oauth/access_token'
48
+ @oauth_config["user_path"] ||= '/oauth/user'
49
+ @oauth_config["callback_path"] ||= '/auth/callback'
50
+ end
51
+
52
+ def has_roles?(user)
53
+ return true unless @oauth_config["required_roles"].present?
54
+ user["roles"].is_a?(Array) && (user["roles"].to_a & @oauth_config["required_roles"].to_a).present?
55
+ end
56
+
57
+ def client
58
+ OAuth2::Client.new(@oauth_config["client_id"], @oauth_config["client_secret"],
59
+ :site => @oauth_config["site"],
60
+ :authorize_path => @oauth_config["authorize_path"],
61
+ :access_token_path => @oauth_config["access_token_path"]
62
+ )
63
+ end
64
+
65
+ def redirect_uri
66
+ uri = URI.parse(request.url)
67
+ uri.path = @oauth_config["callback_path"]
68
+ uri.query = nil
69
+ uri.to_s
70
+ end
71
+
72
+ end
73
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,7 @@
1
+ Rails.application.routes.draw do
2
+ resource :auth, :controller => "vouch/auth", :only => [:show, :destroy] do
3
+ member do
4
+ get :callback
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,16 @@
1
+ module Vouch
2
+ class ClientGenerator < Rails::Generators::Base
3
+ source_root File.expand_path("../templates", __FILE__)
4
+
5
+ # all public methods in here will be run in order
6
+ def add_oauth_file
7
+ copy_file "oauth.yml", "config/oauth.yml"
8
+ end
9
+
10
+ def inject_application_controller
11
+ inject_into_file "app/controllers/application_controller.rb", " include Vouch::Client\n", :after => "class ApplicationController < ActionController::Base\n"
12
+ end
13
+
14
+ end
15
+ end
16
+
@@ -0,0 +1,26 @@
1
+ development: &DEV
2
+ client_id: CLIENT_ID
3
+ client_secret: CLIENT_SECRET
4
+ site: http://localhost:3000
5
+ logout_url: http://localhost:3000/logout
6
+ # authorize_path: /oauth/authorize
7
+ # access_token_path: /oauth/access_token
8
+ # user_path: /oauth/user
9
+ # callback_path: /auth/callback
10
+ required_roles:
11
+ # - "blogUser"
12
+
13
+ production:
14
+ client_id: CLIENT_ID
15
+ client_secret: CLIENT_SECRET
16
+ site: https://somewhere.com
17
+ logout_url: https://somewhere.com/logout
18
+ # authorize_path: /oauth/authorize
19
+ # access_token_path: /oauth/access_token
20
+ # user_path: /oauth/user
21
+ # callback_path: /auth/callback
22
+ required_roles:
23
+ # - "blogUser"
24
+
25
+ test:
26
+ <<: *DEV
@@ -0,0 +1,38 @@
1
+ module Vouch
2
+ module Client
3
+
4
+ def login_url
5
+ auth_path
6
+ end
7
+
8
+ def current_user
9
+ if session[:user_id]
10
+ if session[:expires_at] < lambda{Time.now}.call
11
+ session[:user_id] = nil
12
+ else
13
+ User.find(session[:user_id])
14
+ end
15
+ end
16
+ end
17
+
18
+ def require_user
19
+ unless current_user
20
+ store_location
21
+ if request.xhr?
22
+ render :update do |page|
23
+ page.redirect_to(auth_path)
24
+ end
25
+ else
26
+ flash[:notice] = "You must be logged in to access this page"
27
+ redirect_to auth_path
28
+ end
29
+ return false
30
+ end
31
+ end
32
+
33
+ def store_location
34
+ session[:return_to] = request.url
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,7 @@
1
+ require "vouch"
2
+ require "rails"
3
+
4
+ module Vouch
5
+ class Engine < Rails::Engine
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ module Vouch
2
+ VERSION = "0.0.3"
3
+ end
4
+
data/lib/vouch.rb ADDED
@@ -0,0 +1,4 @@
1
+ module Vouch
2
+ require 'vouch/engine' if defined?(Rails)
3
+ require 'vouch/client'
4
+ end
data/vouch.gemspec ADDED
@@ -0,0 +1,51 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{vouch}
8
+ s.version = "0.0.3.pre1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Daniel McNevin"]
12
+ s.date = %q{2010-08-21}
13
+ s.description = %q{OAuth2 Server and Client Engines}
14
+ s.email = %q{dpmcnevin@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ "Gemfile",
21
+ "LICENSE",
22
+ "README.md",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "app/controllers/vouch/auth_controller.rb",
26
+ "config/routes.rb",
27
+ "lib/generators/vouch/client_generator.rb",
28
+ "lib/generators/vouch/templates/oauth.yml",
29
+ "lib/vouch.rb",
30
+ "lib/vouch/client.rb",
31
+ "lib/vouch/engine.rb",
32
+ "lib/vouch/version.rb",
33
+ "vouch.gemspec"
34
+ ]
35
+ s.homepage = %q{http://github.com/dpmcnevin/vouch}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.7}
39
+ s.summary = %q{OAuth2 Server and Client Engines}
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
+ else
47
+ end
48
+ else
49
+ end
50
+ end
51
+
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vouch
3
+ version: !ruby/object:Gem::Version
4
+ hash: -1876988180
5
+ prerelease: true
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 3
10
+ - pre1
11
+ version: 0.0.3.pre1
12
+ platform: ruby
13
+ authors:
14
+ - Daniel McNevin
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-08-21 00:00:00 -04:00
20
+ default_executable:
21
+ dependencies: []
22
+
23
+ description: OAuth2 Server and Client Engines
24
+ email: dpmcnevin@gmail.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files:
30
+ - LICENSE
31
+ - README.md
32
+ files:
33
+ - Gemfile
34
+ - LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - VERSION
38
+ - app/controllers/vouch/auth_controller.rb
39
+ - config/routes.rb
40
+ - lib/generators/vouch/client_generator.rb
41
+ - lib/generators/vouch/templates/oauth.yml
42
+ - lib/vouch.rb
43
+ - lib/vouch/client.rb
44
+ - lib/vouch/engine.rb
45
+ - lib/vouch/version.rb
46
+ - vouch.gemspec
47
+ has_rdoc: true
48
+ homepage: http://github.com/dpmcnevin/vouch
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --charset=UTF-8
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">"
69
+ - !ruby/object:Gem::Version
70
+ hash: 25
71
+ segments:
72
+ - 1
73
+ - 3
74
+ - 1
75
+ version: 1.3.1
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.3.7
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: OAuth2 Server and Client Engines
83
+ test_files: []
84
+