softwear-lib 1.5.6 → 1.5.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 557217d84e460a14db3f2e7fdaa3f8d23baab04b
4
- data.tar.gz: 6ed4d9f028f3e2ff0ed9a82f0a881531d8f53be1
3
+ metadata.gz: 4f0b0ffdf353b1125791532b04b4c69178f3848d
4
+ data.tar.gz: b5ffb662dafdbd0ced2e2b3fda77c68314ed4a9e
5
5
  SHA512:
6
- metadata.gz: 50776b3c2a6b20ad8c6a817e9924b5ee183d39f887bab0106a763d8fdfa593bbefb0f8aa16e9dc8414f94f2c1a958f2ead01d686da635046afb40968dadc905b
7
- data.tar.gz: 599f451f5d2102733836e5c43c3aade4b70e578dde3ee5db92e214295b82f22943cc976623cf465dfc2d57afaca6afbf99d5bbefdefb56046aa02411676787b9
6
+ metadata.gz: 51397655427dfd487c7690028a486f73e2b35bd4a5fa4bfd7a92b02a3b5f1cfe651d63d26238c4f5d0e5f3620e263264d8535abc8eb6f2bf9678921354fda310
7
+ data.tar.gz: 536a85856023e46c1897ba453764710d257cc940beb5923b6510c1f3ff9ffaf40ab7c605443a469f927c9b3f4d0c6c25720f6ca64a43f537e16a3d36d2447a65
@@ -0,0 +1,56 @@
1
+ module Softwear
2
+ module Auth
3
+ module TokenAuthentication
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ cattr_accessor :user_class
8
+ cattr_accessor :token_auth_options
9
+ end
10
+
11
+ def token_authenticate_user!
12
+ user_class = self.class.user_class || base_class.user_class || User
13
+ options = (self.class.token_auth_options || base_class.token_auth_options || {}).with_indifferent_access
14
+ params_options = (options[:params] || {}).with_indifferent_access
15
+ headers_options = (options[:headers] || {}).with_indifferent_access
16
+
17
+ email_param = params_options[:email] || 'user_email'
18
+ token_param = params_options[:authentication_token] || 'user_token'
19
+ email_header = headers_options[:email] || 'X-User-Email'
20
+ token_header = headers_options[:authentication_token] || 'X-User-Token'
21
+
22
+ email = params[email_param] || headers[email_header]
23
+ token = params[token_param] || headers[token_header]
24
+
25
+ return render_unauthorized if email.blank? || token.blank?
26
+
27
+ case user_class.query "token #{Figaro.env.hub_app_name} #{email} #{token}"
28
+ when 'no' then render_unauthorized
29
+ when 'invaild' then render_unauthorized
30
+ when 'sorry' then render_internal_server_error
31
+ when 'yes' then true
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def render_unauthorized
38
+ respond_to do |format|
39
+ format.json do
40
+ render status: :unauthorized,
41
+ json: { error: "Invalid or missing credentials" }
42
+ end
43
+ end
44
+ end
45
+
46
+ def render_internal_server_error
47
+ respond_to do |format|
48
+ format.json do
49
+ render status: :internal_server_error,
50
+ json: { error: "Authentication server broke" }
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
data/lib/softwear/lib.rb CHANGED
@@ -7,6 +7,7 @@ require "softwear/auth/helper"
7
7
  require "softwear/auth/model"
8
8
  require "softwear/auth/belongs_to_user"
9
9
  require "softwear/auth/spec"
10
+ require "softwear/auth/token_authentication"
10
11
 
11
12
  module Softwear
12
13
  module Lib
@@ -19,6 +19,17 @@ module Softwear
19
19
  self.class_attribute :resource_class, :instance_writer => false unless self.respond_to? :resource_class
20
20
  self.class_attribute :parents_symbols, :resources_configuration, :instance_writer => false
21
21
 
22
+ def self.base_class
23
+ Softwear::Lib::ApiController
24
+ end
25
+
26
+ def self.token_authenticate(user_class, options = {})
27
+ include Softwear::Auth::TokenAuthentication
28
+ self.user_class = user_class
29
+ self.token_auth_options = options
30
+ prepend_before_filter :token_authenticate_user!
31
+ end
32
+
22
33
  def index(&block)
23
34
  yield if block_given?
24
35
 
@@ -77,6 +88,10 @@ module Softwear
77
88
 
78
89
  protected
79
90
 
91
+ def base_class
92
+ self.class.base_class
93
+ end
94
+
80
95
  def render_json(options = {})
81
96
  proc do
82
97
  if options.is_a?(Hash)
@@ -32,7 +32,7 @@ module Softwear
32
32
  # Action called when a NotSignedInError is raised.
33
33
  # ====================
34
34
  def user_not_signed_in
35
- redirect_to Figaro.env.softwear_hub_url + "/users/sign_in?#{{return_to: request.original_url}.to_param}"
35
+ redirect_to softwear_hub_url + "/users/sign_in?#{{return_to: request.original_url}.to_param}"
36
36
  end
37
37
 
38
38
  # ====================
@@ -97,21 +97,21 @@ module Softwear
97
97
  end
98
98
 
99
99
  def destroy_user_session_path
100
- Figaro.env.softwear_hub_url + "/users/sign_out"
100
+ softwear_hub_url + "/users/sign_out"
101
101
  end
102
102
 
103
103
  def user_path(user)
104
104
  user_id = user.is_a?(user_class) ? user.id : user
105
- Figaro.env.softwear_hub_url + "/users/#{user_id}"
105
+ softwear_hub_url + "/users/#{user_id}"
106
106
  end
107
107
 
108
108
  def edit_user_path(user)
109
109
  user_id = user.is_a?(user_class) ? user.id : user
110
- Figaro.env.softwear_hub_url + "/users/#{user_id}/edit"
110
+ softwear_hub_url + "/users/#{user_id}/edit"
111
111
  end
112
112
 
113
113
  def users_path
114
- Figaro.env.softwear_hub_url + "/users"
114
+ softwear_hub_url + "/users"
115
115
  end
116
116
 
117
117
  private
@@ -1,5 +1,5 @@
1
1
  module Softwear
2
2
  module Lib
3
- VERSION = "1.5.6"
3
+ VERSION = "1.5.8"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: softwear-lib
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.6
4
+ version: 1.5.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nigel Baillie
@@ -88,6 +88,7 @@ files:
88
88
  - lib/softwear/auth/helper.rb
89
89
  - lib/softwear/auth/model.rb
90
90
  - lib/softwear/auth/spec.rb
91
+ - lib/softwear/auth/token_authentication.rb
91
92
  - lib/softwear/lib.rb
92
93
  - lib/softwear/lib/api_controller.rb
93
94
  - lib/softwear/lib/authentication.rb