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.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +54 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.md +198 -0
  5. data/Rakefile +23 -0
  6. data/app/controllers/oidc_provider/application_controller.rb +27 -0
  7. data/app/controllers/oidc_provider/authorizations_controller.rb +102 -0
  8. data/app/controllers/oidc_provider/concerns/connect_endpoint.rb +41 -0
  9. data/app/controllers/oidc_provider/discovery_controller.rb +63 -0
  10. data/app/controllers/oidc_provider/sessions_controller.rb +15 -0
  11. data/app/controllers/oidc_provider/user_infos_controller.rb +18 -0
  12. data/app/models/oidc_provider/access_token.rb +19 -0
  13. data/app/models/oidc_provider/application_record.rb +7 -0
  14. data/app/models/oidc_provider/authorization.rb +147 -0
  15. data/app/models/oidc_provider/id_token.rb +99 -0
  16. data/config/initializers/inflections.rb +5 -0
  17. data/config/routes.rb +12 -0
  18. data/db/migrate/20170928211111_create_oidc_provider_authorizations.rb +21 -0
  19. data/db/migrate/20170928212427_create_oidc_provider_access_tokens.rb +14 -0
  20. data/db/migrate/20170929155959_create_oidc_provider_id_tokens.rb +13 -0
  21. data/db/migrate/20230721112432_add_claims_to_oidc_provider_authorization_if_missing.rb +13 -0
  22. data/lib/generators/oidc_provider/install_generator.rb +33 -0
  23. data/lib/generators/oidc_provider/orm_helpers.rb +41 -0
  24. data/lib/generators/oidc_provider/templates/initializer.rb +31 -0
  25. data/lib/oidc_provider/account_to_user_info.rb +25 -0
  26. data/lib/oidc_provider/client/builder.rb +23 -0
  27. data/lib/oidc_provider/client.rb +16 -0
  28. data/lib/oidc_provider/client_store.rb +15 -0
  29. data/lib/oidc_provider/engine.rb +20 -0
  30. data/lib/oidc_provider/errors.rb +11 -0
  31. data/lib/oidc_provider/response_object_builder.rb +23 -0
  32. data/lib/oidc_provider/scope.rb +33 -0
  33. data/lib/oidc_provider/scope_attributes_collector.rb +39 -0
  34. data/lib/oidc_provider/scope_config.rb +23 -0
  35. data/lib/oidc_provider/token_endpoint.rb +38 -0
  36. data/lib/oidc_provider/version.rb +7 -0
  37. data/lib/oidc_provider.rb +96 -0
  38. data/lib/rails_oidc_provider/version.rb +5 -0
  39. data/lib/rails_oidc_provider.rb +28 -0
  40. data/lib/tasks/oidc_provider.rake +33 -0
  41. metadata +129 -0
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OIDCProvider
4
+ class TokenEndpoint
5
+ attr_accessor :app
6
+
7
+ delegate :call, to: :app
8
+
9
+ def initialize
10
+ @app = Rack::OAuth2::Server::Token.new do |req, res|
11
+ case req.grant_type
12
+ when :authorization_code
13
+ client = find_valid_client_from(req) || req.invalid_client!
14
+
15
+ authorization = Authorization.valid.where(client_id: client.identifier,
16
+ code: req.code).first || req.invalid_grant!
17
+ res.access_token = authorization.access_token.to_bearer_token
18
+ res.id_token = authorization.id_token.to_jwt if authorization.scopes.include?('openid')
19
+ else
20
+ req.unsupported_grant_type!
21
+ end
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def find_valid_client_from(req)
28
+ client = ClientStore.new.find_by(
29
+ identifier: req.client_id,
30
+ secret: req.client_secret
31
+ )
32
+
33
+ return nil unless client
34
+
35
+ client.redirect_uri.include?(req.redirect_uri) ? client : nil
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../rails_oidc_provider/version'
4
+
5
+ module OIDCProvider
6
+ VERSION = RailsOidcProvider::VERSION unless const_defined?(:VERSION, false)
7
+ end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'openid_connect'
4
+ require 'oidc_provider/version'
5
+ require 'oidc_provider/engine'
6
+ require 'oidc_provider/errors'
7
+
8
+ module OIDCProvider
9
+ module Scopes
10
+ OpenID = 'openid'
11
+ Profile = 'profile'
12
+ Email = 'email'
13
+ Address = 'address'
14
+ end
15
+
16
+ autoload :AccountToUserInfo, 'oidc_provider/account_to_user_info'
17
+ autoload :Client, 'oidc_provider/client'
18
+ autoload :ClientStore, 'oidc_provider/client_store'
19
+ autoload :ResponseObjectBuilder, 'oidc_provider/response_object_builder'
20
+ autoload :Scope, 'oidc_provider/scope'
21
+ autoload :ScopeAttributesCollector, 'oidc_provider/scope_attributes_collector'
22
+ autoload :ScopeConfig, 'oidc_provider/scope_config'
23
+ autoload :TokenEndpoint, 'oidc_provider/token_endpoint'
24
+
25
+ mattr_accessor :issuer
26
+
27
+ mattr_accessor :supported_scopes
28
+ @@supported_scopes = []
29
+
30
+ mattr_accessor :clients
31
+ @@clients = []
32
+
33
+ mattr_accessor :account_class
34
+ @@account_class = 'User'
35
+
36
+ mattr_accessor :current_account_method
37
+ @@current_account_method = :current_user
38
+
39
+ mattr_accessor :current_authentication_method
40
+ @@current_authentication_method = :authenticate_user!
41
+
42
+ mattr_accessor :current_unauthenticate_method
43
+ @@current_unauthenticate_method = :sign_out
44
+
45
+ mattr_accessor :account_identifier
46
+ @@account_identifier = :id
47
+
48
+ mattr_accessor :after_sign_out_path
49
+
50
+ def self.add_client(&)
51
+ @@clients << Client::Builder.new(&).build
52
+ end
53
+
54
+ def self.add_scope(name, &)
55
+ @@supported_scopes << Scope.new(name, &)
56
+ end
57
+
58
+ def self.configure
59
+ @@clients = []
60
+
61
+ @@supported_scopes = [open_id_scope]
62
+
63
+ yield self
64
+ end
65
+
66
+ def self.find_all_scopes_with_claim(name)
67
+ @@supported_scopes.select { |scope| scope.claims.include?(name.to_sym) }
68
+ end
69
+
70
+ def self.find_scope(name)
71
+ @@supported_scopes.detect { |scope| scope.name == name }
72
+ end
73
+
74
+ # Returns the claims from a given scope
75
+ def self.claims_from_scope(scope)
76
+ collector = ScopeAttributesCollector.new
77
+ collector.run(&scope.work)
78
+ collector.collecteds
79
+ end
80
+
81
+ def self.open_id_scope
82
+ Scope.new(OIDCProvider::Scopes::OpenID) do |id_token|
83
+ iss OIDCProvider.issuer
84
+ sub id_token.account.send(OIDCProvider.account_identifier)
85
+ aud id_token.authorization.client_id
86
+ nonce id_token.nonce
87
+ exp id_token.expires_at.to_i
88
+ iat id_token.created_at.to_i
89
+ end
90
+ end
91
+
92
+ # Returns all the claims from all the `@@supported_scopes`
93
+ def self.supported_claims
94
+ @@supported_scopes.flat_map(&:claims)
95
+ end
96
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsOidcProvider
4
+ VERSION = '1.0.0'
5
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'rails_oidc_provider/version'
4
+ require 'oidc_provider'
5
+
6
+ # Transition alias: prefer `rails-oidc_provider` / RailsOidcProvider while keeping
7
+ # the historical OIDCProvider API as the implementation.
8
+ module RailsOidcProvider
9
+ class << self
10
+ def method_missing(method_name, ...)
11
+ return super unless OIDCProvider.respond_to?(method_name)
12
+
13
+ OIDCProvider.public_send(method_name, ...)
14
+ end
15
+
16
+ def respond_to_missing?(method_name, include_private = false)
17
+ OIDCProvider.respond_to?(method_name, include_private) || super
18
+ end
19
+
20
+ def const_missing(name)
21
+ OIDCProvider.const_get(name)
22
+ end
23
+ end
24
+ end
25
+
26
+ module OIDCProvider
27
+ VERSION = RailsOidcProvider::VERSION unless const_defined?(:VERSION, false)
28
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :oidc_provider do
4
+ desc 'Generate the oidc_provider_key.pem key file'
5
+ task generate_key: :environment do
6
+ key_filepath = OIDCProvider::IdToken.oidc_provider_key_path
7
+
8
+ File.exist?(key_filepath) && raise("ERROR: A key file already exists at #{key_filepath}.")
9
+
10
+ passphrase_env_var = OIDCProvider::IdToken::PASSPHRASE_ENV_VAR
11
+
12
+ pass_phrase = ENV.fetch(passphrase_env_var, '')
13
+
14
+ if pass_phrase == ''
15
+ puts "\033[33mWARNING: You haven't defined a passphrase to be used to " \
16
+ 'generate the new key which is concidered as insecured. You can ' \
17
+ "do it by setting the #{passphrase_env_var} environment variable " \
18
+ "and re-run this task.\033[0m"
19
+
20
+ raise
21
+ end
22
+
23
+ key_file_content = OpenSSL::PKey::RSA.new(2048).export(
24
+ OpenSSL::Cipher.new('AES-128-CBC'),
25
+ pass_phrase
26
+ )
27
+
28
+ File.write(key_filepath, key_file_content)
29
+ FileUtils.chmod(0_600, key_filepath)
30
+
31
+ puts "SUCCESS: A new key file has been created at #{key_filepath}."
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-oidc_provider
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Pharmony SA
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: openid_connect
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: 2.2.0
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: 2.2.0
26
+ - !ruby/object:Gem::Dependency
27
+ name: rails
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '7.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '7.0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: rspec-rails
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '6.0'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '6.0'
54
+ description: |
55
+ A maintained Rails engine for OpenID Connect authorization, historically
56
+ derived from brandnewbox/oidc_provider. The public Ruby API remains
57
+ OIDCProvider, with a RailsOidcProvider alias for the new gem identity.
58
+ email:
59
+ - dev@pharmony.eu
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - CHANGELOG.md
65
+ - MIT-LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - app/controllers/oidc_provider/application_controller.rb
69
+ - app/controllers/oidc_provider/authorizations_controller.rb
70
+ - app/controllers/oidc_provider/concerns/connect_endpoint.rb
71
+ - app/controllers/oidc_provider/discovery_controller.rb
72
+ - app/controllers/oidc_provider/sessions_controller.rb
73
+ - app/controllers/oidc_provider/user_infos_controller.rb
74
+ - app/models/oidc_provider/access_token.rb
75
+ - app/models/oidc_provider/application_record.rb
76
+ - app/models/oidc_provider/authorization.rb
77
+ - app/models/oidc_provider/id_token.rb
78
+ - config/initializers/inflections.rb
79
+ - config/routes.rb
80
+ - db/migrate/20170928211111_create_oidc_provider_authorizations.rb
81
+ - db/migrate/20170928212427_create_oidc_provider_access_tokens.rb
82
+ - db/migrate/20170929155959_create_oidc_provider_id_tokens.rb
83
+ - db/migrate/20230721112432_add_claims_to_oidc_provider_authorization_if_missing.rb
84
+ - lib/generators/oidc_provider/install_generator.rb
85
+ - lib/generators/oidc_provider/orm_helpers.rb
86
+ - lib/generators/oidc_provider/templates/initializer.rb
87
+ - lib/oidc_provider.rb
88
+ - lib/oidc_provider/account_to_user_info.rb
89
+ - lib/oidc_provider/client.rb
90
+ - lib/oidc_provider/client/builder.rb
91
+ - lib/oidc_provider/client_store.rb
92
+ - lib/oidc_provider/engine.rb
93
+ - lib/oidc_provider/errors.rb
94
+ - lib/oidc_provider/response_object_builder.rb
95
+ - lib/oidc_provider/scope.rb
96
+ - lib/oidc_provider/scope_attributes_collector.rb
97
+ - lib/oidc_provider/scope_config.rb
98
+ - lib/oidc_provider/token_endpoint.rb
99
+ - lib/oidc_provider/version.rb
100
+ - lib/rails_oidc_provider.rb
101
+ - lib/rails_oidc_provider/version.rb
102
+ - lib/tasks/oidc_provider.rake
103
+ homepage: https://github.com/zedtux/rails-oidc_provider
104
+ licenses:
105
+ - MIT
106
+ metadata:
107
+ allowed_push_host: https://rubygems.org
108
+ homepage_uri: https://github.com/zedtux/rails-oidc_provider
109
+ source_code_uri: https://github.com/zedtux/rails-oidc_provider
110
+ changelog_uri: https://github.com/zedtux/rails-oidc_provider/blob/master/CHANGELOG.md
111
+ rubygems_mfa_required: 'true'
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '3.1'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubygems_version: 3.6.7
127
+ specification_version: 4
128
+ summary: Rails engine that turns an application into an OpenID Connect provider.
129
+ test_files: []