itg 0.1.6 → 0.1.7

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
  SHA256:
3
- metadata.gz: afb1a619f9859827f857cc4558a198ae8d753e3f86df4bb0c308df46abecd107
4
- data.tar.gz: 5969d0683fc63c024cc789f5e3a8fcf39cdb1238ad8b416cb1f1c5222f2839b0
3
+ metadata.gz: e1bd046d2cba513820497be3bb7a44105bbaee3d420ba96f22bb016d096c14ac
4
+ data.tar.gz: 26615592add3b3bc1c68845962ef49425efc5ab47127548c8c0a73ce6ceaa09b
5
5
  SHA512:
6
- metadata.gz: 636803d0bbf4672919fc52e95c0cac4c6d4390af1463e304bfe96889b1d6d2ac39516d71ed3492833d83e25c4aabba0412c5a21cb04f77cfdda9f480064e2e40
7
- data.tar.gz: 1ba56414129bef246771f1b7d401a6a26563af8d6de49e456ba27220f936781ff2b718c0f86412d959295bc82bdc6763da873c6b7fb269aa4ea9aff386e5d9e9
6
+ metadata.gz: 951fc43e0fcb89077b1e75c0f9bad2d5594b7bf51e0515c6b073214b215dbc07f0214f95c429abfa4007f5954eb4ff3c96af82c0a2ed8b50ae3d698751714ce2
7
+ data.tar.gz: 6d17eaeab84070b5498cfd39791321ea5cffb360c32816f67a89d5acbe75b8728890d7642384d813ea1242e45a3407e21e07507aa31dbcf676dca3905f6d14c1
@@ -5,6 +5,8 @@ module Itg
5
5
  module ApiKeyBase
6
6
  extend ActiveSupport::Concern
7
7
 
8
+
9
+
8
10
  # include Itg::MongoBase
9
11
  # include Itg::Sec
10
12
  # include Mongoid::Fields
@@ -20,26 +22,56 @@ module Itg
20
22
  include Itg::MongoBase
21
23
  include Itg::Sec
22
24
 
23
- # field :bearer_id, type: Integer
24
- # field :bearer_type, type: String
25
- # field :token_digest, type: String
26
- #
27
- # index({ bearer_id: 1, bearer_type: 1}, { name: "bearer_id_type_index" })
28
- # index({ token_digest: 1 }, { unique: true, name: "token_digest_index" })
29
-
30
- # Virtual attribute for raw token value, allowing us to respond with the
31
- # API key's non-hashed token value. but only directly after creation.
32
- # attr_accessor :token
33
- #
34
- # belongs_to :bearer, polymorphic: true
35
-
36
- # Add virtual token attribute to serializable attributes, and exclude
37
- # the token's HMAC digest
25
+ field :bearer_id, type: Integer
26
+ field :bearer_type, type: String
27
+ field :token_digest, type: String
28
+
29
+ index({ bearer_id: 1, bearer_type: 1}, { name: 'bearer_id_type_index' })
30
+ index({ token_digest: 1 }, { unique: true, name: 'token_digest_index' })
31
+
32
+ attr_accessor :token
33
+
34
+ belongs_to :bearer, polymorphic: true
35
+
36
+ before_create :generate_token_hmac
37
+
38
38
  def serializable_hash(options = nil)
39
- h = super options.merge(except: 'token_digest')
40
- h.merge! 'token' => token if token.present?
39
+ h = super options.merge(except: "token_digest")
40
+ h.merge! "token" => token if token.present?
41
41
  h
42
42
  end
43
+
44
+ private
45
+
46
+ def generate_token_hmac
47
+ raise Mongoid::Errors::InvalidValue, 'token is required' unless token.present?
48
+
49
+ digest = OpenSSL::HMAC.hexdigest 'SHA256', self.class.hmac_secret_key, token
50
+
51
+ self.token_digest = digest
52
+ end
53
+ end
54
+
55
+ class_methods do
56
+ attr_reader :hmac_secret_key
57
+
58
+ def authenticate_by_token!(token)
59
+ digest = OpenSSL::HMAC.hexdigest "SHA256", hmac_secret_key, token
60
+
61
+ find_by! token_digest: digest
62
+ end
63
+
64
+ def authenticate_by_token(token)
65
+ authenticate_by_token! token
66
+ rescue Mongoid::Errors::DocumentNotFound
67
+ nil
68
+ end
69
+
70
+ private
71
+
72
+ def itg_api_key_base(hmac_secret_key: "not-yet-specified")
73
+ @hmac_secret_key = hmac_secret_key
74
+ end
43
75
  end
44
76
  end
45
77
  end
data/lib/itg/version.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Itg
4
- VERSION = "0.1.6"
5
- DATE = "17/2/2024"
4
+ VERSION = "0.1.7"
5
+ DATE = "18/2/2024"
6
6
 
7
7
  def self.version_info
8
8
  "Itg gem v.#{VERSION} #{DATE}"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: itg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - aAon
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-17 00:00:00.000000000 Z
11
+ date: 2024-02-18 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: