|
1 |
| -# frozen_string_literal: true |
2 |
| - |
3 |
| -class EgController < ApplicationController |
4 |
| - skip_before_action :verify_authenticity_token |
5 |
| - before_action :eg_name, :set_eg, :set_meta, :ensure_manifest |
6 |
| - |
7 |
| - def file_name |
8 |
| - "#{controller_path}_service.rb" |
9 |
| - end |
10 |
| - |
11 |
| - def eg_name |
12 |
| - controller_name.to(4) |
13 |
| - end |
14 |
| - |
15 |
| - def set_eg |
16 |
| - session[:eg] = controller_name.split('_', 2).first |
17 |
| - end |
18 |
| - |
19 |
| - def get |
20 |
| - @messages = '' |
21 |
| - |
22 |
| - # to have the user authenticate or re-authenticate. |
23 |
| - @token_ok = check_token |
24 |
| - @config = Rails.application.config |
25 |
| - if @token_ok || controller_name.include?('cneg') |
26 |
| - # addSpecialAttributes(model) |
27 |
| - @envelope_ok = session[:envelope_id].present? |
28 |
| - @documents_ok = session[:envelope_documents].present? |
29 |
| - @document_options = session.fetch(:envelope_documents, {})['documents'] |
30 |
| - @gateway_ok = @config.gateway_account_id.try(:length) > 25 |
31 |
| - @template_ok = session[:template_id].present? |
32 |
| - @documentation = "#{@config.documentation}#{eg_name}" #= Config.documentation + EgName |
33 |
| - @show_doc = @config.documentation |
34 |
| - else |
35 |
| - redirect_to '/ds/mustAuthenticate' |
36 |
| - end |
37 |
| - end |
38 |
| - |
39 |
| - def set_meta |
40 |
| - @source_file = file_name.to_s |
41 |
| - #remove extra character that doesn't exist in service file |
42 |
| - index = @source_file.index('/') |
43 |
| - @source_file = index.nil? ? @source_file.sub(/^.*?eg/, 'eg') : @source_file.sub(%r{/.+?eg}, '/eg') |
44 |
| - @source_url = "#{Rails.application.config.github_example_url}#{@source_file}" |
45 |
| - end |
46 |
| - |
47 |
| - def check_token(buffer_in_min = 10) |
48 |
| - buffer = buffer_in_min * 60 |
49 |
| - expires_at = session[:ds_expires_at] |
50 |
| - remaining_duration = expires_at.nil? ? 0 : expires_at - buffer.seconds.from_now.to_i |
51 |
| - if expires_at.nil? |
52 |
| - Rails.logger.info '==> Token expiration is not available: fetching token' |
53 |
| - elsif remaining_duration.negative? |
54 |
| - Rails.logger.debug "==> Token is about to expire in #{time_in_words(remaining_duration)} at: #{Time.at(expires_at)}: fetching token" |
55 |
| - else |
56 |
| - Rails.logger.debug "==> Token is OK for #{time_in_words(remaining_duration)} at: #{Time.at(expires_at)}" |
57 |
| - end |
58 |
| - remaining_duration.positive? |
59 |
| - end |
60 |
| - |
61 |
| - private |
62 |
| - |
63 |
| - def time_in_words(duration) |
64 |
| - "#{Object.new.extend(ActionView::Helpers::DateHelper).distance_of_time_in_words(duration)}#{duration.negative? ? ' ago' : ''}" |
65 |
| - end |
66 |
| - |
67 |
| - def param_gsub(parameter) |
68 |
| - parameter.gsub(/([^\w \-@.,])+/, '') |
69 |
| - end |
70 |
| - |
71 |
| - def check_auth(api) |
72 |
| - # if not authorized for same API type example or |
73 |
| - # if it is an attempt to authorize from home page |
74 |
| - # then user will be redirected to login page |
75 |
| - unless (session[:api] == api) || ((api == 'eSignature') && !session[:api]) |
76 |
| - session[:api] = api |
77 |
| - params[:auth] = 'jwt-auth' if api == 'Monitor' |
78 |
| - |
79 |
| - return redirect_to '/ds/mustAuthenticate' |
80 |
| - end |
81 |
| - |
82 |
| - minimum_buffer_min = 10 |
83 |
| - token_ok = check_token(minimum_buffer_min) |
84 |
| - return if token_ok |
85 |
| - |
86 |
| - flash[:messages] = 'Sorry, you need to re-authenticate.' |
87 |
| - # We could store the parameters of the requested operation so it could be restarted automatically |
88 |
| - # But since it should be rare to have a token issue here, we'll make the user re-enter the form data after authentication |
89 |
| - redirect_to '/ds/mustAuthenticate' |
90 |
| - end |
91 |
| - |
92 |
| - def handle_error(e) |
93 |
| - error = JSON.parse e.response_body |
94 |
| - @error_code = e.code || error['errorCode'] |
95 |
| - @error_message = error['error_description'] || error['message'] || error['error'] |
96 |
| - render 'ds_common/error' |
97 |
| - end |
98 |
| - |
99 |
| - def create_source_path |
100 |
| - # code here |
101 |
| - end |
102 |
| - |
103 |
| - def ensure_manifest |
104 |
| - @manifest = Utils::ManifestUtils.new.get_manifest(Rails.configuration.example_manifest_url) |
105 |
| - end |
106 |
| - |
107 |
| - def format_string(string, *args) |
108 |
| - string.gsub(/\{(\d+)\}/) { |s| args[s.to_i] } |
109 |
| - end |
110 |
| -end |
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +class EgController < ApplicationController |
| 4 | + skip_before_action :verify_authenticity_token |
| 5 | + before_action :eg_name, :set_eg, :set_meta, :ensure_manifest |
| 6 | + |
| 7 | + def file_name |
| 8 | + "#{controller_path}_service.rb" |
| 9 | + end |
| 10 | + |
| 11 | + def eg_name |
| 12 | + controller_name.to(4) |
| 13 | + end |
| 14 | + |
| 15 | + def set_eg |
| 16 | + session[:eg] = controller_name.split('_', 2).first |
| 17 | + end |
| 18 | + |
| 19 | + def get |
| 20 | + @messages = '' |
| 21 | + |
| 22 | + # to have the user authenticate or re-authenticate. |
| 23 | + @token_ok = check_token |
| 24 | + @config = Rails.application.config |
| 25 | + if @token_ok || controller_name.include?('cneg') |
| 26 | + # addSpecialAttributes(model) |
| 27 | + @envelope_ok = session[:envelope_id].present? |
| 28 | + @documents_ok = session[:envelope_documents].present? |
| 29 | + @document_options = session.fetch(:envelope_documents, {})['documents'] |
| 30 | + @gateway_ok = @config.gateway_account_id.try(:length) > 25 |
| 31 | + @template_ok = session[:template_id].present? |
| 32 | + @documentation = "#{@config.documentation}#{eg_name}" #= Config.documentation + EgName |
| 33 | + @show_doc = @config.documentation |
| 34 | + else |
| 35 | + redirect_to '/ds/mustAuthenticate' |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + def set_meta |
| 40 | + @source_file = file_name.to_s |
| 41 | + #remove extra character that doesn't exist in service file |
| 42 | + index = @source_file.index('/') |
| 43 | + @source_file = index.nil? ? @source_file.sub(/^.*?eg/, 'eg') : @source_file.sub(%r{/.+?eg}, '/eg') |
| 44 | + @source_url = "#{Rails.application.config.github_example_url}#{@source_file}" |
| 45 | + end |
| 46 | + |
| 47 | + def check_token(buffer_in_min = 10) |
| 48 | + buffer = buffer_in_min * 60 |
| 49 | + expires_at = session[:ds_expires_at] |
| 50 | + remaining_duration = expires_at.nil? ? 0 : expires_at - buffer.seconds.from_now.to_i |
| 51 | + if expires_at.nil? |
| 52 | + Rails.logger.info '==> Token expiration is not available: fetching token' |
| 53 | + elsif remaining_duration.negative? |
| 54 | + Rails.logger.debug "==> Token is about to expire in #{time_in_words(remaining_duration)} at: #{Time.at(expires_at)}: fetching token" |
| 55 | + else |
| 56 | + Rails.logger.debug "==> Token is OK for #{time_in_words(remaining_duration)} at: #{Time.at(expires_at)}" |
| 57 | + end |
| 58 | + remaining_duration.positive? |
| 59 | + end |
| 60 | + |
| 61 | + private |
| 62 | + |
| 63 | + def time_in_words(duration) |
| 64 | + "#{Object.new.extend(ActionView::Helpers::DateHelper).distance_of_time_in_words(duration)}#{duration.negative? ? ' ago' : ''}" |
| 65 | + end |
| 66 | + |
| 67 | + def param_gsub(parameter) |
| 68 | + parameter.gsub(/([^\w \-@.,])+/, '') |
| 69 | + end |
| 70 | + |
| 71 | + def check_auth(api) |
| 72 | + # if not authorized for same API type example or |
| 73 | + # if it is an attempt to authorize from home page |
| 74 | + # then user will be redirected to login page |
| 75 | + unless (session[:api] == api) || ((api == 'eSignature') && !session[:api]) |
| 76 | + session[:api] = api |
| 77 | + params[:auth] = 'jwt-auth' if api == 'Monitor' |
| 78 | + |
| 79 | + return redirect_to '/ds/mustAuthenticate' |
| 80 | + end |
| 81 | + |
| 82 | + minimum_buffer_min = 10 |
| 83 | + token_ok = check_token(minimum_buffer_min) |
| 84 | + return if token_ok |
| 85 | + |
| 86 | + flash[:messages] = 'Sorry, you need to re-authenticate.' |
| 87 | + # We could store the parameters of the requested operation so it could be restarted automatically |
| 88 | + # But since it should be rare to have a token issue here, we'll make the user re-enter the form data after authentication |
| 89 | + redirect_to '/ds/mustAuthenticate' |
| 90 | + end |
| 91 | + |
| 92 | + def handle_error(e) |
| 93 | + error = JSON.parse e.response_body |
| 94 | + @error_code = e.code || error['errorCode'] |
| 95 | + @error_message = error['error_description'] || error['message'] || error['error'] |
| 96 | + render 'ds_common/error' |
| 97 | + end |
| 98 | + |
| 99 | + def create_source_path |
| 100 | + # code here |
| 101 | + end |
| 102 | + |
| 103 | + def ensure_manifest |
| 104 | + @manifest = Utils::ManifestUtils.new.get_manifest(Rails.configuration.example_manifest_url) |
| 105 | + end |
| 106 | + |
| 107 | + def format_string(string, *args) |
| 108 | + string.gsub(/\{(\d+)\}/) { args[::Regexp.last_match(1).to_i] } |
| 109 | + end |
| 110 | +end |
0 commit comments