summaryrefslogtreecommitdiff
path: root/lib/atlassian/jira_connect/jwt/symmetric.rb
blob: 61e5bd923a47bdb69feb86fdca7fa1491c425d99 (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
# frozen_string_literal: true

module Atlassian
  module JiraConnect
    module Jwt
      class Symmetric
        include Gitlab::Utils::StrongMemoize

        CONTEXT_QSH_STRING = 'context-qsh'

        def initialize(jwt)
          @jwt = jwt
        end

        def iss_claim
          jwt_headers['iss']
        end

        def sub_claim
          jwt_headers['sub']
        end

        def valid?(shared_secret)
          Atlassian::Jwt.decode(@jwt, shared_secret).present?
        rescue JWT::DecodeError
          false
        end

        def verify_qsh_claim(url_with_query, method, url)
          qsh_claim == Atlassian::Jwt.create_query_string_hash(url_with_query, method, url)
        rescue StandardError
          false
        end

        def verify_context_qsh_claim
          qsh_claim == CONTEXT_QSH_STRING
        end

        private

        def qsh_claim
          jwt_headers['qsh']
        end

        def jwt_headers
          strong_memoize(:jwt_headers) do
            Atlassian::Jwt.decode(@jwt, nil, false).first
          rescue JWT::DecodeError
            {}
          end
        end
      end
    end
  end
end