debian-mirror-gitlab/app/controllers/jira_connect/application_controller.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

90 lines
2.2 KiB
Ruby
Raw Normal View History

2020-11-24 15:15:51 +05:30
# frozen_string_literal: true
class JiraConnect::ApplicationController < ApplicationController
include Gitlab::Utils::StrongMemoize
2023-01-13 00:05:48 +05:30
CORS_ALLOWED_METHODS = {
'/-/jira_connect/oauth_application_id' => %i[GET OPTIONS],
'/-/jira_connect/subscriptions/*' => %i[DELETE OPTIONS]
}.freeze
2020-11-24 15:15:51 +05:30
skip_before_action :authenticate_user!
skip_before_action :verify_authenticity_token
before_action :verify_atlassian_jwt!
2021-01-03 14:25:43 +05:30
feature_category :integrations
2020-11-24 15:15:51 +05:30
attr_reader :current_jira_installation
private
def verify_atlassian_jwt!
return render_403 unless atlassian_jwt_valid?
@current_jira_installation = installation_from_jwt
end
def verify_qsh_claim!
2022-07-16 23:28:13 +05:30
return if request.format.json? && jwt.verify_context_qsh_claim
2022-06-21 17:19:12 +05:30
2020-11-24 15:15:51 +05:30
# Make sure `qsh` claim matches the current request
2022-07-16 23:28:13 +05:30
render_403 unless jwt.verify_qsh_claim(request.url, request.method, jira_connect_base_url)
2020-11-24 15:15:51 +05:30
end
def atlassian_jwt_valid?
return false unless installation_from_jwt
# Verify JWT signature with our stored `shared_secret`
2022-07-16 23:28:13 +05:30
jwt.valid?(installation_from_jwt.shared_secret)
2020-11-24 15:15:51 +05:30
end
def installation_from_jwt
strong_memoize(:installation_from_jwt) do
2022-07-16 23:28:13 +05:30
next unless jwt.iss_claim
2021-09-04 01:27:46 +05:30
2022-07-16 23:28:13 +05:30
JiraConnectInstallation.find_by_client_key(jwt.iss_claim)
2021-09-04 01:27:46 +05:30
end
end
def jira_user
strong_memoize(:jira_user) do
next unless installation_from_jwt
2022-07-16 23:28:13 +05:30
next unless jwt.sub_claim
2021-09-04 01:27:46 +05:30
# This only works for Jira Cloud installations.
2022-07-16 23:28:13 +05:30
installation_from_jwt.client.user_info(jwt.sub_claim)
2020-11-24 15:15:51 +05:30
end
end
2022-07-16 23:28:13 +05:30
def jwt
strong_memoize(:jwt) do
Atlassian::JiraConnect::Jwt::Symmetric.new(auth_token)
end
2020-11-24 15:15:51 +05:30
end
def auth_token
2022-07-16 23:28:13 +05:30
params[:jwt] || request.headers['Authorization']&.split(' ', 2)&.last
2020-11-24 15:15:51 +05:30
end
2023-01-13 00:05:48 +05:30
def cors_allowed_methods
CORS_ALLOWED_METHODS[resource]
end
def resource
request.path.gsub(%r{/\d+$}, '/*')
end
def set_cors_headers
return unless allow_cors_request?
response.set_header('Access-Control-Allow-Origin', Gitlab::CurrentSettings.jira_connect_proxy_url)
response.set_header('Access-Control-Allow-Methods', cors_allowed_methods.join(', '))
end
def allow_cors_request?
return false if cors_allowed_methods.nil?
!Gitlab.com? && Gitlab::CurrentSettings.jira_connect_proxy_url.present?
end
2020-11-24 15:15:51 +05:30
end