File: sign_key_fixture_helper.rb

package info (click to toggle)
ruby-json-jwt 1.16.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 324 kB
  • sloc: ruby: 2,858; makefile: 4
file content (66 lines) | stat: -rw-r--r-- 1,340 bytes parent folder | download | duplicates (2)
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
module SignKeyFixtureHelper
  def shared_secret
    'shared-secret'
  end

  def pem_file(file_name)
    File.new pem_file_path(file_name)
  end

  def pem_file_path(file_name)
    File.join(
      File.dirname(__FILE__),
      "../fixtures/#{file_name}.pem"
    )
  end

  def der_file_path(file_name)
    File.join(
      File.dirname(__FILE__),
      "../fixtures/#{file_name}.der"
    )
  end

  def private_key(algorithm = :rsa, digest_length: 256, curve_name: nil)
    case algorithm
    when :rsa
      OpenSSL::PKey::RSA.new(
        pem_file("#{algorithm}/private_key"),
        'pass-phrase'
      )
    when :ecdsa
      OpenSSL::PKey::EC.new(
        pem_file(
          File.join([
            algorithm,
            digest_length,
            curve_name,
            'private_key',
          ].compact.collect(&:to_s))
        )
      )
    end
  end

  def public_key(algorithm = :rsa, digest_length: 256, curve_name: nil)
    case algorithm
    when :rsa
      OpenSSL::PKey::RSA.new(
        pem_file("#{algorithm}/public_key")
      )
    when :ecdsa
      OpenSSL::PKey::EC.new(
        pem_file(
          File.join([
            algorithm,
            digest_length,
            curve_name,
            'public_key',
          ].compact.collect(&:to_s))
        )
      )
    end
  end
end

include SignKeyFixtureHelper