whoru 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 20ecb51a2518f329a6ca83c9adeef61f3c2b61a5
4
+ data.tar.gz: f69a3af052aef7701c0def555e6e9072073213e0
5
+ SHA512:
6
+ metadata.gz: 97ecfa8695af491d7a367b9d6cd1fe9fc17456e25683b4c14a67de6bd4dd966913cbcac86b230eb399ea63ff6f5508d0982fa97782708b39132ff9e9de49a65d
7
+ data.tar.gz: fe2ce79080d46c6e807d79bd89077d3421d5357faf8e4364fa5363b8725a7d83619db7420397880ffad258c7bee2ad78ed524cf4ee4f0db3278b7e7c828111ee
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
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.
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Whoru'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :whoru do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,5 @@
1
+ module Whoru
2
+ require 'whoru/exceptions'
3
+ require 'whoru/login'
4
+ require 'whoru/authenticate'
5
+ end
@@ -0,0 +1,17 @@
1
+ require 'whoru/filters/authenticate_filter'
2
+
3
+ module Whoru
4
+ module Authenticate
5
+ def self.included base
6
+ def base.authenticate options
7
+ AuthenticateFilter.config options
8
+ auth_method = options[:with] || :whoru_authenticate
9
+
10
+ define_method(auth_method) do
11
+ AuthenticateFilter.before(self)
12
+ end
13
+ before_filter auth_method
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1 @@
1
+ class Whoru::MethodNotImplementedException < Exception; end
@@ -0,0 +1,48 @@
1
+ module Whoru
2
+ class AuthenticateFilter
3
+ TOKEN_KEY_IN_HEADERS = 'x-access-token'
4
+
5
+ @default_options = @options = {
6
+ :user_id => :user_id,
7
+ :header_token => 'x-access-token'
8
+ }
9
+
10
+ class << self
11
+ def config options
12
+ @options = @options.merge(options)
13
+ end
14
+
15
+ def before(controller)
16
+ return unless controller.params[@options[:user_id]]
17
+
18
+ token_string = access_token(controller)
19
+ user = User.where(:id => controller.params[@options[:user_id]]).first
20
+ return_404(controller) and return unless user
21
+ return_401(controller) and return unless token_string
22
+ return_401(controller) and return unless user.has_access_token?(token_string)
23
+ end
24
+
25
+
26
+ def access_token controller
27
+ controller.request.headers[@options[:header_token]]
28
+ end
29
+ private :access_token
30
+
31
+ def return_401 controller
32
+ controller.render :json => { :reason => 'user is not authenticated' },
33
+ :status => 401
34
+ true
35
+ end
36
+ private :return_401
37
+
38
+ def return_404 controller
39
+ controller.render :json => { :reason => 'user not found' },
40
+ :status => 404
41
+
42
+ true
43
+ end
44
+ private :return_404
45
+
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,81 @@
1
+ module Whoru::Login
2
+ def self.included base
3
+
4
+ base.class_eval do
5
+
6
+ def _postman_login user
7
+ if params[:cookie]
8
+ ensure_method_exists(user, :generate_web_access_token)
9
+ cookies['TOKEN_POSTMAN'] = user.generate_web_access_token
10
+ render :json => user
11
+ else
12
+ ensure_method_exists(user, :generate_access_token)
13
+ user_hash = user.as_json.merge(
14
+ :access_token => user.generate_access_token)
15
+ render :json => user_hash
16
+ end
17
+ end
18
+ private :_postman_login
19
+
20
+ def ensure_method_exists(user, method_name)
21
+ unless user.respond_to?(method_name)
22
+ raise ::Whoru::MethodNotImplementedException,
23
+ "#{user.class} should have `##{method_name}` method"
24
+ end
25
+ end
26
+ private :ensure_method_exists
27
+
28
+ def _postman_conflict
29
+ render :json => { :reason => 'login failed' },
30
+ :status => 409
31
+ end
32
+ private :_postman_conflict
33
+
34
+ def _postman_bad_request e
35
+ render :json => { :reason => e.message },
36
+ :status => 400
37
+ end
38
+ private :_postman_bad_request
39
+
40
+ end
41
+
42
+ class << base
43
+ # usage:
44
+ # class UsersController
45
+ # login FacebookUser, :with => :facebook_login
46
+ # end
47
+ #
48
+ # # route.rb
49
+ # post '/users/fb_login' => 'users#facebook_login'
50
+ def login user_class, options
51
+ ensure_login_method_exists(user_class)
52
+
53
+ instance_method_name = options[:with]
54
+
55
+ define_method instance_method_name do
56
+ begin
57
+ user = user_class.login(params.require(:account), params.require(:validator))
58
+
59
+ if user
60
+ _postman_login(user)
61
+ else
62
+ _postman_conflict
63
+ end
64
+ rescue ActionController::ParameterMissing => e
65
+ _postman_bad_request(e)
66
+ end
67
+ end
68
+ end
69
+
70
+ def ensure_login_method_exists user_class
71
+ unless user_class.respond_to?(:login)
72
+ raise ::Whoru::MethodNotImplementedException,
73
+ "#{user_class} should have `::login` method"
74
+ end
75
+ end
76
+ private :ensure_login_method_exists
77
+
78
+ end
79
+
80
+ end
81
+ end
@@ -0,0 +1,7 @@
1
+ module Whoru
2
+ module Stub
3
+ def stub_authenticate
4
+ allow(Whoru::AuthenticateFilter).to receive(:before)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Whoru
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: whoru
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yang-Hsing Lin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.10
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.10
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A rails plugin handling login stuff in controller
56
+ email:
57
+ - yanghsing.lin@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - Rakefile
64
+ - lib/tasks/whoru_tasks.rake
65
+ - lib/whoru.rb
66
+ - lib/whoru/authenticate.rb
67
+ - lib/whoru/exceptions.rb
68
+ - lib/whoru/filters/authenticate_filter.rb
69
+ - lib/whoru/login.rb
70
+ - lib/whoru/stub.rb
71
+ - lib/whoru/version.rb
72
+ homepage: https://github.com/mz026/whoru
73
+ licenses: []
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.4.2
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: A rails plugin handling login stuff in controller
95
+ test_files: []