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
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: def5a98707e364b76862f489e089a761de257f56fc5e9cef5cccd72e2188d8e6
4
+ data.tar.gz: 50dc19a68bc750f29d9c919d2c58a5642cb006492c008115f9e685da3905d203
5
+ SHA512:
6
+ metadata.gz: b82c74fe61aae38c6eee78f7becebb9cdf6882521b3593cfb526be17e00a25e523580959517598fb4946c2be72013359b70a883db9b3bb2a8f98459b7680ca67
7
+ data.tar.gz: 980447a4deb97ca2fbba63f798f293190486fd654b0bcf6e94de496a210df9d398a4f2e8021106304adbc8461eb843238938eb53ca90dee11c236eaefa26d446
data/CHANGELOG.md ADDED
@@ -0,0 +1,54 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [1.0.0] - 2026-03-26
11
+
12
+ First release of **`rails-oidc_provider`**, a maintained OpenID Connect provider
13
+ engine for Rails. Historically derived from
14
+ [brandnewbox/oidc_provider](https://github.com/brandnewbox/oidc_provider) and the
15
+ accumulated community patches previously published on Pharmony forks. This is a
16
+ **new gem**, not an official continuation of the original project.
17
+
18
+ The runtime Ruby API remains `OIDCProvider` for drop-in compatibility. A
19
+ `RailsOidcProvider` module aliases configuration helpers for the new gem name.
20
+
21
+ ### Added
22
+
23
+ - Gem name `rails-oidc_provider` at version `1.0.0`.
24
+ - `RailsOidcProvider` transition alias delegating to `OIDCProvider`.
25
+ - RSpec request suite ported from a consuming application (authorization,
26
+ tokens, claims, clients, discovery, userinfo, JWKS).
27
+ - Docker image and `docker compose` workflow for running the test suite locally.
28
+ - GitHub Actions CI with Ruby × Rails RSpec matrix, RuboCop, Docker smoke; local bin/test-matrix.
29
+ - RuboCop (`rubocop`, `rubocop-rails`, `rubocop-rspec`) for local and CI linting.
30
+ - This changelog.
31
+
32
+ ### Fixed (inherited from prior patch line)
33
+
34
+ - JWKS endpoint returns an RFC 7517 §5 JWK Set object (`{"keys":[...]}`) instead
35
+ of a bare array of JWKs.
36
+ - Unsupported OAuth grant types respond with `unsupported_grant_type` (HTTP 400)
37
+ instead of an unhandled 500.
38
+ - Authorization redirects to external client hosts work on Rails 7+
39
+ (`allow_other_host: true`).
40
+ - `Authorization` claims/scopes serialization works on Rails 7.1+ and older
41
+ Rails (`coder:` keyword when required).
42
+ - Configurable private key path via `OIDC_PROVIDER_PRIVATE_KEY_PATH`.
43
+ - OpenID `claims` request parameter support on the authorization endpoint.
44
+ - Multiple `redirect_uri` values per client.
45
+
46
+ ### Changed
47
+
48
+ - Single `master` branch replaces the former cumulative `master+patch-*` model.
49
+ - Development dependency stack centered on RSpec + SQLite dummy app (Minitest
50
+ engine smoke tests removed).
51
+ - Runtime dependency floor raised to Rails `>= 7.0` and Ruby `>= 3.1`.
52
+
53
+ [Unreleased]: https://github.com/pharmony/rails-oidc_provider/compare/v1.0.0...HEAD
54
+ [1.0.0]: https://github.com/pharmony/rails-oidc_provider/releases/tag/v1.0.0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,198 @@
1
+ # rails-oidc_provider
2
+
3
+ > **This repository is a new gem** (`rails-oidc_provider`), historically derived
4
+ > from [brandnewbox/oidc_provider](https://github.com/brandnewbox/oidc_provider).
5
+ > It is **not** an official continuation of that project. The maintainers of the
6
+ > original gem were unresponsive; this codebase packages the engine under a new
7
+ > name with its own release line, tests, and CI.
8
+ >
9
+ > The public Ruby API remains **`OIDCProvider`** (engine, models, configuration)
10
+ > so existing applications can migrate with minimal code changes. Prefer the
11
+ > **`RailsOidcProvider`** alias for new code when you want the new gem identity.
12
+
13
+ A Rails engine that turns a host application into an [OpenID Connect](https://openid.net/connect/)
14
+ provider, built on [`openid_connect`](https://github.com/nov/openid_connect).
15
+
16
+ ## Requirements
17
+
18
+ - Ruby >= 3.1
19
+ - Rails >= 7.0
20
+ - A host-app authentication stack exposing `authenticate_user!`, `current_user`,
21
+ and `sign_out` (Devise is the usual choice; the dummy app uses a minimal
22
+ session equivalent for tests)
23
+
24
+ ## Installation
25
+
26
+ Add this line to your application's Gemfile:
27
+
28
+ ```ruby
29
+ gem 'rails-oidc_provider'
30
+ ```
31
+
32
+ Then:
33
+
34
+ ```bash
35
+ bundle install
36
+ rails generate oidc_provider:install
37
+ rails oidc_provider:install:migrations
38
+ rails db:migrate
39
+ ```
40
+
41
+ The install generator still writes `config/initializers/oidc_provider.rb` and
42
+ mounts:
43
+
44
+ ```ruby
45
+ mount OIDCProvider::Engine, at: '/accounts/oauth'
46
+ get '/.well-known/:id', to: 'oidc_provider/discovery#show'
47
+ ```
48
+
49
+ ### Migrating from `oidc_provider`
50
+
51
+ 1. Replace `gem 'oidc_provider', ...` with `gem 'rails-oidc_provider'`.
52
+ 2. Keep `OIDCProvider.configure` and existing mounts/migrations — table names and
53
+ the `OIDCProvider` constant are unchanged.
54
+ 3. Optionally start using `RailsOidcProvider.configure` (delegates to the same API).
55
+
56
+ ### Private key
57
+
58
+ ID tokens are signed with an RSA private key at `lib/oidc_provider_key.pem` in the
59
+ host app (override with `OIDC_PROVIDER_PRIVATE_KEY_PATH`). Passphrase:
60
+ `OIDC_PROVIDER_KEY_PASSPHRASE`.
61
+
62
+ ```bash
63
+ rails oidc_provider:generate_key
64
+ ```
65
+
66
+ ## Configuration
67
+
68
+ ```ruby
69
+ OIDCProvider.configure do |config|
70
+ config.issuer = 'https://www.example.com'
71
+ config.account_class = 'User'
72
+
73
+ config.add_client do
74
+ name 'Test Client'
75
+ identifier '0001'
76
+ secret 'secret'
77
+ redirect_uri 'https://app.example.com/oidc/callback'
78
+ end
79
+
80
+ config.add_scope OIDCProvider::Scopes::Profile do |user|
81
+ name user.full_name
82
+ given_name user.first_name
83
+ family_name user.last_name
84
+ end
85
+
86
+ config.add_scope OIDCProvider::Scopes::Email do |user|
87
+ email user.email
88
+ email_verified true
89
+ end
90
+ end
91
+ ```
92
+
93
+ ## Running tests with Docker
94
+
95
+ The suite runs against a SQLite dummy Rails app inside Docker so you do not need
96
+ a local Ruby/Rails toolchain matching CI.
97
+
98
+ ### One Ruby × Rails pair
99
+
100
+ ```bash
101
+ # Defaults: Ruby 3.3 × Rails 7.1
102
+ bin/test-matrix
103
+
104
+ # Explicit pair
105
+ bin/test-matrix 3.2 7.2
106
+ bin/test-matrix --rubocop 3.3 7.1
107
+
108
+ # Compose equivalent (MATRIX_* avoids clashing with host rbenv RUBY_VERSION)
109
+ MATRIX_RUBY=3.2 MATRIX_RAILS=7.2 docker compose build test
110
+ MATRIX_RUBY=3.2 MATRIX_RAILS=7.2 docker compose run --rm test
111
+
112
+ # RuboCop in the test image
113
+ docker compose run --rm test bundle exec rubocop
114
+ ```
115
+
116
+ ### Full local matrix
117
+
118
+ ```bash
119
+ bin/test-matrix --all
120
+ ```
121
+
122
+ Supported pairs match CI: Ruby 3.1–3.3 × Rails 7.0/7.1/7.2/8.0 (compatible only).
123
+
124
+ ### Without Docker
125
+
126
+ ```bash
127
+ RAILS_VERSION=7.1 bundle install
128
+ OIDC_PROVIDER_KEY_PASSPHRASE=test-passphrase bundle exec rake app:db:prepare
129
+ OIDC_PROVIDER_KEY_PASSPHRASE=test-passphrase bundle exec rspec
130
+ bundle exec rubocop
131
+ ```
132
+
133
+ ## Continuous integration
134
+
135
+ GitHub Actions (`.github/workflows/ci.yml`) on `master` PRs/pushes:
136
+
137
+ - **RSpec matrix** — Ruby × Rails pairs (see workflow)
138
+ - **RuboCop** — Ruby 3.3 / Rails 7.1
139
+ - **Docker smoke** — `bin/test-matrix 3.3 7.1`
140
+
141
+ ## Releasing to RubyGems.org
142
+
143
+ Publishing is done by CI when you push a version tag (`v*`). Prefer
144
+ [Trusted Publishing](https://guides.rubygems.org/trusted-publishing/) (OIDC) over
145
+ long-lived API keys.
146
+
147
+ ### One-time setup
148
+
149
+ 1. Create an account on [rubygems.org](https://rubygems.org) (enable MFA).
150
+ 2. Push the gem once if it does not exist yet (optional first bootstrap):
151
+ ```bash
152
+ gem build rails-oidc_provider.gemspec
153
+ gem push rails-oidc_provider-1.0.0.gem # prompts for credentials / OTP
154
+ ```
155
+ 3. On the gem’s RubyGems page → **Trusted Publishing** → add a GitHub provider:
156
+ - **Organization or user:** `pharmony`
157
+ - **Repository:** `rails-oidc_provider`
158
+ - **Workflow filename:** `release.yml`
159
+ - **Environment:** leave blank (unless you set `environment:` in the workflow)
160
+
161
+ No `RUBYGEMS_API_KEY` secret is required when Trusted Publishing is configured.
162
+
163
+ ### Cut a release
164
+
165
+ 1. Bump `RailsOidcProvider::VERSION` in `lib/rails_oidc_provider/version.rb`.
166
+ 2. Update `CHANGELOG.md` for that version.
167
+ 3. Commit and push `master`.
168
+ 4. Tag and push (triggers `.github/workflows/release.yml`):
169
+ ```bash
170
+ bin/release # tags v$VERSION, pushes branch + tag
171
+ bin/release --dry-run # show what would run
172
+ ```
173
+ Equivalent manual steps:
174
+ ```bash
175
+ version=$(ruby -e "require_relative 'lib/rails_oidc_provider/version'; puts RailsOidcProvider::VERSION")
176
+ git tag -a "v${version}" -m "Release v${version}"
177
+ git push origin master
178
+ git push origin "v${version}"
179
+ ```
180
+ 5. Confirm the **Release** workflow is green and the version appears on
181
+ https://rubygems.org/gems/rails-oidc_provider
182
+
183
+ The workflow fails if the tag (e.g. `v1.0.1`) does not match `VERSION` (`1.0.1`).
184
+
185
+ ### Local build only (no push)
186
+
187
+ ```bash
188
+ bundle exec rake build # writes pkg/rails-oidc_provider-*.gem
189
+ bundle exec rake install # install into local gems for smoke testing
190
+ ```
191
+
192
+ ## License
193
+
194
+ The gem is available as open source under the terms of the [MIT License](MIT-LICENSE).
195
+
196
+ Original engine copyright Brand New Box / contributors; this package continues
197
+ under MIT with additional maintenance copyright for the `rails-oidc_provider`
198
+ release line.
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ APP_RAKEFILE = File.expand_path('spec/dummy/Rakefile', __dir__)
10
+ load 'rails/tasks/engine.rake'
11
+
12
+ require 'bundler/gem_tasks'
13
+
14
+ begin
15
+ require 'rspec/core/rake_task'
16
+ RSpec::Core::RakeTask.new(:spec)
17
+ task default: :spec
18
+ rescue LoadError
19
+ task spec: :environment do
20
+ abort 'RSpec is not available. Run `bundle install` first.'
21
+ end
22
+ task default: :spec
23
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OIDCProvider
4
+ # Inherits from the host app's ApplicationController so methods like
5
+ # authenticate_user!, current_user and sign_out (Devise or custom) work.
6
+ class ApplicationController < ::ApplicationController
7
+ def oidc_current_account
8
+ send(OIDCProvider.current_account_method)
9
+ end
10
+
11
+ def current_token
12
+ @current_token ||= request.env[Rack::OAuth2::Server::Resource::ACCESS_TOKEN]
13
+ end
14
+
15
+ def require_authentication
16
+ send(OIDCProvider.current_authentication_method)
17
+ end
18
+
19
+ def require_access_token
20
+ raise Rack::OAuth2::Server::Resource::Bearer::Unauthorized unless current_token
21
+ end
22
+
23
+ def unauthenticate!
24
+ send(OIDCProvider.current_unauthenticate_method)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OIDCProvider
4
+ class AuthorizationsController < ApplicationController
5
+ include Concerns::ConnectEndpoint
6
+
7
+ before_action :require_oauth_request
8
+ before_action :require_response_type_code
9
+ before_action :ensure_claims_is_valid
10
+ before_action :require_client
11
+ before_action :reset_login_if_necessary
12
+ before_action :require_authentication
13
+
14
+ def create
15
+ authorization = build_authorization
16
+
17
+ oauth_response.code = authorization.code
18
+ oauth_response.redirect_uri = @redirect_uri
19
+ oauth_response.approve!
20
+ # allow_other_host: Rails 7+ blocks cross-host redirects by default;
21
+ # OIDC clients register external redirect_uris by design.
22
+ redirect_to oauth_response.location, allow_other_host: true
23
+
24
+ # If we ever need to support denied authorizations that is done by:
25
+ # oauth_request.access_denied!
26
+ end
27
+
28
+ private
29
+
30
+ def build_authorization
31
+ authorization = Authorization.new(
32
+ client_id: @client.identifier,
33
+ nonce: oauth_request.nonce,
34
+ scopes: requested_scopes,
35
+ account: oidc_current_account
36
+ )
37
+
38
+ authorization.claims = JSON.parse(oauth_request.claims) if oauth_request.claims
39
+
40
+ authorization.save
41
+ authorization
42
+ end
43
+
44
+ def ensure_claims_is_valid
45
+ return true unless oauth_request.claims
46
+
47
+ validate_json_is_a_hash!(parse_claims_as_json!)
48
+ rescue Errors::InvalidClaimsFormatError => e
49
+ Rails.logger.error "Invalid claims passed: #{e.message}"
50
+ oauth_request.invalid_request! 'invalid claims format'
51
+ end
52
+
53
+ def parse_claims_as_json!
54
+ JSON.parse(oauth_request.claims)
55
+ rescue JSON::ParserError => e
56
+ Rails.logger.error "Invalid claims passed: #{e.message}"
57
+ oauth_request.invalid_request! 'claims just be a JSON'
58
+ end
59
+
60
+ def require_client
61
+ @client = ClientStore.new.find_by(identifier: oauth_request.client_id) ||
62
+ oauth_request.invalid_request!('not a valid client')
63
+ @redirect_uri = oauth_request.verify_redirect_uri! @client.redirect_uri
64
+ end
65
+
66
+ def requested_scopes
67
+ @requested_scopes ||= (['openid'] + OIDCProvider.supported_scopes.map(&:name)) & oauth_request.scope
68
+ end
69
+ helper_method :requested_scopes
70
+
71
+ def require_response_type_code
72
+ return if oauth_request.response_type == :code
73
+
74
+ oauth_request.unsupported_response_type!
75
+ end
76
+
77
+ def reset_login_if_necessary
78
+ return unless params[:prompt] == 'login'
79
+
80
+ # A `prompt=login` param means that we must prompt the user for sign in.
81
+ # So we will forcibly sign out the user here and then redirect them so they
82
+ # don't get redirected back to the url that contains `prompt=login`
83
+ unauthenticate!
84
+ redirect_to url_for(request.query_parameters.except(:prompt))
85
+ end
86
+
87
+ # Recursive method validating the given `json` is a hash of hashes
88
+ def validate_json_is_a_hash!(json)
89
+ # When reaching the end of the json/hash path, we're getting a `nil`, or
90
+ # a String (hard coded value) or the `essential` boolean value (not yet
91
+ # implemented).
92
+ #
93
+ # For example, when the previous call of this method received
94
+ # `{ email: nil }`, the current call of this method receives `nil`.
95
+ return if json.nil? || json.is_a?(String)
96
+
97
+ raise Errors::InvalidClaimsFormatError unless json.is_a?(Hash)
98
+
99
+ json.each_key { |key| validate_json_is_a_hash!(json[key]) }
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OIDCProvider
4
+ module Concerns
5
+ module ConnectEndpoint
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ helper_method :oauth_request
10
+ rescue_from Rack::OAuth2::Server::Authorize::BadRequest, with: :handle_oauth_error!
11
+ end
12
+
13
+ def oauth_request
14
+ request.env[Rack::OAuth2::Server::Rails::REQUEST]
15
+ end
16
+
17
+ def oauth_response
18
+ request.env[Rack::OAuth2::Server::Rails::RESPONSE]
19
+ end
20
+
21
+ def oauth_error
22
+ request.env[Rack::OAuth2::Server::Rails::ERROR]
23
+ end
24
+
25
+ def handle_oauth_error!(error)
26
+ raise error if error.redirect?
27
+
28
+ # NOTE: rack middleware should handle this error.
29
+
30
+ render plain: error.message, status: error.status
31
+ end
32
+
33
+ def require_oauth_request
34
+ raise oauth_error if oauth_error
35
+ return if oauth_request && oauth_response
36
+
37
+ raise 'should\'t happen'
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OIDCProvider
4
+ class DiscoveryController < ApplicationController
5
+ def show
6
+ case params[:id]
7
+ when 'webfinger'
8
+ webfinger_discovery
9
+ when 'openid-configuration'
10
+ openid_configuration
11
+ else
12
+ render plain: 'Not found', status: :not_found
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def webfinger_discovery
19
+ jrd = {
20
+ links: [{
21
+ rel: OpenIDConnect::Discovery::Provider::Issuer::REL_VALUE,
22
+ href: OIDCProvider.issuer
23
+ }]
24
+ }
25
+ jrd[:subject] = params[:resource] if params[:resource].present?
26
+ render json: jrd, content_type: 'application/jrd+json'
27
+ end
28
+
29
+ def openid_configuration
30
+ config = OpenIDConnect::Discovery::Provider::Config::Response.new(
31
+ authorization_endpoint: absolute_engine_url(:authorizations_url),
32
+ claims_parameter_supported: true,
33
+ claims_supported: OIDCProvider.supported_claims.map(&:to_s),
34
+ end_session_endpoint: absolute_engine_url(:end_session_url),
35
+ grant_types_supported: [:authorization_code],
36
+ id_token_signing_alg_values_supported: [:RS256],
37
+ issuer: OIDCProvider.issuer,
38
+ jwks_uri: absolute_engine_url(:jwks_url),
39
+ response_types_supported: [:code],
40
+ scopes_supported: OIDCProvider.supported_scopes.map(&:name),
41
+ subject_types_supported: [:public],
42
+ token_endpoint: absolute_engine_url(:tokens_url),
43
+ token_endpoint_auth_methods_supported: %w[client_secret_basic client_secret_post],
44
+ userinfo_endpoint: absolute_engine_url(:user_info_url)
45
+ )
46
+ render json: config
47
+ end
48
+
49
+ # issuer may be a full origin (https://host); Rails url helpers want host + protocol.
50
+ def absolute_engine_url(helper_name)
51
+ uri = URI.parse(OIDCProvider.issuer.to_s)
52
+ opts = {}
53
+ if uri.host
54
+ opts[:host] = uri.host
55
+ opts[:protocol] = uri.scheme if uri.scheme
56
+ opts[:port] = uri.port if uri.port && [80, 443].exclude?(uri.port)
57
+ else
58
+ opts[:host] = OIDCProvider.issuer
59
+ end
60
+ public_send(helper_name, **opts)
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OIDCProvider
4
+ class SessionsController < ApplicationController
5
+ before_action :require_authentication
6
+
7
+ def destroy
8
+ unauthenticate!
9
+ return redirect_to '/' unless OIDCProvider.after_sign_out_path
10
+
11
+ path = OIDCProvider.after_sign_out_path
12
+ redirect_to(path.respond_to?(:call) ? path.call(params) : path)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OIDCProvider
4
+ class UserInfosController < ApplicationController
5
+ before_action :require_access_token
6
+
7
+ def show
8
+ render json: user_info
9
+ end
10
+
11
+ private
12
+
13
+ def user_info
14
+ AccountToUserInfo.new(current_token.authorization.user_info_scopes)
15
+ .call(current_token.authorization.account)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OIDCProvider
4
+ class AccessToken < ApplicationRecord
5
+ belongs_to :authorization
6
+
7
+ scope :valid, -> { where(arel_table[:expires_at].gteq(Time.now.utc)) }
8
+
9
+ attribute :token, :string, default: -> { SecureRandom.hex 32 }
10
+ attribute :expires_at, :datetime, default: -> { 1.hour.from_now }
11
+
12
+ def to_bearer_token
13
+ Rack::OAuth2::AccessToken::Bearer.new(
14
+ access_token: token,
15
+ expires_in: (expires_at - Time.zone.now).to_i
16
+ )
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OIDCProvider
4
+ class ApplicationRecord < ActiveRecord::Base
5
+ self.abstract_class = true
6
+ end
7
+ end