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

Feature Request: Use Friendly Keys in URLs for Tenant Identification #103

Open
MichMich opened this issue Oct 16, 2024 · 4 comments
Open

Comments

@MichMich
Copy link

Currently, tenants (SAML providers) are registered via Artisan commands and can be accessed through URLs like:

/saml2/b2dae2e6-e814-4553-a3a5-a56ddaca1110/login

While this works, I would prefer to use a more friendly identifier (such as a key) in the URLs, like:

/saml2/okta/login

To achieve this, I created a custom middleware that attempts to resolve the tenant by the friendly key and then falls back to the default UUID-based behavior if the key is not found. Here’s the code I used:

<?php

namespace App\Http\Middleware;

use Slides\Saml2\Repositories\TenantRepository;
use \Slides\Saml2\Http\Middleware\ResolveTenant;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class Saml2TenantKeyResolverMiddleware extends ResolveTenant
{
    public function handle($request, \Closure $next)
    {
        try {
            parent::handle($request, $next);
        } catch (NotFoundHttpException $e) {
            $tenant = (new TenantRepository)->findByKey($request->route('uuid'));
            if (!$tenant) throw $e;

            $request->route()->setParameter('uuid', $tenant->uuid);
            parent::handle($request, $next);
        }
    }
}

I also overrode the saml2.resolveTenant middleware in bootstrap/app.php like so:

return Application::configure(basePath: dirname(__DIR__))
    ->booted(function (Application $app) {
        $app['router']->aliasMiddleware('saml2.resolveTenant', \App\Http\Middleware\Saml2TenantKeyResolverMiddleware::class);
    })

Question:

Is there a cleaner or more integrated way to accomplish this, or perhaps a feature that could be added to the package to support friendly keys natively?

Thanks for your work on this package, and I appreciate any insights you might have!

@sarisia
Copy link

sarisia commented Oct 17, 2024

This seems really sucks but worked 🤔

$ php artisan db
mysql> update saml2_tenants set uuid='okta' where id=1;

Then all urls are now under /saml2/okta/[...]

@MichMich
Copy link
Author

Yeah that probably works but seems like a dirty hack. Thanks for the suggestion though.

@saboya
Copy link

saboya commented Nov 14, 2024

This would be relatively simple to add with a unique slug field. I did just that using the metadata field.

1 - Add a unique constraint to metadata->>slug (this is using Postgres):

create unique index saml2_tenants_metadata_slug_idx
    on public.saml2_tenants ((metadata ->> 'slug'::text));

2 - Create some route in your application such as /sso/{tenant_slug}/login
3 - In the controller, fetch the tenant by the metadata->>slug field and redirect to the UUID route.

@RoSk0
Copy link

RoSk0 commented Nov 26, 2024

Cross linking. See #66 also

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

No branches or pull requests

4 participants