Skip to content

Allow checking of authenticaded resource via callable object#180

Merged
mcelicalderon merged 1 commit intomasterfrom
add-conditional-authentication
May 17, 2021
Merged

Allow checking of authenticaded resource via callable object#180
mcelicalderon merged 1 commit intomasterfrom
add-conditional-authentication

Conversation

@mcelicalderon
Copy link
Copy Markdown
Member

With this change you can now use a custom callable object to check for additional conditions on the authenticated resource. Before we would only check for the presence of context[:current_resource] after token authentication. Now you can do something like this:

field :admin_field, String, null: false, authenticate: ->(user) { user.admin? }

This does not intend to replace a more complete authorization framework, but just support a bit more complex authentication conditions that I can imagine would work for most fields.

I would still advise to look into gems like graphql-guard as a more complete authorization gem in GraphQL. Also, the GraphQL gem has premium support for authorization and a basic API that you could use for this purpose.

Resolves #118

@mcelicalderon mcelicalderon added the enhancement New feature or request label May 17, 2021
@mcelicalderon mcelicalderon requested a review from 00dav00 May 17, 2021 01:55
Comment thread lib/graphql_devise/schema_plugin.rb Outdated
def raise_on_missing_resource(context, field)
@unauthenticated_proc.call(field.name) if context[:current_resource].blank?
def raise_on_missing_resource(context, field, auth_required)
if auth_required.respond_to?(:call)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about

if auth_required.respond_to?(:call) && auth_required.call(context[:current_resource])
        @unauthenticated_proc.call(field.name) 

@mcelicalderon mcelicalderon force-pushed the add-conditional-authentication branch from dce2377 to 0f5129c Compare May 17, 2021 22:10
@mcelicalderon mcelicalderon enabled auto-merge May 17, 2021 22:12
@mcelicalderon mcelicalderon merged commit 0343c07 into master May 17, 2021
@mcelicalderon mcelicalderon deleted the add-conditional-authentication branch May 17, 2021 22:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How to authenticate for sending multiple values to the graphql_context and mounting operations Into DummySchema

2 participants