itg 0.1.6 → 0.1.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
  SHA256:
3
- metadata.gz: afb1a619f9859827f857cc4558a198ae8d753e3f86df4bb0c308df46abecd107
4
- data.tar.gz: 5969d0683fc63c024cc789f5e3a8fcf39cdb1238ad8b416cb1f1c5222f2839b0
3
+ metadata.gz: 3d053cf4b8d8c39243e96253436bb4287d51c0f05c350e4974274547a3054ccd
4
+ data.tar.gz: ca430ed912df96c4d35439f78b40b4a041649430f3fdeecb13f29440a9e4a6f1
5
5
  SHA512:
6
- metadata.gz: 636803d0bbf4672919fc52e95c0cac4c6d4390af1463e304bfe96889b1d6d2ac39516d71ed3492833d83e25c4aabba0412c5a21cb04f77cfdda9f480064e2e40
7
- data.tar.gz: 1ba56414129bef246771f1b7d401a6a26563af8d6de49e456ba27220f936781ff2b718c0f86412d959295bc82bdc6763da873c6b7fb269aa4ea9aff386e5d9e9
6
+ metadata.gz: be3e9fa71c749a1413158cdf1152d75bd2a6891bce296bb337ae86a005132df6d5b0113ced3750130e764006f3236e5f48f9cec41d0b6a73d0ec6958e05d9535
7
+ data.tar.gz: 9e59e76d5deea88cd5d5ce33a3538288411974623852ff8c0f45249a4e12bd9d15ff1df6114be98c6ffbf4891ff03c121d1169c81da3ccf5b1dc0ac66ee6845d
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ # ItgApiKeyBase module
4
+ module Itg
5
+ module ApiKeyModelBase
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ include Itg::MongoBase
10
+ include Itg::Sec
11
+
12
+ field :bearer_id, type: Integer
13
+ field :bearer_type, type: String
14
+ field :token_digest, type: String
15
+
16
+ index({ bearer_id: 1, bearer_type: 1}, { name: 'bearer_id_type_index' })
17
+ index({ token_digest: 1 }, { unique: true, name: 'token_digest_index' })
18
+
19
+ attr_accessor :token
20
+
21
+ belongs_to :bearer, polymorphic: true
22
+
23
+ before_create :generate_token_hmac
24
+
25
+ def serializable_hash(options = nil)
26
+ h = super options.merge(except: "token_digest")
27
+ h.merge! "token" => token if token.present?
28
+ h
29
+ end
30
+
31
+ private
32
+
33
+ def generate_token_hmac
34
+ raise Mongoid::Errors::InvalidValue, 'token is required' unless token.present?
35
+
36
+ digest = OpenSSL::HMAC.hexdigest 'SHA256', self.class.hmac_secret_key, token
37
+
38
+ self.token_digest = digest
39
+ end
40
+ end
41
+
42
+ class_methods do
43
+ attr_reader :hmac_secret_key
44
+
45
+ def authenticate_by_token!(token)
46
+ digest = OpenSSL::HMAC.hexdigest "SHA256", hmac_secret_key, token
47
+
48
+ find_by! token_digest: digest
49
+ end
50
+
51
+ def authenticate_by_token(token)
52
+ authenticate_by_token! token
53
+ rescue Mongoid::Errors::DocumentNotFound
54
+ nil
55
+ end
56
+
57
+ private
58
+
59
+ def itg_api_key_base(hmac_secret_key: "not-yet-specified")
60
+ @hmac_secret_key = hmac_secret_key
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Itg
4
+ module ContextModelBase
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ include Itg::MongoBase
9
+ include Itg::Sec
10
+
11
+ field :code, type: String
12
+ field :name, type: String
13
+ field :descr, type: String
14
+ field :db, type: String
15
+
16
+ validates_presence_of(:code, :name)
17
+ validates_uniqueness_of :code
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Itg
4
+ module EntityModelBase
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ include Itg::MongoBase
9
+
10
+ store_in collection: 'entities'
11
+
12
+ # field :context, type: Array, default: [AppConfig::DEFAULT_CONTEXT_NAME] # MERGE_WITH_BACKEND
13
+ field :context, type: Array, default: [:default]
14
+ field :permissions, type: Object, default: {}
15
+ field :kind, type: String
16
+ field :attrs, type: Object
17
+ field :roles, type: Array, default: []
18
+ field :tags, type: Array, default: []
19
+
20
+ validates_presence_of :kind
21
+ validates_presence_of :attrs
22
+
23
+ belongs_to :owner, class_name: 'User'
24
+
25
+ def to_s
26
+ attrs['name'] || attrs['descr'] || attrs['code']
27
+ end
28
+
29
+ def itg_print(header: nil, prefix: '', allow_nested: true)
30
+ puts header if header
31
+ puts "#{prefix}#{[attrs['name'] || attrs['descr'] || attrs['code']]}"
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Itg
4
+ module UserModelBase
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ include Itg::MongoBase
9
+ include Itg::Sec
10
+
11
+ devise :database_authenticatable, :registerable, :recoverable, :rememberable,
12
+ :validatable, :confirmable, :lockable, :timeoutable, :trackable
13
+
14
+ ## Database authenticatable
15
+ field :encrypted_password, type: String, default: ''
16
+
17
+ ## Recoverable
18
+ field :reset_password_token, type: String
19
+ field :reset_password_sent_at, type: Time
20
+
21
+ ## Rememberable
22
+ field :remember_created_at, type: Time
23
+
24
+ ## Trackable
25
+ field :sign_in_count, type: Integer, default: 0
26
+ field :current_sign_in_at, type: Time
27
+ field :last_sign_in_at, type: Time
28
+ field :current_sign_in_ip, type: String
29
+ field :last_sign_in_ip, type: String
30
+
31
+ ## Confirmable
32
+ field :confirmation_token, type: String
33
+ field :confirmed_at, type: Time
34
+ field :confirmation_sent_at, type: Time
35
+ field :unconfirmed_email, type: String # Only if using reconfirmable
36
+
37
+ ## Lockable
38
+ field :failed_attempts, type: Integer, default: 0 # Only if lock strategy is :failed_attempts
39
+ field :unlock_token, type: String # Only if unlock strategy is :email or :both
40
+ field :locked_at, type: Time
41
+
42
+
43
+ # include ActiveModel::SecurePassword
44
+
45
+ field :email, type: String
46
+ # field :password_digest, type: String
47
+ field :db, type: String
48
+ field :kind, type: String, default: 'user'
49
+ field :contexts, type: Object, default: {}
50
+
51
+ index({ email: 1 }, { unique: true, name: 'email_index' })
52
+
53
+ has_many :api_keys, as: :bearer
54
+ has_many :requests, dependent: :destroy
55
+
56
+ # has_secure_password
57
+
58
+ validates_presence_of(:email)
59
+ validates_uniqueness_of(:email)
60
+
61
+ def write_attribute(attr_name, value)
62
+ # puts "write_attribute - attr_name: #{attr_name}, value: #{value}"
63
+ if attr_name.to_s.downcase == 'kind'
64
+ value = value.to_s.strip.downcase
65
+ value = 'user' if ['', nil].include? value
66
+ end
67
+ super
68
+ end
69
+
70
+ def add_context(context, role: :user)
71
+ if context.persisted?
72
+ unless contexts.keys.include?(context.code.to_sym)
73
+ contexts[context.code.to_sym] = {role: role.to_sym}
74
+ contexts[context.code.to_sym][:name] = context[:name] if context[:name]
75
+ contexts[context.code.to_sym][:descr] = context[:descr] if context[:descr]
76
+ contexts[context.code.to_sym][:db] = context[:db] if context[:db]
77
+ end
78
+ end
79
+ end
80
+
81
+ def to_s
82
+ attributes.symbolize_keys.slice(:_id, :email, :db, :role).to_s
83
+ end
84
+
85
+ def itg_print(header: nil, prefix: '', allow_nested: true)
86
+ puts header if header
87
+ puts "#{prefix}#{to_s}"
88
+ end
89
+ end
90
+
91
+ class_methods do
92
+
93
+ end
94
+ end
95
+ 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.8"
5
+ DATE = "18/2/2024"
6
6
 
