Module: OmniauthOpenidFederation::Tasks::ClientKeysPreparer

Defined in:
lib/omniauth_openid_federation/tasks/client_keys_preparer.rb

Class Method Summary collapse

Class Method Details

.generate_separate_keys(output_path) ⇒ Object



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
# File 'lib/omniauth_openid_federation/tasks/client_keys_preparer.rb', line 57

def self.generate_separate_keys(output_path)
  signing_private_key = OpenSSL::PKey::RSA.new(2048)
  encryption_private_key = OpenSSL::PKey::RSA.new(2048)

  signing_jwk_hash = Utils.rsa_key_to_jwk(signing_private_key, use: "sig")
  encryption_jwk_hash = Utils.rsa_key_to_jwk(encryption_private_key, use: "enc")

  # Remove private key components and add 'use' field
  signing_public_jwk = signing_jwk_hash.reject { |k, _v| %w[d p q dp dq qi].include?(k.to_s) }.merge("use" => "sig")
  encryption_public_jwk = encryption_jwk_hash.reject { |k, _v| %w[d p q dp dq qi].include?(k.to_s) }.merge("use" => "enc")

  jwks = {keys: [signing_public_jwk, encryption_public_jwk]}

  # Save private keys
  signing_key_path = File.join(output_path, "client-signing-private-key.pem")
  encryption_key_path = File.join(output_path, "client-encryption-private-key.pem")

  File.write(signing_key_path, signing_private_key.to_pem)
  File.write(encryption_key_path, encryption_private_key.to_pem)
  File.chmod(0o600, signing_key_path)
  File.chmod(0o600, encryption_key_path)

  # Save public JWKS
  public_jwks_path = File.join(output_path, "client-jwks.json")
  File.write(public_jwks_path, JSON.pretty_generate(jwks))

  {
    signing_key_path: signing_key_path,
    encryption_key_path: encryption_key_path,
    public_jwks_path: public_jwks_path,
    jwks: jwks
  }
end

.generate_single_key(output_path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/omniauth_openid_federation/tasks/client_keys_preparer.rb', line 33

def self.generate_single_key(output_path)
  private_key = OpenSSL::PKey::RSA.new(2048)
  jwk_hash = Utils.rsa_key_to_jwk(private_key, use: "sig")

  # Remove private key components and 'use' field for backward compatibility
  public_jwk = jwk_hash.reject { |k, _v| %w[d p q dp dq qi use].include?(k.to_s) }
  jwks = {keys: [public_jwk]}

  # Save private key
  private_key_path = File.join(output_path, "client-private-key.pem")
  File.write(private_key_path, private_key.to_pem)
  File.chmod(0o600, private_key_path)

  # Save public JWKS
  public_jwks_path = File.join(output_path, "client-jwks.json")
  File.write(public_jwks_path, JSON.pretty_generate(jwks))

  {
    private_key_path: private_key_path,
    public_jwks_path: public_jwks_path,
    jwks: jwks
  }
end

.prepare(key_type:, output_dir:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/omniauth_openid_federation/tasks/client_keys_preparer.rb', line 10

def self.prepare(key_type:, output_dir:)
  unless %w[single separate].include?(key_type)
    raise ArgumentError, "Invalid key_type: #{key_type}. Valid options: 'single' or 'separate'"
  end

  output_path = PathResolver.resolve(output_dir)

  # Create output directory if it doesn't exist
  FileUtils.mkdir_p(output_path) unless File.directory?(output_path)

  result = if key_type == "single"
    generate_single_key(output_path)
  else
    generate_separate_keys(output_path)
  end

  {
    success: true,
    output_path: output_path,
    **result
  }
end