j1_template 2019.4.10 → 2019.4.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/lib/j1/version.rb +1 -1
  3. data/lib/starter_web/Gemfile +1 -1
  4. data/lib/starter_web/_cc_test_data/_config.yml +1 -1
  5. data/lib/starter_web/_config.yml +1 -1
  6. data/lib/starter_web/assets/themes/j1/core/js/j1.js +9 -1
  7. data/lib/starter_web/package.json +1 -1
  8. data/lib/starter_web/pages/public/start/downloads/quickstarter/100_linux_starter.adoc +1 -1
  9. data/lib/starter_web/utilsrv/_defaults/package.json +1 -1
  10. data/lib/starter_web/utilsrv/package.json +1 -1
  11. metadata +15 -50
  12. data/lib/j1_app/j1_auth_manager/_unused/auth_manager.before_merge_added.rb +0 -1267
  13. data/lib/j1_app/j1_auth_manager/_unused/auth_manager.new.rb +0 -1121
  14. data/lib/j1_app/j1_auth_manager/_unused/auth_manager.no_cookie.mgmt.rb +0 -1130
  15. data/lib/j1_app/j1_auth_manager/_unused/auth_manager.ok.rb +0 -1130
  16. data/lib/j1_app/j1_auth_manager/_unused/auth_manager.org.2.rb +0 -1086
  17. data/lib/j1_app/j1_auth_manager/_unused/auth_manager.org.rb +0 -1548
  18. data/lib/j1_app/j1_auth_manager/_unused/auth_manager.update.web_cookie.rb +0 -1333
  19. data/lib/j1_app/j1_auth_manager/_unused/auth_manager_ui.2.erb +0 -198
  20. data/lib/j1_app/j1_auth_manager/_unused/auth_manager_ui.additional_inits.erb +0 -254
  21. data/lib/j1_app/j1_auth_manager/_unused/auth_manager_ui.manage.cookies.erb +0 -255
  22. data/lib/starter_web/assets/_data/_temp/_banner.html +0 -421
  23. data/lib/starter_web/assets/_data/_temp/_panel.html +0 -585
  24. data/lib/starter_web/assets/_data/_temp/banner.case.snippet.html.asciidoc +0 -91
  25. data/lib/starter_web/assets/_data/_temp/banner.new.html.asciidoc +0 -559
  26. data/lib/starter_web/assets/_data/_temp/banner.with.defaults.html.asciidoc +0 -547
  27. data/lib/starter_web/assets/_data/_temp/footer.html.new +0 -228
  28. data/lib/starter_web/assets/_data/_temp/footer.html.org +0 -234
  29. data/lib/starter_web/assets/_data/_temp/footer.old.html +0 -231
  30. data/lib/starter_web/assets/_data/_temp/footer_new.html +0 -226
  31. data/lib/starter_web/assets/_data/_temp/footer_old.html +0 -222
  32. data/lib/starter_web/assets/_data/_temp/menu.old.html +0 -462
  33. data/lib/starter_web/assets/_data/_temp/panel.new.html +0 -560
  34. data/lib/starter_web/assets/_data/_temp/panel.old.html +0 -597
  35. data/lib/starter_web/assets/_data/_temp/panel.org.html +0 -597
  36. data/lib/starter_web/assets/_data/animate.json +0 -120
  37. data/lib/starter_web/assets/_data/ascii_code_table.json +0 -1794
  38. data/lib/starter_web/assets/_data/authclient.html +0 -365
  39. data/lib/starter_web/assets/_data/banner.html +0 -431
  40. data/lib/starter_web/assets/_data/carousel.json +0 -110
  41. data/lib/starter_web/assets/_data/colors.json +0 -43
  42. data/lib/starter_web/assets/_data/cookie_consent.html +0 -225
  43. data/lib/starter_web/assets/_data/countries.json +0 -974
  44. data/lib/starter_web/assets/_data/font_sizes.json +0 -43
  45. data/lib/starter_web/assets/_data/fontawesome_icons.json +0 -5525
  46. data/lib/starter_web/assets/_data/footer.html +0 -239
  47. data/lib/starter_web/assets/_data/galleries.json +0 -160
  48. data/lib/starter_web/assets/_data/mdi_icons.json +0 -14641
  49. data/lib/starter_web/assets/_data/menu.html +0 -469
  50. data/lib/starter_web/assets/_data/messages.yml +0 -130
  51. data/lib/starter_web/assets/_data/objects.json +0 -458
  52. data/lib/starter_web/assets/_data/panel.html +0 -501
  53. data/lib/starter_web/assets/_data/panel.new.html +0 -498
  54. data/lib/starter_web/assets/_data/quicklinks.html +0 -178
  55. data/lib/starter_web/assets/_data/search.yml +0 -131
  56. data/lib/starter_web/assets/_data/sidebar.html +0 -261
  57. data/lib/starter_web/assets/_data/themes.json +0 -59
  58. data/lib/starter_web/assets/_data/twa_v1.json +0 -6978
  59. data/lib/starter_web/assets/_data/twa_v2.json +0 -12812
  60. data/lib/starter_web/assets/_data/webhook.html +0 -190
