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

Set failure_app per scope #193

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

willianveiga
Copy link

It's possible to configure failure apps per scope:

Current config:

use Warden::Manager do |manager|
  manager.failure_app = MyCustomFailureApp
end

Single scope config:

use Warden::Manager do |manager|
  manager.failure_app = { app: MyCustomFailureApp, scope: :user }
end

Multiple scopes config:

use Warden::Manager do |manager|
  manager.failure_app = [
    { app: MyCustomFailureApp, scope: :user },
    { app: AnotherCustomFailureApp, scope: :psychologist }
  ]
end

A RuntimeError is raised if no matching Failure App is found.

Fixes #40.

It's possible to configure failure apps per scope:

Current config:

```
use Warden::Manager do |manager|
  manager.failure_app = MyCustomFailureApp
end
```

Single scope config:

```
use Warden::Manager do |manager|
  manager.failure_app = { app: MyCustomFailureApp, scope: :user }
end
```

Multiple scopes config:

```
use Warden::Manager do |manager|
  manager.failure_app = [
    { app: MyCustomFailureApp, scope: :user },
    { app: AnotherCustomFailureApp, scope: :psychologist }
  ]
end
```

A RuntimeError is raised if no matching Failure App is found.
@jsmestad jsmestad self-assigned this Oct 23, 2020
@jsmestad
Copy link
Collaborator

Thanks for contributing! At first glance this looks good, but I'll take a closer look soon.

@l4cr0ss
Copy link

l4cr0ss commented Dec 16, 2020

I just ran into this issue, too, as I refactor a jumble of duplicated authentication subsystems inside of a large application.

In my case, a common Rack middleware is divvying up requests to autonomous applications, each of which will be responsible for registering its own warden scope and maintaining its own failure application.

To bounce the failures back to a common handler is possible, but means duplicating the registration logic, so it would be nice to see this fix merged!

@itskingori
Copy link

itskingori commented Mar 13, 2021

Very welcome feature. Ended up here looking for it. I was looking at the "Advanced Setup (with scopes)" and this is the current API for differentiated configuration per scope ...

use Warden::Manager do |config|
  config.failure_app = ->(env) do
    env['REQUEST_METHOD'] = 'GET'
    SessionsController.action(:new).call(env)
  end
  config.default_scope = :user

  config.scope_defaults :user,        :strategies => [:password]
  config.scope_defaults :account,     :store => false,  :strategies => [:account_by_subdomain]
  config.scope_defaults :membership,  :store => false,  :strategies => [:membership, :account_owner]

  config.scope_defaults(
    :api,
    :strategies => [:api_token],
    :store      => false,
    :action     => "unauthenticated_api"
  )
end

I was wondering if it might be better to follow that approach. That is, something like this on scope_defaults ...

use Warden::Manager do |config|
  config.default_scope = :user

  config.scope_defaults(
    :api,
    :strategies    => [:session],
    :store         => false,
    :failure_app   => ->(env) { SessionsControllerExampleForAPI.action(:new).call(env) }
  )

  config.scope_defaults(
    :web,
    :strategies    => [:token],
    :store         => true,
    :failure_app   => ->(env) { SessionsControllerExampleForWeb.action(:new).call(env) }
  )
end

Thoughts?

@masterT
Copy link
Contributor

masterT commented Feb 29, 2024

@itskingori I like this a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Set failure_app per scope, instead of failure_action?
5 participants