|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "uri" |
| 4 | + |
| 5 | +Doorkeeper::OpenidConnect.configure do |
| 6 | + issuer do |resource_owner, application| |
| 7 | + "https://#{URI.parse(application.redirect_uri).host}" |
| 8 | + end |
| 9 | + |
| 10 | + # Set the encryption secret. This would be shared with any other applications |
| 11 | + # that should be able to read the payload of the token. Defaults to "secret". |
| 12 | + key = ENV["JWT_SECRET"] |
| 13 | + key = key.try { |k| Base64.decode64(k) } || DEV_KEY |
| 14 | + signing_key key |
| 15 | + |
| 16 | + subject_types_supported [:public] |
| 17 | + |
| 18 | + resource_owner_from_access_token do |access_token| |
| 19 | + User.find_by(id: access_token.resource_owner_id) |
| 20 | + end |
| 21 | + |
| 22 | + auth_time_from_resource_owner do |resource_owner| |
| 23 | + resource_owner.last_login |
| 24 | + end |
| 25 | + |
| 26 | + reauthenticate_resource_owner do |resource_owner, return_to| |
| 27 | + # Example implementation: |
| 28 | + # store_location_for resource_owner, return_to |
| 29 | + # sign_out resource_owner |
| 30 | + # redirect_to new_user_session_url |
| 31 | + domain = "https://#{request.host}" |
| 32 | + authority = Authority.find_by_domain(request.host) |
| 33 | + url = authority.login_url.gsub("{{url}}", URI.encode_uri_component(return_to)) |
| 34 | + |
| 35 | + redirect_to "#{domain}#{url}" |
| 36 | + end |
| 37 | + |
| 38 | + # Depending on your configuration, a DoubleRenderError could be raised |
| 39 | + # if render/redirect_to is called at some point before this callback is executed. |
| 40 | + # To avoid the DoubleRenderError, you could add these two lines at the beginning |
| 41 | + # of this callback: (Reference: https://github.com/rails/rails/issues/25106) |
| 42 | + # self.response_body = nil |
| 43 | + # @_response_body = nil |
| 44 | + select_account_for_resource_owner do |resource_owner, return_to| |
| 45 | + self.response_body = nil |
| 46 | + @_response_body = nil |
| 47 | + |
| 48 | + # there is no account selection in PlaceOS |
| 49 | + redirect_to return_to |
| 50 | + end |
| 51 | + |
| 52 | + subject do |resource_owner, application| |
| 53 | + # Example implementation: |
| 54 | + # resource_owner.id |
| 55 | + |
| 56 | + # or if you need pairwise subject identifier, implement like below: |
| 57 | + # Digest::SHA256.hexdigest("#{resource_owner.id}#{URI.parse(application.redirect_uri).host}#{'your_secret_salt'}") |
| 58 | + |
| 59 | + resource_owner.id |
| 60 | + end |
| 61 | + |
| 62 | + end_session_endpoint do |
| 63 | + authority = Authority.find_by_domain(request.host) |
| 64 | + authority.logout_url |
| 65 | + end |
| 66 | + |
| 67 | + # Protocol to use when generating URIs for the discovery endpoint, |
| 68 | + # for example if you also use HTTPS in development |
| 69 | + # protocol do |
| 70 | + # :https |
| 71 | + # end |
| 72 | + |
| 73 | + # Expiration time on or after which the ID Token MUST NOT be accepted for processing. (default 120 seconds). |
| 74 | + # expiration 600 |
| 75 | + |
| 76 | + claims do |
| 77 | + claim :sub do |resource_owner| |
| 78 | + resource_owner.id |
| 79 | + end |
| 80 | + |
| 81 | + claim :email do |resource_owner| |
| 82 | + resource_owner.email |
| 83 | + end |
| 84 | + |
| 85 | + claim :full_name do |resource_owner| |
| 86 | + resource_owner.name |
| 87 | + end |
| 88 | + |
| 89 | + claim :given_name do |resource_owner| |
| 90 | + resource_owner.first_name |
| 91 | + end |
| 92 | + |
| 93 | + claim :family_name do |resource_owner| |
| 94 | + resource_owner.last_name |
| 95 | + end |
| 96 | + |
| 97 | + claim :nickname do |resource_owner| |
| 98 | + resource_owner.nickname |
| 99 | + end |
| 100 | + |
| 101 | + claim :phone_number do |resource_owner| |
| 102 | + resource_owner.phone |
| 103 | + end |
| 104 | + |
| 105 | + claim :preferred_username do |resource_owner| |
| 106 | + resource_owner.login_name || resource_owner.email |
| 107 | + end |
| 108 | + end |
| 109 | +end |
0 commit comments