jungle_path 0.0.0

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.
Files changed (129) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +21 -0
  3. data/.ruby-gemset +1 -0
  4. data/.ruby-version +1 -0
  5. data/Gemfile +22 -0
  6. data/README.md +5 -0
  7. data/jungle_path.gemspec +43 -0
  8. data/lib/jungle_path/api/helpers/auth.rb +45 -0
  9. data/lib/jungle_path/api/helpers/auth_local_user.rb +284 -0
  10. data/lib/jungle_path/api/helpers/auth_old.rb +232 -0
  11. data/lib/jungle_path/api/helpers/data_cache.rb +20 -0
  12. data/lib/jungle_path/api/helpers/defaults.rb +83 -0
  13. data/lib/jungle_path/api/helpers/logging.rb +36 -0
  14. data/lib/jungle_path/api/helpers/query_filters.rb +15 -0
  15. data/lib/jungle_path/api/helpers/rescues.rb +15 -0
  16. data/lib/jungle_path/api/helpers/result.rb +16 -0
  17. data/lib/jungle_path/api/helpers/standard_apis.rb +280 -0
  18. data/lib/jungle_path/api/helpers.rb +16 -0
  19. data/lib/jungle_path/api/template.erb +35 -0
  20. data/lib/jungle_path/api.rb +5 -0
  21. data/lib/jungle_path/app/a.gitignore +1 -0
  22. data/lib/jungle_path/app/api/server_base.rb +95 -0
  23. data/lib/jungle_path/app/api/server_custom.rb +121 -0
  24. data/lib/jungle_path/app/api/server_gen.rb +11 -0
  25. data/lib/jungle_path/app/auth/authorization.rb +96 -0
  26. data/lib/jungle_path/app/config/a.gitignore +1 -0
  27. data/lib/jungle_path/app/config/config.rb +240 -0
  28. data/lib/jungle_path/app/config/override.rb +3 -0
  29. data/lib/jungle_path/app/config.ru +28 -0
  30. data/lib/jungle_path/app/logs/log_files_go_here +0 -0
  31. data/lib/jungle_path/app/run.sh +4 -0
  32. data/lib/jungle_path/app/schemas/schema.rb +21 -0
  33. data/lib/jungle_path/app/schemas/schema_all_in_one.rb +181 -0
  34. data/lib/jungle_path/app.rb +8 -0
  35. data/lib/jungle_path/authentication/auth_provider/default.rb +83 -0
  36. data/lib/jungle_path/authentication/auth_provider.rb +7 -0
  37. data/lib/jungle_path/authentication/data_provider/default.rb +144 -0
  38. data/lib/jungle_path/authentication/data_provider.rb +7 -0
  39. data/lib/jungle_path/authentication/helpers.rb +19 -0
  40. data/lib/jungle_path/authentication/identity.rb +30 -0
  41. data/lib/jungle_path/authentication/password_hash.rb +124 -0
  42. data/lib/jungle_path/authentication.rb +9 -0
  43. data/lib/jungle_path/authorization/filter.rb +106 -0
  44. data/lib/jungle_path/authorization/paths.rb +71 -0
  45. data/lib/jungle_path/authorization.rb +5 -0
  46. data/lib/jungle_path/cache.rb +36 -0
  47. data/lib/jungle_path/config.rb +65 -0
  48. data/lib/jungle_path/controller/authentication.rb +129 -0
  49. data/lib/jungle_path/controller/base.rb +193 -0
  50. data/lib/jungle_path/controller/helpers.rb +47 -0
  51. data/lib/jungle_path/controller/template.erb +14 -0
  52. data/lib/jungle_path/controller.rb +7 -0
  53. data/lib/jungle_path/db_access/import/db_dir.rb +74 -0
  54. data/lib/jungle_path/db_access/import/delete.rb +30 -0
  55. data/lib/jungle_path/db_access/import/insert.rb +168 -0
  56. data/lib/jungle_path/db_access/import/schema.rb +34 -0
  57. data/lib/jungle_path/db_access/import/select.rb +68 -0
  58. data/lib/jungle_path/db_access/import.rb +15 -0
  59. data/lib/jungle_path/db_access/io/chunked_file_reader.rb +62 -0
  60. data/lib/jungle_path/db_access/io/config.rb +19 -0
  61. data/lib/jungle_path/db_access/io/copy.rb +73 -0
  62. data/lib/jungle_path/db_access/io/db.rb +82 -0
  63. data/lib/jungle_path/db_access/io/delete.rb +23 -0
  64. data/lib/jungle_path/db_access/io/init_db.rb +39 -0
  65. data/lib/jungle_path/db_access/io/insert.rb +24 -0
  66. data/lib/jungle_path/db_access/io/schema.rb +21 -0
  67. data/lib/jungle_path/db_access/io/select.rb +44 -0
  68. data/lib/jungle_path/db_access/io/update.rb +36 -0
  69. data/lib/jungle_path/db_access/io.rb +104 -0
  70. data/lib/jungle_path/db_model/column.rb +186 -0
  71. data/lib/jungle_path/db_model/params.rb +60 -0
  72. data/lib/jungle_path/db_model/schema.rb +100 -0
  73. data/lib/jungle_path/db_model/string.rb +9 -0
  74. data/lib/jungle_path/db_model/table.rb +307 -0
  75. data/lib/jungle_path/db_model.rb +34 -0
  76. data/lib/jungle_path/exceptions.rb +10 -0
  77. data/lib/jungle_path/gen/api.rb +52 -0
  78. data/lib/jungle_path/gen/controller.rb +0 -0
  79. data/lib/jungle_path/gen/db.rb +0 -0
  80. data/lib/jungle_path/gen/schema.rb +47 -0
  81. data/lib/jungle_path/gen/schema_tree/filter.rb +33 -0
  82. data/lib/jungle_path/gen/schema_tree/match_columns.rb +54 -0
  83. data/lib/jungle_path/gen/schema_tree/match_table_data.rb +22 -0
  84. data/lib/jungle_path/gen/schema_tree/match_tables.rb +70 -0
  85. data/lib/jungle_path/gen/schema_tree/node.rb +39 -0
  86. data/lib/jungle_path/gen/schema_tree.rb +105 -0
  87. data/lib/jungle_path/gen.rb +9 -0
  88. data/lib/jungle_path/json/base.rb +29 -0
  89. data/lib/jungle_path/json/time.rb +8 -0
  90. data/lib/jungle_path/json.rb +6 -0
  91. data/lib/jungle_path/logging.rb +23 -0
  92. data/lib/jungle_path/query/alias_info.rb +16 -0
  93. data/lib/jungle_path/query/engine.rb +878 -0
  94. data/lib/jungle_path/query/entity.rb +141 -0
  95. data/lib/jungle_path/query/field.rb +28 -0
  96. data/lib/jungle_path/query/field_primary_key.rb +27 -0
  97. data/lib/jungle_path/query/filter.rb +34 -0
  98. data/lib/jungle_path/query/float_value.rb +16 -0
  99. data/lib/jungle_path/query/from.rb +33 -0
  100. data/lib/jungle_path/query/int_value.rb +16 -0
  101. data/lib/jungle_path/query/limit.rb +19 -0
  102. data/lib/jungle_path/query/nested_hash_sorter.rb +94 -0
  103. data/lib/jungle_path/query/operator.rb +17 -0
  104. data/lib/jungle_path/query/query.rb +23 -0
  105. data/lib/jungle_path/query/sort_field.rb +34 -0
  106. data/lib/jungle_path/query/sql_string.rb +145 -0
  107. data/lib/jungle_path/query/string_value.rb +16 -0
  108. data/lib/jungle_path/query.rb +19 -0
  109. data/lib/jungle_path/rack/basic_credentials.rb +70 -0
  110. data/lib/jungle_path/rack/json_body_parser.rb +41 -0
  111. data/lib/jungle_path/rack.rb +6 -0
  112. data/lib/jungle_path/schema/auth.rb +83 -0
  113. data/lib/jungle_path/schema/base.rb +6 -0
  114. data/lib/jungle_path/schema/db.rb +10 -0
  115. data/lib/jungle_path/schema/version.rb +19 -0
  116. data/lib/jungle_path/schema.rb +8 -0
  117. data/lib/jungle_path/sql/auth_local_user.rb +5 -0
  118. data/lib/jungle_path/sql/general.rb +10 -0
  119. data/lib/jungle_path/sql/helpers.rb +11 -0
  120. data/lib/jungle_path/sql/key.rb +107 -0
  121. data/lib/jungle_path/sql/query_filter.rb +5 -0
  122. data/lib/jungle_path/sql/role.rb +5 -0
  123. data/lib/jungle_path/sql/user.rb +35 -0
  124. data/lib/jungle_path/sql/user_role.rb +5 -0
  125. data/lib/jungle_path/sql.rb +12 -0
  126. data/lib/jungle_path.rb +13 -0
  127. data/test.rb +33 -0
  128. data/test2.rb +15 -0
  129. metadata +200 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d7c640c8f8482983063d216433074adca552b14c
