summaryrefslogtreecommitdiff
path: root/app/controllers/jira_connect/application_controller.rb
blob: b9f0ea795e1bc889483718fecf2ebc01d98dcbc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# frozen_string_literal: true

class JiraConnect::ApplicationController < ApplicationController
  include Gitlab::Utils::StrongMemoize

  CORS_ALLOWED_METHODS = {
    '/-/jira_connect/oauth_application_id' => %i[GET OPTIONS],
    '/-/jira_connect/subscriptions/*' => %i[DELETE OPTIONS]
  }.freeze

  skip_before_action :authenticate_user!
  skip_before_action :verify_authenticity_token
  before_action :verify_atlassian_jwt!

  feature_category :integrations

  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!
    return if request.format.json? && jwt.verify_context_qsh_claim

    # Make sure `qsh` claim matches the current request
    render_403 unless jwt.verify_qsh_claim(request.url, request.method, jira_connect_base_url)
  end

  def atlassian_jwt_valid?
    return false unless installation_from_jwt

    # Verify JWT signature with our stored `shared_secret`
    jwt.valid?(installation_from_jwt.shared_secret)
  end

  def installation_from_jwt
    strong_memoize(:installation_from_jwt) do
      next unless jwt.iss_claim

      JiraConnectInstallation.find_by_client_key(jwt.iss_claim)
    end
  end

  def jira_user
    strong_memoize(:jira_user) do
      next unless installation_from_jwt
      next unless jwt.sub_claim

      # This only works for Jira Cloud installations.
      installation_from_jwt.client.user_info(jwt.sub_claim)
    end
  end

  def jwt
    strong_memoize(:jwt) do
      Atlassian::JiraConnect::Jwt::Symmetric.new(auth_token)
    end
  end

  def auth_token
    params[:jwt] || request.headers['Authorization']&.split(' ', 2)&.last
  end

  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
end