remi-rack-oauth 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +11 -0
- data/Rakefile +68 -0
- data/VERSION +1 -0
- data/lib/rack-oauth.rb +132 -0
- data/lib/rack/oauth.rb +1 -0
- metadata +57 -0
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'spec/rake/spectask'
|
5
|
+
|
6
|
+
puts "\nGem: rack-oauth\n\n"
|
7
|
+
|
8
|
+
begin
|
9
|
+
require 'jeweler'
|
10
|
+
Jeweler::Tasks.new do |s|
|
11
|
+
s.name = 'rack-oauth'
|
12
|
+
s.summary = 'Rack Middleware for OAuth Authorization'
|
13
|
+
s.email = 'remi@remitaylor.com'
|
14
|
+
s.homepage = 'http://github.com/remi/rack-oauth'
|
15
|
+
s.description = 'Rack Middleware for OAuth Authorization'
|
16
|
+
s.authors = %w( remi )
|
17
|
+
s.files = FileList['[A-Z]*', '{lib,spec,bin,examples}/**/*']
|
18
|
+
# s.add_dependency 'person-gemname'
|
19
|
+
# s.executables << 'script'
|
20
|
+
# s.rubyforge_project = 'gemname'
|
21
|
+
s.extra_rdoc_files = %w( README.rdoc )
|
22
|
+
end
|
23
|
+
rescue LoadError
|
24
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new do |t|
|
28
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
29
|
+
end
|
30
|
+
|
31
|
+
desc "Run all examples with RCov"
|
32
|
+
Spec::Rake::SpecTask.new('rcov') do |t|
|
33
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
34
|
+
t.rcov = true
|
35
|
+
end
|
36
|
+
|
37
|
+
# require 'hanna'
|
38
|
+
# require 'darkfish-rdoc'
|
39
|
+
|
40
|
+
Rake::RDocTask.new do |rdoc|
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = 'rack-oauth'
|
43
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
44
|
+
# rdoc.options += ["--template=#{`allison --path`}"] # sudo gem install allison
|
45
|
+
# rdoc.options += %w( -f darkfish ) # sudo gem install darkfish-rdoc
|
46
|
+
# rdoc.options += %w( -T hanna ) # sudo gem install mislav-hanna
|
47
|
+
rdoc.options += %w( -m README.rdoc ) # the initial page displayed
|
48
|
+
rdoc.rdoc_files.include('README.rdoc')
|
49
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
50
|
+
end
|
51
|
+
|
52
|
+
desc 'Confirm that gemspec is $SAFE'
|
53
|
+
task :safe do
|
54
|
+
require 'yaml'
|
55
|
+
require 'rubygems/specification'
|
56
|
+
data = File.read('rack-oauth.gemspec')
|
57
|
+
spec = nil
|
58
|
+
if data !~ %r{!ruby/object:Gem::Specification}
|
59
|
+
Thread.new { spec = eval("$SAFE = 3\n#{data}") }.join
|
60
|
+
else
|
61
|
+
spec = YAML.load(data)
|
62
|
+
end
|
63
|
+
spec.validate
|
64
|
+
puts spec
|
65
|
+
puts "OK"
|
66
|
+
end
|
67
|
+
|
68
|
+
task :default => :spec
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/rack-oauth.rb
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rack'
|
3
|
+
require 'oauth'
|
4
|
+
|
5
|
+
puts "HI!"
|
6
|
+
|
7
|
+
module Rack #:nodoc:
|
8
|
+
|
9
|
+
# Rack Middleware for integrating OAuth into your application
|
10
|
+
#
|
11
|
+
# Note: this *requires* that a Rack::Session middleware be enabled
|
12
|
+
#
|
13
|
+
class OAuth
|
14
|
+
|
15
|
+
DEFAULT_OPTIONS = {
|
16
|
+
:login_path => '/oauth_login',
|
17
|
+
:callback_path => '/oauth_callback',
|
18
|
+
:redirect_to => '/oauth_complete',
|
19
|
+
:session_key => 'oauth_user',
|
20
|
+
:rack_session => 'rack.session',
|
21
|
+
:json_parser => lambda {|json_string| require 'json'; JSON.parse(json_string); }
|
22
|
+
}
|
23
|
+
|
24
|
+
# [internal] the URL that should initiate OAuth and redirect to the OAuth provider's login page
|
25
|
+
attr_accessor :login_path
|
26
|
+
alias login login_path
|
27
|
+
alias login= login_path=
|
28
|
+
|
29
|
+
# [internal] the URL that the OAuth provider should callback to after OAuth login is complete
|
30
|
+
attr_accessor :callback_path
|
31
|
+
alias callback callback_path
|
32
|
+
alias callback= callback_path=
|
33
|
+
|
34
|
+
# [external] the URL that Rack::OAuth should redirect to after the OAuth has been completed (part of your app)
|
35
|
+
attr_accessor :redirect_to
|
36
|
+
alias redirect redirect_to
|
37
|
+
alias redirect= redirect_to=
|
38
|
+
|
39
|
+
# the name of the Session key to use to store user account information (if OAuth completed OK)
|
40
|
+
attr_accessor :session_key
|
41
|
+
|
42
|
+
# the name of the Rack env variable used for the session
|
43
|
+
attr_accessor :rack_session
|
44
|
+
|
45
|
+
# [required] Your OAuth consumer key
|
46
|
+
attr_accessor :consumer_key
|
47
|
+
alias key consumer_key
|
48
|
+
alias key= consumer_key=
|
49
|
+
|
50
|
+
# [required] Your OAuth consumer secret
|
51
|
+
attr_accessor :consumer_secret
|
52
|
+
alias secret consumer_secret
|
53
|
+
alias secret= consumer_secret=
|
54
|
+
|
55
|
+
# [required] The site you want to request OAuth for, eg. 'http://twitter.com'
|
56
|
+
attr_accessor :consumer_site
|
57
|
+
alias site consumer_site
|
58
|
+
alias site= consumer_site=
|
59
|
+
|
60
|
+
# a Proc that accepts a JSON string and returns a Ruby object. Defaults to using the 'json' gem, if available.
|
61
|
+
attr_accessor :json_parser
|
62
|
+
|
63
|
+
def initialize app, options = {}
|
64
|
+
@app = app
|
65
|
+
|
66
|
+
DEFAULT_OPTIONS.each {|name, value| send "#{name}=", value }
|
67
|
+
options.each {|name, value| send "#{name}=", value } if options
|
68
|
+
|
69
|
+
raise_validation_exception unless valid?
|
70
|
+
end
|
71
|
+
|
72
|
+
def call env
|
73
|
+
case env['PATH_INFO']
|
74
|
+
when login_path; do_login env
|
75
|
+
when callback_path; do_callback env
|
76
|
+
else; @app.call env
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def do_login env
|
81
|
+
request = consumer.get_request_token :oauth_callback => ::File.join("http://#{ env['HTTP_HOST'] }", callback_path)
|
82
|
+
session(env)[:oauth_request_token] = request.token
|
83
|
+
session(env)[:oauth_request_secret] = request.secret
|
84
|
+
[ 302, {'Location' => request.authorize_url}, [] ]
|
85
|
+
end
|
86
|
+
|
87
|
+
def do_callback env
|
88
|
+
request = ::OAuth::RequestToken.new consumer, session(env)[:oauth_request_token], session(env)[:oauth_request_secret]
|
89
|
+
access = request.get_access_token :oauth_verifier => Rack::Request.new(env).params['oauth_verifier']
|
90
|
+
response = consumer.request :get, '/account/verify_credentials.json', access, :scheme => :query_string
|
91
|
+
|
92
|
+
# put the user information received (json -> ruby) in the session
|
93
|
+
session(env)[session_key] = json_parser.call response.body if response
|
94
|
+
|
95
|
+
[ 302, {'Location' => redirect_to}, [] ]
|
96
|
+
end
|
97
|
+
|
98
|
+
protected
|
99
|
+
|
100
|
+
def consumer
|
101
|
+
@consumer ||= ::OAuth::Consumer.new consumer_key, consumer_secret, :site => consumer_site
|
102
|
+
end
|
103
|
+
|
104
|
+
def valid?
|
105
|
+
@errors = []
|
106
|
+
@errors << ":consumer_key option is required" unless consumer_key
|
107
|
+
@errors << ":consumer_secret option is required" unless consumer_secret
|
108
|
+
@errors << ":consumer_site option is required" unless consumer_site
|
109
|
+
@errors.empty?
|
110
|
+
end
|
111
|
+
|
112
|
+
def raise_validation_exception
|
113
|
+
raise @errors.join(', ')
|
114
|
+
end
|
115
|
+
|
116
|
+
def session env
|
117
|
+
raise "Rack env['rack.session'] is nil ... has a Rack::Session middleware be enabled? " +
|
118
|
+
"use :rack_session for custom key" if env[rack_session].nil?
|
119
|
+
env[rack_session]
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
|
124
|
+
module Auth #:nodoc:
|
125
|
+
|
126
|
+
class OAuth
|
127
|
+
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
data/lib/rack/oauth.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'rack-oauth'
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: remi-rack-oauth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- remi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-06-23 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Rack Middleware for OAuth Authorization
|
17
|
+
email: remi@remitaylor.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
files:
|
25
|
+
- README.rdoc
|
26
|
+
- Rakefile
|
27
|
+
- VERSION
|
28
|
+
- lib/rack-oauth.rb
|
29
|
+
- lib/rack/oauth.rb
|
30
|
+
has_rdoc: false
|
31
|
+
homepage: http://github.com/remi/rack-oauth
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options:
|
34
|
+
- --charset=UTF-8
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: "0"
|
42
|
+
version:
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
requirements: []
|
50
|
+
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.2.0
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: Rack Middleware for OAuth Authorization
|
56
|
+
test_files: []
|
57
|
+
|