From 84f1ea9d1c4475321152e5c208ed70381ff273b2 Mon Sep 17 00:00:00 2001 From: James Smith Date: Fri, 4 Apr 2025 16:17:12 +0100 Subject: [PATCH 01/17] Add doorkeeper and run installer --- Gemfile | 2 + Gemfile.lock | 3 + config/initializers/doorkeeper.rb | 529 ++++++++++++++++++ config/locales/doorkeeper.en.yml | 155 +++++ config/routes.rb | 1 + ...20250404151644_create_doorkeeper_tables.rb | 99 ++++ 6 files changed, 789 insertions(+) create mode 100644 config/initializers/doorkeeper.rb create mode 100644 config/locales/doorkeeper.en.yml create mode 100644 db/migrate/20250404151644_create_doorkeeper_tables.rb diff --git a/Gemfile b/Gemfile index cc0c02bab..d0f0dd08c 100644 --- a/Gemfile +++ b/Gemfile @@ -183,3 +183,5 @@ gem "rswag", "~> 2.16" gem "warning", "~> 1.5" gem "rack-cors", "~> 2.0" + +gem "doorkeeper", "~> 5.8" diff --git a/Gemfile.lock b/Gemfile.lock index 32cc8d107..0631bc6d4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -206,6 +206,8 @@ GEM diff-lcs (1.5.1) docile (1.4.0) domain_name (0.6.20240107) + doorkeeper (5.8.2) + railties (>= 5) dotenv (3.1.7) dotenv-rails (3.1.7) dotenv (= 3.1.7) @@ -858,6 +860,7 @@ DEPENDENCIES devise (~> 4.9) devise-i18n (~> 1.13) devise_zxcvbn (~> 6.0) + doorkeeper (~> 5.8) dotenv-rails (~> 3.1) erb_lint (~> 0.9.0) factory_bot diff --git a/config/initializers/doorkeeper.rb b/config/initializers/doorkeeper.rb new file mode 100644 index 000000000..7341fbd3c --- /dev/null +++ b/config/initializers/doorkeeper.rb @@ -0,0 +1,529 @@ +# frozen_string_literal: true + +Doorkeeper.configure do + # Change the ORM that doorkeeper will use (requires ORM extensions installed). + # Check the list of supported ORMs here: https://github.com/doorkeeper-gem/doorkeeper#orms + orm :active_record + + # This block will be called to check whether the resource owner is authenticated or not. + resource_owner_authenticator do + raise "Please configure doorkeeper resource_owner_authenticator block located in #{__FILE__}" + # Put your resource owner authentication logic here. + # Example implementation: + # User.find_by(id: session[:user_id]) || redirect_to(new_user_session_url) + end + + # If you didn't skip applications controller from Doorkeeper routes in your application routes.rb + # file then you need to declare this block in order to restrict access to the web interface for + # adding oauth authorized applications. In other case it will return 403 Forbidden response + # every time somebody will try to access the admin web interface. + # + # admin_authenticator do + # # Put your admin authentication logic here. + # # Example implementation: + # + # if current_user + # head :forbidden unless current_user.admin? + # else + # redirect_to sign_in_url + # end + # end + + # You can use your own model classes if you need to extend (or even override) default + # Doorkeeper models such as `Application`, `AccessToken` and `AccessGrant. + # + # By default Doorkeeper ActiveRecord ORM uses its own classes: + # + # access_token_class "Doorkeeper::AccessToken" + # access_grant_class "Doorkeeper::AccessGrant" + # application_class "Doorkeeper::Application" + # + # Don't forget to include Doorkeeper ORM mixins into your custom models: + # + # * ::Doorkeeper::Orm::ActiveRecord::Mixins::AccessToken - for access token + # * ::Doorkeeper::Orm::ActiveRecord::Mixins::AccessGrant - for access grant + # * ::Doorkeeper::Orm::ActiveRecord::Mixins::Application - for application (OAuth2 clients) + # + # For example: + # + # access_token_class "MyAccessToken" + # + # class MyAccessToken < ApplicationRecord + # include ::Doorkeeper::Orm::ActiveRecord::Mixins::AccessToken + # + # self.table_name = "hey_i_wanna_my_name" + # + # def destroy_me! + # destroy + # end + # end + + # Enables polymorphic Resource Owner association for Access Tokens and Access Grants. + # By default this option is disabled. + # + # Make sure you properly setup you database and have all the required columns (run + # `bundle exec rails generate doorkeeper:enable_polymorphic_resource_owner` and execute Rails + # migrations). + # + # If this option enabled, Doorkeeper will store not only Resource Owner primary key + # value, but also it's type (class name). See "Polymorphic Associations" section of + # Rails guides: https://guides.rubyonrails.org/association_basics.html#polymorphic-associations + # + # [NOTE] If you apply this option on already existing project don't forget to manually + # update `resource_owner_type` column in the database and fix migration template as it will + # set NOT NULL constraint for Access Grants table. + # + # use_polymorphic_resource_owner + + # If you are planning to use Doorkeeper in Rails 5 API-only application, then you might + # want to use API mode that will skip all the views management and change the way how + # Doorkeeper responds to a requests. + # + # api_only + + # Enforce token request content type to application/x-www-form-urlencoded. + # It is not enabled by default to not break prior versions of the gem. + # + # enforce_content_type + + # Authorization Code expiration time (default: 10 minutes). + # + # authorization_code_expires_in 10.minutes + + # Access token expiration time (default: 2 hours). + # If you set this to `nil` Doorkeeper will not expire the token and omit expires_in in response. + # It is RECOMMENDED to set expiration time explicitly. + # Prefer access_token_expires_in 100.years or similar, + # which would be functionally equivalent and avoid the risk of unexpected behavior by callers. + # + # access_token_expires_in 2.hours + + # Assign custom TTL for access tokens. Will be used instead of access_token_expires_in + # option if defined. In case the block returns `nil` value Doorkeeper fallbacks to + # +access_token_expires_in+ configuration option value. If you really need to issue a + # non-expiring access token (which is not recommended) then you need to return + # Float::INFINITY from this block. + # + # `context` has the following properties available: + # + # * `client` - the OAuth client application (see Doorkeeper::OAuth::Client) + # * `grant_type` - the grant type of the request (see Doorkeeper::OAuth) + # * `scopes` - the requested scopes (see Doorkeeper::OAuth::Scopes) + # * `resource_owner` - authorized resource owner instance (if present) + # + # custom_access_token_expires_in do |context| + # context.client.additional_settings.implicit_oauth_expiration + # end + + # Use a custom class for generating the access token. + # See https://doorkeeper.gitbook.io/guides/configuration/other-configurations#custom-access-token-generator + # + # access_token_generator '::Doorkeeper::JWT' + + # The controller +Doorkeeper::ApplicationController+ inherits from. + # Defaults to +ActionController::Base+ unless +api_only+ is set, which changes the default to + # +ActionController::API+. The return value of this option must be a stringified class name. + # See https://doorkeeper.gitbook.io/guides/configuration/other-configurations#custom-controllers + # + # base_controller 'ApplicationController' + + # Reuse access token for the same resource owner within an application (disabled by default). + # + # This option protects your application from creating new tokens before old **valid** one becomes + # expired so your database doesn't bloat. Keep in mind that when this option is enabled Doorkeeper + # doesn't update existing token expiration time, it will create a new token instead if no active matching + # token found for the application, resources owner and/or set of scopes. + # Rationale: https://github.com/doorkeeper-gem/doorkeeper/issues/383 + # + # You can not enable this option together with +hash_token_secrets+. + # + # reuse_access_token + + # In case you enabled `reuse_access_token` option Doorkeeper will try to find matching + # token using `matching_token_for` Access Token API that searches for valid records + # in batches in order not to pollute the memory with all the database records. By default + # Doorkeeper uses batch size of 10 000 records. You can increase or decrease this value + # depending on your needs and server capabilities. + # + # token_lookup_batch_size 10_000 + + # Set a limit for token_reuse if using reuse_access_token option + # + # This option limits token_reusability to some extent. + # If not set then access_token will be reused unless it expires. + # Rationale: https://github.com/doorkeeper-gem/doorkeeper/issues/1189 + # + # This option should be a percentage(i.e. (0,100]) + # + # token_reuse_limit 100 + + # Only allow one valid access token obtained via client credentials + # per client. If a new access token is obtained before the old one + # expired, the old one gets revoked (disabled by default) + # + # When enabling this option, make sure that you do not expect multiple processes + # using the same credentials at the same time (e.g. web servers spanning + # multiple machines and/or processes). + # + # revoke_previous_client_credentials_token + + # Only allow one valid access token obtained via authorization code + # per client. If a new access token is obtained before the old one + # expired, the old one gets revoked (disabled by default) + # + # revoke_previous_authorization_code_token + + # Require non-confidential clients to use PKCE when using an authorization code + # to obtain an access_token (disabled by default) + # + # force_pkce + + # Hash access and refresh tokens before persisting them. + # This will disable the possibility to use +reuse_access_token+ + # since plain values can no longer be retrieved. + # + # Note: If you are already a user of doorkeeper and have existing tokens + # in your installation, they will be invalid without adding 'fallback: :plain'. + # + # hash_token_secrets + # By default, token secrets will be hashed using the + # +Doorkeeper::Hashing::SHA256+ strategy. + # + # If you wish to use another hashing implementation, you can override + # this strategy as follows: + # + # hash_token_secrets using: '::Doorkeeper::Hashing::MyCustomHashImpl' + # + # Keep in mind that changing the hashing function will invalidate all existing + # secrets, if there are any. + + # Hash application secrets before persisting them. + # + # hash_application_secrets + # + # By default, applications will be hashed + # with the +Doorkeeper::SecretStoring::SHA256+ strategy. + # + # If you wish to use bcrypt for application secret hashing, uncomment + # this line instead: + # + # hash_application_secrets using: '::Doorkeeper::SecretStoring::BCrypt' + + # When the above option is enabled, and a hashed token or secret is not found, + # you can allow to fall back to another strategy. For users upgrading + # doorkeeper and wishing to enable hashing, you will probably want to enable + # the fallback to plain tokens. + # + # This will ensure that old access tokens and secrets + # will remain valid even if the hashing above is enabled. + # + # This can be done by adding 'fallback: plain', e.g. : + # + # hash_application_secrets using: '::Doorkeeper::SecretStoring::BCrypt', fallback: :plain + + # Issue access tokens with refresh token (disabled by default), you may also + # pass a block which accepts `context` to customize when to give a refresh + # token or not. Similar to +custom_access_token_expires_in+, `context` has + # the following properties: + # + # `client` - the OAuth client application (see Doorkeeper::OAuth::Client) + # `grant_type` - the grant type of the request (see Doorkeeper::OAuth) + # `scopes` - the requested scopes (see Doorkeeper::OAuth::Scopes) + # + # use_refresh_token + + # Provide support for an owner to be assigned to each registered application (disabled by default) + # Optional parameter confirmation: true (default: false) if you want to enforce ownership of + # a registered application + # NOTE: you must also run the rails g doorkeeper:application_owner generator + # to provide the necessary support + # + # enable_application_owner confirmation: false + + # Define access token scopes for your provider + # For more information go to + # https://doorkeeper.gitbook.io/guides/ruby-on-rails/scopes + # + # default_scopes :public + # optional_scopes :write, :update + + # Allows to restrict only certain scopes for grant_type. + # By default, all the scopes will be available for all the grant types. + # + # Keys to this hash should be the name of grant_type and + # values should be the array of scopes for that grant type. + # Note: scopes should be from configured_scopes (i.e. default or optional) + # + # scopes_by_grant_type password: [:write], client_credentials: [:update] + + # Forbids creating/updating applications with arbitrary scopes that are + # not in configuration, i.e. +default_scopes+ or +optional_scopes+. + # (disabled by default) + # + # enforce_configured_scopes + + # Change the way client credentials are retrieved from the request object. + # By default it retrieves first from the `HTTP_AUTHORIZATION` header, then + # falls back to the `:client_id` and `:client_secret` params from the `params` object. + # Check out https://github.com/doorkeeper-gem/doorkeeper/wiki/Changing-how-clients-are-authenticated + # for more information on customization + # + # client_credentials :from_basic, :from_params + + # Change the way access token is authenticated from the request object. + # By default it retrieves first from the `HTTP_AUTHORIZATION` header, then + # falls back to the `:access_token` or `:bearer_token` params from the `params` object. + # Check out https://github.com/doorkeeper-gem/doorkeeper/wiki/Changing-how-clients-are-authenticated + # for more information on customization + # + # access_token_methods :from_bearer_authorization, :from_access_token_param, :from_bearer_param + + # Forces the usage of the HTTPS protocol in non-native redirect uris (enabled + # by default in non-development environments). OAuth2 delegates security in + # communication to the HTTPS protocol so it is wise to keep this enabled. + # + # Callable objects such as proc, lambda, block or any object that responds to + # #call can be used in order to allow conditional checks (to allow non-SSL + # redirects to localhost for example). + # + # force_ssl_in_redirect_uri !Rails.env.development? + # + # force_ssl_in_redirect_uri { |uri| uri.host != 'localhost' } + + # Specify what redirect URI's you want to block during Application creation. + # Any redirect URI is allowed by default. + # + # You can use this option in order to forbid URI's with 'javascript' scheme + # for example. + # + # forbid_redirect_uri { |uri| uri.scheme.to_s.downcase == 'javascript' } + + # Allows to set blank redirect URIs for Applications in case Doorkeeper configured + # to use URI-less OAuth grant flows like Client Credentials or Resource Owner + # Password Credentials. The option is on by default and checks configured grant + # types, but you **need** to manually drop `NOT NULL` constraint from `redirect_uri` + # column for `oauth_applications` database table. + # + # You can completely disable this feature with: + # + # allow_blank_redirect_uri false + # + # Or you can define your custom check: + # + # allow_blank_redirect_uri do |grant_flows, client| + # client.superapp? + # end + + # Specify how authorization errors should be handled. + # By default, doorkeeper renders json errors when access token + # is invalid, expired, revoked or has invalid scopes. + # + # If you want to render error response yourself (i.e. rescue exceptions), + # set +handle_auth_errors+ to `:raise` and rescue Doorkeeper::Errors::InvalidToken + # or following specific errors: + # + # Doorkeeper::Errors::TokenForbidden, Doorkeeper::Errors::TokenExpired, + # Doorkeeper::Errors::TokenRevoked, Doorkeeper::Errors::TokenUnknown + # + # handle_auth_errors :raise + # + # If you want to redirect back to the client application in accordance with + # https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.2.1, you can set + # +handle_auth_errors+ to :redirect + # + # handle_auth_errors :redirect + + # Customize token introspection response. + # Allows to add your own fields to default one that are required by the OAuth spec + # for the introspection response. It could be `sub`, `aud` and so on. + # This configuration option can be a proc, lambda or any Ruby object responds + # to `.call` method and result of it's invocation must be a Hash. + # + # custom_introspection_response do |token, context| + # { + # "sub": "Z5O3upPC88QrAjx00dis", + # "aud": "https://protected.example.net/resource", + # "username": User.find(token.resource_owner_id).username + # } + # end + # + # or + # + # custom_introspection_response CustomIntrospectionResponder + + # Specify what grant flows are enabled in array of Strings. The valid + # strings and the flows they enable are: + # + # "authorization_code" => Authorization Code Grant Flow + # "implicit" => Implicit Grant Flow + # "password" => Resource Owner Password Credentials Grant Flow + # "client_credentials" => Client Credentials Grant Flow + # + # If not specified, Doorkeeper enables authorization_code and + # client_credentials. + # + # implicit and password grant flows have risks that you should understand + # before enabling: + # https://datatracker.ietf.org/doc/html/rfc6819#section-4.4.2 + # https://datatracker.ietf.org/doc/html/rfc6819#section-4.4.3 + # + # grant_flows %w[authorization_code client_credentials] + + # Allows to customize OAuth grant flows that +each+ application support. + # You can configure a custom block (or use a class respond to `#call`) that must + # return `true` in case Application instance supports requested OAuth grant flow + # during the authorization request to the server. This configuration +doesn't+ + # set flows per application, it only allows to check if application supports + # specific grant flow. + # + # For example you can add an additional database column to `oauth_applications` table, + # say `t.array :grant_flows, default: []`, and store allowed grant flows that can + # be used with this application there. Then when authorization requested Doorkeeper + # will call this block to check if specific Application (passed with client_id and/or + # client_secret) is allowed to perform the request for the specific grant type + # (authorization, password, client_credentials, etc). + # + # Example of the block: + # + # ->(flow, client) { client.grant_flows.include?(flow) } + # + # In case this option invocation result is `false`, Doorkeeper server returns + # :unauthorized_client error and stops the request. + # + # @param allow_grant_flow_for_client [Proc] Block or any object respond to #call + # @return [Boolean] `true` if allow or `false` if forbid the request + # + # allow_grant_flow_for_client do |grant_flow, client| + # # `grant_flows` is an Array column with grant + # # flows that application supports + # + # client.grant_flows.include?(grant_flow) + # end + + # If you need arbitrary Resource Owner-Client authorization you can enable this option + # and implement the check your need. Config option must respond to #call and return + # true in case resource owner authorized for the specific application or false in other + # cases. + # + # By default all Resource Owners are authorized to any Client (application). + # + # authorize_resource_owner_for_client do |client, resource_owner| + # resource_owner.admin? || client.owners_allowlist.include?(resource_owner) + # end + + # Allows additional data fields to be sent while granting access to an application, + # and for this additional data to be included in subsequently generated access tokens. + # The 'authorizations/new' page will need to be overridden to include this additional data + # in the request params when granting access. The access grant and access token models + # will both need to respond to these additional data fields, and have a database column + # to store them in. + # + # Example: + # You have a multi-tenanted platform and want to be able to grant access to a specific + # tenant, rather than all the tenants a user has access to. You can use this config + # option to specify that a ':tenant_id' will be passed when authorizing. This tenant_id + # will be included in the access tokens. When a request is made with one of these access + # tokens, you can check that the requested data belongs to the specified tenant. + # + # Default value is an empty Array: [] + # custom_access_token_attributes [:tenant_id] + + # Hook into the strategies' request & response life-cycle in case your + # application needs advanced customization or logging: + # + # before_successful_strategy_response do |request| + # puts "BEFORE HOOK FIRED! #{request}" + # end + # + # after_successful_strategy_response do |request, response| + # puts "AFTER HOOK FIRED! #{request}, #{response}" + # end + + # Hook into Authorization flow in order to implement Single Sign Out + # or add any other functionality. Inside the block you have an access + # to `controller` (authorizations controller instance) and `context` + # (Doorkeeper::OAuth::Hooks::Context instance) which provides pre auth + # or auth objects with issued token based on hook type (before or after). + # + # before_successful_authorization do |controller, context| + # Rails.logger.info(controller.request.params.inspect) + # + # Rails.logger.info(context.pre_auth.inspect) + # end + # + # after_successful_authorization do |controller, context| + # controller.session[:logout_urls] << + # Doorkeeper::Application + # .find_by(controller.request.params.slice(:redirect_uri)) + # .logout_uri + # + # Rails.logger.info(context.auth.inspect) + # Rails.logger.info(context.issued_token) + # end + + # Under some circumstances you might want to have applications auto-approved, + # so that the user skips the authorization step. + # For example if dealing with a trusted application. + # + # skip_authorization do |resource_owner, client| + # client.superapp? or resource_owner.admin? + # end + + # Configure custom constraints for the Token Introspection request. + # By default this configuration option allows to introspect a token by another + # token of the same application, OR to introspect the token that belongs to + # authorized client (from authenticated client) OR when token doesn't + # belong to any client (public token). Otherwise requester has no access to the + # introspection and it will return response as stated in the RFC. + # + # Block arguments: + # + # @param token [Doorkeeper::AccessToken] + # token to be introspected + # + # @param authorized_client [Doorkeeper::Application] + # authorized client (if request is authorized using Basic auth with + # Client Credentials for example) + # + # @param authorized_token [Doorkeeper::AccessToken] + # Bearer token used to authorize the request + # + # In case the block returns `nil` or `false` introspection responses with 401 status code + # when using authorized token to introspect, or you'll get 200 with { "active": false } body + # when using authorized client to introspect as stated in the + # RFC 7662 section 2.2. Introspection Response. + # + # Using with caution: + # Keep in mind that these three parameters pass to block can be nil as following case: + # `authorized_client` is nil if and only if `authorized_token` is present, and vice versa. + # `token` will be nil if and only if `authorized_token` is present. + # So remember to use `&` or check if it is present before calling method on + # them to make sure you doesn't get NoMethodError exception. + # + # You can define your custom check: + # + # allow_token_introspection do |token, authorized_client, authorized_token| + # if authorized_token + # # customize: require `introspection` scope + # authorized_token.application == token&.application || + # authorized_token.scopes.include?("introspection") + # elsif token.application + # # `protected_resource` is a new database boolean column, for example + # authorized_client == token.application || authorized_client.protected_resource? + # else + # # public token (when token.application is nil, token doesn't belong to any application) + # true + # end + # end + # + # Or you can completely disable any token introspection: + # + # allow_token_introspection false + # + # If you need to block the request at all, then configure your routes.rb or web-server + # like nginx to forbid the request. + + # WWW-Authenticate Realm (default: "Doorkeeper"). + # + # realm "Doorkeeper" +end diff --git a/config/locales/doorkeeper.en.yml b/config/locales/doorkeeper.en.yml new file mode 100644 index 000000000..c9190526f --- /dev/null +++ b/config/locales/doorkeeper.en.yml @@ -0,0 +1,155 @@ +en: + activerecord: + attributes: + doorkeeper/application: + name: 'Name' + redirect_uri: 'Redirect URI' + errors: + models: + doorkeeper/application: + attributes: + redirect_uri: + fragment_present: 'cannot contain a fragment.' + invalid_uri: 'must be a valid URI.' + unspecified_scheme: 'must specify a scheme.' + relative_uri: 'must be an absolute URI.' + secured_uri: 'must be an HTTPS/SSL URI.' + forbidden_uri: 'is forbidden by the server.' + scopes: + not_match_configured: "doesn't match configured on the server." + + doorkeeper: + applications: + confirmations: + destroy: 'Are you sure?' + buttons: + edit: 'Edit' + destroy: 'Destroy' + submit: 'Submit' + cancel: 'Cancel' + authorize: 'Authorize' + form: + error: 'Whoops! Check your form for possible errors' + help: + confidential: 'Application will be used where the client secret can be kept confidential. Native mobile apps and Single Page Apps are considered non-confidential.' + redirect_uri: 'Use one line per URI' + blank_redirect_uri: "Leave it blank if you configured your provider to use Client Credentials, Resource Owner Password Credentials or any other grant type that doesn't require redirect URI." + scopes: 'Separate scopes with spaces. Leave blank to use the default scopes.' + edit: + title: 'Edit application' + index: + title: 'Your applications' + new: 'New Application' + name: 'Name' + callback_url: 'Callback URL' + confidential: 'Confidential?' + actions: 'Actions' + confidentiality: + 'yes': 'Yes' + 'no': 'No' + new: + title: 'New Application' + show: + title: 'Application: %{name}' + application_id: 'UID' + secret: 'Secret' + secret_hashed: 'Secret hashed' + scopes: 'Scopes' + confidential: 'Confidential' + callback_urls: 'Callback urls' + actions: 'Actions' + not_defined: 'Not defined' + + authorizations: + buttons: + authorize: 'Authorize' + deny: 'Deny' + error: + title: 'An error has occurred' + new: + title: 'Authorization required' + prompt: 'Authorize %{client_name} to use your account?' + able_to: 'This application will be able to' + show: + title: 'Authorization code' + form_post: + title: 'Submit this form' + + authorized_applications: + confirmations: + revoke: 'Are you sure?' + buttons: + revoke: 'Revoke' + index: + title: 'Your authorized applications' + application: 'Application' + created_at: 'Created At' + date_format: '%Y-%m-%d %H:%M:%S' + + pre_authorization: + status: 'Pre-authorization' + + errors: + messages: + # Common error messages + invalid_request: + unknown: 'The request is missing a required parameter, includes an unsupported parameter value, or is otherwise malformed.' + missing_param: 'Missing required parameter: %{value}.' + request_not_authorized: 'Request need to be authorized. Required parameter for authorizing request is missing or invalid.' + invalid_code_challenge: 'Code challenge is required.' + invalid_redirect_uri: "The requested redirect uri is malformed or doesn't match client redirect URI." + unauthorized_client: 'The client is not authorized to perform this request using this method.' + access_denied: 'The resource owner or authorization server denied the request.' + invalid_scope: 'The requested scope is invalid, unknown, or malformed.' + invalid_code_challenge_method: + zero: 'The authorization server does not support PKCE as there are no accepted code_challenge_method values.' + one: 'The code_challenge_method must be %{challenge_methods}.' + other: 'The code_challenge_method must be one of %{challenge_methods}.' + server_error: 'The authorization server encountered an unexpected condition which prevented it from fulfilling the request.' + temporarily_unavailable: 'The authorization server is currently unable to handle the request due to a temporary overloading or maintenance of the server.' + + # Configuration error messages + credential_flow_not_configured: 'Resource Owner Password Credentials flow failed due to Doorkeeper.configure.resource_owner_from_credentials being unconfigured.' + resource_owner_authenticator_not_configured: 'Resource Owner find failed due to Doorkeeper.configure.resource_owner_authenticator being unconfigured.' + admin_authenticator_not_configured: 'Access to admin panel is forbidden due to Doorkeeper.configure.admin_authenticator being unconfigured.' + + # Access grant errors + unsupported_response_type: 'The authorization server does not support this response type.' + unsupported_response_mode: 'The authorization server does not support this response mode.' + + # Access token errors + invalid_client: 'Client authentication failed due to unknown client, no client authentication included, or unsupported authentication method.' + invalid_grant: 'The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client.' + unsupported_grant_type: 'The authorization grant type is not supported by the authorization server.' + + invalid_token: + revoked: "The access token was revoked" + expired: "The access token expired" + unknown: "The access token is invalid" + revoke: + unauthorized: "You are not authorized to revoke this token" + + forbidden_token: + missing_scope: 'Access to this resource requires scope "%{oauth_scopes}".' + + flash: + applications: + create: + notice: 'Application created.' + destroy: + notice: 'Application deleted.' + update: + notice: 'Application updated.' + authorized_applications: + destroy: + notice: 'Application revoked.' + + layouts: + admin: + title: 'Doorkeeper' + nav: + oauth2_provider: 'OAuth2 Provider' + applications: 'Applications' + home: 'Home' + application: + title: 'OAuth authorization required' diff --git a/config/routes.rb b/config/routes.rb index a039583ae..bf252a8ec 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,7 @@ require "federails" Rails.application.routes.draw do + use_doorkeeper get ".well-known/change-password", to: redirect("/users/edit") get "health" => "rails/health#show", :as => :rails_health_check get "problems/index" diff --git a/db/migrate/20250404151644_create_doorkeeper_tables.rb b/db/migrate/20250404151644_create_doorkeeper_tables.rb new file mode 100644 index 000000000..8458de649 --- /dev/null +++ b/db/migrate/20250404151644_create_doorkeeper_tables.rb @@ -0,0 +1,99 @@ +# frozen_string_literal: true + +class CreateDoorkeeperTables < ActiveRecord::Migration[7.2] + def change + create_table :oauth_applications do |t| + t.string :name, null: false + t.string :uid, null: false + # Remove `null: false` or use conditional constraint if you are planning to use public clients. + t.string :secret, null: false + + # Remove `null: false` if you are planning to use grant flows + # that doesn't require redirect URI to be used during authorization + # like Client Credentials flow or Resource Owner Password. + t.text :redirect_uri, null: false + t.string :scopes, null: false, default: '' + t.boolean :confidential, null: false, default: true + t.timestamps null: false + end + + add_index :oauth_applications, :uid, unique: true + + create_table :oauth_access_grants do |t| + t.references :resource_owner, null: false + t.references :application, null: false + t.string :token, null: false + t.integer :expires_in, null: false + t.text :redirect_uri, null: false + t.string :scopes, null: false, default: '' + t.datetime :created_at, null: false + t.datetime :revoked_at + end + + add_index :oauth_access_grants, :token, unique: true + add_foreign_key( + :oauth_access_grants, + :oauth_applications, + column: :application_id + ) + + create_table :oauth_access_tokens do |t| + t.references :resource_owner, index: true + + # Remove `null: false` if you are planning to use Password + # Credentials Grant flow that doesn't require an application. + t.references :application, null: false + + # If you use a custom token generator you may need to change this column + # from string to text, so that it accepts tokens larger than 255 + # characters. More info on custom token generators in: + # https://github.com/doorkeeper-gem/doorkeeper/tree/v3.0.0.rc1#custom-access-token-generator + # + # t.text :token, null: false + t.string :token, null: false + + t.string :refresh_token + t.integer :expires_in + t.string :scopes + t.datetime :created_at, null: false + t.datetime :revoked_at + + # The authorization server MAY issue a new refresh token, in which case + # *the client MUST discard the old refresh token* and replace it with the + # new refresh token. The authorization server MAY revoke the old + # refresh token after issuing a new refresh token to the client. + # @see https://datatracker.ietf.org/doc/html/rfc6749#section-6 + # + # Doorkeeper implementation: if there is a `previous_refresh_token` column, + # refresh tokens will be revoked after a related access token is used. + # If there is no `previous_refresh_token` column, previous tokens are + # revoked as soon as a new access token is created. + # + # Comment out this line if you want refresh tokens to be instantly + # revoked after use. + t.string :previous_refresh_token, null: false, default: "" + end + + add_index :oauth_access_tokens, :token, unique: true + + # See https://github.com/doorkeeper-gem/doorkeeper/issues/1592 + if ActiveRecord::Base.connection.adapter_name == "SQLServer" + execute <<~SQL.squish + CREATE UNIQUE NONCLUSTERED INDEX index_oauth_access_tokens_on_refresh_token ON oauth_access_tokens(refresh_token) + WHERE refresh_token IS NOT NULL + SQL + else + add_index :oauth_access_tokens, :refresh_token, unique: true + end + + add_foreign_key( + :oauth_access_tokens, + :oauth_applications, + column: :application_id + ) + + # Uncomment below to ensure a valid reference to the resource owner's table + # add_foreign_key :oauth_access_grants, , column: :resource_owner_id + # add_foreign_key :oauth_access_tokens, , column: :resource_owner_id + end +end From 1c0259708d2fcf417fd0b108624bc6c5723b07cc Mon Sep 17 00:00:00 2001 From: James Smith Date: Fri, 4 Apr 2025 16:20:43 +0100 Subject: [PATCH 02/17] lint doorkeeper generated files --- ...20250404151644_create_doorkeeper_tables.rb | 56 ++++++++++--------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/db/migrate/20250404151644_create_doorkeeper_tables.rb b/db/migrate/20250404151644_create_doorkeeper_tables.rb index 8458de649..e08b19a21 100644 --- a/db/migrate/20250404151644_create_doorkeeper_tables.rb +++ b/db/migrate/20250404151644_create_doorkeeper_tables.rb @@ -3,30 +3,30 @@ class CreateDoorkeeperTables < ActiveRecord::Migration[7.2] def change create_table :oauth_applications do |t| - t.string :name, null: false - t.string :uid, null: false + t.string :name, null: false + t.string :uid, null: false # Remove `null: false` or use conditional constraint if you are planning to use public clients. - t.string :secret, null: false + t.string :secret, null: false # Remove `null: false` if you are planning to use grant flows # that doesn't require redirect URI to be used during authorization # like Client Credentials flow or Resource Owner Password. - t.text :redirect_uri, null: false - t.string :scopes, null: false, default: '' + t.text :redirect_uri, null: false + t.string :scopes, null: false, default: "" t.boolean :confidential, null: false, default: true - t.timestamps null: false + t.timestamps null: false end add_index :oauth_applications, :uid, unique: true create_table :oauth_access_grants do |t| - t.references :resource_owner, null: false - t.references :application, null: false - t.string :token, null: false - t.integer :expires_in, null: false - t.text :redirect_uri, null: false - t.string :scopes, null: false, default: '' - t.datetime :created_at, null: false + t.references :resource_owner, null: false + t.references :application, null: false + t.string :token, null: false + t.integer :expires_in, null: false + t.text :redirect_uri, null: false + t.string :scopes, null: false, default: "" + t.datetime :created_at, null: false t.datetime :revoked_at end @@ -42,7 +42,7 @@ def change # Remove `null: false` if you are planning to use Password # Credentials Grant flow that doesn't require an application. - t.references :application, null: false + t.references :application, null: false # If you use a custom token generator you may need to change this column # from string to text, so that it accepts tokens larger than 255 @@ -52,9 +52,9 @@ def change # t.text :token, null: false t.string :token, null: false - t.string :refresh_token - t.integer :expires_in - t.string :scopes + t.string :refresh_token + t.integer :expires_in + t.string :scopes t.datetime :created_at, null: false t.datetime :revoked_at @@ -71,19 +71,23 @@ def change # # Comment out this line if you want refresh tokens to be instantly # revoked after use. - t.string :previous_refresh_token, null: false, default: "" + t.string :previous_refresh_token, null: false, default: "" end add_index :oauth_access_tokens, :token, unique: true - # See https://github.com/doorkeeper-gem/doorkeeper/issues/1592 - if ActiveRecord::Base.connection.adapter_name == "SQLServer" - execute <<~SQL.squish - CREATE UNIQUE NONCLUSTERED INDEX index_oauth_access_tokens_on_refresh_token ON oauth_access_tokens(refresh_token) - WHERE refresh_token IS NOT NULL - SQL - else - add_index :oauth_access_tokens, :refresh_token, unique: true + reversible do |dir| + dir.up do + # See https://github.com/doorkeeper-gem/doorkeeper/issues/1592 + if ActiveRecord::Base.connection.adapter_name == "SQLServer" + execute <<~SQL.squish + CREATE UNIQUE NONCLUSTERED INDEX index_oauth_access_tokens_on_refresh_token ON oauth_access_tokens(refresh_token) + WHERE refresh_token IS NOT NULL + SQL + else + add_index :oauth_access_tokens, :refresh_token, unique: true + end + end end add_foreign_key( From 7a2543cc37ff1995ab29f8a71d2963ab6d895d50 Mon Sep 17 00:00:00 2001 From: James Smith Date: Fri, 4 Apr 2025 16:20:59 +0100 Subject: [PATCH 03/17] link doorkeeper tables to user model --- app/models/user.rb | 12 ++++++++++++ .../20250404151644_create_doorkeeper_tables.rb | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index f9820bff7..1d3fbc6f1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -46,6 +46,18 @@ class User < ApplicationRecord attribute :problem_settings, :json attribute :file_list_settings, :json + has_many :access_grants, + class_name: "Doorkeeper::AccessGrant", + foreign_key: :resource_owner_id, + dependent: :delete_all, + inverse_of: :resource_owner + + has_many :access_tokens, + class_name: "Doorkeeper::AccessToken", + foreign_key: :resource_owner_id, + dependent: :delete_all, + inverse_of: :resource_owner + def federails_name username end diff --git a/db/migrate/20250404151644_create_doorkeeper_tables.rb b/db/migrate/20250404151644_create_doorkeeper_tables.rb index e08b19a21..2dcbd3997 100644 --- a/db/migrate/20250404151644_create_doorkeeper_tables.rb +++ b/db/migrate/20250404151644_create_doorkeeper_tables.rb @@ -97,7 +97,7 @@ def change ) # Uncomment below to ensure a valid reference to the resource owner's table - # add_foreign_key :oauth_access_grants, , column: :resource_owner_id - # add_foreign_key :oauth_access_tokens, , column: :resource_owner_id + add_foreign_key :oauth_access_grants, :users, column: :resource_owner_id + add_foreign_key :oauth_access_tokens, :users, column: :resource_owner_id end end From 31f00d7a0d1826dfdf25ada4748d6efc3b5b88b6 Mon Sep 17 00:00:00 2001 From: James Smith Date: Fri, 4 Apr 2025 16:27:55 +0100 Subject: [PATCH 04/17] run doorkeeper migration --- ...0250408111644_create_doorkeeper_tables.rb} | 7 +-- ...20250408145956_add_owner_to_application.rb | 9 ++++ db/schema.rb | 51 ++++++++++++++++++- 3 files changed, 60 insertions(+), 7 deletions(-) rename db/migrate/{20250404151644_create_doorkeeper_tables.rb => 20250408111644_create_doorkeeper_tables.rb} (91%) create mode 100644 db/migrate/20250408145956_add_owner_to_application.rb diff --git a/db/migrate/20250404151644_create_doorkeeper_tables.rb b/db/migrate/20250408111644_create_doorkeeper_tables.rb similarity index 91% rename from db/migrate/20250404151644_create_doorkeeper_tables.rb rename to db/migrate/20250408111644_create_doorkeeper_tables.rb index 2dcbd3997..e9fba4fcd 100644 --- a/db/migrate/20250404151644_create_doorkeeper_tables.rb +++ b/db/migrate/20250408111644_create_doorkeeper_tables.rb @@ -5,13 +5,8 @@ def change create_table :oauth_applications do |t| t.string :name, null: false t.string :uid, null: false - # Remove `null: false` or use conditional constraint if you are planning to use public clients. t.string :secret, null: false - - # Remove `null: false` if you are planning to use grant flows - # that doesn't require redirect URI to be used during authorization - # like Client Credentials flow or Resource Owner Password. - t.text :redirect_uri, null: false + t.text :redirect_uri t.string :scopes, null: false, default: "" t.boolean :confidential, null: false, default: true t.timestamps null: false diff --git a/db/migrate/20250408145956_add_owner_to_application.rb b/db/migrate/20250408145956_add_owner_to_application.rb new file mode 100644 index 000000000..897ee0b60 --- /dev/null +++ b/db/migrate/20250408145956_add_owner_to_application.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class AddOwnerToApplication < ActiveRecord::Migration[7.2] + def change + add_column :oauth_applications, :owner_id, :bigint, null: true + add_column :oauth_applications, :owner_type, :string, null: true + add_index :oauth_applications, [:owner_id, :owner_type] + end +end diff --git a/db/schema.rb b/db/schema.rb index c1ac4e15c..d8fe4dc34 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2025_04_07_160722) do +ActiveRecord::Schema[7.2].define(version: 2025_04_08_145956) do create_table "caber_relations", force: :cascade do |t| t.string "subject_type" t.integer "subject_id" @@ -262,6 +262,51 @@ t.index ["slug"], name: "index_models_on_slug" end + create_table "oauth_access_grants", force: :cascade do |t| + t.integer "resource_owner_id", null: false + t.integer "application_id", null: false + t.string "token", null: false + t.integer "expires_in", null: false + t.text "redirect_uri", null: false + t.string "scopes", default: "", null: false + t.datetime "created_at", null: false + t.datetime "revoked_at" + t.index ["application_id"], name: "index_oauth_access_grants_on_application_id" + t.index ["resource_owner_id"], name: "index_oauth_access_grants_on_resource_owner_id" + t.index ["token"], name: "index_oauth_access_grants_on_token", unique: true + end + + create_table "oauth_access_tokens", force: :cascade do |t| + t.integer "resource_owner_id" + t.integer "application_id", null: false + t.string "token", null: false + t.string "refresh_token" + t.integer "expires_in" + t.string "scopes" + t.datetime "created_at", null: false + t.datetime "revoked_at" + t.string "previous_refresh_token", default: "", null: false + t.index ["application_id"], name: "index_oauth_access_tokens_on_application_id" + t.index ["refresh_token"], name: "index_oauth_access_tokens_on_refresh_token", unique: true + t.index ["resource_owner_id"], name: "index_oauth_access_tokens_on_resource_owner_id" + t.index ["token"], name: "index_oauth_access_tokens_on_token", unique: true + end + + create_table "oauth_applications", force: :cascade do |t| + t.string "name", null: false + t.string "uid", null: false + t.string "secret", null: false + t.text "redirect_uri" + t.string "scopes", default: "", null: false + t.boolean "confidential", default: true, null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.bigint "owner_id" + t.string "owner_type" + t.index ["owner_id", "owner_type"], name: "index_oauth_applications_on_owner_id_and_owner_type" + t.index ["uid"], name: "index_oauth_applications_on_uid", unique: true + end + create_table "problems", force: :cascade do |t| t.string "problematic_type" t.integer "problematic_id" @@ -367,5 +412,9 @@ add_foreign_key "models", "collections" add_foreign_key "models", "creators" add_foreign_key "models", "libraries" + add_foreign_key "oauth_access_grants", "oauth_applications", column: "application_id" + add_foreign_key "oauth_access_grants", "users", column: "resource_owner_id" + add_foreign_key "oauth_access_tokens", "oauth_applications", column: "application_id" + add_foreign_key "oauth_access_tokens", "users", column: "resource_owner_id" add_foreign_key "taggings", "tags" end From c1f0cb632114610a936b9dead9d662aa12da9941 Mon Sep 17 00:00:00 2001 From: James Smith Date: Fri, 4 Apr 2025 16:28:09 +0100 Subject: [PATCH 05/17] add user and scope config to doorkeeper config --- config/initializers/doorkeeper.rb | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/config/initializers/doorkeeper.rb b/config/initializers/doorkeeper.rb index 7341fbd3c..fe8ef05ee 100644 --- a/config/initializers/doorkeeper.rb +++ b/config/initializers/doorkeeper.rb @@ -7,27 +7,12 @@ # This block will be called to check whether the resource owner is authenticated or not. resource_owner_authenticator do - raise "Please configure doorkeeper resource_owner_authenticator block located in #{__FILE__}" - # Put your resource owner authentication logic here. - # Example implementation: - # User.find_by(id: session[:user_id]) || redirect_to(new_user_session_url) + current_user || warden.authenticate!(scope: :user) end - # If you didn't skip applications controller from Doorkeeper routes in your application routes.rb - # file then you need to declare this block in order to restrict access to the web interface for - # adding oauth authorized applications. In other case it will return 403 Forbidden response - # every time somebody will try to access the admin web interface. - # - # admin_authenticator do - # # Put your admin authentication logic here. - # # Example implementation: - # - # if current_user - # head :forbidden unless current_user.admin? - # else - # redirect_to sign_in_url - # end - # end + admin_authenticator do |_routes| + current_user || warden.authenticate!(scope: :user) + end # You can use your own model classes if you need to extend (or even override) default # Doorkeeper models such as `Application`, `AccessToken` and `AccessGrant. @@ -244,8 +229,8 @@ # For more information go to # https://doorkeeper.gitbook.io/guides/ruby-on-rails/scopes # - # default_scopes :public - # optional_scopes :write, :update + default_scopes :read + optional_scopes :write # Allows to restrict only certain scopes for grant_type. # By default, all the scopes will be available for all the grant types. @@ -260,7 +245,7 @@ # not in configuration, i.e. +default_scopes+ or +optional_scopes+. # (disabled by default) # - # enforce_configured_scopes + enforce_configured_scopes # Change the way client credentials are retrieved from the request object. # By default it retrieves first from the `HTTP_AUTHORIZATION` header, then From b941588f806db1049d23b5a8f1be0fc4f35fba55 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 7 Apr 2025 16:45:07 +0100 Subject: [PATCH 06/17] applications are owned by users --- app/models/user.rb | 6 + config/initializers/doorkeeper.rb | 266 +----------------------------- 2 files changed, 8 insertions(+), 264 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 1d3fbc6f1..bdb2329ee 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -58,6 +58,12 @@ class User < ApplicationRecord dependent: :delete_all, inverse_of: :resource_owner + has_many :oauth_applications, + class_name: "Doorkeeper::Application", + as: :owner, + dependent: :delete_all, + inverse_of: :owner + def federails_name username end diff --git a/config/initializers/doorkeeper.rb b/config/initializers/doorkeeper.rb index fe8ef05ee..900dd1362 100644 --- a/config/initializers/doorkeeper.rb +++ b/config/initializers/doorkeeper.rb @@ -247,268 +247,6 @@ # enforce_configured_scopes - # Change the way client credentials are retrieved from the request object. - # By default it retrieves first from the `HTTP_AUTHORIZATION` header, then - # falls back to the `:client_id` and `:client_secret` params from the `params` object. - # Check out https://github.com/doorkeeper-gem/doorkeeper/wiki/Changing-how-clients-are-authenticated - # for more information on customization - # - # client_credentials :from_basic, :from_params - - # Change the way access token is authenticated from the request object. - # By default it retrieves first from the `HTTP_AUTHORIZATION` header, then - # falls back to the `:access_token` or `:bearer_token` params from the `params` object. - # Check out https://github.com/doorkeeper-gem/doorkeeper/wiki/Changing-how-clients-are-authenticated - # for more information on customization - # - # access_token_methods :from_bearer_authorization, :from_access_token_param, :from_bearer_param - - # Forces the usage of the HTTPS protocol in non-native redirect uris (enabled - # by default in non-development environments). OAuth2 delegates security in - # communication to the HTTPS protocol so it is wise to keep this enabled. - # - # Callable objects such as proc, lambda, block or any object that responds to - # #call can be used in order to allow conditional checks (to allow non-SSL - # redirects to localhost for example). - # - # force_ssl_in_redirect_uri !Rails.env.development? - # - # force_ssl_in_redirect_uri { |uri| uri.host != 'localhost' } - - # Specify what redirect URI's you want to block during Application creation. - # Any redirect URI is allowed by default. - # - # You can use this option in order to forbid URI's with 'javascript' scheme - # for example. - # - # forbid_redirect_uri { |uri| uri.scheme.to_s.downcase == 'javascript' } - - # Allows to set blank redirect URIs for Applications in case Doorkeeper configured - # to use URI-less OAuth grant flows like Client Credentials or Resource Owner - # Password Credentials. The option is on by default and checks configured grant - # types, but you **need** to manually drop `NOT NULL` constraint from `redirect_uri` - # column for `oauth_applications` database table. - # - # You can completely disable this feature with: - # - # allow_blank_redirect_uri false - # - # Or you can define your custom check: - # - # allow_blank_redirect_uri do |grant_flows, client| - # client.superapp? - # end - - # Specify how authorization errors should be handled. - # By default, doorkeeper renders json errors when access token - # is invalid, expired, revoked or has invalid scopes. - # - # If you want to render error response yourself (i.e. rescue exceptions), - # set +handle_auth_errors+ to `:raise` and rescue Doorkeeper::Errors::InvalidToken - # or following specific errors: - # - # Doorkeeper::Errors::TokenForbidden, Doorkeeper::Errors::TokenExpired, - # Doorkeeper::Errors::TokenRevoked, Doorkeeper::Errors::TokenUnknown - # - # handle_auth_errors :raise - # - # If you want to redirect back to the client application in accordance with - # https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.2.1, you can set - # +handle_auth_errors+ to :redirect - # - # handle_auth_errors :redirect - - # Customize token introspection response. - # Allows to add your own fields to default one that are required by the OAuth spec - # for the introspection response. It could be `sub`, `aud` and so on. - # This configuration option can be a proc, lambda or any Ruby object responds - # to `.call` method and result of it's invocation must be a Hash. - # - # custom_introspection_response do |token, context| - # { - # "sub": "Z5O3upPC88QrAjx00dis", - # "aud": "https://protected.example.net/resource", - # "username": User.find(token.resource_owner_id).username - # } - # end - # - # or - # - # custom_introspection_response CustomIntrospectionResponder - - # Specify what grant flows are enabled in array of Strings. The valid - # strings and the flows they enable are: - # - # "authorization_code" => Authorization Code Grant Flow - # "implicit" => Implicit Grant Flow - # "password" => Resource Owner Password Credentials Grant Flow - # "client_credentials" => Client Credentials Grant Flow - # - # If not specified, Doorkeeper enables authorization_code and - # client_credentials. - # - # implicit and password grant flows have risks that you should understand - # before enabling: - # https://datatracker.ietf.org/doc/html/rfc6819#section-4.4.2 - # https://datatracker.ietf.org/doc/html/rfc6819#section-4.4.3 - # - # grant_flows %w[authorization_code client_credentials] - - # Allows to customize OAuth grant flows that +each+ application support. - # You can configure a custom block (or use a class respond to `#call`) that must - # return `true` in case Application instance supports requested OAuth grant flow - # during the authorization request to the server. This configuration +doesn't+ - # set flows per application, it only allows to check if application supports - # specific grant flow. - # - # For example you can add an additional database column to `oauth_applications` table, - # say `t.array :grant_flows, default: []`, and store allowed grant flows that can - # be used with this application there. Then when authorization requested Doorkeeper - # will call this block to check if specific Application (passed with client_id and/or - # client_secret) is allowed to perform the request for the specific grant type - # (authorization, password, client_credentials, etc). - # - # Example of the block: - # - # ->(flow, client) { client.grant_flows.include?(flow) } - # - # In case this option invocation result is `false`, Doorkeeper server returns - # :unauthorized_client error and stops the request. - # - # @param allow_grant_flow_for_client [Proc] Block or any object respond to #call - # @return [Boolean] `true` if allow or `false` if forbid the request - # - # allow_grant_flow_for_client do |grant_flow, client| - # # `grant_flows` is an Array column with grant - # # flows that application supports - # - # client.grant_flows.include?(grant_flow) - # end - - # If you need arbitrary Resource Owner-Client authorization you can enable this option - # and implement the check your need. Config option must respond to #call and return - # true in case resource owner authorized for the specific application or false in other - # cases. - # - # By default all Resource Owners are authorized to any Client (application). - # - # authorize_resource_owner_for_client do |client, resource_owner| - # resource_owner.admin? || client.owners_allowlist.include?(resource_owner) - # end - - # Allows additional data fields to be sent while granting access to an application, - # and for this additional data to be included in subsequently generated access tokens. - # The 'authorizations/new' page will need to be overridden to include this additional data - # in the request params when granting access. The access grant and access token models - # will both need to respond to these additional data fields, and have a database column - # to store them in. - # - # Example: - # You have a multi-tenanted platform and want to be able to grant access to a specific - # tenant, rather than all the tenants a user has access to. You can use this config - # option to specify that a ':tenant_id' will be passed when authorizing. This tenant_id - # will be included in the access tokens. When a request is made with one of these access - # tokens, you can check that the requested data belongs to the specified tenant. - # - # Default value is an empty Array: [] - # custom_access_token_attributes [:tenant_id] - - # Hook into the strategies' request & response life-cycle in case your - # application needs advanced customization or logging: - # - # before_successful_strategy_response do |request| - # puts "BEFORE HOOK FIRED! #{request}" - # end - # - # after_successful_strategy_response do |request, response| - # puts "AFTER HOOK FIRED! #{request}, #{response}" - # end - - # Hook into Authorization flow in order to implement Single Sign Out - # or add any other functionality. Inside the block you have an access - # to `controller` (authorizations controller instance) and `context` - # (Doorkeeper::OAuth::Hooks::Context instance) which provides pre auth - # or auth objects with issued token based on hook type (before or after). - # - # before_successful_authorization do |controller, context| - # Rails.logger.info(controller.request.params.inspect) - # - # Rails.logger.info(context.pre_auth.inspect) - # end - # - # after_successful_authorization do |controller, context| - # controller.session[:logout_urls] << - # Doorkeeper::Application - # .find_by(controller.request.params.slice(:redirect_uri)) - # .logout_uri - # - # Rails.logger.info(context.auth.inspect) - # Rails.logger.info(context.issued_token) - # end - - # Under some circumstances you might want to have applications auto-approved, - # so that the user skips the authorization step. - # For example if dealing with a trusted application. - # - # skip_authorization do |resource_owner, client| - # client.superapp? or resource_owner.admin? - # end - - # Configure custom constraints for the Token Introspection request. - # By default this configuration option allows to introspect a token by another - # token of the same application, OR to introspect the token that belongs to - # authorized client (from authenticated client) OR when token doesn't - # belong to any client (public token). Otherwise requester has no access to the - # introspection and it will return response as stated in the RFC. - # - # Block arguments: - # - # @param token [Doorkeeper::AccessToken] - # token to be introspected - # - # @param authorized_client [Doorkeeper::Application] - # authorized client (if request is authorized using Basic auth with - # Client Credentials for example) - # - # @param authorized_token [Doorkeeper::AccessToken] - # Bearer token used to authorize the request - # - # In case the block returns `nil` or `false` introspection responses with 401 status code - # when using authorized token to introspect, or you'll get 200 with { "active": false } body - # when using authorized client to introspect as stated in the - # RFC 7662 section 2.2. Introspection Response. - # - # Using with caution: - # Keep in mind that these three parameters pass to block can be nil as following case: - # `authorized_client` is nil if and only if `authorized_token` is present, and vice versa. - # `token` will be nil if and only if `authorized_token` is present. - # So remember to use `&` or check if it is present before calling method on - # them to make sure you doesn't get NoMethodError exception. - # - # You can define your custom check: - # - # allow_token_introspection do |token, authorized_client, authorized_token| - # if authorized_token - # # customize: require `introspection` scope - # authorized_token.application == token&.application || - # authorized_token.scopes.include?("introspection") - # elsif token.application - # # `protected_resource` is a new database boolean column, for example - # authorized_client == token.application || authorized_client.protected_resource? - # else - # # public token (when token.application is nil, token doesn't belong to any application) - # true - # end - # end - # - # Or you can completely disable any token introspection: - # - # allow_token_introspection false - # - # If you need to block the request at all, then configure your routes.rb or web-server - # like nginx to forbid the request. - - # WWW-Authenticate Realm (default: "Doorkeeper"). - # - # realm "Doorkeeper" + # Per-user applications + enable_application_owner confirmation: true end From c6a90fda5131f71afe8b696992e646ff4cea94d3 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 7 Apr 2025 16:45:55 +0100 Subject: [PATCH 07/17] doorkeeper configuration for client credntials flow only --- config/initializers/doorkeeper.rb | 251 ++---------------------------- 1 file changed, 13 insertions(+), 238 deletions(-) diff --git a/config/initializers/doorkeeper.rb b/config/initializers/doorkeeper.rb index 900dd1362..a5e0148e2 100644 --- a/config/initializers/doorkeeper.rb +++ b/config/initializers/doorkeeper.rb @@ -1,250 +1,25 @@ # frozen_string_literal: true Doorkeeper.configure do - # Change the ORM that doorkeeper will use (requires ORM extensions installed). - # Check the list of supported ORMs here: https://github.com/doorkeeper-gem/doorkeeper#orms orm :active_record + base_controller "ActionController::Base" - # This block will be called to check whether the resource owner is authenticated or not. - resource_owner_authenticator do - current_user || warden.authenticate!(scope: :user) - end + # Enabled grant flows + grant_flows %w[client_credentials] - admin_authenticator do |_routes| - current_user || warden.authenticate!(scope: :user) - end + # Security + hash_token_secrets + authorization_code_expires_in 10.minutes + access_token_expires_in 2.hours + force_ssl_in_redirect_uri Manyfold::Application.config.force_ssl + forbid_redirect_uri { |uri| uri.scheme.to_s.downcase == "javascript" } - # You can use your own model classes if you need to extend (or even override) default - # Doorkeeper models such as `Application`, `AccessToken` and `AccessGrant. - # - # By default Doorkeeper ActiveRecord ORM uses its own classes: - # - # access_token_class "Doorkeeper::AccessToken" - # access_grant_class "Doorkeeper::AccessGrant" - # application_class "Doorkeeper::Application" - # - # Don't forget to include Doorkeeper ORM mixins into your custom models: - # - # * ::Doorkeeper::Orm::ActiveRecord::Mixins::AccessToken - for access token - # * ::Doorkeeper::Orm::ActiveRecord::Mixins::AccessGrant - for access grant - # * ::Doorkeeper::Orm::ActiveRecord::Mixins::Application - for application (OAuth2 clients) - # - # For example: - # - # access_token_class "MyAccessToken" - # - # class MyAccessToken < ApplicationRecord - # include ::Doorkeeper::Orm::ActiveRecord::Mixins::AccessToken - # - # self.table_name = "hey_i_wanna_my_name" - # - # def destroy_me! - # destroy - # end - # end + # Authentication + resource_owner_from_credentials { nil } + admin_authenticator { current_user } # Temporary - # Enables polymorphic Resource Owner association for Access Tokens and Access Grants. - # By default this option is disabled. - # - # Make sure you properly setup you database and have all the required columns (run - # `bundle exec rails generate doorkeeper:enable_polymorphic_resource_owner` and execute Rails - # migrations). - # - # If this option enabled, Doorkeeper will store not only Resource Owner primary key - # value, but also it's type (class name). See "Polymorphic Associations" section of - # Rails guides: https://guides.rubyonrails.org/association_basics.html#polymorphic-associations - # - # [NOTE] If you apply this option on already existing project don't forget to manually - # update `resource_owner_type` column in the database and fix migration template as it will - # set NOT NULL constraint for Access Grants table. - # - # use_polymorphic_resource_owner - - # If you are planning to use Doorkeeper in Rails 5 API-only application, then you might - # want to use API mode that will skip all the views management and change the way how - # Doorkeeper responds to a requests. - # - # api_only - - # Enforce token request content type to application/x-www-form-urlencoded. - # It is not enabled by default to not break prior versions of the gem. - # - # enforce_content_type - - # Authorization Code expiration time (default: 10 minutes). - # - # authorization_code_expires_in 10.minutes - - # Access token expiration time (default: 2 hours). - # If you set this to `nil` Doorkeeper will not expire the token and omit expires_in in response. - # It is RECOMMENDED to set expiration time explicitly. - # Prefer access_token_expires_in 100.years or similar, - # which would be functionally equivalent and avoid the risk of unexpected behavior by callers. - # - # access_token_expires_in 2.hours - - # Assign custom TTL for access tokens. Will be used instead of access_token_expires_in - # option if defined. In case the block returns `nil` value Doorkeeper fallbacks to - # +access_token_expires_in+ configuration option value. If you really need to issue a - # non-expiring access token (which is not recommended) then you need to return - # Float::INFINITY from this block. - # - # `context` has the following properties available: - # - # * `client` - the OAuth client application (see Doorkeeper::OAuth::Client) - # * `grant_type` - the grant type of the request (see Doorkeeper::OAuth) - # * `scopes` - the requested scopes (see Doorkeeper::OAuth::Scopes) - # * `resource_owner` - authorized resource owner instance (if present) - # - # custom_access_token_expires_in do |context| - # context.client.additional_settings.implicit_oauth_expiration - # end - - # Use a custom class for generating the access token. - # See https://doorkeeper.gitbook.io/guides/configuration/other-configurations#custom-access-token-generator - # - # access_token_generator '::Doorkeeper::JWT' - - # The controller +Doorkeeper::ApplicationController+ inherits from. - # Defaults to +ActionController::Base+ unless +api_only+ is set, which changes the default to - # +ActionController::API+. The return value of this option must be a stringified class name. - # See https://doorkeeper.gitbook.io/guides/configuration/other-configurations#custom-controllers - # - # base_controller 'ApplicationController' - - # Reuse access token for the same resource owner within an application (disabled by default). - # - # This option protects your application from creating new tokens before old **valid** one becomes - # expired so your database doesn't bloat. Keep in mind that when this option is enabled Doorkeeper - # doesn't update existing token expiration time, it will create a new token instead if no active matching - # token found for the application, resources owner and/or set of scopes. - # Rationale: https://github.com/doorkeeper-gem/doorkeeper/issues/383 - # - # You can not enable this option together with +hash_token_secrets+. - # - # reuse_access_token - - # In case you enabled `reuse_access_token` option Doorkeeper will try to find matching - # token using `matching_token_for` Access Token API that searches for valid records - # in batches in order not to pollute the memory with all the database records. By default - # Doorkeeper uses batch size of 10 000 records. You can increase or decrease this value - # depending on your needs and server capabilities. - # - # token_lookup_batch_size 10_000 - - # Set a limit for token_reuse if using reuse_access_token option - # - # This option limits token_reusability to some extent. - # If not set then access_token will be reused unless it expires. - # Rationale: https://github.com/doorkeeper-gem/doorkeeper/issues/1189 - # - # This option should be a percentage(i.e. (0,100]) - # - # token_reuse_limit 100 - - # Only allow one valid access token obtained via client credentials - # per client. If a new access token is obtained before the old one - # expired, the old one gets revoked (disabled by default) - # - # When enabling this option, make sure that you do not expect multiple processes - # using the same credentials at the same time (e.g. web servers spanning - # multiple machines and/or processes). - # - # revoke_previous_client_credentials_token - - # Only allow one valid access token obtained via authorization code - # per client. If a new access token is obtained before the old one - # expired, the old one gets revoked (disabled by default) - # - # revoke_previous_authorization_code_token - - # Require non-confidential clients to use PKCE when using an authorization code - # to obtain an access_token (disabled by default) - # - # force_pkce - - # Hash access and refresh tokens before persisting them. - # This will disable the possibility to use +reuse_access_token+ - # since plain values can no longer be retrieved. - # - # Note: If you are already a user of doorkeeper and have existing tokens - # in your installation, they will be invalid without adding 'fallback: :plain'. - # - # hash_token_secrets - # By default, token secrets will be hashed using the - # +Doorkeeper::Hashing::SHA256+ strategy. - # - # If you wish to use another hashing implementation, you can override - # this strategy as follows: - # - # hash_token_secrets using: '::Doorkeeper::Hashing::MyCustomHashImpl' - # - # Keep in mind that changing the hashing function will invalidate all existing - # secrets, if there are any. - - # Hash application secrets before persisting them. - # - # hash_application_secrets - # - # By default, applications will be hashed - # with the +Doorkeeper::SecretStoring::SHA256+ strategy. - # - # If you wish to use bcrypt for application secret hashing, uncomment - # this line instead: - # - # hash_application_secrets using: '::Doorkeeper::SecretStoring::BCrypt' - - # When the above option is enabled, and a hashed token or secret is not found, - # you can allow to fall back to another strategy. For users upgrading - # doorkeeper and wishing to enable hashing, you will probably want to enable - # the fallback to plain tokens. - # - # This will ensure that old access tokens and secrets - # will remain valid even if the hashing above is enabled. - # - # This can be done by adding 'fallback: plain', e.g. : - # - # hash_application_secrets using: '::Doorkeeper::SecretStoring::BCrypt', fallback: :plain - - # Issue access tokens with refresh token (disabled by default), you may also - # pass a block which accepts `context` to customize when to give a refresh - # token or not. Similar to +custom_access_token_expires_in+, `context` has - # the following properties: - # - # `client` - the OAuth client application (see Doorkeeper::OAuth::Client) - # `grant_type` - the grant type of the request (see Doorkeeper::OAuth) - # `scopes` - the requested scopes (see Doorkeeper::OAuth::Scopes) - # - # use_refresh_token - - # Provide support for an owner to be assigned to each registered application (disabled by default) - # Optional parameter confirmation: true (default: false) if you want to enforce ownership of - # a registered application - # NOTE: you must also run the rails g doorkeeper:application_owner generator - # to provide the necessary support - # - # enable_application_owner confirmation: false - - # Define access token scopes for your provider - # For more information go to - # https://doorkeeper.gitbook.io/guides/ruby-on-rails/scopes - # + # Available scopes default_scopes :read - optional_scopes :write - - # Allows to restrict only certain scopes for grant_type. - # By default, all the scopes will be available for all the grant types. - # - # Keys to this hash should be the name of grant_type and - # values should be the array of scopes for that grant type. - # Note: scopes should be from configured_scopes (i.e. default or optional) - # - # scopes_by_grant_type password: [:write], client_credentials: [:update] - - # Forbids creating/updating applications with arbitrary scopes that are - # not in configuration, i.e. +default_scopes+ or +optional_scopes+. - # (disabled by default) - # enforce_configured_scopes # Per-user applications From 8a87f56c10fa3c3391f7931fe4d530c1082b7832 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 7 Apr 2025 16:47:12 +0100 Subject: [PATCH 08/17] authenticate app owner with token --- app/controllers/application_controller.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 7568c2da5..e5f8793ee 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -6,6 +6,7 @@ class ApplicationController < ActionController::Base after_action :set_content_security_policy_header, if: -> { request.format.html? } before_action :authenticate_user!, unless: -> { SiteSettings.multiuser_enabled? || has_signed_id? } + before_action :doorkeeper_token_authorize!, if: :is_api_request? around_action :switch_locale before_action :check_for_first_use before_action :show_security_alerts @@ -104,6 +105,19 @@ def random_delay private + def is_api_request? + request.format.json_ld? + end + + def doorkeeper_token_authorize! + app_owner = doorkeeper_token&.application&.owner + if app_owner&.active_for_authentication? + sign_in app_owner, store: false + else + doorkeeper_render_error + end + end + def user_not_authorized if current_user raise ActiveRecord::RecordNotFound From 80e5db7fed40faa700b27322ce957921c4b183be Mon Sep 17 00:00:00 2001 From: James Smith Date: Tue, 8 Apr 2025 00:57:27 +0100 Subject: [PATCH 09/17] custom controller for creating oauth applications --- .../doorkeeper_applications_controller.rb | 63 +++++++++++++++++++ app/policies/doorkeeper/application_policy.rb | 48 ++++++++++++++ .../_breadcrumb.html.erb | 12 ++++ .../doorkeeper_applications/_form.html.erb | 7 +++ .../doorkeeper_applications/edit.html.erb | 4 ++ .../doorkeeper_applications/index.html.erb | 12 ++++ .../doorkeeper_applications/new.html.erb | 4 ++ .../doorkeeper_applications/show.html.erb | 34 ++++++++++ config/routes.rb | 6 +- 9 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 app/controllers/doorkeeper_applications_controller.rb create mode 100644 app/policies/doorkeeper/application_policy.rb create mode 100644 app/views/doorkeeper_applications/_breadcrumb.html.erb create mode 100644 app/views/doorkeeper_applications/_form.html.erb create mode 100644 app/views/doorkeeper_applications/edit.html.erb create mode 100644 app/views/doorkeeper_applications/index.html.erb create mode 100644 app/views/doorkeeper_applications/new.html.erb create mode 100644 app/views/doorkeeper_applications/show.html.erb diff --git a/app/controllers/doorkeeper_applications_controller.rb b/app/controllers/doorkeeper_applications_controller.rb new file mode 100644 index 000000000..4d0d593f3 --- /dev/null +++ b/app/controllers/doorkeeper_applications_controller.rb @@ -0,0 +1,63 @@ +class DoorkeeperApplicationsController < ApplicationController + before_action :get_application, except: [:index, :new, :create] + + def index + @applications = policy_scope(Doorkeeper::Application) + end + + def show + end + + def new + authorize Doorkeeper::Application + @application = Doorkeeper::Application.new( + redirect_uri: "urn:ietf:wg:oauth:2.0:oob", + scopes: Doorkeeper.configuration.default_scopes + ) + end + + def edit + end + + def create + authorize Doorkeeper::Application + @application = Doorkeeper::Application.create(application_params.merge(owner: current_user)) + if @application.valid? + redirect_to @application, notice: t(".success") + else + flash.now[:alert] = t(".failure") + render :new + end + end + + def update + application.update(library_params) + if @application.save + redirect_to @application, notice: t(".success") + else + flash.now[:alert] = t(".failure") + render :edit + end + end + + def delete + @application.destroy + redirect_to doorkeeper_applications_path, notice: t(".success") + end + + private + + def application_params + params.require(:doorkeeper_application).permit( + :name, + :redirect_uri, + :confidential, + :scopes + ) + end + + def get_application + @application = policy_scope(Doorkeeper::Application).find(params[:id]) + authorize @application + end +end diff --git a/app/policies/doorkeeper/application_policy.rb b/app/policies/doorkeeper/application_policy.rb new file mode 100644 index 000000000..e08d64403 --- /dev/null +++ b/app/policies/doorkeeper/application_policy.rb @@ -0,0 +1,48 @@ +class Doorkeeper::ApplicationPolicy < ApplicationPolicy + def index? + user.present? + end + + def show? + one_of( + record.owner == user, + user&.is_moderator? + ) + end + + def create? + none_of( + SiteSettings.demo_mode_enabled? + ) + end + + def update? + all_of( + one_of( + user == record, + user&.is_administrator? + ), + SiteSettings.multiuser_enabled?, + none_of( + SiteSettings.demo_mode_enabled? + ) + ) + end + + def destroy? + update? + end + + class Scope + attr_reader :user, :scope + + def initialize(user, scope) + @user = user + @scope = scope + end + + def resolve + user.is_moderator? ? scope : scope.where(owner: user) + end + end +end diff --git a/app/views/doorkeeper_applications/_breadcrumb.html.erb b/app/views/doorkeeper_applications/_breadcrumb.html.erb new file mode 100644 index 000000000..33bec5726 --- /dev/null +++ b/app/views/doorkeeper_applications/_breadcrumb.html.erb @@ -0,0 +1,12 @@ +<% content_for :breadcrumbs do %> + +<% end %> diff --git a/app/views/doorkeeper_applications/_form.html.erb b/app/views/doorkeeper_applications/_form.html.erb new file mode 100644 index 000000000..5fb1e8660 --- /dev/null +++ b/app/views/doorkeeper_applications/_form.html.erb @@ -0,0 +1,7 @@ +<%= form_with model: @application do |form| %> + <%= text_input_row form, :name %> + <%= text_input_row form, :redirect_uri %> + <%= text_input_row form, :scopes %> + <%= checkbox_input_row form, :confidential %> + <%= form.submit "Save", class: "btn btn-primary" %> +<% end %> diff --git a/app/views/doorkeeper_applications/edit.html.erb b/app/views/doorkeeper_applications/edit.html.erb new file mode 100644 index 000000000..a5fff615e --- /dev/null +++ b/app/views/doorkeeper_applications/edit.html.erb @@ -0,0 +1,4 @@ +<%= render "breadcrumb" %> +

