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.
- checksums.yaml +7 -0
- data/.gitignore +21 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +22 -0
- data/README.md +5 -0
- data/jungle_path.gemspec +43 -0
- data/lib/jungle_path/api/helpers/auth.rb +45 -0
- data/lib/jungle_path/api/helpers/auth_local_user.rb +284 -0
- data/lib/jungle_path/api/helpers/auth_old.rb +232 -0
- data/lib/jungle_path/api/helpers/data_cache.rb +20 -0
- data/lib/jungle_path/api/helpers/defaults.rb +83 -0
- data/lib/jungle_path/api/helpers/logging.rb +36 -0
- data/lib/jungle_path/api/helpers/query_filters.rb +15 -0
- data/lib/jungle_path/api/helpers/rescues.rb +15 -0
- data/lib/jungle_path/api/helpers/result.rb +16 -0
- data/lib/jungle_path/api/helpers/standard_apis.rb +280 -0
- data/lib/jungle_path/api/helpers.rb +16 -0
- data/lib/jungle_path/api/template.erb +35 -0
- data/lib/jungle_path/api.rb +5 -0
- data/lib/jungle_path/app/a.gitignore +1 -0
- data/lib/jungle_path/app/api/server_base.rb +95 -0
- data/lib/jungle_path/app/api/server_custom.rb +121 -0
- data/lib/jungle_path/app/api/server_gen.rb +11 -0
- data/lib/jungle_path/app/auth/authorization.rb +96 -0
- data/lib/jungle_path/app/config/a.gitignore +1 -0
- data/lib/jungle_path/app/config/config.rb +240 -0
- data/lib/jungle_path/app/config/override.rb +3 -0
- data/lib/jungle_path/app/config.ru +28 -0
- data/lib/jungle_path/app/logs/log_files_go_here +0 -0
- data/lib/jungle_path/app/run.sh +4 -0
- data/lib/jungle_path/app/schemas/schema.rb +21 -0
- data/lib/jungle_path/app/schemas/schema_all_in_one.rb +181 -0
- data/lib/jungle_path/app.rb +8 -0
- data/lib/jungle_path/authentication/auth_provider/default.rb +83 -0
- data/lib/jungle_path/authentication/auth_provider.rb +7 -0
- data/lib/jungle_path/authentication/data_provider/default.rb +144 -0
- data/lib/jungle_path/authentication/data_provider.rb +7 -0
- data/lib/jungle_path/authentication/helpers.rb +19 -0
- data/lib/jungle_path/authentication/identity.rb +30 -0
- data/lib/jungle_path/authentication/password_hash.rb +124 -0
- data/lib/jungle_path/authentication.rb +9 -0
- data/lib/jungle_path/authorization/filter.rb +106 -0
- data/lib/jungle_path/authorization/paths.rb +71 -0
- data/lib/jungle_path/authorization.rb +5 -0
- data/lib/jungle_path/cache.rb +36 -0
- data/lib/jungle_path/config.rb +65 -0
- data/lib/jungle_path/controller/authentication.rb +129 -0
- data/lib/jungle_path/controller/base.rb +193 -0
- data/lib/jungle_path/controller/helpers.rb +47 -0
- data/lib/jungle_path/controller/template.erb +14 -0
- data/lib/jungle_path/controller.rb +7 -0
- data/lib/jungle_path/db_access/import/db_dir.rb +74 -0
- data/lib/jungle_path/db_access/import/delete.rb +30 -0
- data/lib/jungle_path/db_access/import/insert.rb +168 -0
- data/lib/jungle_path/db_access/import/schema.rb +34 -0
- data/lib/jungle_path/db_access/import/select.rb +68 -0
- data/lib/jungle_path/db_access/import.rb +15 -0
- data/lib/jungle_path/db_access/io/chunked_file_reader.rb +62 -0
- data/lib/jungle_path/db_access/io/config.rb +19 -0
- data/lib/jungle_path/db_access/io/copy.rb +73 -0
- data/lib/jungle_path/db_access/io/db.rb +82 -0
- data/lib/jungle_path/db_access/io/delete.rb +23 -0
- data/lib/jungle_path/db_access/io/init_db.rb +39 -0
- data/lib/jungle_path/db_access/io/insert.rb +24 -0
- data/lib/jungle_path/db_access/io/schema.rb +21 -0
- data/lib/jungle_path/db_access/io/select.rb +44 -0
- data/lib/jungle_path/db_access/io/update.rb +36 -0
- data/lib/jungle_path/db_access/io.rb +104 -0
- data/lib/jungle_path/db_model/column.rb +186 -0
- data/lib/jungle_path/db_model/params.rb +60 -0
- data/lib/jungle_path/db_model/schema.rb +100 -0
- data/lib/jungle_path/db_model/string.rb +9 -0
- data/lib/jungle_path/db_model/table.rb +307 -0
- data/lib/jungle_path/db_model.rb +34 -0
- data/lib/jungle_path/exceptions.rb +10 -0
- data/lib/jungle_path/gen/api.rb +52 -0
- data/lib/jungle_path/gen/controller.rb +0 -0
- data/lib/jungle_path/gen/db.rb +0 -0
- data/lib/jungle_path/gen/schema.rb +47 -0
- data/lib/jungle_path/gen/schema_tree/filter.rb +33 -0
- data/lib/jungle_path/gen/schema_tree/match_columns.rb +54 -0
- data/lib/jungle_path/gen/schema_tree/match_table_data.rb +22 -0
- data/lib/jungle_path/gen/schema_tree/match_tables.rb +70 -0
- data/lib/jungle_path/gen/schema_tree/node.rb +39 -0
- data/lib/jungle_path/gen/schema_tree.rb +105 -0
- data/lib/jungle_path/gen.rb +9 -0
- data/lib/jungle_path/json/base.rb +29 -0
- data/lib/jungle_path/json/time.rb +8 -0
- data/lib/jungle_path/json.rb +6 -0
- data/lib/jungle_path/logging.rb +23 -0
- data/lib/jungle_path/query/alias_info.rb +16 -0
- data/lib/jungle_path/query/engine.rb +878 -0
- data/lib/jungle_path/query/entity.rb +141 -0
- data/lib/jungle_path/query/field.rb +28 -0
- data/lib/jungle_path/query/field_primary_key.rb +27 -0
- data/lib/jungle_path/query/filter.rb +34 -0
- data/lib/jungle_path/query/float_value.rb +16 -0
- data/lib/jungle_path/query/from.rb +33 -0
- data/lib/jungle_path/query/int_value.rb +16 -0
- data/lib/jungle_path/query/limit.rb +19 -0
- data/lib/jungle_path/query/nested_hash_sorter.rb +94 -0
- data/lib/jungle_path/query/operator.rb +17 -0
- data/lib/jungle_path/query/query.rb +23 -0
- data/lib/jungle_path/query/sort_field.rb +34 -0
- data/lib/jungle_path/query/sql_string.rb +145 -0
- data/lib/jungle_path/query/string_value.rb +16 -0
- data/lib/jungle_path/query.rb +19 -0
- data/lib/jungle_path/rack/basic_credentials.rb +70 -0
- data/lib/jungle_path/rack/json_body_parser.rb +41 -0
- data/lib/jungle_path/rack.rb +6 -0
- data/lib/jungle_path/schema/auth.rb +83 -0
- data/lib/jungle_path/schema/base.rb +6 -0
- data/lib/jungle_path/schema/db.rb +10 -0
- data/lib/jungle_path/schema/version.rb +19 -0
- data/lib/jungle_path/schema.rb +8 -0
- data/lib/jungle_path/sql/auth_local_user.rb +5 -0
- data/lib/jungle_path/sql/general.rb +10 -0
- data/lib/jungle_path/sql/helpers.rb +11 -0
- data/lib/jungle_path/sql/key.rb +107 -0
- data/lib/jungle_path/sql/query_filter.rb +5 -0
- data/lib/jungle_path/sql/role.rb +5 -0
- data/lib/jungle_path/sql/user.rb +35 -0
- data/lib/jungle_path/sql/user_role.rb +5 -0
- data/lib/jungle_path/sql.rb +12 -0
- data/lib/jungle_path.rb +13 -0
- data/test.rb +33 -0
- data/test2.rb +15 -0
- metadata +200 -0
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'rack'
|
2
|
+
require 'rack/mobile-detect'
|
3
|
+
|
4
|
+
module JunglePath
|
5
|
+
require 'jungle_path/rack/json_body_parser'
|
6
|
+
require 'jungle_path/rack/basic_credentials'
|
7
|
+
|
8
|
+
module API
|
9
|
+
module Helpers
|
10
|
+
module Defaults
|
11
|
+
# default mixin that may be included in your Sinatra application class.
|
12
|
+
|
13
|
+
def set_default_rack_middleware issue_challenge=true
|
14
|
+
use Rack::MobileDetect
|
15
|
+
use Rack::CommonLogger, configatron.application.logger
|
16
|
+
use JunglePath::Rack::JsonBodyParser, true
|
17
|
+
# This is rack middleware that adds 'REMOTE_USER' and 'REMOTE_PASSWORD'
|
18
|
+
# keys with their associated basic auth values to request.env (if present in the HTTP header).
|
19
|
+
# The "Authorization: Basic ..."" header must be present. Looks like this:
|
20
|
+
# Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
|
21
|
+
# If this header is missing or incorrect. Error codes are returned.
|
22
|
+
# See basic_credentials.rb for details.
|
23
|
+
use JunglePath::Rack::BasicCredentials::Basic, "Basic Authentication Required.", issue_challenge
|
24
|
+
end
|
25
|
+
|
26
|
+
def set_default_error_handling
|
27
|
+
# These must be disabled to allow error methods to fire.
|
28
|
+
disable :raise_errors
|
29
|
+
disable :show_exceptions
|
30
|
+
|
31
|
+
error JunglePath::Exceptions::MissingRequiredFields do
|
32
|
+
e = env['sinatra.error']
|
33
|
+
trace = e.backtrace.join("\n")
|
34
|
+
logger.error "#{e.message}\n#{trace}."
|
35
|
+
halt 400, e.message
|
36
|
+
end
|
37
|
+
|
38
|
+
error JunglePath::Exceptions::NotFoundException do
|
39
|
+
e = env['sinatra.error']
|
40
|
+
trace = e.backtrace.join("\n")
|
41
|
+
logger.error "#{e.message}\n#{trace}."
|
42
|
+
halt 404, e.message
|
43
|
+
end
|
44
|
+
|
45
|
+
error do
|
46
|
+
e = env['sinatra.error']
|
47
|
+
trace = e.backtrace.join("\n")
|
48
|
+
logger.error "#{e.message}\n#{trace}."
|
49
|
+
halt 500, e.message
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def set_default_helpers
|
54
|
+
# standard helpers:
|
55
|
+
helpers JunglePath::API::Helpers::DataCache
|
56
|
+
helpers JunglePath::API::Helpers::Logging
|
57
|
+
helpers JunglePath::API::Helpers::Result
|
58
|
+
helpers JunglePath::API::Helpers::Auth
|
59
|
+
helpers JunglePath::API::Helpers::QueryFilters
|
60
|
+
end
|
61
|
+
|
62
|
+
def set_default_authentication_check
|
63
|
+
before do
|
64
|
+
puts ""
|
65
|
+
puts "::::[request_start: #{Time.now.utc}]::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"
|
66
|
+
puts "::::[#{request.env['REMOTE_USER']}: #{request.request_method} #{request.url}"
|
67
|
+
puts "(api_helpsers - before do - default authentication check)"
|
68
|
+
puts "[params] #{params.to_h}" if configatron.debug.show_params
|
69
|
+
puts "authenticate..."
|
70
|
+
# These three request paths return user info, so force no_cache = true so that stale user data is not returned!
|
71
|
+
no_cache = request.path_info == '/authenticate' or request.path_info == '/current/user' or request.path_info == '/current/user/auth'
|
72
|
+
puts "authenticate no_cache: #{no_cache}."
|
73
|
+
authenticate no_cache
|
74
|
+
end
|
75
|
+
|
76
|
+
after do
|
77
|
+
puts "[request end]"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'zlib'
|
2
|
+
require 'logger'
|
3
|
+
|
4
|
+
module JunglePath
|
5
|
+
module API
|
6
|
+
module Helpers
|
7
|
+
module Logging
|
8
|
+
def log(info)
|
9
|
+
logger.info info
|
10
|
+
puts info
|
11
|
+
end
|
12
|
+
|
13
|
+
def logger
|
14
|
+
#API.logger
|
15
|
+
Logger.new(configatron.application.logger)
|
16
|
+
end
|
17
|
+
|
18
|
+
def get_log_file_list
|
19
|
+
Dir.entries('logs').map {|n| "#{n.split('.')[0]}.log.gz"}
|
20
|
+
end
|
21
|
+
|
22
|
+
def gzip_log_file filename
|
23
|
+
filename = "#{filename.split('.')[0]}.log"
|
24
|
+
Dir.mkdir('temp') unless File.exists? 'temp'
|
25
|
+
data = File.read(File.join('logs', filename))
|
26
|
+
zfilename = File.join('temp', "#{filename}.gz")
|
27
|
+
Zlib::GzipWriter.open(zfilename) do |gzip|
|
28
|
+
gzip << data
|
29
|
+
gzip.close
|
30
|
+
end
|
31
|
+
zfilename
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module JunglePath
|
2
|
+
module API
|
3
|
+
module Helpers
|
4
|
+
module QueryFilters
|
5
|
+
def app_defined_query_filters
|
6
|
+
# override (redefine) or replace this as needed to define your own query filters.
|
7
|
+
# make sure your redefine happens after this code runs and before
|
8
|
+
# the authentication check (the call to "authenticate" (APIHelpers::Auth.authenticate)).
|
9
|
+
#puts "default query_filters..."
|
10
|
+
[]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rack'
|
2
|
+
|
3
|
+
module JunglePath
|
4
|
+
module API
|
5
|
+
module Helpers
|
6
|
+
module Rescues
|
7
|
+
def self.handle_not_found(e)
|
8
|
+
response = {error_code: 404, error_message: e.message}.to_json
|
9
|
+
log response
|
10
|
+
Rack::Response.new(response, 404).finish
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'jungle_path/json'
|
2
|
+
|
3
|
+
module JunglePath
|
4
|
+
module API
|
5
|
+
module Helpers
|
6
|
+
module Result
|
7
|
+
def handle_result(item)
|
8
|
+
# handles the api result (converts to json).
|
9
|
+
halt 404 unless item
|
10
|
+
return JunglePath::Json.dump item if item.class == Array or item.class == Hash
|
11
|
+
return item.to_json if item
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,280 @@
|
|
1
|
+
module JunglePath
|
2
|
+
module API
|
3
|
+
module Helpers
|
4
|
+
module StandardAPIs
|
5
|
+
def set_standard_api_default_html_page
|
6
|
+
get '/' do
|
7
|
+
send_file './public/index.html'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def set_standard_api_query
|
12
|
+
post '/query' do
|
13
|
+
result = {}
|
14
|
+
|
15
|
+
query = params[:query]
|
16
|
+
|
17
|
+
#default to true:
|
18
|
+
temp = params[:apply_limit_offset_to_sql]
|
19
|
+
if temp == nil
|
20
|
+
apply_limit_offset_to_sql = true
|
21
|
+
else
|
22
|
+
apply_limit_offset_to_sql = JunglePath::DBModel::Params.to_bool(temp)
|
23
|
+
end
|
24
|
+
|
25
|
+
##default to false: (will be false if nil).
|
26
|
+
#apply_limit_offset_to_sql = DBModel::Params.to_bool(params[:apply_limit_offset_to_sql])
|
27
|
+
|
28
|
+
result[:query] = query
|
29
|
+
log "\napply_limit_offset_to_sql: #{apply_limit_offset_to_sql}."
|
30
|
+
log "\nuql query:\n\n#{query}."
|
31
|
+
log ""
|
32
|
+
# todo: pass generated node tree instead of models:
|
33
|
+
#engine = Query::Engine.new(Schema::Base.models, current_user, apply_limit_offset_to_sql)
|
34
|
+
node_tree = current_auth.schema_node_tree
|
35
|
+
puts "node_tree: #{node_tree.to_str}."
|
36
|
+
engine = Query::Engine.new(node_tree, current_user, apply_limit_offset_to_sql)
|
37
|
+
puts "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
|
38
|
+
q = engine.get_query_from_string(query)
|
39
|
+
result[:sql] = q.sql
|
40
|
+
result[:sql_parameter_values] = q.values
|
41
|
+
|
42
|
+
log "sql query:\n\n#{q.sql}."
|
43
|
+
log ""
|
44
|
+
#log "sql parameter values: #{q.values}."
|
45
|
+
#log ""
|
46
|
+
|
47
|
+
data = Query::Engine.run(q, db.base)
|
48
|
+
result[:data] = data
|
49
|
+
#puts data
|
50
|
+
|
51
|
+
result[:count] = data.length
|
52
|
+
#log "results count: #{data.length}."
|
53
|
+
#log ""
|
54
|
+
|
55
|
+
handle_result(result)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def set_standard_forward_api
|
60
|
+
get '/forward/:target/*' do
|
61
|
+
#log "forward get"
|
62
|
+
target = params[:target]
|
63
|
+
splat = params[:splat].join('/')
|
64
|
+
host = nil
|
65
|
+
#log "forward target: #{target}."
|
66
|
+
#log "forward splat: #{splat}."
|
67
|
+
user_name = request.env['REMOTE_USER']
|
68
|
+
#log "forward user_name: #{user_name}"
|
69
|
+
host, port, user_name, password = APIHelpers::StandardAPIs.get_forward_host target
|
70
|
+
if host
|
71
|
+
response = Remote.get user_name, password, host, port, "/#{splat}"
|
72
|
+
#log "response: #{response}."
|
73
|
+
if response.code == 200
|
74
|
+
response = JsonWrap.load(response.to_s)
|
75
|
+
else
|
76
|
+
halt response.to_a
|
77
|
+
end
|
78
|
+
else
|
79
|
+
raise ArgumentError.new("Invalid forward GET target: \"#{target}\".")
|
80
|
+
end
|
81
|
+
handle_result(response)
|
82
|
+
end
|
83
|
+
|
84
|
+
post '/forward/:target/*' do
|
85
|
+
#log "forward post"
|
86
|
+
target = params[:target]
|
87
|
+
splat = params[:splat].join('/')
|
88
|
+
host = nil
|
89
|
+
#log "forward target: #{target}."
|
90
|
+
#log "forward splat: #{splat}."
|
91
|
+
user_name = request.env['REMOTE_USER']
|
92
|
+
#log "forward user_name: #{user_name}"
|
93
|
+
host, port, user_name, password = APIHelpers::StandardAPIs.get_forward_host target
|
94
|
+
if host
|
95
|
+
response = Remote.post user_name, password, host, port, "/#{splat}", params
|
96
|
+
#log "response: #{response}."
|
97
|
+
if response.code == 200
|
98
|
+
response = JsonWrap.load(response.to_s)
|
99
|
+
else
|
100
|
+
halt response.to_a
|
101
|
+
end
|
102
|
+
else
|
103
|
+
raise ArgumentError.new("Invalid forward POST target: \"#{target}\".")
|
104
|
+
end
|
105
|
+
handle_result(response)
|
106
|
+
end
|
107
|
+
|
108
|
+
put '/forward/:target/*' do
|
109
|
+
#log "forward post"
|
110
|
+
target = params[:target]
|
111
|
+
splat = params[:splat].join('/')
|
112
|
+
host = nil
|
113
|
+
#log "forward target: #{target}."
|
114
|
+
#log "forward splat: #{splat}."
|
115
|
+
user_name = request.env['REMOTE_USER']
|
116
|
+
#log "forward user_name: #{user_name}"
|
117
|
+
host, port, user_name, password = APIHelpers::StandardAPIs.get_forward_host target
|
118
|
+
if host
|
119
|
+
response = Remote.put user_name, password, host, port, "/#{splat}", params
|
120
|
+
#log "response: #{response}."
|
121
|
+
response = JsonWrap.load(response.to_s) if response.code == 200
|
122
|
+
else
|
123
|
+
raise ArgumentError.new("Invalid forward POST target: \"#{target}\".")
|
124
|
+
end
|
125
|
+
handle_result(response)
|
126
|
+
end
|
127
|
+
|
128
|
+
delete '/forward/:target/*' do
|
129
|
+
#log "forward post"
|
130
|
+
target = params[:target]
|
131
|
+
splat = params[:splat].join('/')
|
132
|
+
host = nil
|
133
|
+
#log "forward target: #{target}."
|
134
|
+
#log "forward splat: #{splat}."
|
135
|
+
user_name = request.env['REMOTE_USER']
|
136
|
+
#log "forward user_name: #{user_name}"
|
137
|
+
host, port, user_name, password = APIHelpers::StandardAPIs.get_forward_host target
|
138
|
+
if host
|
139
|
+
response = Remote.delete user_name, password, host, port, "/#{splat}"
|
140
|
+
#log "response: #{response}."
|
141
|
+
response = JsonWrap.load(response.to_s) if response.code == 200
|
142
|
+
else
|
143
|
+
raise ArgumentError.new("Invalid forward POST target: \"#{target}\".")
|
144
|
+
end
|
145
|
+
handle_result(response)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
def self.get_forward_host target
|
150
|
+
if target == 'gbwho'
|
151
|
+
host = configatron.who.host
|
152
|
+
port = configatron.who.port
|
153
|
+
user_name = configatron.who.user_name
|
154
|
+
password = configatron.who.password
|
155
|
+
elsif target == 'gbwhat'
|
156
|
+
host = configatron.what.host
|
157
|
+
port = configatron.what.port
|
158
|
+
user_name = request.env['REMOTE_USER']
|
159
|
+
password = request.env['REMOTE_PASSWORD']
|
160
|
+
end
|
161
|
+
return host, port, user_name, password
|
162
|
+
end
|
163
|
+
|
164
|
+
def set_standard_api_sample_queries_list
|
165
|
+
get '/v1/sample/queries' do
|
166
|
+
files = Dir["./public/query/queries/*.uql"]
|
167
|
+
files = files.map {|n| n.split('/').pop().split('.')[0] }
|
168
|
+
files.sort!
|
169
|
+
handle_result(files)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
def set_standard_api_get_log_file_list
|
174
|
+
get '/logs' do
|
175
|
+
handle_result get_log_file_list
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
def set_standard_api_get_log_file
|
180
|
+
get '/logs/:filename' do
|
181
|
+
send_file(gzip_log_file(params[:filename]))
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
def set_standard_users_api
|
186
|
+
get '/users/:user_name/:password' do
|
187
|
+
#puts "get '/users/:user_name/:password'"
|
188
|
+
user = get_any_user(params[:user_name], params[:password])
|
189
|
+
puts "user.is_valid: #{(user and user.is_valid)}"
|
190
|
+
return handle_result(user) if (user and user.is_valid)
|
191
|
+
handle_result(nil)
|
192
|
+
end
|
193
|
+
|
194
|
+
delete '/users/:id' do
|
195
|
+
user_id = params[:id].to_i
|
196
|
+
puts "delete: /users/#{user_id}"
|
197
|
+
if user_id == current_user.id
|
198
|
+
halt 403, "Self deletion not allowed."
|
199
|
+
end
|
200
|
+
handle_result(Controller::User.new(current_user, current_key, params, db).delete)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
def set_standard_keys_api
|
205
|
+
get '/test' do
|
206
|
+
puts "valid_user: #{valid_user}"
|
207
|
+
end
|
208
|
+
|
209
|
+
delete '/keys/:id' do
|
210
|
+
key_id = params[:id]
|
211
|
+
#puts "delete /keys/#{key_id}"
|
212
|
+
if key_id == current_key.id
|
213
|
+
halt 403, "Self deletion of key not allowed."
|
214
|
+
end
|
215
|
+
handle_result(Controller::Key.new(current_user, current_key, params, db).delete)
|
216
|
+
end
|
217
|
+
|
218
|
+
begin # authorization
|
219
|
+
get '/current/user' do
|
220
|
+
result = {user: current_user, key: current_key, roles: current_roles}
|
221
|
+
puts "current/user: #{result}."
|
222
|
+
puts "current_key.key: #{current_key.key}."
|
223
|
+
handle_result(result)
|
224
|
+
end
|
225
|
+
|
226
|
+
get '/current/user/auth' do
|
227
|
+
message = {
|
228
|
+
user_id: current_user.id,
|
229
|
+
user_name: current_user.user_name,
|
230
|
+
key_id: current_key.id,
|
231
|
+
key_name: current_key.name,
|
232
|
+
key_value: current_key.key,
|
233
|
+
roles: current_auth.roles,
|
234
|
+
permissions: current_auth.permissions,
|
235
|
+
restrictions: current_auth.restrictions,
|
236
|
+
query_filters: current_user.query_filters
|
237
|
+
}
|
238
|
+
handle_result message
|
239
|
+
end
|
240
|
+
|
241
|
+
get '/roles/key/:id' do
|
242
|
+
key = Schema::Key.new(params, false, true)
|
243
|
+
roles = SQL::Role.by_key(db, key)
|
244
|
+
handle_result(roles)
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
begin # api_keys gets
|
249
|
+
get '/keys/:key' do
|
250
|
+
pass if params['key'].match(/^\d+$/) # if key is an integer goto route /api_keys/:id
|
251
|
+
handle_result(Controller::Key.new(current_user, current_key, params, db).select_by_key)
|
252
|
+
end
|
253
|
+
|
254
|
+
get '/keys/user/:user_id' do
|
255
|
+
# get the api keys for this user.
|
256
|
+
handle_result(Controller::Key.new(current_user, current_key, params, db).select_by_user)
|
257
|
+
end
|
258
|
+
|
259
|
+
get '/keys/user/:user_id/default' do
|
260
|
+
# get the default api keys for this user.
|
261
|
+
handle_result(Controller::Key.new(current_user, current_key, params, db).select_default_by_user)
|
262
|
+
end
|
263
|
+
|
264
|
+
get '/keys/user/:user_id/application/:application_id' do
|
265
|
+
# get the api keys for this user for this application.
|
266
|
+
handle_result(Controller::Key.new(current_user, current_key, params, db).select_by_user)
|
267
|
+
end
|
268
|
+
|
269
|
+
get '/keys/user/:user_id/application/:application_id/default' do
|
270
|
+
# get the default api keys for this user for this application.
|
271
|
+
result = Controller::Key.new(current_user, current_key, params, db).select_default_by_user
|
272
|
+
#puts "result: #{result}."
|
273
|
+
handle_result(result)
|
274
|
+
end
|
275
|
+
end
|
276
|
+
end
|
277
|
+
end
|
278
|
+
end
|
279
|
+
end
|
280
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module JunglePath
|
2
|
+
module API
|
3
|
+
module Helpers
|
4
|
+
require 'jungle_path/api/helpers/logging'
|
5
|
+
require 'jungle_path/api/helpers/rescues'
|
6
|
+
require 'jungle_path/api/helpers/auth'
|
7
|
+
require 'jungle_path/api/helpers/auth_local_user'
|
8
|
+
require 'jungle_path/api/helpers/auth_old'
|
9
|
+
require 'jungle_path/api/helpers/result'
|
10
|
+
require 'jungle_path/api/helpers/query_filters'
|
11
|
+
require 'jungle_path/api/helpers/standard_apis'
|
12
|
+
require 'jungle_path/api/helpers/defaults'
|
13
|
+
require 'jungle_path/api/helpers/data_cache'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
<% # api_template.erb %>
|
2
|
+
<%= "#This file was generated using the ../lib/api_template.erb. Do not modify directly." %>
|
3
|
+
require 'sinatra/base'
|
4
|
+
require_relative '../db/db'
|
5
|
+
require_relative '../schemas/schema'
|
6
|
+
require_relative 'server_base'
|
7
|
+
require_relative '../controllers/controller'
|
8
|
+
|
9
|
+
module <%= name_space %>
|
10
|
+
class API < <%= name_space %>::BaseAPI
|
11
|
+
<% for table in tables %>
|
12
|
+
begin # <%= table.plural_table_name %>
|
13
|
+
get '/<%= table.plural_table_name %>' do
|
14
|
+
handle_result(<%= controller_name_space %>::<%= table.name.split('::').last %>.new(current_user, current_key, params, db).select)
|
15
|
+
end
|
16
|
+
|
17
|
+
get '/<%= table.plural_table_name %><%= table.template_pk_url %>' do
|
18
|
+
handle_result(<%= controller_name_space %>::<%= table.name.split('::').last %>.new(current_user, current_key, params, db).select)
|
19
|
+
end
|
20
|
+
|
21
|
+
post '/<%= table.plural_table_name %>' do
|
22
|
+
handle_result(<%= controller_name_space %>::<%= table.name.split('::').last %>.new(current_user, current_key, params, db).insert)
|
23
|
+
end
|
24
|
+
|
25
|
+
put '/<%= table.plural_table_name %><%= table.template_pk_url %>' do
|
26
|
+
handle_result(<%= controller_name_space %>::<%= table.name.split('::').last %>.new(current_user, current_key, params, db).update)
|
27
|
+
end
|
28
|
+
|
29
|
+
delete '/<%= table.plural_table_name %><%= table.template_pk_url %>' do
|
30
|
+
handle_result(<%= controller_name_space %>::<%= table.name.split('::').last %>.new(current_user, current_key, params, db).delete)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
<% end %>
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
logs/
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'rack'
|
2
|
+
require 'sinatra/base'
|
3
|
+
require 'jungle_path/json'
|
4
|
+
require 'jungle_path/api/helpers'
|
5
|
+
require 'jungle_path/authentication/auth_provider'
|
6
|
+
require 'jungle_path/authentication/data_provider'
|
7
|
+
require 'jungle_path/query/filter'
|
8
|
+
|
9
|
+
|
10
|
+
#require_relative '../config/base_config'
|
11
|
+
#require_relative '../../lib/basic_credentials'
|
12
|
+
#require_relative '../../lib/sql'
|
13
|
+
#require_relative '../../lib/api_helpers'
|
14
|
+
#require_relative '../../lib/authorization'
|
15
|
+
#require_relative '../db/db'
|
16
|
+
#require_relative '../schemas/schema'
|
17
|
+
#require_relative '../auth/authorization'
|
18
|
+
|
19
|
+
module Server
|
20
|
+
#class BaseAPI < Sinatra::Application
|
21
|
+
class BaseAPI < Sinatra::Base
|
22
|
+
configure do
|
23
|
+
set :public_folder, configatron.application.public_dir
|
24
|
+
set :root, configatron.application.root_dir
|
25
|
+
set :dump_errors, true
|
26
|
+
set :sessions, false
|
27
|
+
set :logging, true
|
28
|
+
mime_type :uql, 'text' #for sample queries.
|
29
|
+
set :static, true
|
30
|
+
puts "settings.root: #{settings.root}"
|
31
|
+
puts "settings.public_folder: #{settings.public_folder}"
|
32
|
+
end
|
33
|
+
|
34
|
+
# Turns off "X-Frame-Options: SAMEORIGIN" http response header:
|
35
|
+
#set :protection, :except => :frame_options
|
36
|
+
|
37
|
+
extend JunglePath::API::Helpers::Defaults
|
38
|
+
extend JunglePath::API::Helpers::StandardAPIs
|
39
|
+
|
40
|
+
# Your own:
|
41
|
+
#extend ServerAPI::Authorization
|
42
|
+
|
43
|
+
def self.inherited(subclass)
|
44
|
+
super
|
45
|
+
subclass.instance_eval do
|
46
|
+
set_default_rack_middleware false
|
47
|
+
set_default_error_handling
|
48
|
+
|
49
|
+
helpers JunglePath::API::Helpers::DataCache
|
50
|
+
helpers JunglePath::API::Helpers::Logging
|
51
|
+
helpers JunglePath::API::Helpers::Result
|
52
|
+
helpers JunglePath::API::Helpers::Auth
|
53
|
+
helpers Server::Base::Database
|
54
|
+
|
55
|
+
# Your own:
|
56
|
+
#set_authorization
|
57
|
+
|
58
|
+
set_standard_api_get_log_file_list
|
59
|
+
set_standard_api_get_log_file
|
60
|
+
set_standard_api_query
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
after do
|
65
|
+
GC.start
|
66
|
+
end
|
67
|
+
|
68
|
+
before do
|
69
|
+
puts ""
|
70
|
+
puts "[request_start: #{Time.now.utc}]::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"
|
71
|
+
puts "[#{request.env['REMOTE_USER']}: #{request.request_method} #{request.url}"
|
72
|
+
puts "(base_api - before do - authentication check)"
|
73
|
+
puts "[params] #{params.to_h}" if configatron.debug.show_params
|
74
|
+
if ServerAPI::Authorization.is_open_path?(request, configatron)
|
75
|
+
puts "is open path: #{request.path_info}"
|
76
|
+
else
|
77
|
+
puts "is secured path: #{request.path_info}"
|
78
|
+
# These three request paths return user info, so force no_cache = true so that stale user data is not returned!
|
79
|
+
no_cache = (request.path_info == '/authenticate' or request.path_info == '/current/user' or request.path_info == '/current/user/auth')
|
80
|
+
puts "authenticate no_cache: #{no_cache}."
|
81
|
+
auth_provider = JunglePath::Authentication::AuthProvider::Default.new
|
82
|
+
data_provider = JunglePath::Authentication::DataProvider::Default.new
|
83
|
+
JunglePath::API::Helpers::Auth.authenticate auth_provider, data_provider, no_cache
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
module Database
|
88
|
+
def db
|
89
|
+
ServerAPI::DB.instance
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|