rails-oidc_provider 1.0.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 +7 -0
- data/CHANGELOG.md +54 -0
- data/MIT-LICENSE +20 -0
- data/README.md +198 -0
- data/Rakefile +23 -0
- data/app/controllers/oidc_provider/application_controller.rb +27 -0
- data/app/controllers/oidc_provider/authorizations_controller.rb +102 -0
- data/app/controllers/oidc_provider/concerns/connect_endpoint.rb +41 -0
- data/app/controllers/oidc_provider/discovery_controller.rb +63 -0
- data/app/controllers/oidc_provider/sessions_controller.rb +15 -0
- data/app/controllers/oidc_provider/user_infos_controller.rb +18 -0
- data/app/models/oidc_provider/access_token.rb +19 -0
- data/app/models/oidc_provider/application_record.rb +7 -0
- data/app/models/oidc_provider/authorization.rb +147 -0
- data/app/models/oidc_provider/id_token.rb +99 -0
- data/config/initializers/inflections.rb +5 -0
- data/config/routes.rb +12 -0
- data/db/migrate/20170928211111_create_oidc_provider_authorizations.rb +21 -0
- data/db/migrate/20170928212427_create_oidc_provider_access_tokens.rb +14 -0
- data/db/migrate/20170929155959_create_oidc_provider_id_tokens.rb +13 -0
- data/db/migrate/20230721112432_add_claims_to_oidc_provider_authorization_if_missing.rb +13 -0
- data/lib/generators/oidc_provider/install_generator.rb +33 -0
- data/lib/generators/oidc_provider/orm_helpers.rb +41 -0
- data/lib/generators/oidc_provider/templates/initializer.rb +31 -0
- data/lib/oidc_provider/account_to_user_info.rb +25 -0
- data/lib/oidc_provider/client/builder.rb +23 -0
- data/lib/oidc_provider/client.rb +16 -0
- data/lib/oidc_provider/client_store.rb +15 -0
- data/lib/oidc_provider/engine.rb +20 -0
- data/lib/oidc_provider/errors.rb +11 -0
- data/lib/oidc_provider/response_object_builder.rb +23 -0
- data/lib/oidc_provider/scope.rb +33 -0
- data/lib/oidc_provider/scope_attributes_collector.rb +39 -0
- data/lib/oidc_provider/scope_config.rb +23 -0
- data/lib/oidc_provider/token_endpoint.rb +38 -0
- data/lib/oidc_provider/version.rb +7 -0
- data/lib/oidc_provider.rb +96 -0
- data/lib/rails_oidc_provider/version.rb +5 -0
- data/lib/rails_oidc_provider.rb +28 -0
- data/lib/tasks/oidc_provider.rake +33 -0
- metadata +129 -0
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OIDCProvider
|
|
4
|
+
class Authorization < ApplicationRecord
|
|
5
|
+
# account_class is configurable (default "User"); cannot be a string literal.
|
|
6
|
+
belongs_to :account, class_name: OIDCProvider.account_class # rubocop:disable Rails/ReflectionClassName
|
|
7
|
+
has_one :access_token, dependent: :destroy
|
|
8
|
+
has_one :id_token, dependent: :destroy
|
|
9
|
+
|
|
10
|
+
scope :valid, -> { where(arel_table[:expires_at].gteq(Time.now.utc)) }
|
|
11
|
+
|
|
12
|
+
attribute :code, :string, default: -> { SecureRandom.hex 32 }
|
|
13
|
+
attribute :expires_at, :datetime, default: -> { 5.minutes.from_now }
|
|
14
|
+
|
|
15
|
+
# Rails 7.1 introduced the `coder:` keyword argument for `serialize`, and
|
|
16
|
+
# Rails 7.2 removed the positional coder argument (its signature is now
|
|
17
|
+
# `serialize(attr_name, coder: nil, type: Object, yaml: {}, **options)`).
|
|
18
|
+
# The coder is therefore passed as a keyword argument on Rails 7.1+ and
|
|
19
|
+
# positionally on older versions, keeping the same behaviour everywhere.
|
|
20
|
+
if Gem::Version.new(ActiveSupport::VERSION::STRING) >= Gem::Version.new('7.1')
|
|
21
|
+
serialize :scopes, coder: JSON
|
|
22
|
+
serialize :claims, coder: JSON
|
|
23
|
+
else
|
|
24
|
+
serialize :scopes, JSON
|
|
25
|
+
serialize :claims, JSON
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def access_token
|
|
29
|
+
super || (expire! && generate_access_token!)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def scope_configs_for(type)
|
|
33
|
+
if claims_request_for?(type)
|
|
34
|
+
build_scope_configs_from_claims_request_for(type)
|
|
35
|
+
else
|
|
36
|
+
type == :id_token ? [open_id_scope_config] : user_info_scope_configs
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def expire!
|
|
41
|
+
self.expires_at = Time.zone.now
|
|
42
|
+
|
|
43
|
+
save!
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def id_token
|
|
47
|
+
super || generate_id_token!
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def user_info_scopes
|
|
51
|
+
scopes - ['openid']
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
def build_scope_config_for(scope, type, key)
|
|
57
|
+
ScopeConfig.new(scope, [key.to_sym]).tap do |scope_config|
|
|
58
|
+
scope_config.add_force_claim(key.to_sym => claims[type.to_s][key])
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def build_scope_configs_from_claims_request_for(type)
|
|
63
|
+
# No matter the `claims` config, when we are about to create an IdToken
|
|
64
|
+
# response, we need the OpenID scope claims since there's mandatory ones
|
|
65
|
+
scope_configs = type == :id_token ? [open_id_scope_config] : []
|
|
66
|
+
|
|
67
|
+
claims[type.to_s].each_key do |key|
|
|
68
|
+
scopes_with_claim = OIDCProvider.find_all_scopes_with_claim(key)
|
|
69
|
+
|
|
70
|
+
next unless scope_found?(scopes_with_claim, key)
|
|
71
|
+
|
|
72
|
+
warn_when_many_scopes_found_in(scopes_with_claim, key, type)
|
|
73
|
+
|
|
74
|
+
scope = scopes_with_claim.first
|
|
75
|
+
|
|
76
|
+
next unless scope_has_been_requested?(scope)
|
|
77
|
+
|
|
78
|
+
scope_configs << build_scope_config_for(scope, type, key)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
scope_configs
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def claims_request_for?(type)
|
|
85
|
+
return false unless claims
|
|
86
|
+
|
|
87
|
+
claims.keys.include?(type.to_s)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def generate_access_token!
|
|
91
|
+
create_access_token!
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def generate_id_token!
|
|
95
|
+
token = build_id_token
|
|
96
|
+
token.nonce = nonce
|
|
97
|
+
token.save!
|
|
98
|
+
token
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def open_id_scope_config
|
|
102
|
+
scope = OIDCProvider.find_scope(OIDCProvider::Scopes::OpenID)
|
|
103
|
+
|
|
104
|
+
ScopeConfig.new(scope, scope.claims)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def scope_found?(scopes_with_claim, key)
|
|
108
|
+
return true unless scopes_with_claim.empty?
|
|
109
|
+
|
|
110
|
+
Rails.logger.warn(
|
|
111
|
+
"WARNING: No scope found providing the '#{key}' claim. " \
|
|
112
|
+
'OIDCProvider will skip it.'
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
false
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def scope_has_been_requested?(scope)
|
|
119
|
+
return true if scopes.include?(scope.name)
|
|
120
|
+
|
|
121
|
+
Rails.logger.warn(
|
|
122
|
+
"WARNING: The scope #{scope.name} has not being requested " \
|
|
123
|
+
'on authorization creation, there fore OIDCProvider will skip it.'
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
false
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def user_info_scope_configs
|
|
130
|
+
user_info_scopes.map do |scope_name|
|
|
131
|
+
scope = OIDCProvider.find_scope(scope_name)
|
|
132
|
+
|
|
133
|
+
ScopeConfig.new(scope, scope.claims)
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def warn_when_many_scopes_found_in(scopes_with_claim, key, type)
|
|
138
|
+
return unless scopes_with_claim.size > 1
|
|
139
|
+
|
|
140
|
+
Rails.logger.warn(
|
|
141
|
+
"WARNING: Scopes #{scopes_with_claim.map(&:name).to_sentence} " \
|
|
142
|
+
"have the #{key} claim declared. OIDCProvider will use the first " \
|
|
143
|
+
"one to populate the #{type} response."
|
|
144
|
+
)
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OIDCProvider
|
|
4
|
+
class IdToken < ApplicationRecord
|
|
5
|
+
PASSPHRASE_ENV_VAR = 'OIDC_PROVIDER_KEY_PASSPHRASE'
|
|
6
|
+
|
|
7
|
+
belongs_to :authorization
|
|
8
|
+
|
|
9
|
+
attribute :expires_at, :datetime, default: -> { 1.hour.from_now }
|
|
10
|
+
|
|
11
|
+
delegate :account, to: :authorization
|
|
12
|
+
|
|
13
|
+
def to_response_object
|
|
14
|
+
OpenIDConnect::ResponseObject::IdToken.new(id_token_attributes)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def to_jwt
|
|
18
|
+
to_response_object.to_jwt(self.class.private_jwk)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
# Return a Struct accepting all the possible attributes from an instance of
|
|
24
|
+
# the OpenIDConnect::ResponseObject::IdToken class used to collect the scope
|
|
25
|
+
# values and populate the OpenIDConnect::ResponseObject::IdToken instance
|
|
26
|
+
# that will be returned by the above `to_response_object`.
|
|
27
|
+
#
|
|
28
|
+
# At first I used an OpenStruct but since it has been officially discouraged
|
|
29
|
+
# for performance, version compatibility, and potential security issues,
|
|
30
|
+
# a `Struct` with predefined attributes is used instead.
|
|
31
|
+
# See https://docs.ruby-lang.org/en/3.0/OpenStruct.html#class-OpenStruct-label-Caveats
|
|
32
|
+
def build_id_token_struct
|
|
33
|
+
Struct.new(*OpenIDConnect::ResponseObject::IdToken.all_attributes)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def build_user_info_struct
|
|
37
|
+
Struct.new(*OpenIDConnect::ResponseObject::UserInfo.all_attributes)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def build_values_from_scope(scope_config)
|
|
41
|
+
attributes, context = prepare_response_object_builder_from(scope_config)
|
|
42
|
+
|
|
43
|
+
ResponseObjectBuilder.new(attributes, context, scope_config.requested_claims)
|
|
44
|
+
.run(&scope_config.scope.work)
|
|
45
|
+
|
|
46
|
+
response_attributes = attributes.to_h.compact
|
|
47
|
+
|
|
48
|
+
scope_config.force_claim.each do |key, value|
|
|
49
|
+
response_attributes[key] = value
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
response_attributes
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def id_token_attributes
|
|
56
|
+
scope_configs.each_with_object({}) do |scope_config, memo|
|
|
57
|
+
output = build_values_from_scope(scope_config)
|
|
58
|
+
memo.merge!(output)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def prepare_response_object_builder_from(scope_config)
|
|
63
|
+
if scope_config.name == OIDCProvider::Scopes::OpenID
|
|
64
|
+
[build_id_token_struct.new, self]
|
|
65
|
+
else
|
|
66
|
+
[build_user_info_struct.new, account]
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def scope_configs
|
|
71
|
+
authorization.scope_configs_for(:id_token)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
class << self
|
|
75
|
+
def config
|
|
76
|
+
{
|
|
77
|
+
issuer: OIDCProvider.issuer,
|
|
78
|
+
jwk_set: JSON::JWK::Set.new(public_jwk)
|
|
79
|
+
}
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def oidc_provider_key_path
|
|
83
|
+
ENV.fetch('OIDC_PROVIDER_PRIVATE_KEY_PATH', Rails.root.join('lib/oidc_provider_key.pem'))
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def key_pair
|
|
87
|
+
@key_pair ||= OpenSSL::PKey::RSA.new(File.read(oidc_provider_key_path), ENV.fetch(PASSPHRASE_ENV_VAR, nil))
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def private_jwk
|
|
91
|
+
JSON::JWK.new key_pair
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def public_jwk
|
|
95
|
+
JSON::JWK.new key_pair.public_key
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
data/config/routes.rb
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
OIDCProvider::Engine.routes.draw do
|
|
4
|
+
match 'authorizations' => 'authorizations#create', via: %i[get post], as: :authorizations
|
|
5
|
+
resource :user_info, only: :show
|
|
6
|
+
get 'sessions/logout', to: 'sessions#destroy', as: :end_session
|
|
7
|
+
|
|
8
|
+
post 'tokens', as: :tokens, to: proc { |env| OIDCProvider::TokenEndpoint.new.call(env) }
|
|
9
|
+
# JSON::JWK::Set subclasses Array. Calling to_json on it can emit a bare
|
|
10
|
+
# array; as_json returns the RFC 7517 §5 JWK Set object {"keys":[...]}.
|
|
11
|
+
get 'jwks.json', as: :jwks, to: proc { |_env| [200, { 'Content-Type' => 'application/json' }, [OIDCProvider::IdToken.config[:jwk_set].as_json.to_json]] }
|
|
12
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class CreateOIDCProviderAuthorizations < ActiveRecord::Migration[7.0]
|
|
4
|
+
def change
|
|
5
|
+
create_table :oidc_provider_authorizations do |t|
|
|
6
|
+
t.integer :account_id, null: false
|
|
7
|
+
t.index :account_id
|
|
8
|
+
t.string :client_id, null: false
|
|
9
|
+
t.string :nonce
|
|
10
|
+
t.string :code, null: false
|
|
11
|
+
t.text :scopes, null: false
|
|
12
|
+
t.text :claims
|
|
13
|
+
t.datetime :expires_at, null: false
|
|
14
|
+
|
|
15
|
+
t.timestamps
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
add_index :oidc_provider_authorizations, :client_id
|
|
19
|
+
add_index :oidc_provider_authorizations, :code, unique: true
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class CreateOIDCProviderAccessTokens < ActiveRecord::Migration[7.0]
|
|
4
|
+
def change
|
|
5
|
+
create_table :oidc_provider_access_tokens do |t|
|
|
6
|
+
t.references :authorization, foreign_key: { to_table: :oidc_provider_authorizations }, null: false
|
|
7
|
+
t.string :token, null: false
|
|
8
|
+
t.datetime :expires_at, null: false
|
|
9
|
+
|
|
10
|
+
t.timestamps
|
|
11
|
+
end
|
|
12
|
+
add_index :oidc_provider_access_tokens, :token, unique: true
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class CreateOIDCProviderIdTokens < ActiveRecord::Migration[7.0]
|
|
4
|
+
def change
|
|
5
|
+
create_table :oidc_provider_id_tokens do |t|
|
|
6
|
+
t.references :authorization, foreign_key: { to_table: :oidc_provider_authorizations }
|
|
7
|
+
t.string :nonce, null: false
|
|
8
|
+
t.datetime :expires_at, null: false
|
|
9
|
+
|
|
10
|
+
t.timestamps
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class AddClaimsToOIDCProviderAuthorizationIfMissing < ActiveRecord::Migration[7.0]
|
|
4
|
+
def up
|
|
5
|
+
return if ActiveRecord::Base.connection.column_exists?(:oidc_provider_authorizations, :claims)
|
|
6
|
+
|
|
7
|
+
add_column :oidc_provider_authorizations, :claims, :text
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def down
|
|
11
|
+
remove_column :oidc_provider_authorizations, :claims
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# require 'rails/generators/base'
|
|
4
|
+
# require 'securerandom'
|
|
5
|
+
|
|
6
|
+
module OidcProvider
|
|
7
|
+
class InstallGenerator < Rails::Generators::Base
|
|
8
|
+
source_root File.expand_path('templates', __dir__)
|
|
9
|
+
|
|
10
|
+
desc 'Creates a OIDCProvider initializer.'
|
|
11
|
+
|
|
12
|
+
def copy_initializer_file
|
|
13
|
+
copy_file 'initializer.rb', 'config/initializers/oidc_provider.rb'
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def setup_routes
|
|
17
|
+
route "get '/.well-known/:id', to: 'oidc_provider/discovery#show'"
|
|
18
|
+
route "mount OIDCProvider::Engine, at: '/accounts/oauth'"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# def copy_locale
|
|
22
|
+
# copy_file "../../../config/locales/en.yml", "config/locales/devise.en.yml"
|
|
23
|
+
# end
|
|
24
|
+
|
|
25
|
+
# def show_readme
|
|
26
|
+
# readme "README" if behavior == :invoke
|
|
27
|
+
# end
|
|
28
|
+
|
|
29
|
+
# def rails_4?
|
|
30
|
+
# Rails::VERSION::MAJOR == 4
|
|
31
|
+
# end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OIDCProvider
|
|
4
|
+
module Generators
|
|
5
|
+
module OrmHelpers
|
|
6
|
+
# def model_contents
|
|
7
|
+
# buffer = <<-CONTENT
|
|
8
|
+
# # Include default devise modules. Others available are:
|
|
9
|
+
# # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
|
10
|
+
# devise :database_authenticatable, :registerable,
|
|
11
|
+
# :recoverable, :rememberable, :validatable
|
|
12
|
+
|
|
13
|
+
# CONTENT
|
|
14
|
+
# buffer
|
|
15
|
+
# end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
# def model_exists?
|
|
20
|
+
# File.exist?(File.join(destination_root, model_path))
|
|
21
|
+
# end
|
|
22
|
+
|
|
23
|
+
# def migration_exists?(table_name)
|
|
24
|
+
# pattern = /\d+_add_devise_to_#{table_name}.rb$/
|
|
25
|
+
# Dir.glob("#{File.join(destination_root, migration_path)}/[0-9]*_*.rb").grep(pattern).first
|
|
26
|
+
# end
|
|
27
|
+
|
|
28
|
+
def migration_path
|
|
29
|
+
if Rails.version >= '5.0.3'
|
|
30
|
+
db_migrate_path
|
|
31
|
+
else
|
|
32
|
+
@migration_path ||= File.join('db', 'migrate')
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# def model_path
|
|
37
|
+
# @model_path ||= File.join("app", "models", "#{file_path}.rb")
|
|
38
|
+
# end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
OIDCProvider.configure do |config|
|
|
4
|
+
config.issuer = 'https://myoidcprovider.org'
|
|
5
|
+
# config.account_class = "Administrator"
|
|
6
|
+
|
|
7
|
+
config.add_client do
|
|
8
|
+
name 'Test Client'
|
|
9
|
+
identifier '0001'
|
|
10
|
+
secret '27ffb...'
|
|
11
|
+
redirect_uri 'https://demo.c2id.com/oidc-client/cb'
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
config.add_client do
|
|
15
|
+
name 'Client 1'
|
|
16
|
+
identifier 'client_01'
|
|
17
|
+
secret '6f14c...'
|
|
18
|
+
redirect_uri 'https://clientone.com/TDE/openid_connect_login'
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
config.add_scope OIDCProvider::Scopes::Profile do |user|
|
|
22
|
+
name user.full_name
|
|
23
|
+
given_name user.first_name
|
|
24
|
+
family_name user.last_name
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
config.add_scope OIDCProvider::Scopes::Email do |user|
|
|
28
|
+
email user.email
|
|
29
|
+
email_verified false
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OIDCProvider
|
|
4
|
+
class AccountToUserInfo
|
|
5
|
+
def initialize(scope_names)
|
|
6
|
+
@scope_names = scope_names
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def call(account)
|
|
10
|
+
OpenIDConnect::ResponseObject::UserInfo.new(
|
|
11
|
+
sub: account.send(OIDCProvider.account_identifier)
|
|
12
|
+
).tap do |user_info|
|
|
13
|
+
scopes.each do |scope|
|
|
14
|
+
ResponseObjectBuilder.new(user_info, account).run(&scope.work)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
def scopes
|
|
22
|
+
@scope_names.map { |name| OIDCProvider.find_scope(name) }.compact
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OIDCProvider
|
|
4
|
+
class Client
|
|
5
|
+
class Builder
|
|
6
|
+
def initialize(&block)
|
|
7
|
+
@client = Client.new
|
|
8
|
+
@operation = block
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def build
|
|
12
|
+
instance_exec(&@operation)
|
|
13
|
+
@client
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
%i[identifier secret redirect_uri name].each do |attr|
|
|
17
|
+
define_method attr do |val|
|
|
18
|
+
@client.send("#{attr}=", val)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OIDCProvider
|
|
4
|
+
class Client
|
|
5
|
+
attr_accessor :identifier, :secret, :redirect_uri, :name
|
|
6
|
+
|
|
7
|
+
def initialize(options = {})
|
|
8
|
+
@identifier = options[:identifier]
|
|
9
|
+
@secret = options[:secret]
|
|
10
|
+
@redirect_uri = Array(options[:redirect_uri])
|
|
11
|
+
@name = options[:name]
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
require 'oidc_provider/client/builder'
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OIDCProvider
|
|
4
|
+
class ClientStore
|
|
5
|
+
attr_reader :clients
|
|
6
|
+
|
|
7
|
+
def initialize(clients = OIDCProvider.clients)
|
|
8
|
+
@clients = clients
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def find_by(attrs)
|
|
12
|
+
clients.detect { |client| attrs.all? { |key, value| client.send(key) == value } }
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'rack/oauth2'
|
|
4
|
+
|
|
5
|
+
module OIDCProvider
|
|
6
|
+
class Engine < ::Rails::Engine
|
|
7
|
+
isolate_namespace OIDCProvider
|
|
8
|
+
|
|
9
|
+
config.middleware.use Rack::OAuth2::Server::Rails::Authorize
|
|
10
|
+
config.middleware.use Rack::OAuth2::Server::Resource::Bearer, 'OpenID Connect' do |req|
|
|
11
|
+
AccessToken.valid.find_by(token: req.access_token) ||
|
|
12
|
+
req.invalid_token!
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Host apps install migrations with:
|
|
16
|
+
# rails oidc_provider:install:migrations
|
|
17
|
+
# The engine dummy app under spec/dummy receives them via railties engine.rake
|
|
18
|
+
# (do not also append here or migrations load twice).
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OIDCProvider
|
|
4
|
+
module Errors
|
|
5
|
+
# Allows one to catch all OIDCProvider errors
|
|
6
|
+
class OIDCProviderError < StandardError; end
|
|
7
|
+
|
|
8
|
+
# Raised when passed claims is not a Hash of hashes only.
|
|
9
|
+
class InvalidClaimsFormatError < OIDCProviderError; end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OIDCProvider
|
|
4
|
+
class ResponseObjectBuilder
|
|
5
|
+
attr_reader :response_object
|
|
6
|
+
|
|
7
|
+
def initialize(response_object, context, filter_claims = nil)
|
|
8
|
+
@context = context
|
|
9
|
+
@filter_claims = filter_claims
|
|
10
|
+
@response_object = response_object
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def run(&)
|
|
14
|
+
instance_exec(@context, &)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def method_missing(sym, *args) # rubocop:disable Style/MissingRespondToMissing
|
|
18
|
+
return if @filter_claims.present? && @filter_claims.include?(sym) == false
|
|
19
|
+
|
|
20
|
+
@response_object.send("#{sym}=", *args)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OIDCProvider
|
|
4
|
+
class Scope
|
|
5
|
+
attr_accessor :claims, :name, :work
|
|
6
|
+
|
|
7
|
+
def initialize(name, &block)
|
|
8
|
+
@name = name
|
|
9
|
+
@work = block
|
|
10
|
+
|
|
11
|
+
@claims = OIDCProvider.claims_from_scope(self)
|
|
12
|
+
inject_claims_to_id_token_if_needed!
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
# Since the openid_connect gem is limiting the allowed attributes on the
|
|
18
|
+
# response classes, this gem declares more optional attributes on the
|
|
19
|
+
# OpenIDConnect::ResponseObject::IdToken class.
|
|
20
|
+
#
|
|
21
|
+
# NOTE : This is done when adding a scope to this gem, so only once at the
|
|
22
|
+
# app's boot time when initializing this gem.
|
|
23
|
+
# NOTE : Ideally the openid_connect gem should be patched in order to allow
|
|
24
|
+
# more claims without this hack.
|
|
25
|
+
def inject_claims_to_id_token_if_needed!
|
|
26
|
+
missings = @claims - OpenIDConnect::ResponseObject::IdToken.all_attributes
|
|
27
|
+
|
|
28
|
+
return if missings.empty?
|
|
29
|
+
|
|
30
|
+
OpenIDConnect::ResponseObject::IdToken.attr_optional(*missings)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# The aim of this class is to collect the method names from a given scope block
|
|
4
|
+
# added to the configuration from `add_scope` method.
|
|
5
|
+
class ScopeAttributesCollector
|
|
6
|
+
# This class is used in order to create a context which is always happy about
|
|
7
|
+
# the requested method so that this `ScopeAttributesCollector` never reach an
|
|
8
|
+
# `undefined method` error and such and collect happilly all the block
|
|
9
|
+
# method names.
|
|
10
|
+
class HappyWorld
|
|
11
|
+
# When using something like [user.first_name, user.last_name].join(' ') in a
|
|
12
|
+
# scope, the `to_str` method is called and must return a string.
|
|
13
|
+
# Since we are in an Happy World, let's say it!
|
|
14
|
+
def to_str
|
|
15
|
+
'HappyWorld'
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def method_missing(*_) # rubocop:disable Style/MissingRespondToMissing
|
|
19
|
+
HappyWorld.new
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
attr_reader :collecteds
|
|
24
|
+
|
|
25
|
+
def initialize
|
|
26
|
+
@source = HappyWorld.new
|
|
27
|
+
@collecteds = []
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def run(&)
|
|
31
|
+
# Redirects all method calls to this class so that all the method_missing
|
|
32
|
+
# are forwarded bellow in this class.
|
|
33
|
+
instance_exec(@source, &)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def method_missing(sym, *_) # rubocop:disable Style/MissingRespondToMissing
|
|
37
|
+
@collecteds |= [sym]
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OIDCProvider
|
|
4
|
+
class ScopeConfig
|
|
5
|
+
attr_accessor :force_claim, :requested_claims, :scope
|
|
6
|
+
|
|
7
|
+
def initialize(scope, requested_claims)
|
|
8
|
+
@force_claim = {}
|
|
9
|
+
@requested_claims = requested_claims
|
|
10
|
+
@scope = scope
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def add_force_claim(key_value)
|
|
14
|
+
raise ArgumentError unless key_value.is_a?(Hash)
|
|
15
|
+
|
|
16
|
+
# Only stores keys where the value is not `nil` thanks to the `.compact`
|
|
17
|
+
# method.
|
|
18
|
+
@force_claim.merge!(key_value.compact)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
delegate :name, to: :@scope
|
|
22
|
+
end
|
|
23
|
+
end
|