koinz 0.0.5 → 0.0.6

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/lib/koinz.rb CHANGED
@@ -2,3 +2,9 @@ require 'koinz/koinz'
2
2
  require 'koinz/omniauth_client'
3
3
  require 'koinz/redis'
4
4
  require 'koinz/notification'
5
+
6
+ module Koinz
7
+ module Controllers
8
+ autoload :InternalHelpers, 'koinz/controllers/internal_helpers'
9
+ end
10
+ end
@@ -0,0 +1,71 @@
1
+ module Koinz
2
+ module Controllers
3
+ module InternalHelpers
4
+
5
+ # before_filter: called explicity if login_required
6
+ # handles auth_token and two-legged token too
7
+ def login_required
8
+ if !current_user
9
+ session[:user_id] = nil
10
+ redirect_to login_required_path
11
+ end
12
+ end
13
+
14
+ # Only checks and returns admin status
15
+ def is_admin?
16
+ session[:user_id] && session[:user_id]["extra"]["admin"] == true
17
+ end
18
+
19
+ # before_filter: Called when you want to redirect if not admin
20
+ def is_admin
21
+ # We could later save the admin flag in the User Session
22
+ if is_admin?
23
+ return true
24
+ else
25
+ flash[:notice] = "You don't have sufficient privilege."
26
+ redirect_to no_privilage_path
27
+ end
28
+ end
29
+
30
+ def get_session_access_token
31
+ session ? session[:user_id]['credentials']['token'] : nil
32
+ end
33
+
34
+ protected
35
+ # override this method if you require a different redirect path on
36
+ # authentication failure!
37
+ def login_required_path
38
+ '/auth/koinz'
39
+ end
40
+
41
+ # override this method if you need to redirect to a path other than root
42
+ def no_privilage_path
43
+ root_path
44
+ end
45
+
46
+ # override this method if you have a different way of finding the
47
+ # current user -- or incace your model is different
48
+ # Assumption: User model exists with field 'km_user_id'
49
+ def get_koinz_user(km_user_id)
50
+ User.find_by_km_user_id(km_user_id)
51
+ end
52
+
53
+ private
54
+
55
+ def authenticate_token
56
+ if params[:access_token]
57
+ result = Koinz::OAuth2Client::UserManager.call(params[:access_token], '/auth/koinz/user')
58
+ return if result.is_a?(Hash) and result[:error]
59
+ session[:user_id] = result
60
+ end
61
+ end
62
+
63
+ def current_user
64
+ authenticate_token unless @current_user
65
+ # if not session, bail out
66
+ return nil unless session[:user_id]
67
+ @current_user ||= get_koinz_user(session[:user_id]['uid'])
68
+ end
69
+ end
70
+ end
71
+ end
@@ -46,7 +46,7 @@ module Koinz
46
46
 
47
47
  # Raise an exception if response is not valid
48
48
  # A redirect implies token is not valid
49
- raise OAuth2::AccessDenied.new if [:found, :unauthorized].include?(response.code)
49
+ raise OAuth2::AccessDenied.new if ['302', '301', '401'].include?(response.code)
50
50
 
51
51
  result = ActiveSupport::JSON.decode(response.body)
52
52
  return result
@@ -65,7 +65,7 @@ module Koinz
65
65
  @@SECURE_TOKEN ||= secure_token
66
66
  result = oauth_call(@@SECURE_TOKEN.token, host, uri, params)
67
67
 
68
- if result.is_a?(Hash) and result[:error] && result[:error] =~ /401|302/
68
+ if result.is_a?(Hash) and result[:error] && result[:error] =~ /OAuth2::AccessDenied/
69
69
  # OAuth2::AccessDenied (Received HTTP 401 during request.)
70
70
  # Token expired -- refresh and retry
71
71
 
data/lib/koinz/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Koinz
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: koinz
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Gautam Rege
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-14 00:00:00 +05:30
18
+ date: 2011-01-17 00:00:00 +05:30
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -76,6 +76,7 @@ files:
76
76
  - Rakefile
77
77
  - koinz.gemspec
78
78
  - lib/koinz.rb
79
+ - lib/koinz/controllers/internal_helpers.rb
79
80
  - lib/koinz/koinz.rb
80
81
  - lib/koinz/notification.rb
81
82
  - lib/koinz/omniauth_client.rb