7
7
  def self.version_info
8
8
  "Itg gem v.#{VERSION} #{DATE}"
data/lib/itg.rb CHANGED
@@ -1,11 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "itg/version"
4
- require_relative "itg/itg_model_base"
4
+ require_relative "itg/models/itg_model_base"
5
+ require_relative "itg/models/itg_api_key_model_base"
6
+ require_relative "itg/models/itg_user_model_base"
7
+ require_relative "itg/models/itg_context_model_base"
8
+ require_relative "itg/models/itg_entity_model_base"
5
9
  require_relative "itg/itg_mongo_base"
6
10
  require_relative "itg/itg_printable"
7
- # require_relative "itg/itg_sec"
8
- require_relative "itg/itg_api_key_base"
9
11
  require_relative "itg/itg_api_key_authenticatable"
10
12
  require_relative "itg/itg_response"
11
13
  require_relative "itg/itg_sec"
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.8
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:
@@ -30,12 +30,15 @@ files:
30
30
  - itg.gemspec
31
31
  - lib/itg.rb
32
32
  - lib/itg/itg_api_key_authenticatable.rb
33
- - lib/itg/itg_api_key_base.rb
34
- - lib/itg/itg_model_base.rb
35
33
  - lib/itg/itg_mongo_base.rb
36
34
  - lib/itg/itg_printable.rb
37
35
  - lib/itg/itg_response.rb
38
36
  - lib/itg/itg_sec.rb
37
+ - lib/itg/models/itg_api_key_model_base.rb
38
+ - lib/itg/models/itg_context_model_base.rb
39
+ - lib/itg/models/itg_entity_model_base.rb
40
+ - lib/itg/models/itg_model_base.rb
41
+ - lib/itg/models/itg_user_model_base.rb
39
42
  - lib/itg/version.rb
40
43
  - sig/itg.rbs
41
44
  homepage: https://aaon.aggate.gr
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # ItgApiKeyBase module
4
- module Itg
5
- module ApiKeyBase
6
- extend ActiveSupport::Concern
7
-
8
- # include Itg::MongoBase
9
- # include Itg::Sec
10
- # include Mongoid::Fields
11
- #
12
- # field :bearer_id, type: Integer
13
- # field :bearer_type, type: String
14
- # field :token_digest, type: String
15
- #
16
- # index({ bearer_id: 1, bearer_type: 1}, { name: "bearer_id_type_index" })
17
- # index({ token_digest: 1 }, { unique: true, name: "token_digest_index" })
18
-
19
- included do
20
- include Itg::MongoBase
21
- include Itg::Sec
22
-
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
38
- def serializable_hash(options = nil)
39
- h = super options.merge(except: 'token_digest')
40
- h.merge! 'token' => token if token.present?
41
- h
42
- end
43
- end
44
- end
45
- end