rakomi 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 13210bc74df8c1620a84afc20a77e21cb3879d51835e2f45f37f48babab57d7e
4
+ data.tar.gz: 7f516a97a20155005aae28f8f7e19719b73b9d7224a26860db6c2e16f6bcaedb
5
+ SHA512:
6
+ metadata.gz: 1ee00241854bb80dc5d87b5ac92863f976c386153b3cb4ed5bb662e6e358ed973b37797096bd9141358e641c9f447c7acb928ea0a394f98693ffc07548b4386c
7
+ data.tar.gz: 279682dbdd54889f304385ead9ab1d0ab0ed5d6e2945b6c8bae8c8c6f31d006486719256cd5a0b11c9a41f88fcf027a9bb9a2a3c4321f8496850b28087c6b3de
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0
4
+
5
+ - `Rakomi.discover(issuer)`: fetches the OpenID Connect discovery document and the JWKS of an issuer.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 CRE8EVE Sp. z o.o.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # rakomi
2
+
3
+ Ruby client for [Rakomi](https://rakomi.com). Version 0.1.0 does one thing: OpenID Connect discovery of a
4
+ Rakomi issuer. It uses only the Ruby standard library (`net/http`, `json`).
5
+
6
+ ## Install
7
+
8
+ ```sh
9
+ gem install rakomi
10
+ ```
11
+
12
+ ## Use
13
+
14
+ ```ruby
15
+ require "rakomi"
16
+
17
+ discovery = Rakomi.discover # issuer https://api.rakomi.com
18
+ discovery.issuer # => "https://api.rakomi.com"
19
+ discovery.authorization_endpoint
20
+ discovery.token_endpoint
21
+ discovery.userinfo_endpoint
22
+ discovery.jwks_uri
23
+ discovery.keys # the keys of the issuer's JWKS, as hashes
24
+ discovery.configuration # the whole discovery document
25
+ ```
26
+
27
+ `Rakomi.discover(issuer = "https://api.rakomi.com", open_timeout: 5, read_timeout: 5)` fetches
28
+ `<issuer>/.well-known/openid-configuration` and then the JWKS at its `jwks_uri`. It raises
29
+ `Rakomi::DiscoveryError` when:
30
+
31
+ - the issuer, `jwks_uri` or an endpoint URL is not `https`;
32
+ - a response is not HTTP 200 (redirects are not followed), is not a JSON object, or is larger than 1 MiB;
33
+ - the document names an issuer other than exactly the one passed in (a trailing slash counts);
34
+ - the document lacks `issuer`, `authorization_endpoint`, `token_endpoint`, `jwks_uri`,
35
+ `response_types_supported`, `subject_types_supported` or `id_token_signing_alg_values_supported`;
36
+ - the JWKS has no keys;
37
+ - a connection or read times out.
38
+
39
+ TLS certificates are verified. This gem does not verify tokens; for signing users in to a Rack or Rails
40
+ app, see [`omniauth-rakomi`](../omniauth-rakomi/).
41
+
42
+ ## License
43
+
44
+ MIT, see [LICENSE](LICENSE).
@@ -0,0 +1,127 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "uri"
5
+
6
+ module Rakomi
7
+ # The OpenID Provider metadata of an issuer and the JSON Web Key Set it publishes.
8
+ Discovery = Struct.new(:configuration, :jwks, keyword_init: true) do
9
+ def issuer
10
+ configuration.fetch("issuer")
11
+ end
12
+
13
+ def authorization_endpoint
14
+ configuration.fetch("authorization_endpoint")
15
+ end
16
+
17
+ def token_endpoint
18
+ configuration.fetch("token_endpoint")
19
+ end
20
+
21
+ def userinfo_endpoint
22
+ configuration["userinfo_endpoint"]
23
+ end
24
+
25
+ def jwks_uri
26
+ configuration.fetch("jwks_uri")
27
+ end
28
+
29
+ def keys
30
+ jwks.fetch("keys")
31
+ end
32
+ end
33
+
34
+ # Fetches and checks OpenID Connect Discovery 1.0 metadata.
35
+ module Discoverer
36
+ WELL_KNOWN_PATH = "/.well-known/openid-configuration"
37
+
38
+ REQUIRED_FIELDS = %w[
39
+ issuer
40
+ authorization_endpoint
41
+ token_endpoint
42
+ jwks_uri
43
+ response_types_supported
44
+ subject_types_supported
45
+ id_token_signing_alg_values_supported
46
+ ].freeze
47
+
48
+ URL_FIELDS = %w[
49
+ authorization_endpoint
50
+ token_endpoint
51
+ userinfo_endpoint
52
+ jwks_uri
53
+ registration_endpoint
54
+ end_session_endpoint
55
+ revocation_endpoint
56
+ introspection_endpoint
57
+ ].freeze
58
+
59
+ module_function
60
+
61
+ def call(issuer, http:)
62
+ issuer_uri = parse_https(issuer, "issuer")
63
+ raise DiscoveryError, "issuer must not carry a query or fragment" if issuer_uri.query || issuer_uri.fragment
64
+
65
+ configuration = fetch_json(http, configuration_uri(issuer_uri), "openid-configuration")
66
+ check_configuration(configuration, issuer)
67
+
68
+ jwks = fetch_json(http, parse_https(configuration["jwks_uri"], "jwks_uri"), "jwks")
69
+ check_jwks(jwks)
70
+
71
+ Discovery.new(configuration: configuration.freeze, jwks: jwks.freeze)
72
+ end
73
+
74
+ def configuration_uri(issuer_uri)
75
+ uri = issuer_uri.dup
76
+ uri.path = uri.path.sub(%r{/\z}, "") + WELL_KNOWN_PATH
77
+ uri
78
+ end
79
+
80
+ def parse_https(value, name)
81
+ raise DiscoveryError, "#{name} must be a string" unless value.is_a?(String)
82
+
83
+ uri = URI.parse(value)
84
+ unless uri.is_a?(URI::HTTPS) && uri.host && !uri.host.empty?
85
+ raise DiscoveryError, "#{name} must be an https URL: #{value.inspect}"
86
+ end
87
+ raise DiscoveryError, "#{name} must not carry user information" if uri.userinfo
88
+
89
+ uri
90
+ rescue URI::InvalidURIError
91
+ raise DiscoveryError, "#{name} is not a valid URL: #{value.inspect}"
92
+ end
93
+
94
+ def fetch_json(http, uri, what)
95
+ status, body = http.get(uri)
96
+ raise DiscoveryError, "#{what} at #{uri} answered HTTP #{status}" unless status == 200
97
+
98
+ document = JSON.parse(body)
99
+ raise DiscoveryError, "#{what} at #{uri} is not a JSON object" unless document.is_a?(Hash)
100
+
101
+ document
102
+ rescue JSON::ParserError
103
+ raise DiscoveryError, "#{what} at #{uri} is not valid JSON"
104
+ end
105
+
106
+ def check_configuration(configuration, issuer)
107
+ missing = REQUIRED_FIELDS.reject { |field| configuration.key?(field) }
108
+ raise DiscoveryError, "openid-configuration lacks #{missing.join(", ")}" unless missing.empty?
109
+
110
+ unless configuration["issuer"] == issuer
111
+ raise DiscoveryError,
112
+ "openid-configuration names issuer #{configuration["issuer"].inspect}, expected #{issuer.inspect}"
113
+ end
114
+
115
+ URL_FIELDS.each do |field|
116
+ parse_https(configuration[field], field) if configuration.key?(field)
117
+ end
118
+ end
119
+
120
+ def check_jwks(jwks)
121
+ keys = jwks["keys"]
122
+ raise DiscoveryError, "jwks has no keys array" unless keys.is_a?(Array)
123
+ raise DiscoveryError, "jwks has no keys" if keys.empty?
124
+ raise DiscoveryError, "jwks holds an entry that is not an object" unless keys.all?(Hash)
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "openssl"
5
+ require "uri"
6
+
7
+ module Rakomi
8
+ # Minimal HTTPS GET client on Net::HTTP: certificate verification on, no redirects followed,
9
+ # timeouts on every phase and a cap on the response size.
10
+ class HTTPClient
11
+ DEFAULT_TIMEOUT = 5
12
+ MAX_BODY_BYTES = 1_048_576
13
+
14
+ def initialize(open_timeout: DEFAULT_TIMEOUT, read_timeout: DEFAULT_TIMEOUT, max_body_bytes: MAX_BODY_BYTES)
15
+ @open_timeout = open_timeout
16
+ @read_timeout = read_timeout
17
+ @max_body_bytes = max_body_bytes
18
+ end
19
+
20
+ # Returns [status, body] for an https URI.
21
+ def get(uri)
22
+ raise DiscoveryError, "refusing a non-https URL: #{uri}" unless uri.is_a?(URI::HTTPS)
23
+
24
+ Net::HTTP.start(uri.host, uri.port, **connection_options) do |http|
25
+ request = Net::HTTP::Get.new(uri)
26
+ request["Accept"] = "application/json"
27
+ http.request(request) do |response|
28
+ return [response.code.to_i, read_body(response, uri)]
29
+ end
30
+ end
31
+ rescue Net::OpenTimeout, Net::ReadTimeout, Net::WriteTimeout
32
+ raise DiscoveryError, "timed out fetching #{uri}"
33
+ rescue OpenSSL::SSL::SSLError, SystemCallError, SocketError, IOError, EOFError => e
34
+ raise DiscoveryError, "could not fetch #{uri}: #{e.class}"
35
+ end
36
+
37
+ private
38
+
39
+ def connection_options
40
+ {
41
+ use_ssl: true,
42
+ verify_mode: OpenSSL::SSL::VERIFY_PEER,
43
+ open_timeout: @open_timeout,
44
+ read_timeout: @read_timeout,
45
+ write_timeout: @read_timeout,
46
+ ssl_timeout: @open_timeout
47
+ }
48
+ end
49
+
50
+ def read_body(response, uri)
51
+ body = +""
52
+ response.read_body do |chunk|
53
+ body << chunk
54
+ raise DiscoveryError, "response from #{uri} exceeds #{@max_body_bytes} bytes" if body.bytesize > @max_body_bytes
55
+ end
56
+ body
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rakomi
4
+ VERSION = "0.1.0"
5
+ end
data/lib/rakomi.rb ADDED
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "rakomi/version"
4
+
5
+ # Ruby client for Rakomi.
6
+ module Rakomi
7
+ DEFAULT_ISSUER = "https://api.rakomi.com"
8
+
9
+ class Error < StandardError; end
10
+
11
+ class DiscoveryError < Error; end
12
+
13
+ # Fetches <issuer>/.well-known/openid-configuration and the JWKS at its jwks_uri.
14
+ #
15
+ # Both URLs must be https. The document must name exactly the issuer given here and carry the
16
+ # fields OpenID Connect Discovery 1.0 requires. Raises Rakomi::DiscoveryError otherwise.
17
+ #
18
+ # discovery = Rakomi.discover
19
+ # discovery.token_endpoint # => "https://api.rakomi.com/oauth/token"
20
+ # discovery.keys # => [{ "kty" => "RSA", "kid" => "...", ... }]
21
+ def self.discover(issuer = DEFAULT_ISSUER, open_timeout: HTTPClient::DEFAULT_TIMEOUT,
22
+ read_timeout: HTTPClient::DEFAULT_TIMEOUT, http: nil)
23
+ http ||= HTTPClient.new(open_timeout: open_timeout, read_timeout: read_timeout)
24
+ Discoverer.call(issuer, http: http)
25
+ end
26
+ end
27
+
28
+ require_relative "rakomi/http_client"
29
+ require_relative "rakomi/discovery"
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rakomi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - CRE8EVE Sp. z o.o.
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Fetches and checks the OpenID Connect discovery document and the JSON
13
+ Web Key Set of a Rakomi issuer over HTTPS, using only the Ruby standard library.
14
+ email:
15
+ - security@rakomi.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - CHANGELOG.md
21
+ - LICENSE
22
+ - README.md
23
+ - lib/rakomi.rb
24
+ - lib/rakomi/discovery.rb
25
+ - lib/rakomi/http_client.rb
26
+ - lib/rakomi/version.rb
27
+ homepage: https://rakomi.com
28
+ licenses:
29
+ - MIT
30
+ metadata:
31
+ homepage_uri: https://rakomi.com
32
+ source_code_uri: https://github.com/rakomidev/rakomi-ruby/tree/main/rakomi
33
+ bug_tracker_uri: https://github.com/rakomidev/rakomi-ruby/issues
34
+ changelog_uri: https://github.com/rakomidev/rakomi-ruby/blob/main/rakomi/CHANGELOG.md
35
+ rubygems_mfa_required: 'true'
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '3.1'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.6.9
51
+ specification_version: 4
52
+ summary: 'Ruby client for Rakomi: OpenID Connect discovery of a Rakomi issuer.'
53
+ test_files: []