Module: ZendeskConfiguration

Extended by:
ActiveSupport::Concern
Defined in:
lib/app/models/concerns/zendesk_configuration.rb

Overview

Add zendesk capability to an object to integrate via JWT with Zendesk. Most commonly this will be added to SystemConfiguration, however this can also be added to a different object (i.e., switchboard).

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/app/models/concerns/zendesk_configuration.rb', line 9

def self.included(base)
  base.class_eval do
    #
    # Fields
    #
    field :zendesk_token, type: String
    field :zendesk_base_url, type: String, default: 'https://app47.zendesk.com'
    field :zendesk_documentation_path, type: String, default: 'hc'
    field :zendesk_support_path, type: String, default: 'hc/en-us/requests'
    field :zendesk_updates_path, type: String, default: 'hc'
    #
    # Validations
    #
    validates :zendesk_base_url, url: true
  end
end

Instance Method Details

#secure_fieldsObject

Make sure the password doesn’t get blanked out on an update



29
30
31
# File 'lib/app/models/concerns/zendesk_configuration.rb', line 29

def secure_fields
  super + %i[zendesk_token]
end

#zendesk_configured?Boolean

Is zendesk configured?

Returns:

  • (Boolean)


86
87
88
89
90
91
# File 'lib/app/models/concerns/zendesk_configuration.rb', line 86

def zendesk_configured?
  [zendesk_token.present?,
   zendesk_base_url.present?,
   zendesk_documentation_path.present?,
   zendesk_support_path.present?].all?
end

#zendesk_documentation_url(user = nil) ⇒ Object

Return the zendesk documentation URL



36
37
38
# File 'lib/app/models/concerns/zendesk_configuration.rb', line 36

def zendesk_documentation_url(user = nil)
  zendesk_url(forward_to: zendesk_documentation_path, user: user)
end

#zendesk_new_request_url(user = nil) ⇒ Object

Return the zendesk support URL



50
51
52
# File 'lib/app/models/concerns/zendesk_configuration.rb', line 50

def zendesk_new_request_url(user = nil)
  zendesk_url(forward_to: "#{zendesk_support_path}/new", user: user)
end

#zendesk_requests_url(user = nil) ⇒ Object

Return the zendesk support URL



43
44
45
# File 'lib/app/models/concerns/zendesk_configuration.rb', line 43

def zendesk_requests_url(user = nil)
  zendesk_url(forward_to: zendesk_support_path, user: user)
end

#zendesk_updates_url(user = nil) ⇒ Object

Return the zendesk update URL



57
58
59
# File 'lib/app/models/concerns/zendesk_configuration.rb', line 57

def zendesk_updates_url(user = nil)
  zendesk_url(forward_to: zendesk_updates_path, user: user)
end

#zendesk_url(forward_to: zendesk_documentation_path, user: nil) ⇒ Object

Generate a Zendesk URL

If a user is passed in and Zendesk is configured then return a JWT enabled URL for SSO authentication to Zendesk.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/app/models/concerns/zendesk_configuration.rb', line 67

def zendesk_url(forward_to: zendesk_documentation_path, user: nil)
  path = if zendesk_configured? && user.present?
           time_now = Time.now.to_i
           jti = "#{time_now}/#{rand(36**64).to_s(36)}"
           payload = { jwt: JWT.encode({ iat: time_now, # Seconds since epoch, determine when this token is stale
                                         jti: jti, # Unique token identifier, helps prevent replay attacks
                                         name: user.name,
                                         email: user.email }, zendesk_token),
                       return_to: CGI.escape([zendesk_base_url, forward_to].join('/')) }
           ['access/jwt', payload.to_query].join('?')
         else
           forward_to
         end
  [zendesk_base_url, path].join('/')
end