<%= t("oauth_applications.edit.title") %>

+ +<%= render "form" %> diff --git a/app/views/doorkeeper_applications/index.html.erb b/app/views/doorkeeper_applications/index.html.erb new file mode 100644 index 000000000..38944b127 --- /dev/null +++ b/app/views/doorkeeper_applications/index.html.erb @@ -0,0 +1,12 @@ +

<%= t("oauth_applications.index.title") %>

+ + + <% @applications.find_each do |app| %> + + + + + <% end %> +
<%= link_to app.name, app %><%= app.owner.username %>
+ +<%= link_to t(".new"), new_doorkeeper_application_path %> diff --git a/app/views/doorkeeper_applications/new.html.erb b/app/views/doorkeeper_applications/new.html.erb new file mode 100644 index 000000000..9519761a7 --- /dev/null +++ b/app/views/doorkeeper_applications/new.html.erb @@ -0,0 +1,4 @@ +<%= render "breadcrumb" %> +

<%= t("oauth_applications.new.title") %>

+ +<%= render "form" %> diff --git a/app/views/doorkeeper_applications/show.html.erb b/app/views/doorkeeper_applications/show.html.erb new file mode 100644 index 000000000..a3716326e --- /dev/null +++ b/app/views/doorkeeper_applications/show.html.erb @@ -0,0 +1,34 @@ +<%= render "breadcrumb" %> +

