Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined method `openid_request' using custom access grant class. #127

Closed
phlegx opened this issue Oct 7, 2020 · 4 comments
Closed

Undefined method `openid_request' using custom access grant class. #127

phlegx opened this issue Oct 7, 2020 · 4 comments
Labels

Comments

@phlegx
Copy link
Contributor

phlegx commented Oct 7, 2020

I use doorkeeper config:

# config/initializers/doorkeeper.rb
...
access_token_class 'Oauth::AccessToken'
access_grant_class 'Oauth::AccessGrant'
application_class 'Oauth::Application'

Call POST "/oauth/token" I get:

NoMethodError (undefined method `openid_request' for #<Oauth::AccessGrant:0x00005593c7743f28>):

activemodel (6.0.3.2) lib/active_model/attribute_methods.rb:432:in `method_missing'
doorkeeper-openid_connect (325068fd2f38) lib/doorkeeper/openid_connect/oauth/authorization_code_request.rb:13:in `after_successful_response'
doorkeeper (5.4.0) lib/doorkeeper/oauth/base_request.rb:16:in `authorize'

If I remove access_grant_class 'Oauth::AccessGrant' config. All works fine.

Here my Oauth::AccessGrant class:

module Oauth
  class AccessGrant < ApplicationRecord
    include ::Doorkeeper::Orm::ActiveRecord::Mixins::AccessGrant
  end
end

Maybe in this line:

the access_grant_class config must be used if set in config.

Workaround

module Oauth
  class AccessGrant < ApplicationRecord
    include ::Doorkeeper::Orm::ActiveRecord::Mixins::AccessGrant

    # Bugfix: see https://github.com/doorkeeper-gem/doorkeeper-openid_connect/issues/127
    has_one :openid_request,
            class_name: 'Doorkeeper::OpenidConnect::Request',
            inverse_of: :access_grant,
            dependent:  :delete
  end
end
@toupeira
Copy link
Member

@phlegx thanks for the report! I'm not sure if we already have access to Doorkeeper's configuration when that prepend call happens. 🤔

But the has_one association is defined at

has_one :openid_request,
class_name: 'Doorkeeper::OpenidConnect::Request',
inverse_of: :access_grant,
dependent: :delete
, so I think you should be able to replace your copy with prepend ::Doorkeeper::OpenidConnect::AccessGrant?

Let me know if that works and is an acceptable solution, and I'll add a note to the README similar to https://doorkeeper.gitbook.io/guides/configuration/models. We could also support include in addition to prepend to make it look more consistent.

@phlegx
Copy link
Contributor Author

phlegx commented Dec 15, 2020

So, I should put prepend ::Doorkeeper::OpenidConnect::AccessGrant in my AccessGrant model instead of the has_one relation workaround?

@toupeira
Copy link
Member

toupeira commented Jan 4, 2021

@phlegx yes, that should pull in the has_one definition and any other future changes (prepend acts similarly to include). Let me know if that works for you!

I was also curious now if we could just monkey-patch Doorkeeper::Orm::ActiveRecord::Mixins::AccessGrant rather than Doorkeeper::Orm::ActiveRecord::AccessGrant, but I couldn't find a way to inject code into the existing included block there (getting a ActiveSupport::Concern::MultipleIncludedBlocks error).

@jcdennen
Copy link

jcdennen commented Aug 18, 2021

Just adding a note here that I needed to specify:

    has_one :openid_request,
            class_name: 'Doorkeeper::OpenidConnect::Request',
            inverse_of: :access_grant,
            dependent:  :delete

and include foreign_key: 'access_grant_id' due to my team customizing the AccessGrant table_name. Without the foreign_key constraint specified, I received the following MySQL error:

Mysql2::Error: Unknown column 'oauth_openid_requests.{custom_table_name}_id' in 'where clause': SELECT  `oauth_openid_requests`.* FROM `oauth_openid_requests` WHERE `oauth_openid_requests`.`{custom_table_name}_id` = X LIMIT 1

The schema at this point had already been using access_grant_id as the column name. It may be useful to add this to the Doorkeeper custom model docs.

@phlegx phlegx closed this as completed May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants