hellhound 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Pedro Del Gallego
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.rdoc ADDED
@@ -0,0 +1,41 @@
1
+ = hellhound
2
+
3
+ Description goes here.
4
+
5
+
6
+ == How to use it ==
7
+
8
+ You will need to override the method get user. This method will search the user in the database in case that the user is not in the cache or the cache was not enabled
9
+
10
+ =code
11
+ def get_user id
12
+ User.search(:twitter_id => id)
13
+ end
14
+ =
15
+
16
+ Hellhound provides some helpers:
17
+
18
+ * current_user
19
+ * logged_in?
20
+ * logout!
21
+
22
+
23
+ * create_or_retrive_xxx_user(&block)
24
+ ** create_or_retrive_twitter_user(&block)
25
+ ** create_or_retrive_facebook_user(&block)
26
+ ** ...
27
+
28
+
29
+ == Note on Patches/Pull Requests
30
+
31
+ * Fork the project.
32
+ * Make your feature addition or bug fix.
33
+ * Add tests for it. This is important so I don't break it in a
34
+ future version unintentionally.
35
+ * Commit, do not mess with rakefile, version, or history.
36
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
37
+ * Send me a pull request. Bonus points for topic branches.
38
+
39
+ == Copyright
40
+
41
+ Copyright (c) 2010 Pedro Del Gallego. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,58 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "hellhound"
8
+ gem.summary = %Q{An authentication system based in third oauth party providers.}
9
+ # gem.description = %Q{}
10
+ gem.email = "pedro.delgallego@gmail.com"
11
+ gem.homepage = "http://github.com/pedrodelgallego/hellhound"
12
+ gem.authors = ["Pedro Del Gallego"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_development_dependency "yard", ">= 0"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ begin
37
+ require 'reek/adapters/rake_task'
38
+ Reek::RakeTask.new do |t|
39
+ t.fail_on_error = true
40
+ t.verbose = false
41
+ t.source_files = 'lib/**/*.rb'
42
+ end
43
+ rescue LoadError
44
+ task :reek do
45
+ abort "Reek is not available. In order to run reek, you must: sudo gem install reek"
46
+ end
47
+ end
48
+
49
+ task :default => :spec
50
+
51
+ begin
52
+ require 'yard'
53
+ YARD::Rake::YardocTask.new
54
+ rescue LoadError
55
+ task :yardoc do
56
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
57
+ end
58
+ end
data/TAGS ADDED
@@ -0,0 +1,6 @@
1
+ Exception lib/hellhound.rb 12
2
+ expand_path examples/facebook-app.rb /^require File.expand_path(File.join(File.dirname(__/
3
+ expand_path spec/hellhound_spec.rb /^require File.expand_path(File.dirname(__FILE__) + /
4
+ key examples/twitter-app.rb 8
5
+ Rack::OAuth examples/twitter-app.rb 8
6
+ unshift spec/spec_helper.rb /^$LOAD_PATH.unshift(File.dirname(__FILE__))$/
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,44 @@
1
+ %w( rubygems sinatra
2
+ json memcached
3
+ ).each {|gem| require gem}
4
+
5
+ require File.expand_path(File.join(File.dirname(__FILE__), "../lib/hellhound.rb"))
6
+
7
+ configure do
8
+
9
+ set :fb_api_key, '85b58936a196dcf2b57b863e2f3f6a37'
10
+ set :fb_app_secret, 'd596ecc4fdc2d7753c15b2c920d50100'
11
+
12
+ enable :hellhound_cache
13
+ end
14
+
15
+ helpers do
16
+ include Sinatra::Hellhound::FacebookHelpers
17
+
18
+ def get_user facebook_id
19
+ # If the user is not in the cache so do something to get it from there
20
+ session[:user]
21
+ end
22
+ end
23
+
24
+ get "/" do
25
+ logged_in? ? "Hi #{current_user}! " : "Hi stranger"
26
+ end
27
+
28
+ get '/auth/facebook/callback' do
29
+ create_or_retrieve_facebook_user do |user_data|
30
+ # Do something smart to create you user and return it to the method
31
+ user_data["name"]
32
+ end
33
+ redirect "/"
34
+ end
35
+
36
+ get '/logout' do
37
+ logout!
38
+ redirect "/"
39
+ end
40
+
41
+ get '/me' do
42
+ photos = JSON.parse(current_access_token.get('/me/photos'))
43
+ "#{photos.inspect}"
44
+ end
@@ -0,0 +1,39 @@
1
+ %w( rubygems sinatra rack-oauth json
2
+ memcached hellhound ).each {|gem| require gem}
3
+
4
+ # require File.expand_path(File.join(File.dirname(__FILE__), "../lib/hellhound.rb"))
5
+
6
+ configure do
7
+ include Rack::OAuth::Methods
8
+ use Rack::OAuth, :key => '97DyOmKn7gn1ki0wlyoGA',
9
+ :secret => 'bBYIkLEEsdbLFEflWOV2fqaCieiBmYwzV4gXdvwwk',
10
+ :site => 'http://twitter.com'
11
+
12
+ enable :hellhound_cache
13
+ end
14
+
15
+ helpers do
16
+ include Sinatra::Hellhound::TwitterHelpers
17
+
18
+ def get_user twitter_id
19
+ # The user is not in the cache so do something to get it from there
20
+ session[:user]
21
+ end
22
+ end
23
+
24
+ get "/" do
25
+ logged_in? ? "Hi #{current_user}! " : "Hi stranger"
26
+ end
27
+
28
+ get "/oauth_complete" do
29
+ create_or_retrieve_twitter_user do |user_data|
30
+ # Do something smart to create you user and return it to the method
31
+ user_data["name"]
32
+ end
33
+ redirect "/oauth_login"
34
+ end
35
+
36
+ get "/logout" do
37
+ logout!
38
+ redirect "/"
39
+ end
data/hellhound.gemspec ADDED
@@ -0,0 +1,62 @@
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{hellhound}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Pedro Del Gallego"]
12
+ s.date = %q{2010-05-17}
13
+ s.email = %q{pedro.delgallego@gmail.com}
14
+ s.extra_rdoc_files = [
15
+ "LICENSE",
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".document",
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "TAGS",
25
+ "VERSION",
26
+ "examples/facebook-app.rb",
27
+ "examples/twitter-app.rb",
28
+ "hellhound.gemspec",
29
+ "lib/hellhound.rb",
30
+ "spec/hellhound_spec.rb",
31
+ "spec/spec.opts",
32
+ "spec/spec_helper.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/pedrodelgallego/hellhound}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.7}
38
+ s.summary = %q{An authentication system based in third oauth party providers.}
39
+ s.test_files = [
40
+ "spec/hellhound_spec.rb",
41
+ "spec/spec_helper.rb",
42
+ "examples/twitter-app.rb",
43
+ "examples/facebook-app.rb"
44
+ ]
45
+
46
+ if s.respond_to? :specification_version then
47
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
48
+ s.specification_version = 3
49
+
50
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
51
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
52
+ s.add_development_dependency(%q<yard>, [">= 0"])
53
+ else
54
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
55
+ s.add_dependency(%q<yard>, [">= 0"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
59
+ s.add_dependency(%q<yard>, [">= 0"])
60
+ end
61
+ end
62
+
data/lib/hellhound.rb ADDED
@@ -0,0 +1,150 @@
1
+ require 'sinatra/base'
2
+ require 'oauth2'
3
+
4
+ module Sinatra
5
+ module Hellhound
6
+ module Helpers
7
+ ##
8
+ # Returns the user from the database, it can be overwritten to
9
+ # adapat to the other strategies
10
+ #
11
+ def get_user id
12
+ raise Exception, "You need to override this method"
13
+ end
14
+
15
+ ##
16
+ # Returns the current_user, it's an instance of <tt>user</tt> model
17
+ #
18
+ def current_user
19
+ if session[:user]
20
+ options.hellhound_cache ? CACHE.get("user-#{session[:user]}") : get_user(session[:user])
21
+ end
22
+ end
23
+
24
+ ##
25
+ # Returns true if +current_user+ is logged and active.
26
+ #
27
+ def logged_in?
28
+ !! session[:user]
29
+ end
30
+
31
+ ##
32
+ # Returns the user that logged out and remove user from the
33
+ # cache and the session
34
+ #
35
+ def logout!
36
+ user = session[:user]
37
+ session.clear
38
+ user
39
+ end
40
+
41
+ end
42
+
43
+ module TwitterHelpers
44
+ ##
45
+ # Retrieve the user from the database or create a new one if it
46
+ # doesn't exists.
47
+ #
48
+ # If you want to store the user in memcached them you need to
49
+ # return the user at the end of the block
50
+ #
51
+ # I.e
52
+ #
53
+ # create_or_retrive_user do |user_data| #
54
+ # user = User.find :twitter_id => user_data["id"].to_s
55
+ # User.create :twitter_id => user_data["id"].to_s unless user
56
+ # user
57
+ # end
58
+ #
59
+ def create_or_retrieve_twitter_user(&block)
60
+ user_data = get_credentials
61
+ session[:user] = user_data["id"].to_s
62
+ user = yield user_data
63
+
64
+ CACHE.set "user-#{session[:user]}", user if options.hellhound_cache
65
+ end
66
+
67
+ ##
68
+ # Returns a Hash with the user data.
69
+ #
70
+ def get_credentials
71
+ user_data = JSON.parse(get_access_token.get('/account/verify_credentials.json').body)
72
+ end
73
+ end
74
+
75
+ module FacebookHelpers
76
+ ##
77
+ # Returns the current_user, it's an instance of <tt>user</tt> model
78
+ #
79
+ def current_access_token
80
+ client.web_server.access_token(session[:user_code], :redirect_uri => redirect_uri)
81
+ end
82
+
83
+ ##
84
+ # Retrive the user from the database or create a new one if it
85
+ # doesn't exists.
86
+ #
87
+ # If you want to store the user in memcached them you need to
88
+ # return the user at the end of the block
89
+ #
90
+ # I.e
91
+ #
92
+ # create_or_retrive_user do |user_data| #
93
+ # user = User.find :twitter_id => user_data["id"].to_s
94
+ # User.create :twitter_id => user_data["id"].to_s unless user
95
+ # user
96
+ # end
97
+ #
98
+ def create_or_retrieve_facebook_user(&block)
99
+ access_token = client.web_server.access_token(params[:code], :redirect_uri => redirect_uri)
100
+ session[:user_code] = params[:code]
101
+
102
+ user_data = JSON.parse(access_token.get('/me'))
103
+ session[:user] = user_data["id"].to_s
104
+
105
+ user = yield user_data
106
+
107
+ CACHE.set "user-#{session[:user]}", user if options.hellhound_cache
108
+ end
109
+
110
+ ##
111
+ # Returns a Hash with the user data.
112
+ #
113
+ def get_credentials
114
+ access_token = client.web_server.access_token(params[:code], :redirect_uri => redirect_uri)
115
+ JSON.parse(access_token.get('/me'))
116
+ end
117
+
118
+ def client
119
+ OAuth2::Client.new(options.fb_api_key, options.fb_app_secret, :site => 'https://graph.facebook.com')
120
+ end
121
+
122
+ def redirect_uri
123
+ uri = URI.parse(request.url)
124
+ uri.path = '/auth/facebook/callback'
125
+ uri.query = nil
126
+ uri.to_s
127
+ end
128
+ end
129
+
130
+ CACHE = Memcached.new
131
+
132
+ def self.registered(app)
133
+ app.enable :sessions
134
+ app.disable :hellhound_cache
135
+ app.helpers
136
+
137
+ app.get '/auth/facebook' do
138
+ redirect client.web_server.authorize_url(
139
+ :redirect_uri => redirect_uri,
140
+ :scope => 'email,offline_access'
141
+ )
142
+ end
143
+
144
+ end
145
+
146
+ end
147
+ register Hellhound
148
+
149
+ helpers Hellhound::Helpers
150
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Hellhound" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'hellhound'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hellhound
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Pedro Del Gallego
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-05-17 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: yard
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :development
50
+ version_requirements: *id002
51
+ description:
52
+ email: pedro.delgallego@gmail.com
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ extra_rdoc_files:
58
+ - LICENSE
59
+ - README.rdoc
60
+ files:
61
+ - .document
62
+ - .gitignore
63
+ - LICENSE
64
+ - README.rdoc
65
+ - Rakefile
66
+ - TAGS
67
+ - VERSION
68
+ - examples/facebook-app.rb
69
+ - examples/twitter-app.rb
70
+ - hellhound.gemspec
71
+ - lib/hellhound.rb
72
+ - spec/hellhound_spec.rb
73
+ - spec/spec.opts
74
+ - spec/spec_helper.rb
75
+ has_rdoc: true
76
+ homepage: http://github.com/pedrodelgallego/hellhound
77
+ licenses: []
78
+
79
+ post_install_message:
80
+ rdoc_options:
81
+ - --charset=UTF-8
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ requirements: []
103
+
104
+ rubyforge_project:
105
+ rubygems_version: 1.3.7
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: An authentication system based in third oauth party providers.
109
+ test_files:
110
+ - spec/hellhound_spec.rb
111
+ - spec/spec_helper.rb
112
+ - examples/twitter-app.rb
113
+ - examples/facebook-app.rb