<%= t("oauth_applications.show.title") %>

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
<%= Doorkeeper::Application.human_attribute_name :name %><%= @application.name %>
<%= Doorkeeper::Application.human_attribute_name :uid %><%= @application.uid %>
<%= Doorkeeper::Application.human_attribute_name :secret %><%= @application.secret %>
<%= Doorkeeper::Application.human_attribute_name :confidential %><%= @application.confidential %>
<%= Doorkeeper::Application.human_attribute_name :redirect_uri %><%= @application.redirect_uri %>
<%= Doorkeeper::Application.human_attribute_name :access_token %> + <%= content_tag(:code) { @access_token } if @access_token %> + <%= button_to t(".regenerate"), @application, class: "btn btn-sm btn-outline-warning" %> +
+ +<%= link_to t(".edit"), edit_doorkeeper_application_path(@application) %> diff --git a/config/routes.rb b/config/routes.rb index bf252a8ec..d067ba1a7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,6 @@ require "federails" Rails.application.routes.draw do - use_doorkeeper get ".well-known/change-password", to: redirect("/users/edit") get "health" => "rails/health#show", :as => :rails_health_check get "problems/index" @@ -141,4 +140,9 @@ mount Rswag::Ui::Engine => "/api", :as => :api mount Rswag::Api::Engine => "/api" + + use_doorkeeper do + skip_controllers :applications + end + resources :doorkeeper_applications, path: "/oauth/applications" end From 3065c1ebc8574c50e563900fbfa55ca758b2df08 Mon Sep 17 00:00:00 2001 From: James Smith Date: Tue, 8 Apr 2025 01:02:26 +0100 Subject: [PATCH 10/17] add delete link for applications --- app/controllers/doorkeeper_applications_controller.rb | 2 +- app/views/doorkeeper_applications/show.html.erb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/doorkeeper_applications_controller.rb b/app/controllers/doorkeeper_applications_controller.rb index 4d0d593f3..062434784 100644 --- a/app/controllers/doorkeeper_applications_controller.rb +++ b/app/controllers/doorkeeper_applications_controller.rb @@ -40,7 +40,7 @@ def update end end - def delete + def destroy @application.destroy redirect_to doorkeeper_applications_path, notice: t(".success") end diff --git a/app/views/doorkeeper_applications/show.html.erb b/app/views/doorkeeper_applications/show.html.erb index a3716326e..5e212c8b1 100644 --- a/app/views/doorkeeper_applications/show.html.erb +++ b/app/views/doorkeeper_applications/show.html.erb @@ -32,3 +32,4 @@ <%= link_to t(".edit"), edit_doorkeeper_application_path(@application) %> +<%= link_to t(".destroy"), doorkeeper_application_path(@application), method: :delete %> From 6abd8641fc1bd70de185ba346c863b9123200ebb Mon Sep 17 00:00:00 2001 From: James Smith Date: Tue, 8 Apr 2025 13:44:03 +0100 Subject: [PATCH 11/17] show a few more details about apps in list --- app/views/doorkeeper_applications/index.html.erb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/views/doorkeeper_applications/index.html.erb b/app/views/doorkeeper_applications/index.html.erb index 38944b127..2ed3fd505 100644 --- a/app/views/doorkeeper_applications/index.html.erb +++ b/app/views/doorkeeper_applications/index.html.erb @@ -1,10 +1,18 @@

