openid_config_parser 0.1.2 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 30a717590d0f58f1176e2a3b92e5c0746f1f9b7c3b402921774b0f27d7048b5b
4
- data.tar.gz: 3ec526dd218849b24adf260c4d3691c24dbe6e4cdf224e1347723367102a7676
3
+ metadata.gz: b74e43c69e012a96a6966747a44ee23a9ebf5818e06267ee70c2fa7525ba8d65
4
+ data.tar.gz: 16a2c4c940d28e7b883685adcfbe165b2f9cab5d9e003ebd25a66b495a20d745
5
5
  SHA512:
6
- metadata.gz: fbf2826054ab419f84c5d27be244dc4714d1fd9c2108192ac56e28e4269887eb9a0837c08303c036040daa5a5fe600fa2a45f2e426cbf00d73a8dad62d11f57b
7
- data.tar.gz: 836b5c206236a079a71ad330d11d7d741695693df5399ecc01a47838f483aad2995efff3eb162682fec1748b5c2bdfe6a69468a5ff9c47445b74923fe19ee6d1
6
+ metadata.gz: e5bc59f236f6d88942ee5596fcc2ae0746642357de5223ab1cc47ed111d493e09bf178da2d2e1f103e59e749f13ba6e5c61c5f637a3c65821387ad721adf8ab6
7
+ data.tar.gz: 7b9c246d119c738c45c10ff659e420e8e63ac6454c84ee03030b5beba2cd5e65122bc30fc1e9ca2dde432c7245aa6fe6d9d466001df28d454f2eb9de8ec36225
data/CHANGELOG.md CHANGED
@@ -1,10 +1,16 @@
1
1
  ## [Released]
2
2
 
3
- ## [0.1.2] - 2024-05-26
3
+ ## [0.2.1] - 2024-06-05
4
+ - Fix reference to retryable
5
+
6
+ ## [0.2.0] - 2024-06-04
7
+ - Add retryable, add support to access response values as methods
8
+
9
+ ## [0.1.2] - 2024-05-25
4
10
  - Add support for ruby >=2.7
5
11
 
6
- ## [0.1.1] - 2024-05-26
12
+ ## [0.1.1] - 2024-05-25
7
13
  - Update readme
8
14
 
9
- ## [0.1.0] - 2024-05-26
15
+ ## [0.1.0] - 2024-05-25
10
16
  - Initial release
data/README.md CHANGED
@@ -35,11 +35,11 @@ class ApplicationController < ActionController::Base
35
35
  config = OpenidConfigParser.fetch_openid_configuration(endpoint)
36
36
 
37
37
  if config
38
- issuer = config[:issuer]
39
- auth_endpoint = config[:authorization_endpoint]
40
- token_endpoint = config[:token_endpoint]
41
- jwks_uri = config[:]
42
- userinfo_endpoint = config[:userinfo_endpoint]
38
+ issuer = config.issuer # or config[:issuer]
39
+ auth_endpoint = config.authorization_endpoint # or config[:authorization_endpoint]
40
+ token_endpoint = config.token_endpoint # or config[:token_endpoint]
41
+ jwks_uri = config.jwks_uri # or config[:jwks_uri]
42
+ userinfo_endpoint = config.userinfo_endpoint # or config[:userinfo_endpoint]
43
43
  # and so on
44
44
  else
45
45
  Rails.logger.error "Failed to fetch OpenID configuration"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenidConfigParser
4
- VERSION = "0.1.2"
4
+ VERSION = "0.2.1"
5
5
  end
@@ -4,6 +4,8 @@ require_relative "openid_config_parser/version"
4
4
  require "net/http"
5
5
  require "json"
6
6
  require "uri"
7
+ require "retryable"
8
+ require "ostruct"
7
9
 
8
10
  # OpenidConfigParser is a module that fetches and parses OpenID Connect
9
11
  # configuration data from a specified endpoint URL and returns a Hash object.
@@ -12,6 +14,28 @@ require "uri"
12
14
  module OpenidConfigParser
13
15
  class Error < StandardError; end
14
16
 
17
+ # Config is a class that extends OpenStruct to provide a flexible object
18
+ # for accessing OpenID Connect configuration data. It allows access to
19
+ # configuration values both as methods (e.g., config.issuer) and as hash
20
+ # keys (e.g., config[:issuer]).
21
+ #
22
+ # Example usage:
23
+ # config = OpenidConfigParser.fetch_openid_configuration(endpoint)
24
+ # puts config.issuer # Method access
25
+ # puts config[:issuer] # Hash-like access
26
+ #
27
+ # This class is designed to be used internally by the OpenidConfigParser module
28
+ # and is not intended to be instantiated directly by users.
29
+ class Config < OpenStruct
30
+ def [](key)
31
+ send(key)
32
+ end
33
+
34
+ def []=(key, value)
35
+ send("#{key}=", value)
36
+ end
37
+ end
38
+
15
39
  class << self
16
40
  # Recursively converts keys of a hash to symbols while retaining the original string keys.
17
41
  def deep_symbolize_keys(hash)
@@ -24,11 +48,29 @@ module OpenidConfigParser
24
48
  result
25
49
  end
26
50
 
51
+ def fetch_user_info(access_token)
52
+ Retryable.retryable(tries: 3, on: [Net::ReadTimeout, Net::OpenTimeout]) do
53
+ response = HTTParty.get(ENV["CLOUDFLARE_USERINFO_ENDPOINT"], {
54
+ headers: {
55
+ "Authorization" => "Bearer #{access_token}",
56
+ "Content-Type" => "application/json"
57
+ },
58
+ timeout: 10
59
+ })
60
+ return response.parsed_response
61
+ end
62
+ rescue Net::ReadTimeout, Net::OpenTimeout => e
63
+ puts "Timeout error: #{e.message}"
64
+ nil
65
+ end
66
+
27
67
  def fetch_openid_configuration(endpoint_url)
28
- uri = URI(endpoint_url)
29
- response = Net::HTTP.get(uri)
30
- config = JSON.parse(response)
31
- deep_symbolize_keys(config)
68
+ Retryable.retryable(tries: 3, on: [Net::ReadTimeout, Net::OpenTimeout]) do
69
+ response = Net::HTTP.get(URI(endpoint_url))
70
+ config = JSON.parse(response)
71
+ symbolized_config = deep_symbolize_keys(config)
72
+ return Config.new(symbolized_config)
73
+ end
32
74
  rescue JSON::ParserError => e
33
75
  raise Error, "Failed to parse JSON response: #{e.message}"
34
76
  rescue URI::InvalidURIError => e
@@ -34,7 +34,7 @@ Gem::Specification.new do |spec|
34
34
  spec.require_paths = ["lib"]
35
35
 
36
36
  # Uncomment to register a new dependency of your gem
37
- # spec.add_dependency "example-gem", "~> 1.0"
37
+ spec.add_dependency "retryable"
38
38
 
39
39
  # For more information and examples about making a new gem, check out our
40
40
  # guide at: https://bundler.io/guides/creating_gem.html
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openid_config_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Suleyman Musayev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-05-25 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2024-06-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: retryable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description: |-
14
28
  `openid_config_parser` is a lightweight Ruby gem designed to fetch and parse
15
29
  OpenID Connect configuration data from any specified endpoint URL.