2020-11-24 15:15:51 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class JiraConnect::ApplicationController < ApplicationController
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
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
|
|
|
|
end
|