<%= t("oauth_applications.index.title") %>

+ + + <%= content_tag(:td) { t ".owner" } if current_user.is_moderator? %> + + + <% @applications.find_each do |app| %> - + <%= content_tag(:td) { app.owner.username } if current_user.is_moderator? %> + + <% end %>
<%= t ".name" %><%= t ".scopes" %><%= t ".created" %>
<%= link_to app.name, app %><%= app.owner.username %><%= app.scopes %><%= app.created_at.to_formatted_s(:long) %>
From 22ccf062aadf443ca568772ddfcca82f1e99e681 Mon Sep 17 00:00:00 2001 From: James Smith Date: Tue, 8 Apr 2025 15:05:41 +0100 Subject: [PATCH 12/17] autofocus name field when creating tokens --- app/views/doorkeeper_applications/_form.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/doorkeeper_applications/_form.html.erb b/app/views/doorkeeper_applications/_form.html.erb index 5fb1e8660..40ded7567 100644 --- a/app/views/doorkeeper_applications/_form.html.erb +++ b/app/views/doorkeeper_applications/_form.html.erb @@ -1,5 +1,5 @@ <%= form_with model: @application do |form| %> - <%= text_input_row form, :name %> + <%= text_input_row form, :name, autofocus: true %> <%= text_input_row form, :redirect_uri %> <%= text_input_row form, :scopes %> <%= checkbox_input_row form, :confidential %> From 4023e42b16d9cef9ef48ec4f0e6dfe02951be03f Mon Sep 17 00:00:00 2001 From: James Smith Date: Tue, 8 Apr 2025 15:18:33 +0100 Subject: [PATCH 13/17] generate access tokens --- .../doorkeeper_applications_controller.rb | 27 ++++++++++++++++--- .../doorkeeper_applications/show.html.erb | 11 ++++++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/app/controllers/doorkeeper_applications_controller.rb b/app/controllers/doorkeeper_applications_controller.rb index 062434784..574cd756d 100644 --- a/app/controllers/doorkeeper_applications_controller.rb +++ b/app/controllers/doorkeeper_applications_controller.rb @@ -6,6 +6,7 @@ def index end def show + get_access_token end def new @@ -31,9 +32,11 @@ def create end def update - application.update(library_params) + generate_token if application_params[:generate_token] + @application.update(application_params.except(:generate_token)) if @application.save - redirect_to @application, notice: t(".success") + get_access_token + render :show, notice: t(".success") else flash.now[:alert] = t(".failure") render :edit @@ -52,7 +55,8 @@ def application_params :name, :redirect_uri, :confidential, - :scopes + :scopes, + :generate_token ) end @@ -60,4 +64,21 @@ def get_application @application = policy_scope(Doorkeeper::Application).find(params[:id]) authorize @application end + + def generate_token + # Revoke existing tokens + Doorkeeper::Application.revoke_tokens_and_grants_for(@application, @application.owner) + # Create new access token + token = @application.access_tokens.create( + expires_in: 6.months, + resource_owner_id: @application.owner.id, + scopes: @application.scopes + ) + # Make plaintext available to view + @plaintext_token = token.plaintext_token + end + + def get_access_token + @access_token = @application.access_tokens.where(revoked_at: nil).first + end end diff --git a/app/views/doorkeeper_applications/show.html.erb b/app/views/doorkeeper_applications/show.html.erb index 5e212c8b1..3042f6148 100644 --- a/app/views/doorkeeper_applications/show.html.erb +++ b/app/views/doorkeeper_applications/show.html.erb @@ -25,8 +25,15 @@ <%= Doorkeeper::Application.human_attribute_name :access_token %> - <%= content_tag(:code) { @access_token } if @access_token %> - <%= button_to t(".regenerate"), @application, class: "btn btn-sm btn-outline-warning" %> + <% if @plaintext_token %> +
+

