strongmind-auth 1.0.5 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c354a11e582f73dbffa8303b9f62da41ba670ab4b149521bc449b21136721b97
4
- data.tar.gz: 240af831cd266936721d98f8e8a0796cd7700d8722d8e2dcf7bc62e5b78d7f8c
3
+ metadata.gz: 29554b31cde55064956270ba9bd557e8390a49ec9a847ff417cd13e0983d0460
4
+ data.tar.gz: 534b35f4fcfa0273ec3364b1bceca1b4e0bf9f1f99c681c21d2d489b97b861b5
5
5
  SHA512:
6
- metadata.gz: c2f44a55c2cece8baacf95d9cda467e4ab4d5c1a93cee41b0f1c7e9b7f4efce42111571c05ae303e8e46cedd31f6673b236f7a00d0dc5cb0262cd4ecc262f0e3
7
- data.tar.gz: 9b5ed691d3d9e850f17fec2ba0af08012e046236c56efd37df46b1d0a3dd2dfc2a4f98caff8a42ab29f676d4861e65bf9b67d50fc206a4348a55e1f50dcf22c9
6
+ metadata.gz: c00798ee297949194cb98ff045e46b222bcd3a5d60d244f9f78cf5ee336299c1ba19d9c1414cd2a9f14345cd8fd6372f09769bdcc928599bf610c325552ee342
7
+ data.tar.gz: caeb996433c23a339c251f4617a525ead271377fa97d0d027ae87ae9a694cce1e07c596ebdd21f3fd3a2aab2e10ccbcc210cb0c8c9885a5c826d82e02ba80df8
@@ -1,16 +1,16 @@
1
- require 'platform_sdk'
1
+ require "strongmind/common_nav_fetcher"
2
2
 
3
3
  module StrongMindNav
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  def fetch_common_nav
7
7
  begin
8
- navbar = PlatformSdk::CommonNavFetcher.new(current_user, request).retrieve(menu_items)
8
+ navbar = Strongmind::CommonNavFetcher.new(current_user, request).retrieve(menu_items)
9
9
 
10
10
  @top_navbar_html = navbar[:top_navbar_html]
11
11
  @bottom_navbar_html = navbar[:bottom_navbar_html]
12
12
  @theme_css = navbar[:theme_css]
13
- rescue PlatformSdk::CommonNavFetcher::TokenNotFoundError, PlatformSdk::CommonNavFetcher::UserNotFoundError
13
+ rescue Strongmind::CommonNavFetcher::TokenNotFoundError, Strongmind::CommonNavFetcher::UserNotFoundError
14
14
  render 'logins/index'
15
15
  rescue Faraday::TimeoutError
16
16
  @top_navbar_html = render_to_string(partial: 'layouts/loading_navbar').html_safe
@@ -1,5 +1,5 @@
1
1
  module Strongmind
2
2
  module Auth
3
- VERSION = "1.0.5"
3
+ VERSION = "1.0.6"
4
4
  end
5
5
  end
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'platform_sdk'
4
+
5
+ module Strongmind
6
+ # This class is responsible for fetching the common navbar data from SM Central
7
+ class CommonNavFetcher
8
+ attr_reader :user, :request, :auth_client
9
+
10
+ include Rails.application.routes.url_helpers
11
+
12
+ class TokenNotFoundError < StandardError; end
13
+
14
+ class UserNotFoundError < StandardError; end
15
+
16
+ def initialize(user, request)
17
+ raise UserNotFoundError, 'User not found' unless user.present?
18
+ raise ArgumentError, 'Request not found' unless request.present?
19
+
20
+ @user = user
21
+ @request = request
22
+ @auth_client = PlatformSdk::Identity::AuthClient.new(
23
+ ENV.fetch('IDENTITY_BASE_URL'), ENV.fetch('IDENTITY_CLIENT_ID'), ENV.fetch('IDENTITY_CLIENT_SECRET')
24
+ )
25
+ end
26
+
27
+ def retrieve(menu_items)
28
+ response = fetch_navbar_data(build_nav_items(menu_items))
29
+ parse_navbar(response)
30
+ end
31
+
32
+ private
33
+
34
+ def current_page?(url)
35
+ request.fullpath == URI.parse(url).path
36
+ end
37
+
38
+ def fetch_navbar_data(nav_items)
39
+ refresh_session if auth_client.token_expired?(token)
40
+
41
+ connection.post(navbar_endpoint, nav_items.to_json, 'Authorization' => "Bearer #{token}")
42
+ end
43
+
44
+ def connection
45
+ @connection ||= Faraday.new(url: ENV.fetch('SM_CENTRAL_BASE_URL')) do |conn|
46
+ conn.options.timeout = 3
47
+ conn.response :raise_error
48
+ conn.adapter Faraday.default_adapter
49
+ conn.headers['Content-Type'] = 'application/json'
50
+ end
51
+ end
52
+
53
+ def token
54
+ cache_data = Rails.cache.fetch(user.uid)
55
+ raise TokenNotFoundError, "Token not found for user #{user.id}" unless cache_data&.key?(:access_token)
56
+
57
+ cache_data[:access_token]
58
+ end
59
+
60
+ def refresh_session
61
+ begin
62
+ session = Rails.cache.fetch(user.uid)
63
+ auth_client.refresh_session(session:)
64
+ Rails.cache.write(user.uid, session)
65
+ rescue Faraday::BadRequestError => e
66
+ Sentry.capture_exception(e, extra: { session:, request_body: request.body })
67
+ end
68
+ end
69
+
70
+ def navbar_endpoint
71
+ '/navbar'
72
+ end
73
+
74
+ def parse_navbar(response)
75
+ JSON.parse(response.body, symbolize_names: true) if response
76
+ end
77
+
78
+ def build_nav_items(menu_items)
79
+ { nav_items: menu_items.map { |item| nav_item_data(item) } }
80
+ end
81
+
82
+ def nav_item_data(item)
83
+ url = send(item[:path_method])
84
+ {
85
+ name: item[:name],
86
+ icon: item[:icon],
87
+ url:,
88
+ is_disabled: item[:feature_flag] ? !user.feature_flag_enabled?(item[:feature_flag]) : false,
89
+ is_active: current_page?(url),
90
+ is_external: false
91
+ }
92
+ end
93
+ end
94
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strongmind-auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Team Belding
@@ -124,6 +124,7 @@ files:
124
124
  - lib/strongmind/auth.rb
125
125
  - lib/strongmind/auth/engine.rb
126
126
  - lib/strongmind/auth/version.rb
127
+ - lib/strongmind/common_nav_fetcher.rb
127
128
  - lib/tasks/rails/auth_tasks.rake
128
129
  - lib/tasks/strongmind/auth_tasks.rake
129
130
  homepage: https://www.strongmind.com