Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Slashek committed May 1, 2023
2 parents 64a3bd6 + 2fdafc3 commit 8b96f7d
Show file tree
Hide file tree
Showing 25 changed files with 204 additions and 125 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/spec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Spec

on:
push:
branches:
- master
pull_request:

permissions:
contents: read

jobs:
spec:
strategy:
matrix:
os: ['ubuntu-20.04', 'ubuntu-22.04']
ruby-version: ['3.1', '3.2']
include:
- os: 'ubuntu-20.04'
ruby-version: '3.0'
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true
- name: Run Specs
run: bundle exec rake spec
8 changes: 0 additions & 8 deletions .travis.yml

This file was deleted.

23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## [Unreleased]

## [2.2.0] - 2022-10-11

### Changed

- automatic json response decoding by @nov in https://github.com/nov/openid_connect/pull/77

## [2.1.0] - 2022-10-10

### Changed

- mTLS access token by @nov in https://github.com/nov/openid_connect/pull/76

## [2.0.0] - 2022-10-09

### Added

- start recording CHANGELOG

### Changed

- replace httpclient with faraday v2 by @nov in https://github.com/nov/openid_connect/pull/75
2 changes: 0 additions & 2 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

OpenID Connect Server & Client Library

{<img src="https://secure.travis-ci.org/nov/openid_connect.png" />}[https://travis-ci.org/nov/openid_connect]

== Installation

gem install openid_connect
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.3.1
2.2.0
22 changes: 10 additions & 12 deletions lib/openid_connect.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'json'
require 'logger'
require 'faraday'
require 'faraday/follow_redirects'
require 'swd'
require 'webfinger'
require 'active_model'
Expand Down Expand Up @@ -64,17 +66,14 @@ def self.debug(&block)
self.debugging = false

def self.http_client
_http_client_ = HTTPClient.new(
agent_name: "OpenIDConnect (#{VERSION})"
)

# NOTE: httpclient gem seems stopped maintaining root certtificate set, use OS default.
_http_client_.ssl_config.clear_cert_store
_http_client_.ssl_config.cert_store.set_default_paths

_http_client_.request_filter << Debugger::RequestFilter.new if debugging?
http_config.try(:call, _http_client_)
_http_client_
Faraday.new(headers: {user_agent: "OpenIDConnect (#{VERSION})"}) do |faraday|
faraday.request :url_encoded
faraday.request :json
faraday.response :json
faraday.adapter Faraday.default_adapter
http_config&.call(faraday)
faraday.response :logger, OpenIDConnect.logger, {bodies: true} if debugging?
end
end
def self.http_config(&block)
@sub_protocols.each do |klass|
Expand All @@ -100,4 +99,3 @@ def self.validate_discovery_issuer
require 'openid_connect/jwtnizable'
require 'openid_connect/connect_object'
require 'openid_connect/discovery'
require 'openid_connect/debugger'
13 changes: 11 additions & 2 deletions lib/openid_connect/access_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ def userinfo!(params = {})
ResponseObject::UserInfo.new hash
end

def to_mtls(attributes = {})
(required_attributes + optional_attributes).each do |key|
attributes[key] = self.send(key)
end
MTLS.new attributes
end

private

def requires_token?
Expand All @@ -28,7 +35,7 @@ def resource_request
res = yield
case res.status
when 200
JSON.parse(res.body).with_indifferent_access
res.body.with_indifferent_access
when 400
raise BadRequest.new('API Access Faild', res)
when 401
Expand All @@ -40,4 +47,6 @@ def resource_request
end
end
end
end
end

require 'openid_connect/access_token/mtls'
9 changes: 9 additions & 0 deletions lib/openid_connect/access_token/mtls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module OpenIDConnect
class AccessToken::MTLS < AccessToken
def initialize(attributes = {})
super
http_client.ssl.client_key = attributes[:private_key] || client.private_key
http_client.ssl.client_cert = attributes[:certificate] || client.certificate
end
end
end
4 changes: 1 addition & 3 deletions lib/openid_connect/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ def setup_required_scope(scopes)
end

def handle_success_response(response)
token_hash = JSON.parse(response.body).with_indifferent_access
token_hash = response.body.with_indifferent_access
token_type = (@forced_token_type || token_hash[:token_type]).try(:downcase)
case token_type
when 'bearer'
AccessToken.new token_hash.merge(client: self)
else
raise Exception.new("Unexpected Token Type: #{token_type}")
end
rescue JSON::ParserError
raise Exception.new("Unknown Token Type")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/openid_connect/client/registrar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def handle_response
end

def handle_success_response(response)
credentials = JSON.parse(response.body).with_indifferent_access
credentials = response.body.with_indifferent_access
Client.new(
identifier: credentials[:client_id],
secret: credentials[:client_secret],
Expand Down
3 changes: 0 additions & 3 deletions lib/openid_connect/debugger.rb

This file was deleted.

28 changes: 0 additions & 28 deletions lib/openid_connect/debugger/request_filter.rb

This file was deleted.

9 changes: 6 additions & 3 deletions lib/openid_connect/discovery/provider/config/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,15 @@ def validate!
end

def jwks
@jwks ||= JSON.parse(
OpenIDConnect.http_client.get_content(jwks_uri)
).with_indifferent_access
@jwks ||= OpenIDConnect.http_client.get(jwks_uri).body.with_indifferent_access
JSON::JWK::Set.new @jwks[:keys]
end

def jwk(kid)
@jwks ||= {}
@jwks[kid] ||= JSON::JWK::Set::Fetcher.fetch(jwks_uri, kid: kid)
end

def public_keys
@public_keys ||= jwks.collect(&:to_key)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/openid_connect/request_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def decode(jwt_string, key = nil)
end

def fetch(request_uri, key = nil)
jwt_string = OpenIDConnect.http_client.get_content(request_uri)
jwt_string = OpenIDConnect.http_client.get(request_uri).body
decode jwt_string, key
end
end
Expand Down
11 changes: 8 additions & 3 deletions lib/openid_connect/response_object/id_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,16 @@ def left_half_hash_of(string, hash_length)
end

class << self
def decode(jwt_string, key)
if key == :self_issued
def decode(jwt_string, key_or_config)
case key_or_config
when :self_issued
decode_self_issued jwt_string
when OpenIDConnect::Discovery::Provider::Config::Response
jwt = JSON::JWT.decode jwt_string, :skip_verification
jwt.verify! key_or_config.jwk(jwt.kid)
new jwt
else
new JSON::JWT.decode jwt_string, key
new JSON::JWT.decode jwt_string, key_or_config
end
end

Expand Down
16 changes: 6 additions & 10 deletions openid_connect.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@ Gem::Specification.new do |s|
s.add_runtime_dependency "activemodel"
s.add_runtime_dependency "validate_url"
s.add_runtime_dependency "validate_email"
s.add_runtime_dependency "json-jwt", ">= 1.5.0"
s.add_runtime_dependency "swd", ">= 1.0.0"
s.add_runtime_dependency "webfinger", ">= 1.0.1"
s.add_runtime_dependency "rack-oauth2", ">= 1.6.1"
if Gem.ruby_version >= Gem::Version.create(3.1)
# TODO:
# remove "net-smtp" dependency after mail gem 2.8+ (which supports ruby 3.1+) released.
# ref.) https://rubygems.org/gems/mailhttps://github.com/mikel/mail
s.add_runtime_dependency "net-smtp"
end
s.add_runtime_dependency 'faraday', '~> 2.0'
s.add_runtime_dependency 'faraday-follow_redirects'
s.add_runtime_dependency "json-jwt", ">= 1.16"
s.add_runtime_dependency "swd", "~> 2.0"
s.add_runtime_dependency "webfinger", "~> 2.0"
s.add_runtime_dependency "rack-oauth2", "~> 2.2"
s.add_development_dependency "rake"
s.add_development_dependency "rspec"
s.add_development_dependency "rspec-its"
Expand Down
8 changes: 7 additions & 1 deletion spec/helpers/webmock_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ def request_for(method, options = {})

def response_for(response_file, options = {})
response = {}
response[:body] = File.new(File.join(File.dirname(__FILE__), '../mock_response', "#{response_file}.#{options[:format] || :json}"))
format = options[:format] || :json
if format == :json
response[:headers] = {
'Content-Type': 'application/json'
}
end
response[:body] = File.new(File.join(File.dirname(__FILE__), '../mock_response', "#{response_file}.#{format}"))
if options[:status]
response[:status] = options[:status]
end
Expand Down
4 changes: 3 additions & 1 deletion spec/mock_response/errors/unknown.json
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
Fuckin Unknown Error
{
"unknown": "unknown"
}
8 changes: 8 additions & 0 deletions spec/mock_response/public_keys/jwks_with_private_key.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"keys": [{
"kty": "RSA",
"e": "AQAB",
"n": "vWr1S4T0jBnYU9PIpUYxT48Ca8HK8aitbmqbTM3t3Zzl1GNpIlyePnwXSL6SgNcVbeRhTfvXZUzH4pP8HzPJdpUHnAeYyCzjz9UNykdFCp2YW676wpLDzMkaU7bYLJxGjZlpHU-UJVIm5KX9-NfMyGbFUOuw4AY-OWp8GxrqwAF4U6bJ86TpO24wMxmgm0Vl72aRMGVJkRz66YLYOPNVjXjOI4bUuxg_o3Px5QASxvDCawMeLR3pLCoQcLAZn6WZx7nX3Wu6QzcY0QCqhqUAeY49QRT83Jdg7WUsNa2Rbegi3jJGJf-t9hEcJPmrI6q9zl6WArUueQHS-XUQWq5ptw",
"kid": "DCmKamGtkGAWz-uujePOp-UeATAeT4fi3KouR78r44I"
}]
}
27 changes: 27 additions & 0 deletions spec/mock_response/public_keys/private_key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAvWr1S4T0jBnYU9PIpUYxT48Ca8HK8aitbmqbTM3t3Zzl1GNp
IlyePnwXSL6SgNcVbeRhTfvXZUzH4pP8HzPJdpUHnAeYyCzjz9UNykdFCp2YW676
wpLDzMkaU7bYLJxGjZlpHU+UJVIm5KX9+NfMyGbFUOuw4AY+OWp8GxrqwAF4U6bJ
86TpO24wMxmgm0Vl72aRMGVJ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ljOfBozQdFR
ymRfTvQiZVfjBcyJ/mDv87b2Kh2IV+CPCFXebzlSUB4CtAbVP2zJhD176sMVWPZb
KCOxZi1f/ct5kAUhcre7f5xc7SXKXjrhYlJnqsxBMw2tnOB0hz6sjA4gNPvlGK3w
JkpDMQKBgQCgPoqSjmbroWC9oq5iDwRtx6f6fJG7CE91ZFJulunQj6YWOC3zNHEa
XvPPGM8fZpJS4e8LiPClkk8nsOoC50neEVGZeEuhdP6m6WNPN3SlP7bXozHOJTh0
mHrk2bUHFlQn8f5KWfLQbdyKBzs7WqCRTOR/gIbfxBlUOs0BN37xhw==
-----END RSA PRIVATE KEY-----
2 changes: 1 addition & 1 deletion spec/openid_connect/client/registrar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@
end

context 'otherwise' do
it { should be_instance_of HTTPClient }
it { should be_instance_of Faraday::Connection }
end
end
end
10 changes: 0 additions & 10 deletions spec/openid_connect/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,6 @@
end
end

context 'when invalid JSON is returned' do
it 'should raise OpenIDConnect::Exception' do
mock_json :post, client.token_endpoint, 'access_token/invalid_json', request_header: header_params, params: protocol_params do
expect do
access_token
end.to raise_error OpenIDConnect::Exception, 'Unknown Token Type'
end
end
end

context 'otherwise' do
it 'should raise Unexpected Token Type exception' do
mock_json :post, client.token_endpoint, 'access_token/mac', request_header: header_params, params: protocol_params do
Expand Down
Loading

0 comments on commit 8b96f7d

Please sign in to comment.