<%= t(".access_token_help") %>

+ <%= text_field :access_token, {}, value: @plaintext_token, readonly: true, class: "form-control" %> +
+ <% else %> + <%= t(".access_token_expiry", how_long: time_ago_in_words(@access_token.expires_at)) if @access_token %> + <%= button_to t(".generate_access_token"), doorkeeper_application_path(@application, "doorkeeper_application[generate_token]": "1"), method: :patch, class: "btn btn-sm btn-outline-warning" %> + <% end %> From 01aca6445482ab8a29a1734b06d28b6e0ae48e41 Mon Sep 17 00:00:00 2001 From: James Smith Date: Tue, 8 Apr 2025 16:25:38 +0100 Subject: [PATCH 14/17] layout and translations for oauth apps --- .../_breadcrumb.html.erb | 2 +- .../doorkeeper_applications/_form.html.erb | 8 +- .../doorkeeper_applications/edit.html.erb | 2 +- .../doorkeeper_applications/index.html.erb | 34 +++++---- .../doorkeeper_applications/new.html.erb | 2 +- .../doorkeeper_applications/show.html.erb | 74 +++++++++---------- config/locales/en.yml | 44 +++++++++++ 7 files changed, 106 insertions(+), 60 deletions(-) diff --git a/app/views/doorkeeper_applications/_breadcrumb.html.erb b/app/views/doorkeeper_applications/_breadcrumb.html.erb index 33bec5726..dd595f13d 100644 --- a/app/views/doorkeeper_applications/_breadcrumb.html.erb +++ b/app/views/doorkeeper_applications/_breadcrumb.html.erb @@ -1,7 +1,7 @@ <% content_for :breadcrumbs do %>