openid_config_parser 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 30a717590d0f58f1176e2a3b92e5c0746f1f9b7c3b402921774b0f27d7048b5b
4
- data.tar.gz: 3ec526dd218849b24adf260c4d3691c24dbe6e4cdf224e1347723367102a7676
3
+ metadata.gz: ef26b5603ae4f1d25d2b725a051751a8e217d589cab6c534cb8dce4b253f59c0
4
+ data.tar.gz: e583224a784aeccb0a0531b5e16874e9d6caa6d90e6716b8178fbf820c3e1ce2
5
5
  SHA512:
6
- metadata.gz: fbf2826054ab419f84c5d27be244dc4714d1fd9c2108192ac56e28e4269887eb9a0837c08303c036040daa5a5fe600fa2a45f2e426cbf00d73a8dad62d11f57b
7
- data.tar.gz: 836b5c206236a079a71ad330d11d7d741695693df5399ecc01a47838f483aad2995efff3eb162682fec1748b5c2bdfe6a69468a5ff9c47445b74923fe19ee6d1
6
+ metadata.gz: 9a8889bf8260c9e26118543dbd0b1f9cff8ca702b629eada04f501248d6ef3ee1cd90ce4467c3cf54581302bb8ce3ede93189a86569e8a7bbc69c4e3dafab3f9
7
+ data.tar.gz: e7c4ca292deacca681b3e9767c22b2df064fe8761cb7eb9cc209dd954f16bff023b98cbfb14c0436e294e2b9878963bbb3ff93bb1928c815023b3548b9b5bfb6
data/CHANGELOG.md CHANGED
@@ -1,10 +1,13 @@
1
1
  ## [Released]
2
2
 
3
- ## [0.1.2] - 2024-05-26
3
+ ## [0.2.0] - 2024-06-04
4
+ - Add retryable, add support to access response values as methods
5
+
6
+ ## [0.1.2] - 2024-05-25
4
7
  - Add support for ruby >=2.7
5
8
 
6
- ## [0.1.1] - 2024-05-26
9
+ ## [0.1.1] - 2024-05-25
7
10
  - Update readme
8
11
 
9
- ## [0.1.0] - 2024-05-26
12
+ ## [0.1.0] - 2024-05-25
10
13
  - 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.0"
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
metadata CHANGED
@@ -1,14 +1,14 @@
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.0
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
11
+ date: 2024-06-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |-
14
14
  `openid_config_parser` is a lightweight Ruby gem designed to fetch and parse