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,1121 +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
- log_info! 'Post Authentication', 'Identification', 'Category check failed for', "#{current_provider}"
841
- log_info! 'Post Authentication', 'Identification', "Category checked", "#{category}"
842
- log_info! 'Post Authentication', 'Identification', "Category support", "#{category_condition_state}"
843
-
844
- # log_info! 'Post Authentication', 'Identification', 'SignOut', "#{current_user}"
845
- # warden.logout
846
- # session.clear
847
-
848
- log_info! 'Post Authentication', 'Redirect', 'Pass to error page (access_denied)'
849
- description_title = "Access Denied"
850
- redirect "/access_denied?provider=#{current_provider}&user=#{current_user}&category=#{category}&title=#{description_title}"
851
- end
852
- # end check conditions
853
-
854
- end
855
- # end protected content
856
- end
857
- # end user.nil?
858
-
859
- log_info! 'Post Authentication', 'Identification', 'Provider', "#{user[:provider]}"
860
- log_info! 'Post Authentication', 'Identification', 'User', "#{user[:info]['nickname']}"
861
-
862
- # jadams, 2019-07-22: Check if it's needed to modify the respose header
863
- #
864
- # time = Time.now.ctime.to_s
865
- # log_info! 'API', 'Auth Request', 'Set Last-Modified', "#{time}"
866
- # response.headers['Last-Modified'] = time
867
- # response.headers['Cache-Control'] = 'private,max-age=0,must-revalidate,no-store'
868
- # response.headers['X-J1-AuthManager'] = "page-validated;category=#{category};called=" + time
869
-
870
- log_info! 'Post Authentication', 'Redirect', 'Pass to requested page', "#{user_state_data['requested_page']}"
871
- redirect user_state_data['requested_page']
872
-
873
- end
874
- # END: get /post_authentication
875
- # --------------------------------------------------------------------------
876
-
877
- # ENDPOINT status (called from WEB to get current state of an user)
878
- # --------------------------------------------------------------------------
879
- get '/status' do
880
- requested_page = params.fetch('page')
881
- category = 'public'
882
-
883
- requested_page.scan(/(public|protected|private)/) do |match|
884
- category = match[0]
885
- end
886
-
887
- log_info! 'API', 'Status Request', 'Info request received'
888
- log_info! 'API', 'Status Request', 'Page requested', "#{requested_page}"
889
- log_info! 'API', 'Status Request', 'Content type', "#{category}"
890
-
891
- # if request.warden.user.respond_to?(:info)
892
- #
893
- if warden.authenticated?
894
- user_name = warden.user[:info]['nickname']
895
- user_id = warden.user[:uid]
896
- users_allowed = providers[warden.user[:provider]]['users']
897
- provider = warden.user[:provider]
898
- provider_membership = 'member'
899
- provider_permissions = user_state_data['provider_permissions']
900
-
901
- if provider == 'github'
902
- provider_site_url = warden.user[:info][:urls][:site]
903
- provider_home_url = warden.user[:info][:urls][:blog]
904
- provider_blog_url = warden.user[:info][:urls][:blog]
905
- provider_member_url = warden.user[:info][:urls][:blog]
906
- end
907
-
908
- if provider == 'disqus'
909
- provider_site_url = warden.user[:info][:urls][:site]
910
- provider_home_url = warden.user[:info][:urls][:blog]
911
- provider_blog_url = warden.user[:info][:urls][:blog]
912
- provider_member_url = warden.user[:info][:urls][:blog]
913
- end
914
-
915
- if provider == 'patreon'
916
- provider_membership = warden.user[:extra][:reward][:name]
917
- provider_site_url = warden.user[:info][:urls][:site]
918
- provider_home_url = warden.user[:info][:urls][:home]
919
- provider_blog_url = warden.user[:info][:urls][:blog]
920
- provider_member_url = warden.user[:extra][:reward][:link]
921
- end
922
-
923
- log_info! 'API', 'Status Request', 'User detected', "#{user_name}"
924
- log_info! 'API', 'Status Request', 'User detected as signed in'
925
- else
926
- user_name = 'unknown'
927
- log_info! 'API', 'Status Request', 'User detected', 'signed out'
928
- end
929
-
930
- # time = Time.now.ctime.to_s
931
- time = Time.now.strftime("%Y-%m-%d %H:%M:%S")
932
-
933
- # if request.warden.authenticated?
934
- #
935
- if user_name != 'unknown'
936
- log_info! 'API', 'Status Request', 'Send data', 'SIGNED_IN'
937
- content_type 'application/json'
938
- {
939
- user_name: user_name,
940
- user_id: user_id,
941
- users_allowed: users_allowed,
942
- provider: provider,
943
- provider_membership: provider_membership,
944
- provider_permissions: provider_permissions,
945
- provider_site_url: provider_site_url,
946
- provider_home_url: provider_home_url,
947
- provider_blog_url: provider_blog_url,
948
- provider_member_url: provider_member_url,
949
- provider_privacy_url: provider_privacy_url_default,
950
- page_permission: category,
951
- requested_page: requested_page,
952
- authenticated: 'true',
953
- timestamp: time
954
- }.to_json
955
- else
956
- log_info! 'API', 'Status Request', 'Send data', 'SIGNED_OUT'
957
- content_type 'application/json'
958
- {
959
- user_name: 'visitor',
960
- user_id: 'unknown',
961
- users_allowed: 'all',
962
- provider: 'j1',
963
- provider_membership: 'guest',
964
- provider_permissions: 'public',
965
- provider_site_url: provider_site_url_default,
966
- provider_home_url: provider_home_url_default,
967
- provider_blog_url: provider_blog_url_default,
968
- provider_member_url: provider_member_url_default,
969
- provider_privacy_url: provider_privacy_url_default,
970
- page_permission: category,
971
- requested_page: requested_page,
972
- authenticated: 'false',
973
- timestamp: time
974
- }.to_json
975
- end
976
- end
977
- # END: get /status
978
- # --------------------------------------------------------------------------
979
-
980
- # ENDPOINT access_denied (exception, called from the app|auth manager)
981
- # --------------------------------------------------------------------------
982
- get '/access_denied' do
983
- provider = params.fetch('provider')
984
- category = params.fetch('category')
985
- user = params.fetch('user')
986
- description_title = params.fetch('title')
987
-
988
- log_info! 'API', 'ExceptionHandler', 'Request received'
989
- log_error! 'ExceptionHandler', 'ERROR', 'Access Denied'
990
-
991
- # Read current J1 session cookie
992
- # ------------------------------------------------------------------------
993
- if existsCookie? user_state_cookie
994
- user_state_data = readCookie(user_state_cookie)
995
- log_info! 'ExceptionHandler', 'Cookie', 'Read user state session data' #, "#{session_decoded}"
996
- else
997
- log_error! 'ExceptionHandler', 'Cookie', 'Cookie missing', user_state_cookie
998
- end
999
-
1000
- log_info! 'ExceptionHandler', 'Redirect', 'Pass to error page', "Access Denied"
1001
-
1002
- # Capitalize first char
1003
- provider = provider.sub(/^./, &:upcase)
1004
- route = '/'
1005
-
1006
- @route = route
1007
- @provider = provider
1008
- @modal = "centralModalInfo"
1009
- @info_type = "danger"
1010
- @modal_icon = "account-off"
1011
- @modal_ok_text = "Ok, understood"
1012
- @modal_title = "Authentication Manager"
1013
- @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."
1014
-
1015
- erb :auth_manager_ui
1016
- end
1017
- # END: get '/access_denied'
1018
- # --------------------------------------------------------------------------
1019
-
1020
- # /page_validation ENDPOINT
1021
- # --------------------------------------------------------------------------
1022
- get '/page_validation' do
1023
- requested_page = params.fetch('page')
1024
- category = ''
1025
- provider = ''
1026
- allowed_users = ''
1027
-
1028
- log_info! 'API', 'Validate Request', 'Page requested', "#{requested_page}"
1029
-
1030
- requested_page.scan(/(protected|private)/) do |match|
1031
- category = match[0]
1032
- provider = permissions[:"#{category}"][0]
1033
- allowed_users = providers["#{provider}"]['users'].join(',')
1034
- end
1035
-
1036
- log_info! 'API', 'Validate Request', 'Category detected', "#{category}"
1037
- log_info! 'API', 'Validate Request', 'Provider detected', "#{provider}"
1038
- log_info! 'API', 'Validate Request', 'Allowed users detected', "#{allowed_users}"
1039
-
1040
- if warden.authenticated?
1041
- log_info! 'API', 'Validate Request', 'Pass to SignIn dialog'
1042
- route = requested_page
1043
- else
1044
- log_info! 'API', 'Validate Request', 'User not authenticated'
1045
- route = "/authentication?request=signin&provider=#{provider}&allowed_users=#{allowed_users}"
1046
- end
1047
-
1048
- # Capitalize first char
1049
- provider = provider.sub(/^./, &:upcase)
1050
-
1051
- @provider = provider
1052
- @route = route
1053
- @modal = "signInProtectedContent"
1054
- @modal_icon = "login"
1055
- @modal_agreed_text = "Yes, please"
1056
- @modal_disagreed_text = "No, thanks"
1057
- @modal_title = "SignIn"
1058
- @modal_image = "/assets/images/attics/admin-dashboard-bootstrap-1280x600.png"
1059
- @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>."
1060
-
1061
- erb :auth_manager_ui
1062
- end
1063
- # END: get '/page_validation
1064
- # --------------------------------------------------------------------------
1065
-
1066
- # Rescue OmniAuth::Strategies::OAuth2::CallbackError
1067
- # ------------------------------------------------------------------------
1068
- get '/redirect_on_failure' do
1069
- log_error! 'OmniAuth', 'OAuth2 ', 'Callback error on redirect'
1070
- log_info! 'Redirect on failure', 'Redirect', 'Pass to home page'
1071
- redirect "/" # user_state_data['requested_page']
1072
- end
1073
- # END: get /redirect_on_failure
1074
- # --------------------------------------------------------------------------
1075
-
1076
- # ENDPOINT invalid_funds (exception, called from the app|auth manager)
1077
- # --------------------------------------------------------------------------
1078
- # get '/invalid_funds' do
1079
- # provider = params.fetch('provider')
1080
- # category = params.fetch('category')
1081
- # user = params.fetch('user')
1082
- # description_title = params.fetch('title')
1083
- #
1084
- # log_info! 'API', 'ExceptionHandler', 'Request received'
1085
- # log_info! 'ExceptionHandler', 'ERROR', 'Invalid Funds'
1086
- #
1087
- # session_encoded = request.cookies['j1.user.state']
1088
- # session_decoded = Base64.decode64(session_encoded)
1089
- # user_state_data = JSON.parse(session_decoded)
1090
- #
1091
- # log_info! 'ExceptionHandler', 'Redirect', 'Pass to error page', 'Invalid Funds'
1092
- #
1093
- # # Capitalize first char
1094
- # provider = provider.sub(/^./, &:upcase)
1095
- # route = '/'
1096
- #
1097
- # @route = route
1098
- # @provider = provider
1099
- # @modal = "centralModalInfo"
1100
- # @info_type = "danger"
1101
- # @modal_icon = "account-off"
1102
- # @modal_ok_text = "Ok, understood"
1103
- # @modal_title = "Authentication Manager"
1104
- # @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."
1105
- #
1106
- # erb :auth_manager_ui
1107
- # end
1108
- # END: get /invalid_funds
1109
- # --------------------------------------------------------------------------
1110
-
1111
- # ENDPOINT iframe
1112
- # --------------------------------------------------------------------------
1113
- # get '/iframe' do
1114
- # @website_url = "https://jekyll-one.github.io/"
1115
- # erb :iframe
1116
- # end
1117
- # END: get /iframe
1118
- # --------------------------------------------------------------------------
1119
-
1120
- end
1121
- end