@@ -1,1130 +0,0 @@
1
- # RuboCops - Documentation
2
- # ------------------------------------------------------------------------------
3
- # See: https://rubocop.readthedocs.io/en/latest/
4
-
5
- # RuboCops - Disabled Cops
6
- # ------------------------------------------------------------------------------
7
- # rubocop:disable Metrics/BlockLength
8
- # rubocop:disable Metrics/ClassLength
9
- # rubocop:disable Metrics/LineLength
10
- # rubocop:disable Style/StringLiterals
11
- # rubocop:disable Style/Documentation
12
- # rubocop:disable Metrics/BlockNesting
13
- # rubocop:disable Layout/ClosingParenthesisIndentation
14
- # rubocop:disable Layout/LeadingCommentSpace
15
- # rubocop:disable Layout/EmptyLines
16
- # rubocop:disable Layout/EmptyLinesAroundBlockBody
17
- # rubocop:disable Layout/FirstParameterIndentation
18
- # rubocop:disable Layout/CommentIndentation
19
- # rubocop:disable Layout/AlignParameters
20
- # rubocop:disable Layout/AlignHash
21
- # rubocop:disable Layout/TrailingWhitespace
22
- # rubocop:disable Layout/IndentHash
23
- # rubocop:disable Layout/SpaceAroundOperators
24
- # rubocop:disable Layout/ExtraSpacing
25
- # rubocop:disable Style/UnlessElse
26
- # rubocop:disable Style/HashSyntax
27
-
28
-
29
- # ------------------------------------------------------------------------------
30
- # ~/lib/j1_auth_manager/auth_manager/.rb
31
- #
32
- # Provides authentication services based on Warden|OmniAuth
33
- #
34
- # Product/Info:
35
- # https://jekyll.one
36
- #
37
- # Copyright (C) 2019 Juergen Adams
38
- #
39
- # J1 Template is licensed under the MIT License.
40
- # See: https://github.com/jekyll-one-org/j1_template/blob/master/LICENSE
41
- #
42
- # ------------------------------------------------------------------------------
43
- # NOTES
44
- #
45
- # ------------------------------------------------------------------------------
46
- # frozen_string_literal: true
47
-
48
- module J1App
49
- class AuthManager < Sinatra::Base
50
-
51
- include J1App::Helpers
52
- include J1App::GithubHelpers
53
-
54
- # ==========================================================================
55
- # Sinatra Framework settings
56
- # ==========================================================================
57
-
58
- # NOTE: https://stackoverflow.com/questions/7847536/sinatra-in-facebook-iframe
59
- #
60
- #set :protection, :except => :frame_options
61
-
62
- # Check: http://sinatrarb.com/intro.html
63
- #
64
- #set :static_cache_control, [:public, :max_age => 10]
65
-
66
-
67
- # ==========================================================================
68
- # Base App and Warden Framework settings
69
- # ==========================================================================
70
-
71
- provider_site_url_default = J1App.user_settings['provider_site_url']
72
- provider_home_url_default = J1App.user_settings['provider_home_url']
73
- provider_blog_url_default = J1App.user_settings['provider_blog_url']
74
- provider_member_url_default = J1App.user_settings['provider_member_url']
75
- provider_privacy_url_default = J1App.user_settings['provider_privacy_url']
76
-
77
- session_data = {}
78
-
79
- # user_state_data = {
80
- # :authenticated => 'false',
81
- # :requested_page => '/',
82
- # :user_name => 'unknown',
83
- # :users_allowed => 'unknown',
84
- # :user_id => 'unknown',
85
- # :provider => 'unknown',
86
- # :provider_url => '/',
87
- # :payment_info => 'unknown',
88
- # :permissions => 'unknown',
89
- # :writer => 'middleware'
90
- # }
91
-
92
- user_state_data = {
93
- :authenticated => 'false',
94
- :requested_page => '/',
95
- :user_name => 'visitor',
96
- :users_allowed => 'all',
97
- :user_id => 'unknown',
98
- :provider => 'j1',
99
- :provider_membership => 'guest',
100
- :provider_site_url => "#{provider_site_url_default}",
101
- :provider_home_url => "#{provider_home_url_default}",
102
- :provider_blog_url => "#{provider_blog_url_default}",
103
- :provider_member_url => "#{provider_member_url_default}",
104
- :provider_privacy_url => "#{provider_privacy_url_default}",
105
- :payment_info => 'unknown',
106
- :provider_permissions => ['public'],
107
- :creator => 'middleware',
108
- :writer => 'middleware',
109
- :mode => 'app'
110
- }
111
-
112
- # Enable SSL for the rack session if configured
113
- # --------------------------------------------------------------------------
114
- require 'rack-ssl-enforcer' if J1App.ssl?
115
- use Rack::SslEnforcer if J1App.ssl?
116
-
117
- # Set the session cookie used by Rack to track all relevant data
118
- # for the authentication service
119
- # --------------------------------------------------------------------------
120
- use Rack::Session::Cookie,
121
- http_only: true, # if set to 'true', make session cookie visible to the browser (document) for HTTP
122
- key: 'j1.app.session',
123
- secret: ENV['J1_SESSION_SECRET'] || SecureRandom.hex
124
-
125
- # use Rack::Cache do |config|
126
- # #
127
- # # ------------------------------------------------------------------------
128
- # config.middleware.delete(Rack::Cache)
129
- # end
130
-
131
- # ==========================================================================
132
- # Warden Framework initialisation
133
- # ==========================================================================
134
-
135
- # Define what (user) data should be put (serialized) into the session
136
- # on requests and responses from Rack environment into the warden
137
- # environment (env['warden']).
138
- # --------------------------------------------------------------------------
139
- Warden::Manager.serialize_into_session do |user|
140
- user
141
- end
142
- Warden::Manager.serialize_from_session do |user|
143
- user
144
- end
145
-
146
- # ==========================================================================
147
- # OmniAuth|Warden Framework initialisation
148
- # ==========================================================================
149
-
150
- # Set the 'default' authentication strategy and exception handler
151
- # (for warden) if the user was not explicitly signed in (signin dialog).
152
- # If 'signin' fails, the default exception 'signin_failure' is thrown
153
- # (used for all OmniAuth strategies registered).
154
- # --------------------------------------------------------------------------
155
- signin_failure = ->(_e) { Rack::Response.new("Can't login", 401).finish }
156
- use Warden::Manager do |config|
157
- # OmniAuth strategies are name-spaced by 'omni' (see: warden_omniauth.rb)
158
- # ------------------------------------------------------------------------
159
- config.default_strategies :"omni_#{J1App.default_provider}"
160
- config.failure_app = signin_failure
161
- end
162
-
163
- use OmniAuth::Builder do |config|
164
- # Rescue OmniAuth::Strategies::OAuth2::CallbackError
165
- # ------------------------------------------------------------------------
166
- config.on_failure do
167
- new_path = '/redirect_on_failure'
168
- Rack::Response.new(['302 Moved'], 302, 'Location' => new_path).finish
169
- end
170
-
171
- # Detect and set supported authentication strategies for OmniAuth
172
- # ------------------------------------------------------------------------
173
-
174
- # Additional (strategy) option skip_extra, default: true
175
- #
176
- # If true, skips the collection of raw data (extra) to NOT blow
177
- # up the session cookie (as it is limited to 4K)
178
- skip_extra = true
179
-
180
- if J1App.active_providers.include? 'patreon'
181
- scope = J1App.auth_config['providers']['patreon']['scope'].join(',')
182
- data_collection = J1App.auth_config['providers']['patreon']['data_fields'].join(',')
183
- skip_extra = false if data_collection =~ /raw/i
184
- provider :patreon,
185
- ENV['PATREON_CLIENT_ID'],
186
- ENV['PATREON_CLIENT_SECRET'],
187
- scope: "#{scope}",
188
- skip_extra: skip_extra
189
- end
190
- if J1App.active_providers.include? 'disqus'
191
- scope = J1App.auth_config['providers']['disqus']['scope'].join(',')
192
- data_collection = J1App.auth_config['providers']['disqus']['data_fields'].join(',')
193
- skip_extra = false if data_collection =~ /raw/i
194
- provider :disqus,
195
- ENV['DISQUS_CLIENT_ID'],
196
- ENV['DISQUS_CLIENT_SECRET'],
197
- scope: "#{scope}",
198
- skip_extra: skip_extra
199
- end
200
- if J1App.active_providers.include? 'facebook'
201
- scope = J1App.auth_config['providers']['facebook']['scope'].join(',')
202
- data_collection = J1App.auth_config['providers']['facebook']['data_fields'].join(',')
203
- skip_extra = false if data_collection =~ /raw/i
204
- provider :facebook,
205
- ENV['FACEBOOK_CLIENT_ID'],
206
- ENV['FACEBOOK_CLIENT_SECRET'],
207
- scope: "#{scope}",
208
- skip_extra: skip_extra
209
- end
210
- if J1App.active_providers.include? 'github'
211
- scope = J1App.auth_config['providers']['github']['scope'].join(',')
212
- data_collection = J1App.auth_config['providers']['github']['data_fields'].join(',')
213
- skip_extra = false if data_collection =~ /raw/i
214
- provider :github,
215
- ENV['GITHUB_CLIENT_ID'],
216
- ENV['GITHUB_CLIENT_SECRET'],
217
- scope: "#{scope}",
218
- skip_extra: skip_extra
219
- end
220
- if J1App.active_providers.include? 'twitter'
221
- scope = J1App.auth_config['providers']['twitter']['scope'].join(',')
222
- data_collection = J1App.auth_config['providers']['twitter']['data_fields'].join(',')
223
- skip_extra = false if data_collection =~ /raw/i
224
- provider :twitter,
225
- ENV['TWITTER_CLIENT_ID'],
226
- ENV['TWITTER_CLIENT_SECRET'],
227
- scope: "#{scope}",
228
- skip_extra: skip_extra
229
- end
230
- end
231
-
232
- # Set the (internal) endpoint if a user is successfully authenticated
233
- # --------------------------------------------------------------------------
234
- use J1WardenOmniAuth do |config|
235
- config.redirect_after_callback = '/post_authentication'
236
- end
237
-
238
- # Add the internal logger from Rack to the middleware's of the stack
239
- # --------------------------------------------------------------------------
240
- use Rack::Logger
241
-
242
- # Load user profiles, permissions, conditions and strategies
243
- # --------------------------------------------------------------------------
244
- providers = J1App.auth_config['providers']
245
- permissions = J1App.permissions
246
-
247
-
248
-
249
- # ==========================================================================
250
- # Sinatra (before) FILTER to preprocess all page requests
251
- # ==========================================================================
252
-
253
- # Prepare root (index) page for app detection
254
- # --------------------------------------------------------------------------
255
- before '/' do
256
- log_info! "ROOT", "Prepare", 'Page access'
257
- # log_info! "ROOT", 'Config', 'Read current user config data', "#{provider_site_url}"
258
-
259
- # read existing/current cookie 'j1.user.state' to update all data
260
- # of user_state_data (hash) otherwise set initial data
261
- # ------------------------------------------------------------------------
262
- unless env['HTTP_COOKIE'] == nil
263
- log_info! "ROOT", 'Cookie', 'Read current user state data'
264
- user_state_data = readCookie('j1.user.state')
265
- else
266
- requested_page = env['REQUEST_URI']
267
- session_data['requested_page'] = "#{env['REQUEST_URI']}"
268
- end
269
-
270
- # Create|Initialize the J1 web session cookie
271
- # ------------------------------------------------------------------------
272
- if warden.authenticated?
273
- user = warden.user
274
-
275
- log_info! "ROOT", 'AuthCheck', 'User detected', "#{user[:provider]}"
276
- log_info! "ROOT", 'AuthCheck', 'User detected as signed in'
277
-
278
- session_data['authenticated'] = 'true'
279
- session_data['user_name'] = user[:info]['nickname']
280
- session_data['users_allowed'] = providers["#{user[:provider]}"]['users']
281
- session_data['user_id'] = user[:uid]
282
- session_data['provider'] = user[:provider]
283
- session_data['provider_membership'] = 'member'
284
- session_data['provider_site_url'] = providers["#{user[:provider]}"]['provider_url']
285
- session_data['provider_permissions'] = providers["#{user[:provider]}"]['permissions']
286
- session_data['payment_status'] = user[:info][:payment_status]
287
- else
288
- log_info! "ROOT", 'AuthCheck', 'User detected', 'signed out'
289
-
290
- session_data['authenticated'] = 'false'
291
- session_data['users_allowed'] = 'all'
292
- session_data['user_name'] = 'visitor'
293
- session_data['user_id'] = 'unknown'
294
- session_data['payment_status'] = 'unknown'
295
- session_data['provider'] = 'j1'
296
- session_data['provider_membership'] = 'guest'
297
- session_data['provider_site_url'] = "#{provider_site_url_default}"
298
- session_data['provider_permissions'] = ['public']
299
- end
300
- end
301
-
302
- user_state_cookie = 'j1.user.state'
303
-
304
-
305
- # General content (type) detection (auth pre-flight)
306
- # --------------------------------------------------------------------------
307
- before '/(apps|pages|posts)/*' do
308
-
309
- log_info! 'AuthManager', 'PreFlight', 'Initial checks initiated'
310
-
311
- if existsCookie? user_state_cookie
312
- user_state_data = readCookie(user_state_cookie)
313
- log_info! 'PreFlight', 'Cookie', 'Read user state session data' #, "#{session_decoded}"
314
- else
315
- requested_page = env['REQUEST_URI']
316
- session_data['requested_page'] = "#{requested_page}"
317
- end
318
-
319
- # Create|Initialize the J1 web session cookie
320
- # ------------------------------------------------------------------------
321
- log_info! 'PreFlight', 'AuthCheck', 'Check authentication state'
322
- if warden.authenticated?
323
- user = warden.user
324
- session_data['authenticated'] = 'true'
325
- session_data['user_name'] = user[:info]['nickname']
326
- session_data['user_id'] = user[:uid]
327
- session_data['provider'] = user[:provider]
328
- session_data['provider_site_url'] = providers["#{user[:provider]}"]['provider_url']
329
- session_data['users_allowed'] = providers["#{user[:provider]}"]['users']#
330
- session_data['provider_permissions'] = providers["#{user[:provider]}"]['permissions']
331
- session_data['provider_membership'] = 'member'
332
- session_data['payment_status'] = user[:info][:payment_status]
333
- session_data['writer'] = 'middleware'
334
-
335
- user_state_data = merge( user_state_data, session_data )
336
- log_info! 'PreFlight', 'AuthCheck', 'User authenticated', "#{user[:info]['nickname']}"
337
-
338
- end
339
-
340
- # User state|content detection for implicit authentication
341
- # ------------------------------------------------------------------------
342
- log_info! 'PreFlight', 'CheckConfig', 'Authentication check', 'disabled' if authentication_enabled? == false
343
- log_info! 'PreFlight', 'AuthCheck', 'Pass for all pages' if authentication_enabled? == false
344
- pass if authentication_enabled? == false
345
-
346
- log_info! 'PreFlight', 'CheckConfig', 'Authentication check', 'enabled'
347
- log_info! 'PreFlight', 'DetectContent', 'Public content detected' if public_content?
348
- log_info! 'PreFlight', 'DetectContent', 'Pass all public content' if public_content?
349
- pass if public_content?
350
-
351
- log_info! 'PreFlight', 'DetectCookieConsent', 'Cookie Consent', "#{user_state_data['cookies_accepted']}"
352
- log_info! 'PreFlight', 'DetectContent', 'Check content type'
353
-
354
- requested_page = env['REQUEST_URI']
355
- requested_page.scan(/(protected|private)/) do |match|
356
-
357
- category = match[0]
358
- log_info! 'PreFlight', 'DetectContent', 'Content type', "#{category}"
359
-
360
- log_info! 'PreFlight', 'AuthCheck', 'Check authorisation status'
361
- if warden.authenticated?
362
- user_name = user[:info]['nickname']
363
- log_info! 'PreFlight', 'AuthCheck', 'User detected', "#{user_name}"
364
-
365
- current_provider = warden.user[:provider]
366
- strategy = providers["#{current_provider}"]['strategy']
367
- provider_strategy = :"#{strategy}"
368
-
369
- user_state_data['user_name'] = user_name
370
- user_state_data['provider_url'] = providers["#{current_provider}"]['provider_url']
371
- user_state_data['users_allowed'] = providers["#{current_provider}"]['users']
372
- user_state_data['provider_permissions'] = providers["#{user[:provider]}"]['permissions']
373
- user_state_data['requested_page'] = requested_page
374
-
375
- log_info! 'PreFlight', 'ContentCheck', 'Check permissions'
376
- if permissions[:"#{category}"].include? current_provider
377
- log_info! 'PreFlight', 'ContentCheck', 'Provider detected', "#{current_provider}"
378
- log_info! 'PreFlight', 'ContentCheck', 'Category detected', "#{category}"
379
- log_info! 'PreFlight', 'ContentCheck', 'Category support', 'enabled'
380
-
381
- # Check permissions
382
- #
383
- #log_info! 'Authorisation', 'ConditionCheck', 'Check permissions for provider', "#{current_provider}"
384
- #conditions = J1App.conditions current_provider
385
- # if conditions["#{category}"]
386
- # log_info! 'Authorisation', 'ConditionCheck', 'Conditions detected', "#{category}"
387
- # conditions["#{category}"].each do |k, v|
388
- # case k
389
- # when 'enabled'
390
- # log_info! 'Authorisation', 'ConditionCheck', "#{k}", "#{v}"
391
- # when 'users'
392
- # log_info! 'Authorisation', 'ConditionCheck', 'users'
393
- # v.each do |k, v|
394
- # log_info! 'Authorisation', 'ConditionCheck', "users - #{k}", "#{v}"
395
- # end
396
- # when 'payment'
397
- # log_info! 'Authorisation', 'ConditionCheck', 'payment'
398
- # v.each do |k, v|
399
- # case k
400
- # when 'tiers'
401
- # log_info! 'Authorisation', 'ConditionCheck', "payment - #{k}", "#{v}"
402
- # when 'tier'
403
- # v.each do |k, v|
404
- # log_info! 'Authorisation', 'ConditionCheck', 'payment - tiers - tier : ' "#{k}", "#{v}"
405
- # end
406
- # end
407
- # end
408
- # end
409
- # end
410
- # end
411
- else
412
- provider = permissions[:"#{category}"][0]
413
- log_info! 'PreFlight', 'ContentCheck', 'Provider detected', "#{current_provider}"
414
- log_info! 'PreFlight', 'ContentCheck', 'Category detected', "#{category}"
415
- log_info! 'PreFlight', 'ContentCheck', 'Category supported', 'NO'
416
- log_info! 'PreFlight', 'AuthCheck', 'Authorisation failed for user', "#{user_name}"
417
-
418
- # log_info! 'PreFlight', 'SignOut', 'Sign out user', "#{user_name}"
419
- # warden.logout
420
- # session.clear
421
-
422
- session_json = user_state_data.to_json
423
- log_info! 'PreFlight', 'Cookie', 'Write web session data', "#{session_json}"
424
-
425
- # session_encoded = Base64.encode64(session_json)
426
- # response.set_cookie(
427
- # 'j1.user.state',
428
- # domain: false,
429
- # value: session_encoded.to_s,
430
- # path: '/'
431
- # )
432
-
433
- writeCookie(user_state_cookie, session_json)
434
-
435
- # log_info! 'PreFlight', 'Redirect', 'Call API request', 'PageValidate'
436
- # redirect "/page_validation?page=#{requested_page}"
437
-
438
- log_info! 'PreFlight', 'Redirect', 'Pass to error page (access_denied)'
439
- description_title = "Access Denied"
440
- redirect "/access_denied?provider=#{current_provider}&user=#{user_name}&category=#{category}&title=#{description_title}"
441
- end
442
-
443
- time = Time.now.ctime.to_s
444
- log_info! 'PreFlight', 'AuthCheck', 'Pass to requested page', "#{requested_page}"
445
- log_info! 'PreFlight', 'AuthCheck', 'Set X-Response-Headers'
446
-
447
- # See: https://stackoverflow.com/questions/10438276/how-to-disable-static-file-caching-in-rails-3-thin-on-windows
448
- # response.headers["Cache-Control"] = 'no-cache, no-store, max-age=0, must-revalidate'
449
- # response.headers["Pragma"] = 'no-cache'
450
- # response.headers["Expires"] = 'Fri, 01 Jan 1990 00:00:00 GMT'
451
- # response.headers['X-J1-AuthManager'] = "page-validated;category=#{category};called=" + time
452
- pass
453
- else
454
- log_info! 'PreFlight', 'AuthCheck', 'User detected', 'signed out'
455
- default_provider = permissions[:"#{category}"][0]
456
- log_info! 'PreFlight', 'AuthCheck', 'Set default provider', "#{default_provider}"
457
-
458
- strategy = providers["#{default_provider}"]['strategy']
459
- provider_strategy = :"#{strategy}"
460
-
461
- log_info! 'PreFlight', 'AuthCheck', 'Start processing provider', "#{default_provider}"
462
- log_info! 'PreFlight', 'AuthCheck', 'Authentication strategy', "#{provider_strategy}"
463
-
464
- case provider_strategy
465
-
466
- when :org
467
- warden.authenticate!
468
- github_organization_authenticate! ENV['GITHUB_ORG_NAME']
469
- logger.info "Hi There, #{user_state_data[:user_name]}! You have access to the #{params['id']} organization"
470
-
471
- when :team
472
- warden.authenticate!
473
- github_team_authenticate! ENV['GITHUB_TEAM_ID']
474
- logger.info "Hi There, #{user_state_data[:user_name]}! You have access to the #{params['id']} team"
475
-
476
- when :teams
477
- warden.authenticate!
478
- github_teams_authenticate! ENV['GITHUB_TEAM_IDS'].split(',')
479
- logger.info "Hi There, #{user_state_data[:user_name]}! You have access to the #{params['id']} team"
480
-
481
- when :member
482
- log_info! 'PreFlight', 'AuthCheck', 'Process authentication strategy'
483
-
484
- if env['HTTP_COOKIE'].include? 'j1.user.state'
485
- session_encoded = request.cookies['j1.user.state']
486
- session_decoded = Base64.decode64(session_encoded)
487
- log_info! 'PreFlight', 'Cookie', 'Read user state data' # "#{session_decoded}"
488
- user_state_data = JSON.parse(session_decoded)
489
- end
490
-
491
- # Update cookie data
492
- # ----------------------------------------------------------------------
493
- user_state_data['provider_url'] = providers["#{default_provider}"]['provider_url']
494
- user_state_data['users_allowed'] = providers["#{default_provider}"]['users']
495
- user_state_data['provider_permissions'] = providers["#{default_provider}"]['permissions']
496
- user_state_data['requested_page'] = env['REQUEST_URI']
497
- user_state_data['writer'] = 'middleware'
498
-
499
- # write updated J1 session cookie
500
- #
501
- session_json = user_state_data.to_json
502
- log_info! 'PreFlight', 'Cookie', 'Write user state session data', "#{session_json}"
503
- writeCookie(user_state_cookie, session_json)
504
-
505
- log_info! 'PreFlight', 'Redirect', 'Call API request', 'PageValidate'
506
-
507
- allowed_users = providers["#{default_provider}"]['users'].join(',')
508
- requested_page = env['REQUEST_URI']
509
- redirect "/page_validation?page=#{requested_page}"
510
- else
511
- raise J1App::ConfigError
512
- end
513
-
514
- end
515
- end
516
- end
517
-
518
-
519
- # ==========================================================================
520
- # API ENDPOINTS (Sinatra HANDLERS)
521
- # ==========================================================================
522
-
523
- # ENDPOINT authentication (called from WEB by auth client)
524
- # --------------------------------------------------------------------------
525
- get '/authentication' do
526
- request = params.fetch('request')
527
- provider = params.fetch('provider')
528
-
529
- log_info! 'API', 'Authentication', 'Authentication request received'
530
-
531
- # SignIn
532
- # ------------------------------------------------------------------------
533
- if request === 'signin'
534
-
535
- log_info! 'Authentication', 'SignIn', 'Called for provider', "#{provider}"
536
-
537
- # collect (additional) GET parameter|s
538
- # ----------------------------------------------------------------------
539
- allowed_users = params.fetch('allowed_users')
540
-
541
- if warden.authenticated?
542
- log_info! 'Authentication', 'SignIn', 'User already signed in', "#{warden.user[:info]['nickname']}"
543
- requested_page = user_state_data['requested_page']
544
- log_info! 'Authentication', 'SignIn', 'Pass user for requested page', "#{requested_page}"
545
- redirect "#{requested_page}"
546
- else
547
- log_info! 'Authentication', 'SignIn', 'Initiate OmniAuth authentication'
548
-
549
- # Make (really) sure that old session is cleared before login
550
- # --------------------------------------------------------------------
551
- warden.logout
552
- session.clear
553
- warden.authenticate! :"omni_#{provider}"
554
- end
555
- # SignOut
556
- # ------------------------------------------------------------------------
557
- elsif request === 'signout'
558
- # collect (additional) GET parameter|s
559
- provider_signout = params.fetch('provider_signout')
560
- log_info! 'Authentication', 'SignOut', 'Called for provider', #{provider}"
561
-
562
- if warden.authenticated?
563
- user = warden.user[:info]['nickname']
564
- provider = warden.user[:provider]
565
- provider_url = user_state_data['provider_url']
566
-
567
- log_info! 'Authentication', 'SignOut', 'Sign out user', "#{user}"
568
- warden.logout
569
- session.clear
570
-
571
- # Read current J1 user state cookie
572
- # --------------------------------------------------------------------
573
- if existsCookie? user_state_cookie
574
- user_state_data = readCookie(user_state_cookie)
575
- log_info! 'Authentication', 'Cookie', 'Read user state session data' #, "#{session_decoded}"
576
- else
577
- log_error! 'Authentication', 'Cookie', 'Cookie missing', user_state_cookie
578
- end
579
-
580
- if provider_signout === 'true'
581
- log_info! 'Authentication', 'SignOut', 'Sign out user', "#{user}"
582
- log_info! 'Authentication', 'SignOut', 'Sign out from', "#{provider}"
583
- log_info! 'Authentication', 'Redirect', 'Pass to provider', "#{provider_url}"
584
- redirect "#{provider_url}"
585
- else
586
- log_info! 'Authentication', 'SignOut', 'Sign out user', "#{user}"
587
- log_info! 'Authentication', 'SignOut', 'Sign out from', "session"
588
-
589
- # If signed out, redirect ONLY for PUBLIC pages
590
- # ------------------------------------------------------------------
591
- if redirect_whitelisted?user_state_data['requested_page']
592
- log_info! 'Authentication', 'Redirect', 'Pass to page', "#{user_state_data['requested_page']}"
593
- redirect user_state_data['requested_page']
594
- else
595
- log_info! 'Authentication', 'Redirect', 'Redirect NOT whitelisted'
596
- log_info! 'Authentication', 'Redirect', 'Pass to page', "/"
597
- redirect '/'
598
- end
599
- end
600
- else
601
- # THIS condition should NEVER REACHED because NO logout dialog
602
- # (modal) is provided by the auth client if a user isn't signed in.
603
- # Kept this alternative for cases something went wrong.
604
- # --------------------------------------------------------------------
605
- log_info! 'Authentication', 'API', 'DEAD PATH: Called for sign out', 'NOT signed in'
606
-
607
- # Read current J1 session cookie
608
- # --------------------------------------------------------------------
609
- if existsCookie? user_state_cookie
610
- user_state_data = readCookie(user_state_cookie)
611
- log_info! 'Authentication', 'Cookie', 'DEAD PATH. Read user state session data' #, "#{session_decoded}"
612
- else
613
- log_error! 'Authentication', 'Cookie', 'Cookie missing', user_state_cookie
614
- end
615
-
616
- log_info! 'Authentication', 'Redirect', 'DEAD PATH: Pass to requested page', "#{user_state_data['requested_page']}"
617
- redirect user_state_data['requested_page']
618
- end
619
- else
620
- raise J1App::ConfigError
621
- end
622
- end
623
- # END: get '/authentication'
624
- # --------------------------------------------------------------------------
625
-
626
- # ENDPOINT post_authentication (called after a user is back from OAuth Provider)
627
- # --------------------------------------------------------------------------
628
- get '/post_authentication' do
629
-
630
- reward = {
631
- :id => 'unknown',
632
- :name => 'unknown',
633
- :link => '#'
634
- }
635
- campaign = {
636
- :id => 'unknown',
637
- :link => '#'
638
- }
639
-
640
- log_info! 'API', 'Post Authentication', 'Identification request received'
641
-
642
- # Read current J1 session cookie
643
- # ------------------------------------------------------------------------
644
- if existsCookie? user_state_cookie
645
- user_state_data = readCookie(user_state_cookie)
646
- log_info! 'Post Authentication', 'Cookie', 'Read user state session data' #, "#{session_decoded}"
647
- log_info! 'Post Authentication', 'Cookie', 'Requested page', "#{user_state_data['requested_page']}"
648
- else
649
- log_error! 'Post Authentication', 'Cookie', 'Cookie missing', user_state_cookie
650
- end
651
-
652
- user = warden.user
653
- user_json = user.to_json
654
-
655
- if user[:provider] === 'disqus'
656
- user[:info][:urls][:site] = "https://disqus.com"
657
- user[:info][:urls][:home] = user[:info]['urls']['profileUrl']
658
- user[:info][:urls][:blog] = "https://disqus.com/by/juergen_adams/"
659
- user[:info][:urls][:member] = user[:info]['urls']['profileUrl']
660
- end
661
-
662
- if user[:provider] === 'github'
663
- user[:info][:urls][:site] = "https://github.com"
664
- user[:info][:urls][:home] = user[:info]['urls']['GitHub']
665
- user[:info][:urls][:blog] = "https://github.com/jekyll-one"
666
- user[:info][:urls][:member] = user[:info]['urls']['Blog']
667
- end
668
-
669
- if user[:provider] === 'patreon'
670
-
671
- user[:info][:urls][:site] = "https://patreon.com"
672
- user[:info][:urls][:home] = "https://patreon.com/home"
673
- user[:info][:urls][:blog] = "https://patreon.com/jekyll_one"
674
-
675
- unless user[:info]['payment_info'].empty?
676
- reward_url = user[:info]['payment_info']['relationships']['reward']['links']['related']
677
- reward_json = RestClient.get "#{reward_url}", {:content_type => :json, :accept => :json}
678
- reward_data = JSON.parse(reward_json)
679
- user[:info][:urls][:member] = "https://patreon.com" + reward_data['data']['attributes']['url']
680
- user[:info][:payment_status] = user[:info]['payment_info']['attributes']['declined_since'].nil? ? 'true' : 'false'
681
- else
682
- reward_url = ""
683
- reward_json = ""
684
- reward_data = ""
685
- user[:info][:payment_status] = 'false'
686
- end
687
-
688
- unless reward_data.empty?
689
- reward[:id] = reward_data['data']['id']
690
- reward[:name] = reward_data['data']['attributes']['title']
691
- reward[:link] = "https://patreon.com" + reward_data['data']['attributes']['url']
692
- campaign[:id] = reward_data['data']['relationships']['campaign']['data']['id']
693
- campaign[:link] = reward_data['data']['relationships']['campaign']['links']['related']
694
- else
695
- reward[:id] = ""
696
- reward[:name] = "no tiers"
697
- reward[:link] = ""
698
- campaign[:id] = ""
699
- campaign[:link] = ""
700
- end
701
- end
702
-
703
- user[:extra][:reward] = reward
704
- user[:extra][:campaign] = campaign
705
-
706
- # EXCEPTION: collection of session data failed (e.g cookie > 4K)
707
- #
708
- if user.nil?
709
- log_error! 'Post Authentication', 'Identification', 'Internal error', 'User identification failed'
710
- warden.logout
711
- session.clear
712
- log_info! 'Post Authentication', 'Redirect', 'Pass to error page (access_denied)'
713
- description_title = "Access Denied"
714
- redirect "/access_denied?provider=unknown&user=unknown&category=unknown&title=#{description_title}"
715
- else
716
- log_info! 'Post Authentication', 'Identification', 'User identified successfully'
717
- user_state_data['user_name'] = user[:info]['nickname']
718
- user_state_data['user_id'] = user[:uid]
719
- user_state_data['provider'] = user[:provider]
720
- user_state_data['provider_membership'] = 'member'
721
- user_state_data['provider_permissions'] = providers["#{user[:provider]}"]['permissions']
722
- user_state_data['users_allowed'] = providers["#{user[:provider]}"]['users']
723
- user_state_data['authenticated'] = 'true'
724
- user_state_data['payment_status'] = user[:info][:payment_status]
725
- user_state_data['writer'] = 'middleware'
726
-
727
- current_user = user[:info]['nickname'] = user[:info]['nickname']
728
- current_provider = user[:provider]
729
-
730
- user_state_data['requested_page'].scan(/(protected|private)/) do |match|
731
-
732
- # Set category from requested page
733
- #
734
- category = match[0]
735
- log_info! 'Post Authentication', 'Identification', 'Process content type', "#{category}"
736
-
737
- # Check if user is allowed to access protected content in GENERAL
738
- #
739
- log_info! 'Post Authentication', 'Identification', 'Check for allowed users'
740
- unless user_state_data['users_allowed'].include? 'all'
741
- unless user_state_data['users_allowed'].include? "#{current_user}"
742
- log_info! 'Post Authentication', 'Identification', 'User not allowed', "#{current_user}"
743
- log_info! 'Post Authentication', 'Identification', 'Allowed users', "#{user_state_data['users_allowed']}"
744
-
745
- # log_info! 'Post Authentication', 'Identification', 'Logout user from current session', "#{current_user}"
746
- # warden.logout
747
- # session.clear
748
-
749
- log_info! 'Post Authentication', 'Redirect', 'Pass to error page (access_denied)'
750
- description_title = "Access Denied"
751
- redirect "/access_denied?provider=#{current_provider}&user=#{current_user}&category=#{category}&title=#{description_title}"
752
- end
753
- end
754
- log_info! 'Post Authentication', 'Identification', 'Allowed users', "#{user_state_data['users_allowed']}"
755
-
756
- # Check conditions to access protected content (if any)
757
- #
758
- log_info! 'Post Authentication', 'Identification', 'Check for conditions', "#{current_provider}"
759
- check_conditions = providers["#{user[:provider]}"]['conditions'][category]['enabled']
760
- if check_conditions
761
-
762
- if providers["#{user[:provider]}"]['conditions'][category]['users']['whitelist'].nil?
763
- category_whitelist = 'all'
764
- else
765
- category_whitelist = providers["#{user[:provider]}"]['conditions'][category]['users']['whitelist']
766
- end
767
-
768
- # Check if user is BLACKLISTED
769
- #
770
- blacklist = providers["#{user[:provider]}"]['conditions'][category]['users']['blacklist']
771
- if blacklist.include? "#{current_user}"
772
- log_info! 'Post Authentication', 'Identification', 'Check blacklisting'
773
- log_info! 'Post Authentication', 'Identification', 'User blacklisted', "#{current_user}"
774
- user[:info][:blacklisted] = 'true'
775
-
776
- log_info! 'Post Authentication', 'Identification', 'Logout user from current session', "#{current_user}"
777
- warden.logout
778
- session.clear
779
-
780
- log_info! 'Post Authentication', 'Redirect', 'Pass to error page (access_denied)'
781
- description_title = "User blacklisted"
782
- redirect "/access_denied?provider=#{current_provider}&user=#{current_user}&category=#{category}&title=#{description_title}"
783
- end
784
-
785
- log_info! 'Post Authentication', 'Identification', 'Check whitelisting'
786
- if category_whitelisted? category_whitelist, current_user
787
- user[:info][:whitelisted] = 'true'
788
- reward[:name] = 'whitelisted'
789
- log_info! 'Post Authentication', 'Identification', 'User whitelisted', "#{current_user}"
790
- log_info! 'Post Authentication', 'Identification', 'Reward set to', 'Whitelisted'
791
- else
792
- log_info! 'Post Authentication', 'Identification', 'No whitelisting found', "#{current_user}"
793
- end
794
-
795
- log_info! 'Post Authentication', 'Identification', 'Check conditions'
796
- unless category_whitelisted? category_whitelist, current_user
797
- log_info! 'Post Authentication', 'Identification', 'Check rewards'
798
- payment_tiers = providers["#{user[:provider]}"]['conditions'][category]['payment']['activated']
799
- log_info! 'Post Authentication', 'Identification', 'Check rewards', "#{current_user}"
800
- if payment_activated? payment_tiers
801
- log_info! 'Post Authentication', 'Identification', 'Reward found', "#{reward[:name]}"
802
-
803
- # Check if any payment exists for that user
804
- #
805
- log_info! 'Post Authentication', 'Identification', 'Check payment status'
806
- if user[:info]['payment_info'].empty?
807
- log_info! 'Post Authentication', 'Identification', 'Payment status: NOT AVAILABLE', "#{current_user}"
808
-
809
- log_info! 'Post Authentication', 'Identification', 'Logout user from current session', "#{current_user}"
810
- warden.logout
811
- session.clear
812
-
813
- log_info! 'Post Authentication', 'Redirect', 'Pass to error page (access_denied)'
814
- description_title = "Invalid funds"
815
- redirect "/access_denied?provider=#{current_provider}&user=#{current_user}&category=#{category}&title=#{description_title}"
816
- end
817
-
818
- # Check for VALID payments (scope: pledge-to-me)
819
- #
820
- payment_status = user[:info]['payment_info']['attributes']['declined_since']
821
- unless payment_valid? payment_status
822
- log_info! 'Post Authentication', 'Identification', 'Payment status INVALID', "#{current_user}"
823
-
824
- log_info! 'Post Authentication', 'Identification', 'Logout user from current session', "#{current_user}"
825
- warden.logout
826
- session.clear
827
-
828
- log_info! 'Post Authentication', 'Redirect', 'Pass to error page (access_denied)'
829
- description_title = "Invalid funds"
830
- redirect "/access_denied?provider=#{current_provider}&user=#{current_user}&category=#{category}&title=#{description_title}"
831
- else
832
- log_info! 'Post Authentication', 'Identification', 'Payment status VALID', "#{current_user}"
833
- end
834
- end
835
-
836
- end
837
- # end category_whitelisted
838
- else
839
- category_condition_state = providers["#{user[:provider]}"]['conditions'][category]['enabled']
840
-
841
- log_info! 'Post Authentication', 'Identification', 'Category check failed for', "#{current_provider}"
842
- log_info! 'Post Authentication', 'Identification', "Category checked", "#{category}"
843
- log_info! 'Post Authentication', 'Identification', "Category support", "#{category_condition_state}"
844
-
845
- # log_info! 'Post Authentication', 'Identification', 'SignOut', "#{current_user}"
846
- # warden.logout
847
- # session.clear
848
-
849
- user_state_data = readCookie(user_state_cookie)
850
- user_state_data['category_enabled'] = category_condition_state
851
- user_state_data['writer'] = 'middleware'
852
- session_json = user_state_data.to_json
853
-
854
- log_info! 'Post Authentication', 'Cookie', 'Write user state session data', "#{session_json}"
855
- writeCookie(user_state_cookie, session_json)
856
-
857
- log_info! 'Post Authentication', 'Redirect', 'Pass to error page (access_denied)'
858
- description_title = "Access Denied"
859
- redirect "/access_denied?provider=#{current_provider}&user=#{current_user}&category=#{category}&title=#{description_title}"
860
- end
861
- # end check conditions
862
-
863
- end
864
- # end protected content
865
- end
866
- # end user.nil?
867
-
868
- log_info! 'Post Authentication', 'Identification', 'Provider', "#{user[:provider]}"
869
- log_info! 'Post Authentication', 'Identification', 'User', "#{user[:info]['nickname']}"
870
-
871
- # jadams, 2019-07-22: Check if it's needed to modify the respose header
872
- #
873
- # time = Time.now.ctime.to_s
874
- # log_info! 'API', 'Auth Request', 'Set Last-Modified', "#{time}"
875
- # response.headers['Last-Modified'] = time
876
- # response.headers['Cache-Control'] = 'private,max-age=0,must-revalidate,no-store'
877
- # response.headers['X-J1-AuthManager'] = "page-validated;category=#{category};called=" + time
878
-
879
- log_info! 'Post Authentication', 'Redirect', 'Pass to requested page', "#{user_state_data['requested_page']}"
880
- redirect user_state_data['requested_page']
881
-
882
- end
883
- # END: get /post_authentication
884
- # --------------------------------------------------------------------------
885
-
886
- # ENDPOINT status (called from WEB to get current state of an user)
887
- # --------------------------------------------------------------------------
888
- get '/status' do
889
- requested_page = params.fetch('page')
890
- category = 'public'
891
-
892
- requested_page.scan(/(public|protected|private)/) do |match|
893
- category = match[0]
894
- end
895
-
896
- log_info! 'API', 'Status Request', 'Info request received'
897
- log_info! 'API', 'Status Request', 'Page requested', "#{requested_page}"
898
- log_info! 'API', 'Status Request', 'Content type', "#{category}"
899
-
900
- # if request.warden.user.respond_to?(:info)
901
- #
902
- if warden.authenticated?
903
- user_name = warden.user[:info]['nickname']
904
- user_id = warden.user[:uid]
905
- users_allowed = providers[warden.user[:provider]]['users']
906
- provider = warden.user[:provider]
907
- provider_membership = 'member'
908
- provider_permissions = user_state_data['provider_permissions']
909
-
910
- if provider == 'github'
911
- provider_site_url = warden.user[:info][:urls][:site]
912
- provider_home_url = warden.user[:info][:urls][:blog]
913
- provider_blog_url = warden.user[:info][:urls][:blog]
914
- provider_member_url = warden.user[:info][:urls][:blog]
915
- end
916
-
917
- if provider == 'disqus'
918
- provider_site_url = warden.user[:info][:urls][:site]
919
- provider_home_url = warden.user[:info][:urls][:blog]
920
- provider_blog_url = warden.user[:info][:urls][:blog]
921
- provider_member_url = warden.user[:info][:urls][:blog]
922
- end
923
-
924
- if provider == 'patreon'
925
- provider_membership = warden.user[:extra][:reward][:name]
926
- provider_site_url = warden.user[:info][:urls][:site]
927
- provider_home_url = warden.user[:info][:urls][:home]
928
- provider_blog_url = warden.user[:info][:urls][:blog]
929
- provider_member_url = warden.user[:extra][:reward][:link]
930
- end
931
-
932
- log_info! 'API', 'Status Request', 'User detected', "#{user_name}"
933
- log_info! 'API', 'Status Request', 'User detected as signed in'
934
- else
935
- user_name = 'unknown'
936
- log_info! 'API', 'Status Request', 'User detected', 'signed out'
937
- end
938
-
939
- # time = Time.now.ctime.to_s
940
- time = Time.now.strftime("%Y-%m-%d %H:%M:%S")
941
-
942
- # if request.warden.authenticated?
943
- #
944
- if user_name != 'unknown'
945
- log_info! 'API', 'Status Request', 'Send data', 'SIGNED_IN'
946
- content_type 'application/json'
947
- {
948
- user_name: user_name,
949
- user_id: user_id,
950
- users_allowed: users_allowed,
951
- provider: provider,
952
- provider_membership: provider_membership,
953
- provider_permissions: provider_permissions,
954
- provider_site_url: provider_site_url,
955
- provider_home_url: provider_home_url,
956
- provider_blog_url: provider_blog_url,
957
- provider_member_url: provider_member_url,
958
- provider_privacy_url: provider_privacy_url_default,
959
- page_permission: category,
960
- requested_page: requested_page,
961
- authenticated: 'true',
962
- timestamp: time
963
- }.to_json
964
- else
965
- log_info! 'API', 'Status Request', 'Send data', 'SIGNED_OUT'
966
- content_type 'application/json'
967
- {
968
- user_name: 'visitor',
969
- user_id: 'unknown',
970
- users_allowed: 'all',
971
- provider: 'j1',
972
- provider_membership: 'guest',
973
- provider_permissions: ['public'],
974
- provider_site_url: provider_site_url_default,
975
- provider_home_url: provider_home_url_default,
976
- provider_blog_url: provider_blog_url_default,
977
- provider_member_url: provider_member_url_default,
978
- provider_privacy_url: provider_privacy_url_default,
979
- page_permission: category,
980
- requested_page: requested_page,
981
- authenticated: 'false',
982
- timestamp: time
983
- }.to_json
984
- end
985
- end
986
- # END: get /status
987
- # --------------------------------------------------------------------------
988
-
989
- # ENDPOINT access_denied (exception, called from the app|auth manager)
990
- # --------------------------------------------------------------------------
991
- get '/access_denied' do
992
- provider = params.fetch('provider')
993
- category = params.fetch('category')
994
- user = params.fetch('user')
995
- description_title = params.fetch('title')
996
-
997
- log_info! 'API', 'ExceptionHandler', 'Request received'
998
- log_error! 'ExceptionHandler', 'ERROR', 'Access Denied'
999
-
1000
- # Read current J1 session cookie
1001
- # ------------------------------------------------------------------------
1002
- if existsCookie? user_state_cookie
1003
- user_state_data = readCookie(user_state_cookie)
1004
- log_info! 'ExceptionHandler', 'Cookie', 'Read user state session data' #, "#{session_decoded}"
1005
- else
1006
- log_error! 'ExceptionHandler', 'Cookie', 'Cookie missing', user_state_cookie
1007
- end
1008
-
1009
- log_info! 'ExceptionHandler', 'Redirect', 'Pass to error page', "Access Denied"
1010
-
1011
- # Capitalize first char
1012
- provider = provider.sub(/^./, &:upcase)
1013
- route = '/'
1014
-
1015
- @route = route
1016
- @provider = provider
1017
- @modal = "centralModalInfo"
1018
- @info_type = "danger"
1019
- @modal_icon = "account-off"
1020
- @modal_ok_text = "Ok, understood"
1021
- @modal_title = "Authentication Manager"
1022
- @modal_description = "<h4>#{description_title}</h4></br></br> User <b>#{user}</b> from provider <b>#{provider}</b> is not allowed to access <b>#{category}</b> pages."
1023
-
1024
- erb :auth_manager_ui
1025
- end
1026
- # END: get '/access_denied'
1027
- # --------------------------------------------------------------------------
1028
-
1029
- # /page_validation ENDPOINT
1030
- # --------------------------------------------------------------------------
1031
- get '/page_validation' do
1032
- requested_page = params.fetch('page')
1033
- category = ''
1034
- provider = ''
1035
- allowed_users = ''
1036
-
1037
- log_info! 'API', 'Validate Request', 'Page requested', "#{requested_page}"
1038
-
1039
- requested_page.scan(/(protected|private)/) do |match|
1040
- category = match[0]
1041
- provider = permissions[:"#{category}"][0]
1042
- allowed_users = providers["#{provider}"]['users'].join(',')
1043
- end
1044
-
1045
- log_info! 'API', 'Validate Request', 'Category detected', "#{category}"
1046
- log_info! 'API', 'Validate Request', 'Provider detected', "#{provider}"
1047
- log_info! 'API', 'Validate Request', 'Allowed users detected', "#{allowed_users}"
1048
-
1049
- if warden.authenticated?
1050
- log_info! 'API', 'Validate Request', 'Pass to SignIn dialog'
1051
- route = requested_page
1052
- else
1053
- log_info! 'API', 'Validate Request', 'User not authenticated'
1054
- route = "/authentication?request=signin&provider=#{provider}&allowed_users=#{allowed_users}"
1055
- end
1056
-
1057
- # Capitalize first char
1058
- provider = provider.sub(/^./, &:upcase)
1059
-
1060
- @provider = provider
1061
- @route = route
1062
- @modal = "signInProtectedContent"
1063
- @modal_icon = "login"
1064
- @modal_agreed_text = "Yes, please"
1065
- @modal_disagreed_text = "No, thanks"
1066
- @modal_title = "SignIn"
1067
- @modal_image = "/assets/images/attics/admin-dashboard-bootstrap-1280x600.png"
1068
- @modal_description = "The page <b>#{requested_page}</b> you requested belongs to <b>#{category}</b> content. You'll be redirected to authenticate with the provider <b>#{provider}</b>. If signed in successfully, you get access to all <b>#{category} pages</b>."
1069
-
1070
- erb :auth_manager_ui
1071
- end
1072
- # END: get '/page_validation
1073
- # --------------------------------------------------------------------------
1074
-
1075
- # Rescue OmniAuth::Strategies::OAuth2::CallbackError
1076
- # ------------------------------------------------------------------------
1077
- get '/redirect_on_failure' do
1078
- log_error! 'OmniAuth', 'OAuth2 ', 'Callback error on redirect'
1079
- log_info! 'Redirect on failure', 'Redirect', 'Pass to home page'
1080
- redirect "/" # user_state_data['requested_page']
1081
- end
1082
- # END: get /redirect_on_failure
1083
- # --------------------------------------------------------------------------
1084
-
1085
- # ENDPOINT invalid_funds (exception, called from the app|auth manager)
1086
- # --------------------------------------------------------------------------
1087
- # get '/invalid_funds' do
1088
- # provider = params.fetch('provider')
1089
- # category = params.fetch('category')
1090
- # user = params.fetch('user')
1091
- # description_title = params.fetch('title')
1092
- #
1093
- # log_info! 'API', 'ExceptionHandler', 'Request received'
1094
- # log_info! 'ExceptionHandler', 'ERROR', 'Invalid Funds'
1095
- #
1096
- # session_encoded = request.cookies['j1.user.state']
1097
- # session_decoded = Base64.decode64(session_encoded)
1098
- # user_state_data = JSON.parse(session_decoded)
1099
- #
1100
- # log_info! 'ExceptionHandler', 'Redirect', 'Pass to error page', 'Invalid Funds'
1101
- #
1102
- # # Capitalize first char
1103
- # provider = provider.sub(/^./, &:upcase)
1104
- # route = '/'
1105
- #
1106
- # @route = route
1107
- # @provider = provider
1108
- # @modal = "centralModalInfo"
1109
- # @info_type = "danger"
1110
- # @modal_icon = "account-off"
1111
- # @modal_ok_text = "Ok, understood"
1112
- # @modal_title = "Authentication Manager"
1113
- # @modal_description = "<h4>#{description_title}</h4></br></br> User <b>#{user}</b> from provider <b>#{provider}</b> is not allowed to access <b>#{category}</b> pages."
1114
- #
1115
- # erb :auth_manager_ui
1116
- # end
1117
- # END: get /invalid_funds
1118
- # --------------------------------------------------------------------------
1119
-
1120
- # ENDPOINT iframe
1121
- # --------------------------------------------------------------------------
1122
- # get '/iframe' do
1123
- # @website_url = "https://jekyll-one.github.io/"
1124
- # erb :iframe
1125
- # end
1126
- # END: get /iframe
1127
- # --------------------------------------------------------------------------
1128
-
1129
- end
1130
- end