Module: OpenidConfigParser

Defined in:
lib/openid_config_parser.rb,
lib/openid_config_parser/version.rb

Overview

OpenidConfigParser is a module that fetches and parses OpenID Connect configuration data from a specified endpoint URL and returns a Hash object. It includes error handling to manage various issues that might occur during the HTTP request and JSON parsing process.

Defined Under Namespace

Classes: Config, Error

Constant Summary collapse

VERSION =
"0.2.3"

Class Method Summary collapse

Class Method Details

.deep_symbolize_keys(hash) ⇒ Object

Recursively converts keys of a hash to symbols while retaining the original string keys.



41
42
43
44
45
46
47
48
49
# File 'lib/openid_config_parser.rb', line 41

def deep_symbolize_keys(hash)
  result = {}
  hash.each do |key, value|
    sym_key = key.to_sym
    result[sym_key] = value.is_a?(Hash) ? deep_symbolize_keys(value) : value
    result[key] = result[sym_key] # Add the string key as well
  end
  result
end

.fetch_openid_configuration(endpoint_url) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/openid_config_parser.rb', line 67

def fetch_openid_configuration(endpoint_url)
  Retryable.retryable(tries: 3, on: [Net::ReadTimeout, Net::OpenTimeout]) do
    response = Net::HTTP.get(URI(endpoint_url))
    config = JSON.parse(response)
    symbolized_config = deep_symbolize_keys(config)
    return Config.new(symbolized_config)
  end
rescue JSON::ParserError => e
  raise Error, "Failed to parse JSON response: #{e.message}"
rescue URI::InvalidURIError => e
  raise Error, "Invalid URL provided: #{e.message}"
rescue Net::OpenTimeout, Net::ReadTimeout => e
  raise Error, "Network timeout error: #{e.message}"
rescue SocketError => e
  raise Error, "Failed to open TCP connection: #{e.message}"
rescue StandardError => e
  raise Error, "An unexpected error occurred: #{e.message}"
end

.fetch_user_info(access_token) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/openid_config_parser.rb', line 51

def (access_token)
  Retryable.retryable(tries: 3, on: [Net::ReadTimeout, Net::OpenTimeout]) do
    response = HTTParty.get(ENV["CLOUDFLARE_USERINFO_ENDPOINT"], {
                              headers: {
                                "Authorization" => "Bearer #{access_token}",
                                "Content-Type" => "application/json"
                              },
                              timeout: 10
                            })
    return response.parsed_response
  end
rescue Net::ReadTimeout, Net::OpenTimeout => e
  puts "Timeout error: #{e.message}"
  nil
end