4
+ data.tar.gz: 26812a3c5aa1cfacf98b9fd787fd56b6620a3df1
5
+ SHA512:
6
+ metadata.gz: 2250667bf6a1780bd176fff62161379246a7eb358ae8fe0f497db34d2540087756ee291221f32f73628fc085b516aad4c2eeb492c379da123d51b5567a3009f2
7
+ data.tar.gz: 597fbde8599a34552c5e7db85cf9a0efdf03b6a9463ccf51c1f6840d5c6f5cc6c94971952aa66dc5147121112491e9e77a7103fcc9fc2672cc49c42839e686f1
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .devnotes
6
+ .greenbar
7
+ .yardoc
8
+ .rubocop-*
9
+ Gemfile.lock
10
+ InstalledFiles
11
+ _yardoc
12
+ coverage
13
+ doc/
14
+ lib/bundler/man
15
+ pkg
16
+ rdoc
17
+ spec/reports
18
+ test/tmp
19
+ test/version_tmp
20
+ tmp
21
+ bin
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ junglepathdev
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.1
data/Gemfile ADDED
@@ -0,0 +1,22 @@
1
+ # A sample Gemfile
2
+ source "https://rubygems.org"
3
+
4
+ # gem "rails"
5
+ gem 'pry'
6
+ gem 'pry-byebug'
7
+ gem 'sequel'
8
+ gem 'pg'
9
+ gem 'sequel_pg', :require=>'sequel'
10
+ gem 'sinatra'
11
+ gem 'rack-contrib'
12
+ gem 'rack-mobile-detect'
13
+ gem 'sinatra-param'
14
+ gem 'sinatra-basic-auth'
15
+ gem 'oj', '2.17.4'
16
+ gem 'http'
17
+ gem 'redis'
18
+ gem 'cache'
19
+ gem 'tiny_tds'
20
+ gem 'aws-sdk', '~> 2'
21
+ gem 'memory_profiler'
22
+ gem 'celluloid'
data/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # Jungle Path
2
+
3
+ A new Ruby framework (and gem) for writing modern API services.
4
+
5
+ Version 0.0.0
@@ -0,0 +1,43 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'jungle_path'
7
+ spec.version = '0.0.0'
8
+ spec.authors = ['Michael VanZant']
9
+ spec.email = ['mxvanzant@yahoo.com']
10
+ spec.summary = 'Full-stack web application framework for creating API services.'
11
+ spec.description = 'Jungle Path is a full-stack web application framework for creating API services.'
12
+ spec.homepage = 'https://rubygems.org/gems/jungle_path'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = "exe"
19
+ #spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+ spec.required_ruby_version = '>= 2.3.1'
22
+ spec.add_development_dependency "bundler", "~> 1.14"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+
25
+
26
+ #spec.files = `git ls-files -- lib/* CHANGELOG.md LICENSE.md README.md hanami-utils.gemspec`.split($/)
27
+ #spec.files = ['lib/jungle_path-json.rb', 'lib/jungle_path/json.rb']
28
+ #spec.files = `git ls-files`.split($/)
29
+ #spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
30
+ #spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
31
+ #spec.require_paths = ['lib']
32
+ #spec.required_ruby_version = '>= 2.3.1'
33
+ #spec.add_runtime_dependency 'oj', '2.17.4'
34
+ #spec.add_runtime_dependency 'sequel', '>= 4.0.0'
35
+ #spec.add_runtime_dependency 'pg'
36
+ #spec.add_runtime_dependency 'sequel_pg'
37
+ #spec.add_runtime_dependency 'sinatra', '~> 1.3'
38
+ #spec.add_runtime_dependency 'rack-contrib'
39
+ #spec.add_runtime_dependency 'sinatra-param'
40
+ #spec.add_runtime_dependency 'sinatra-basic-auth'
41
+ #spec.add_development_dependency 'bundler', '~> 1.6'
42
+ #spec.add_development_dependency 'rake', '~> 11'
43
+ end
@@ -0,0 +1,45 @@
1
+ module JunglePath
2
+ require 'jungle_path/authentication/data_provider/default'
3
+ module API
4
+ module Helpers
5
+ module Auth
6
+ def authenticate auth_provider=nil, data_provider=nil, no_cache=false
7
+ puts "Auth.authenticate"
8
+ data_provider = JunglePath::Authentication::DataProvider::Default.new unless data_provider
9
+ auth_provider = JunglePath::Authentication::AuthProvider::Default.new unless auth_provider
10
+ @current_identity = auth_provider.authenticate(data_provider, no_cache)
11
+ halt 401 unless @current_identity and @current_identity.valid?
12
+ request.body.rewind
13
+ end
14
+
15
+ def current_identity
16
+ @current_identity
17
+ end
18
+
19
+ def current_user
20
+ @current_identity and @current_identity.user
21
+ end
22
+
23
+ def current_key
24
+ @current_identity and @current_identity.key
25
+ end
26
+
27
+ def current_roles
28
+ @current_identity and @current_identity.roles
29
+ end
30
+
31
+ def current_role
32
+ @current_identity and @current_identity.default_role
33
+ end
34
+
35
+ def current_auth
36
+ @current_identity and @current_identity.auth
37
+ end
38
+
39
+ def current_query_filters
40
+ @current_identity and @current_identity.query_filters
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,284 @@
1
+ module JunglePath
2
+ module API
3
+ module Helpers
4
+ module AuthLocalUser
5
+ class Identity
6
+ attr_accessor :remote_user, :remote_password, :user_name, :user, :key, :valid, :roles, :auth
7
+ def to_s
8
+ "AuthLocalUser::Identity: {\n remote_user: #{@remote_user},\n remote_password: #{@remote_password},\n user_name: #{@user_name},\n user: #{@user},\n key: #{@key},\n valid: #{@valid},\n roles: #{@roles},\n auth: #{@auth}\n}"
9
+ end
10
+ end
11
+
12
+ # If you are using this module, make sure your user table has these columns:
13
+ # id,
14
+ # user_name,
15
+ # phone,
16
+ # sms_verification_code,
17
+ # hash,
18
+ # key,
19
+ # role
20
+ def authenticate no_cache=false
21
+ puts "AuthLocalUser.authenticate !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
22
+ remote_user = request.env['REMOTE_USER']
23
+ remote_password = request.env['REMOTE_PASSWORD']
24
+ puts "remote_user: #{remote_user}."
25
+ puts "remote_password: #{remote_password}."
26
+ is_authenticated = basic_authentication(remote_user, remote_password, no_cache)
27
+ unless is_authenticated
28
+ # force no_cache = true
29
+ halt 401 unless basic_authentication(remote_user, remote_password, true)
30
+ end
31
+ #puts "content-type: #{request.content_type}."
32
+ request.body.rewind
33
+ #puts "body:\n#{request.body.read}."
34
+ #puts "params:\n#{params}."
35
+ end
36
+
37
+ def basic_authentication remote_user, remote_password, no_cache=false
38
+ identity, assume_identity = parse_identities(remote_user, remote_password)
39
+ puts "identity: #{identity}"
40
+ puts "assume_identity: #{assume_identity}"
41
+ #puts "APIHelpers::AuthLocalUser.#{__method__}."
42
+ #if user_name and user_name.start_with?("sk_")
43
+
44
+ #valid = authenticate_user(auth.user_name, password, no_cache)
45
+ valid = false
46
+ identity = authenticate_identity(identity, no_cache)
47
+ if identity.valid
48
+ identity.roles = get_roles(identity.key, no_cache)
49
+ identity.auth = get_auth(identity.roles, no_cache)
50
+ #set_current_roles roles
51
+ #set_current_auth
52
+ if assume_identity
53
+ puts "assume_identity..."
54
+ assume_identity = authenticate_assumed_identity(assume_identity, no_cache)
55
+ assume_identity.roles = get_roles(assume_identity.key, no_cache)
56
+ assume_identity.auth = get_auth(assume_identity.roles, no_cache)
57
+ valid = assume_identity.valid
58
+ set_current_identity assume_identity, no_cache
59
+ else
60
+ valid = identity.valid
61
+ set_current_identity identity, no_cache
62
+ end
63
+ else
64
+ #set_current_roles nil
65
+ set_current_identity identity, no_cache
66
+ end
67
+ valid
68
+ end
69
+
70
+ def authenticate_identity identity, no_cache=false
71
+ id = identity.dup
72
+ id.user = get_user(identity.user_name, identity.remote_password, no_cache)
73
+ id.key = id.user
74
+ id.valid = (id.user and id.user.is_valid)
75
+ id
76
+ end
77
+
78
+ def authenticate_assumed_identity identity, no_cache=false
79
+ id = identity.dup
80
+ id.user = get_assumed_user(identity.user_name, no_cache)
81
+ id.key = id.user
82
+ id.valid = (id.user and id.user.is_valid)
83
+ id
84
+ end
85
+
86
+ def authenticate_user user_name, password, no_cache=false
87
+ user = get_user(user_name, password, no_cache)
88
+ set_current_user(user)
89
+ valid = (user and user.is_valid)
90
+ end
91
+
92
+ def get_assumed_user user_name, no_cache=false
93
+ cache_key = "#{user_name}.password"
94
+ user = cache[cache_key]
95
+ if user == nil or no_cache
96
+ lowercase_user_name = nil
97
+ lowercase_user_name = user_name.downcase if user_name
98
+ ds = db.base['select id, user_name, name, first_name, last_name, phone, email, hash, key, active from "user" where user_name = ? or email = ?', lowercase_user_name, lowercase_user_name]
99
+ hash = ds.first
100
+ #puts "get_user: hash: #{hash}."
101
+ user = Schema::User.new(hash, false) if hash
102
+ halt 401, "Unauthorized" unless user
103
+ halt 401, "Unauthorized: user #{user.user_name} is not marked as active." unless user.active
104
+ user.is_valid = true
105
+ cache[cache_key] = user if user
106
+ end
107
+ user
108
+ end
109
+
110
+ def get_user user_name, password, no_cache=false
111
+ # is this username/password valid?
112
+ cache_key = "#{user_name}.#{password}"
113
+ user = cache[cache_key]
114
+ if user == nil or no_cache
115
+ #assumed_user = nil
116
+ ds = nil
117
+ if user_name_is_key? user_name
118
+ ds = db.base['select id, user_name, name, first_name, last_name, phone, email, hash, key, active from "user" where key = ?', user_name.downcase]
119
+ else
120
+ lowercase_user_name = nil
121
+ lowercase_user_name = user_name.downcase if user_name
122
+ ds = db.base['select id, user_name, name, first_name, last_name, phone, email, hash, key, active from "user" where user_name = ?', lowercase_user_name]
123
+ end
124
+ hash = ds.first
125
+ #puts "get_user: hash: #{hash}."
126
+ user = Schema::User.new(hash, false) if hash
127
+ halt 401, "Unauthorized" unless user
128
+ halt 401, "Unauthorized: user #{user.user_name} is not marked as active." unless user.active
129
+ if user_name_is_key? user_name
130
+ user.is_valid = true
131
+ else
132
+ user.is_valid = valid_user?(user, password)
133
+ end
134
+ cache[cache_key] = user if user
135
+ end
136
+ user.password = password
137
+ user
138
+ end
139
+
140
+ def user_name_is_key? user_name
141
+ user_name and user_name.start_with?("sk_") and !user_name.include?("@")
142
+ end
143
+
144
+ def parse_identities remote_user, remote_password
145
+ identity = Identity.new
146
+ identity.remote_user = remote_user
147
+ identity.remote_password = remote_password
148
+ assume_identity = nil
149
+ if remote_user and remote_user.include?("|")
150
+ parts = remote_user.split('|')
151
+ identity.user_name = parts[1]
152
+ assume_identity = Identity.new
153
+ assume_identity.user_name = parts[0]
154
+ assume_identity.remote_user = remote_user
155
+ assume_identity.remote_password = remote_password
156
+ else
157
+ identity.user_name = remote_user
158
+ end
159
+ return identity, assume_identity
160
+ end
161
+
162
+ def valid_user?(user, password)
163
+ valid = (user and PasswordHash.validatePassword(password, user.hash))
164
+ end
165
+
166
+ def get_roles key, no_cache=false
167
+ return nil unless key
168
+ cache_key = "#{key.id}_roles"
169
+ roles = cache.get(cache_key)
170
+ if roles == nil or no_cache
171
+ roles = SQL::Role.by_user(db, key)
172
+ cache.set cache_key, roles if roles
173
+ end
174
+ roles
175
+ end
176
+
177
+ def zget_roles no_cache=false
178
+ return nil unless current_key
179
+ cache_key = "#{current_key.id}_roles"
180
+ roles = cache.get(cache_key)
181
+ if roles == nil or no_cache
182
+ roles = SQL::Role.by_user(db, current_key)
183
+ cache.set cache_key, roles if roles
184
+ end
185
+ roles
186
+ end
187
+
188
+ def get_auth roles, no_cache=false
189
+ cache_key = "#{roles}_auth"
190
+ puts "get_auth cache_key: #{cache_key}."
191
+ auth = cache.get(cache_key)
192
+ if auth == nil or no_cache
193
+ auth = JunglePath::Authorization::Filter.new roles, Schema::Base.models, configatron.application.role_permissions, configatron.application.role_restrictions, configatron.application.role_schema_filters, configatron.schema.filters
194
+ end
195
+ auth
196
+ end
197
+
198
+ def get_query_filters no_cache=false
199
+ # get filters from query_filter table.
200
+ return nil unless current_key
201
+ cache_key = "#{current_key.id}_query_filters"
202
+ query_filters = cache.get(cache_key)
203
+ if query_filters == nil or no_cache
204
+ query_filters = []
205
+ #filters = SQL::QueryFilter.by_key(db, current_key)
206
+ #filters.each do |filter|
207
+ # query_filters << Query::Filter.new(filter[:base_table_name], filter[:sub_select])
208
+ #end
209
+ if app_defined_query_filters
210
+ app_defined_query_filters.each do |filter|
211
+ query_filters << filter
212
+ end
213
+ end
214
+ cache.set cache_key, query_filters
215
+ end
216
+ query_filters
217
+ end
218
+
219
+ def set_current_identity identity, no_cache=false
220
+ puts "set_current_identity: #{identity.user.user_name}" if identity and identity.user
221
+ @current_identity = identity
222
+ set_current_user identity.user
223
+ set_current_roles identity.roles
224
+ set_current_auth identity.auth
225
+ set_current_query_filters no_cache
226
+ end
227
+
228
+ def set_current_user user
229
+ @current_user = user
230
+ end
231
+
232
+ def set_current_roles roles
233
+ @current_role = nil #configatron.default.role
234
+ @current_roles = roles
235
+ if roles
236
+ roles.each do |role|
237
+ @current_role = role[:name]
238
+ break
239
+ end
240
+ end
241
+ end
242
+
243
+ def set_current_auth auth
244
+ #@current_auth = ::Authorization::Filter.new current_roles, configatron.application.role_permissions, configatron.application.role_restrictions
245
+ @current_auth = auth
246
+ @current_user.auth = @current_auth if @current_user
247
+ end
248
+
249
+ def set_current_query_filters no_cache=false
250
+ @current_query_filters = get_query_filters(no_cache)
251
+ @current_user.query_filters = @current_query_filters
252
+ end
253
+
254
+ def current_identity
255
+ @current_identity
256
+ end
257
+
258
+ def current_user
259
+ @current_user
260
+ end
261
+
262
+ def current_key
263
+ @current_user
264
+ end
265
+
266
+ def current_roles
267
+ @current_roles
268
+ end
269
+
270
+ def current_role
271
+ @current_role
272
+ end
273
+
274
+ def current_auth
275
+ @current_auth
276
+ end
277
+
278
+ def current_query_filters
279
+ @current_query_filters
280
+ end
281
+ end
282
+ end
283
+ end
284
+ end
@@ -0,0 +1,232 @@
1
+ module JunglePath
2
+ module API
3
+ module Helpers
4
+ module AuthOld
5
+ def authenticate no_cache=false
6
+ user_name = request.env['REMOTE_USER']
7
+ password = request.env['REMOTE_PASSWORD']
8
+ valid, authentication_messages = basic_authentication(user_name, password, no_cache)
9
+ unless valid
10
+ valid, authentication_messages = basic_authentication(user_name, password, true)
11
+ halt 401, authentication_messages.join("\n") unless valid
12
+ end
13
+ request.body.rewind
14
+ end
15
+
16
+ def basic_authentication user_name, password, no_cache=false
17
+ authentication_messages = []
18
+ if user_name and user_name.start_with?("sk_")
19
+ valid = authenticate_key(user_name, no_cache)
20
+ if valid
21
+ authentication_messages << "key is valid: #{current_key.to_h}."
22
+ user = get_user_from_key(current_key, no_cache)
23
+ unless user
24
+ authentication_messages << "User not found for current_key."
25
+ valid = false
26
+ end
27
+ set_current_user user
28
+ else
29
+ authentication_messages << "key #{user_name} is not valid."
30
+ set_current_user nil
31
+ end
32
+ else
33
+ valid = authenticate_user(user_name, password, no_cache)
34
+ if valid
35
+ authentication_messages << "User is valid: #{current_user.to_h}."
36
+ key = get_default_key(current_user.id, no_cache)
37
+ unless key
38
+ authentication_messages << "Default key not found for current_user."
39
+ valid = false
40
+ end
41
+ set_current_key(key)
42
+ else
43
+ authentication_messages << "User #{user_name} is not valid."
44
+ set_current_key( nil)
45
+ end
46
+ end
47
+ messages = authentication_messages.join("\n ")
48
+ if valid
49
+ roles = get_roles(no_cache)
50
+ set_current_roles roles
51
+ set_current_auth
52
+ set_current_query_filters(no_cache)
53
+ else
54
+ set_current_roles nil
55
+ end
56
+ [valid, authentication_messages]
57
+ end
58
+
59
+ def authenticate_key key_string, no_cache=false
60
+ key = get_key(key_string, no_cache)
61
+ set_current_key(key)
62
+ valid = (key and key.valid?)
63
+ end
64
+
65
+ def authenticate_user user_name, password, no_cache=false
66
+ user = get_user(user_name, password, no_cache)
67
+ set_current_user(user)
68
+ valid = (user and user.is_valid)
69
+ end
70
+
71
+ def get_user user_name, password, no_cache=false
72
+ cache_key = "#{user_name}.#{password}"
73
+ user = cache.get(cache_key)
74
+ #puts "user: #{user}."
75
+ if user == nil or no_cache
76
+ hash = SQL::User.by_user_name(db, user_name)
77
+ puts "hash: #{hash}."
78
+ user = Schema::User.new(hash, false) if hash
79
+ user.is_valid = valid_user?(user, password) if user
80
+ cache.set cache_key, user if user
81
+ end
82
+ user
83
+ end
84
+
85
+ def get_user_from_key key, no_cache=false
86
+ user = nil
87
+ if key
88
+ cache_key = "user_by_key_#{key.key}"
89
+ user = cache.get(cache_key)
90
+ if user == nil or no_cache
91
+ user = Controller::User.new(current_user, current_key, {id: key.user_id}, db).select
92
+ cache.set cache_key, user if user
93
+ end
94
+ end
95
+ user
96
+ end
97
+
98
+ def get_any_user user_name, password, no_cache=false
99
+ cache_key = "#{user_name}.#{password}"
100
+ user = cache.get(cache_key)
101
+ puts "user: #{user}."
102
+ if user == nil or no_cache
103
+ hash = SQL::AnyUser.by_user_name(db, user_name)
104
+ puts "hash: #{hash}."
105
+ user = Schema::User.new(hash, false) if hash
106
+ user.is_valid = valid_user?(user, password) if user
107
+ cache.set cache_key, user if user
108
+ end
109
+ user
110
+ end
111
+
112
+ def valid_user? user, password
113
+ valid = (user and PasswordHash.validatePassword(password, user.hash))
114
+ end
115
+
116
+ def get_key key_string, no_cache=false
117
+ key = cache.get(key_string)
118
+ if key == nil or no_cache
119
+ array = SQL::Key.by_key(db, key_string)
120
+ hash = array.first if array
121
+ key = Schema::Key.new(hash) if hash
122
+ cache.set key_string, key if key
123
+ end
124
+ puts "key.key: #{key.key}."
125
+ key
126
+ end
127
+
128
+ def get_default_key user_id, no_cache=false
129
+ cache_key = "#{user_id}.key"
130
+ key = cache.get(cache_key)
131
+ if key == nil or no_cache
132
+ array = SQL::Key.default_by_user_id(db, user_id)
133
+ hash = array.first if array
134
+ key = Schema::Key.new(hash) if hash
135
+ cache.set cache_key, key if key
136
+ end
137
+ if key
138
+ puts "default key.key: #{key.key}."
139
+ else
140
+ puts "A default key was not found for user ID: #{user_id}."
141
+ end
142
+ key
143
+ end
144
+
145
+ def get_roles no_cache=false
146
+ return nil unless current_key
147
+ cache_key = "#{current_key.id}_roles"
148
+ roles = cache.get(cache_key)
149
+ if roles == nil or no_cache
150
+ roles = SQL::Role.by_key(db, current_key)
151
+ cache.set cache_key, roles if roles
152
+ end
153
+ roles
154
+ end
155
+
156
+ def get_query_filters no_cache=false
157
+ # get filters from query_filter table.
158
+ return nil unless current_key
159
+ cache_key = "#{current_key.id}_query_filters"
160
+ query_filters = cache.get(cache_key)
161
+ if query_filters == nil or no_cache
162
+ query_filters = []
163
+ filters = SQL::QueryFilter.by_key(db, current_key)
164
+ filters.each do |filter|
165
+ query_filters << Query::Filter.new(filter[:base_table_name], filter[:sub_select])
166
+ end
167
+ if app_defined_query_filters
168
+ app_defined_query_filters.each do |filter|
169
+ query_filters << filter
170
+ end
171
+ end
172
+ cache.set cache_key, query_filters
173
+ end
174
+ query_filters
175
+ end
176
+
177
+ def set_current_user(user)
178
+ @current_user = user
179
+ end
180
+
181
+ def set_current_key(value)
182
+ @current_key = value
183
+ end
184
+
185
+ def set_current_roles roles
186
+ @current_role = nil
187
+ @current_roles = roles
188
+ if roles
189
+ roles.each do |role|
190
+ @current_role = role[:name]
191
+ break
192
+ end
193
+ end
194
+ end
195
+
196
+ def set_current_auth
197
+ @current_auth = ::Authorization::Filter.new current_roles, configatron.application.role_permissions, configatron.application.role_restrictions
198
+ @current_user.auth = @current_auth
199
+ end
200
+
201
+ def set_current_query_filters no_cache=false
202
+ @current_query_filters = get_query_filters(no_cache)
203
+ @current_user.query_filters = @current_query_filters
204
+ end
205
+
206
+ def current_user
207
+ @current_user
208
+ end
209
+
210
+ def current_key
211
+ @current_key
212
+ end
213
+
214
+ def current_roles
215
+ @current_roles
216
+ end
217
+
218
+ def current_role
219
+ @current_role
220
+ end
221
+
222
+ def current_auth
223
+ @current_auth
224
+ end
225
+
226
+ def current_query_filters
227
+ @current_query_filters
228
+ end
229
+ end
230
+ end
231
+ end
232
+ end
@@ -0,0 +1,20 @@
1
+ require 'jungle_path/cache'
2
+
3
+ module JunglePath
4
+ module API
5
+ module Helpers
6
+ module DataCache
7
+ #def zcache
8
+ # if @cache == nil
9
+ # raw_client = Redis.new
10
+ # @cache = Cache.wrap(raw_client)
11
+ # end
12
+ # @cache
13
+ #end
14
+ def cache
15
+ JunglePath::Cache
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end