exchange.rb 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class ExternalCredential::Exchange
  3. def self.app_verify(params)
  4. request_account_to_link(params, false)
  5. params
  6. end
  7. def self.request_account_to_link(credentials = {}, app_required = true)
  8. external_credential = ExternalCredential.find_by(name: 'exchange')
  9. raise Exceptions::UnprocessableEntity, __('No Exchange app configured!') if !external_credential && app_required
  10. if external_credential
  11. if credentials[:client_id].blank?
  12. credentials[:client_id] = external_credential.credentials['client_id']
  13. end
  14. if credentials[:client_secret].blank?
  15. credentials[:client_secret] = external_credential.credentials['client_secret']
  16. end
  17. # client_tenant may be empty. Set only if key is nonexistant at all
  18. if !credentials.key? :client_tenant
  19. credentials[:client_tenant] = external_credential.credentials['client_tenant']
  20. end
  21. end
  22. raise Exceptions::UnprocessableEntity, __("The required parameter 'client_id' is missing.") if credentials[:client_id].blank?
  23. raise Exceptions::UnprocessableEntity, __("The required parameter 'client_secret' is missing.") if credentials[:client_secret].blank?
  24. authorize_url = generate_authorize_url(credentials)
  25. {
  26. authorize_url: authorize_url,
  27. }
  28. end
  29. def self.link_account(_request_token, params)
  30. # return to admin interface if admin Consent is in process and user clicks on "Back to app"
  31. return "#{Setting.get('http_type')}://#{Setting.get('fqdn')}/#system/integration/exchange/error/AADSTS65004" if params[:error_description].present? && params[:error_description].include?('AADSTS65004')
  32. external_credential = ExternalCredential.find_by(name: 'exchange')
  33. raise Exceptions::UnprocessableEntity, __('No Exchange app configured!') if !external_credential
  34. raise Exceptions::UnprocessableEntity, __("The required parameter 'code' is missing.") if !params[:code]
  35. response = authorize_tokens(external_credential.credentials, params[:code])
  36. %w[refresh_token access_token expires_in scope token_type id_token].each do |key|
  37. raise Exceptions::UnprocessableEntity, "No #{key} for authorization request found!" if response[key.to_sym].blank?
  38. end
  39. user_data = user_info(response[:id_token])
  40. raise Exceptions::UnprocessableEntity, __("The user's 'preferred_username' could not be extracted from 'id_token'.") if user_data[:preferred_username].blank?
  41. config = response.merge(
  42. user: user_data[:preferred_username],
  43. client_id: external_credential.credentials[:client_id],
  44. client_secret: external_credential.credentials[:client_secret],
  45. client_tenant: external_credential.credentials[:client_tenant],
  46. status: 200,
  47. )
  48. Setting.set('exchange_oauth', config)
  49. "#{Setting.get('http_type')}://#{Setting.get('fqdn')}/#system/integration/exchange/success/1"
  50. end
  51. def self.generate_authorize_url(credentials, scope = 'https://outlook.office365.com/EWS.AccessAsUser.All offline_access openid profile email')
  52. params = {
  53. 'client_id' => credentials[:client_id],
  54. 'redirect_uri' => ExternalCredential.callback_url('exchange'),
  55. 'scope' => scope,
  56. 'response_type' => 'code',
  57. 'access_type' => 'offline',
  58. 'prompt' => credentials[:prompt] || 'login',
  59. }
  60. tenant = credentials[:client_tenant].presence || 'common'
  61. uri = URI::HTTPS.build(
  62. host: 'login.microsoftonline.com',
  63. path: "/#{tenant}/oauth2/v2.0/authorize",
  64. query: params.to_query
  65. )
  66. uri.to_s
  67. end
  68. def self.authorize_tokens(credentials, authorization_code)
  69. uri = authorize_tokens_uri(credentials[:client_tenant])
  70. params = authorize_tokens_params(credentials, authorization_code)
  71. response = Net::HTTP.post_form(uri, params)
  72. if response.code != 200 && response.body.blank?
  73. Rails.logger.error "Request failed! (code: #{response.code})"
  74. raise "Request failed! (code: #{response.code})"
  75. end
  76. result = JSON.parse(response.body)
  77. if result['error'] && response.code != 200
  78. Rails.logger.error "Request failed! ERROR: #{result['error']} (#{result['error_description']}, params: #{params.to_json})"
  79. raise "Request failed! ERROR: #{result['error']} (#{result['error_description']})"
  80. end
  81. result[:created_at] = Time.zone.now
  82. result.symbolize_keys
  83. end
  84. def self.authorize_tokens_params(credentials, authorization_code)
  85. {
  86. client_secret: credentials[:client_secret],
  87. code: authorization_code,
  88. grant_type: 'authorization_code',
  89. client_id: credentials[:client_id],
  90. redirect_uri: ExternalCredential.callback_url('exchange'),
  91. }
  92. end
  93. def self.authorize_tokens_uri(tenant)
  94. URI::HTTPS.build(
  95. host: 'login.microsoftonline.com',
  96. path: "/#{tenant.presence || 'common'}/oauth2/v2.0/token",
  97. )
  98. end
  99. def self.refresh_token
  100. config = Setting.get('exchange_oauth')
  101. return {} if config.blank?
  102. return config if config[:created_at] >= 50.minutes.ago
  103. params = refresh_token_params(config)
  104. uri = refresh_token_uri(config)
  105. response = Net::HTTP.post_form(uri, params)
  106. if response.code != 200 && response.body.blank?
  107. HttpLog.create(
  108. direction: 'out',
  109. facility: 'EWS',
  110. url: uri,
  111. status: response.code,
  112. ip: nil,
  113. request: { content: params },
  114. response: { content: false },
  115. method: 'refresh_token',
  116. created_by_id: 1,
  117. updated_by_id: 1,
  118. )
  119. config_state(response.code)
  120. Rails.logger.error "Exchange refresh token: Request failed! (code: #{response.code})"
  121. raise "Request failed! (code: #{response.code})"
  122. end
  123. result = JSON.parse(response.body)
  124. if result['error'] && response.code != 200
  125. HttpLog.create(
  126. direction: 'out',
  127. facility: 'EWS',
  128. url: uri,
  129. status: response.code,
  130. ip: nil,
  131. request: { content: params },
  132. response: { content: result },
  133. method: 'refresh_token',
  134. created_by_id: 1,
  135. updated_by_id: 1,
  136. )
  137. config_state(response.code)
  138. Rails.logger.error "Exchange refresh token: Request failed! ERROR: #{result['error']} (#{result['error_description']}, params: #{params.to_json})"
  139. raise "Request failed! ERROR: #{result['error']} (#{result['error_description']})"
  140. end
  141. config = config.merge(result.symbolize_keys).merge(
  142. created_at: Time.zone.now,
  143. status: 200,
  144. )
  145. Setting.set('exchange_oauth', config)
  146. config
  147. end
  148. def self.config_state(status)
  149. config = Setting.get('exchange_oauth')
  150. config = config.merge(status: status)
  151. Setting.set('exchange_oauth', config)
  152. end
  153. def self.refresh_token_params(credentials)
  154. {
  155. client_id: credentials[:client_id],
  156. client_secret: credentials[:client_secret],
  157. refresh_token: credentials[:refresh_token],
  158. grant_type: 'refresh_token',
  159. }
  160. end
  161. def self.refresh_token_uri(credentials)
  162. tenant = credentials[:client_tenant].presence || 'common'
  163. URI::HTTPS.build(
  164. host: 'login.microsoftonline.com',
  165. path: "/#{tenant}/oauth2/v2.0/token",
  166. )
  167. end
  168. def self.user_info(id_token)
  169. split = id_token.split('.')[1]
  170. return if split.blank?
  171. JSON.parse(Base64.decode64(split)).symbolize_keys
  172. end
  173. end