2020-11-24 15:15:51 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class JiraConnect::EventsController < JiraConnect::ApplicationController
|
2021-01-03 14:25:43 +05:30
|
|
|
# See https://developer.atlassian.com/cloud/jira/software/app-descriptor/#lifecycle
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
skip_before_action :verify_atlassian_jwt!
|
2021-12-11 22:18:48 +05:30
|
|
|
before_action :verify_asymmetric_atlassian_jwt!
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
def installed
|
2022-05-07 20:08:51 +05:30
|
|
|
success = current_jira_installation ? update_installation : create_installation
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
if success
|
2020-11-24 15:15:51 +05:30
|
|
|
head :ok
|
|
|
|
else
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def uninstalled
|
2021-09-30 23:02:18 +05:30
|
|
|
if JiraConnectInstallations::DestroyService.execute(current_jira_installation, jira_connect_base_path, jira_connect_events_uninstalled_path)
|
2020-11-24 15:15:51 +05:30
|
|
|
head :ok
|
|
|
|
else
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
def create_installation
|
|
|
|
JiraConnectInstallation.new(create_params).save
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_installation
|
2023-03-04 22:38:38 +05:30
|
|
|
JiraConnectInstallations::UpdateService.execute(
|
|
|
|
current_jira_installation,
|
|
|
|
update_params
|
|
|
|
).success?
|
2022-05-07 20:08:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def create_params
|
|
|
|
transformed_params.permit(:client_key, :shared_secret, :base_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_params
|
|
|
|
transformed_params.permit(:shared_secret, :base_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def transformed_params
|
|
|
|
@transformed_params ||= params.transform_keys(&:underscore)
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
2021-11-18 22:05:49 +05:30
|
|
|
|
|
|
|
def verify_asymmetric_atlassian_jwt!
|
2022-07-16 23:28:13 +05:30
|
|
|
asymmetric_jwt = Atlassian::JiraConnect::Jwt::Asymmetric.new(auth_token, jwt_verification_claims)
|
2021-11-18 22:05:49 +05:30
|
|
|
|
|
|
|
return head :unauthorized unless asymmetric_jwt.valid?
|
|
|
|
|
|
|
|
@current_jira_installation = JiraConnectInstallation.find_by_client_key(asymmetric_jwt.iss_claim)
|
|
|
|
end
|
|
|
|
|
|
|
|
def jwt_verification_claims
|
|
|
|
{
|
2023-03-04 22:38:38 +05:30
|
|
|
aud: Gitlab.config.jira_connect.enforce_jira_base_url_https ? jira_connect_base_url(protocol: 'https') : jira_connect_base_url,
|
2022-05-07 20:08:51 +05:30
|
|
|
iss: transformed_params[:client_key],
|
2021-11-18 22:05:49 +05:30
|
|
|
qsh: Atlassian::Jwt.create_query_string_hash(request.url, request.method, jira_connect_base_url)
|
|
|
|
}
|
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|