capcoauth 0.2.0 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 413293494a485db6a7d32e5f7ebb599b0a140dd1
4
- data.tar.gz: f37a26cb400e3c5092b2958799551e13ee433af0
3
+ metadata.gz: 88d88d5d24669f7c09057d039ed04bcf8f7e4215
4
+ data.tar.gz: e3126ba3d76375574f63dd3b7f65177e66f1a781
5
5
  SHA512:
6
- metadata.gz: a6401723ff07e5dc867d4fbbac9a2142c5bba2139d34b0c5969ff1737cb83f5d90fa261103e689eb289b23dce44ba0b046a488f1845496d1554d277ec1878162
7
- data.tar.gz: 2d591b42756602ac29116859f224a0780408ddefaf303f53ce3b41d5340504d61fbc49a84242bcb32c655f2b9f5f39830749ca4bf43f5d62753f677e5452e606
6
+ metadata.gz: c7ed882f4e5786775fa4f858c994398f6bd01720f27ea2da660eec2dd2ae2e3f1d98029c50a9ce49b2c45a2292d0bfd54430d5a87deb13e467739a842022fbc3
7
+ data.tar.gz: c41136838fe6f2e529b2f290a1b27e7860983c756e089e1751bb3bece86c61bf9d54598c38741a077357fc161ba2f3c2222224a24f8d8104f8c0f48fdced2b84
@@ -6,6 +6,7 @@ module Capcoauth
6
6
 
7
7
  def initialize(token)
8
8
  @token = token
9
+ @user_id = TTLCache.user_id_for(token)
9
10
  end
10
11
 
11
12
  def verify
@@ -9,7 +9,7 @@ module Capcoauth
9
9
 
10
10
  def self.verify(access_token)
11
11
  raise UnauthorizedError if access_token.blank? or access_token.token.blank?
12
- return access_token if TTLCache.valid?(access_token.token)
12
+ return access_token if TTLCache.user_id_for(access_token.token)
13
13
 
14
14
  # Call Capcoauth
15
15
  response = ::HTTParty.get("#{Capcoauth.configuration.capcoauth_url}/oauth/token/info", {
@@ -23,7 +23,7 @@ module Capcoauth
23
23
  access_token.user_id = response.parsed_response['resource_owner_id']
24
24
  if response.parsed_response.fetch('application', {}).fetch('uid', nil) === Capcoauth.configuration.client_id
25
25
  logger.info("CapcOAuth: The access token for user ##{access_token.user_id} was verified successfully") unless logger.nil?
26
- TTLCache.update(access_token.token)
26
+ TTLCache.update(access_token.token, access_token.user_id)
27
27
  access_token
28
28
  else
29
29
  logger.info("CapcOAuth: The access token for user ##{access_token.user_id} was valid, but for a different OAuth client ID") unless logger.nil?
@@ -3,13 +3,14 @@ module Capcoauth
3
3
  class TTLCache
4
4
  @@cache = {}
5
5
 
6
- def self.valid?(access_token)
6
+ def self.user_id_for(access_token)
7
7
  purge
8
- !!@@cache[access_token]
8
+ return @@cache[access_token][:user_id] if @@cache[access_token].present?
9
+ nil
9
10
  end
10
11
 
11
- def self.update(access_token)
12
- @@cache[access_token] = Time.zone.now
12
+ def self.update(access_token, user_id)
13
+ @@cache[access_token] = { last_checked: Time.zone.now, user_id: user_id }
13
14
  end
14
15
 
15
16
  def self.remove(access_token)
@@ -18,7 +19,7 @@ module Capcoauth
18
19
 
19
20
  def self.purge
20
21
  @@cache.delete_if do |k, v|
21
- Time.zone.now > v + Capcoauth.configuration.token_verify_ttl
22
+ Time.zone.now > v[:last_checked] + Capcoauth.configuration.token_verify_ttl
22
23
  end
23
24
  end
24
25
  end
@@ -29,7 +29,8 @@ module Capcoauth
29
29
  end
30
30
 
31
31
  def current_user
32
- @user ||= User.find_by_id verify_authorized!
32
+ verify_authorized!
33
+ @current_user ||= User.find_by_id @current_user_id
33
34
  end
34
35
 
35
36
  protected
@@ -1,3 +1,3 @@
1
1
  module Capcoauth
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capcoauth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Robertson