MohammadWaleed / keycloak-admin-client

PHP Client to connect to Keycloak admin rest apis
MIT License
93 stars 61 forks source link

Custom Keycloak endpoints raw parameter #105

Closed dfeneck closed 1 year ago

dfeneck commented 1 year ago

Am I missing something or is this a bug?

I need one of my custom keycloak endpoint parameters to be sent as a raw array in the body, like this: ["ONE", "TWO", "THREE"]

But it is its being passed to guzzle always as an associative array, like this: paramName=["ONE", "TWO", "THREE"]

My custom endpoint is therefore failing to consume the parameter. Is there any way to specify raw or inject into the body as is?


I've also noticed that by failing to specify a location it seems to throw an error, despite the guzzle specifications stating No Location will be treated as data. https://guzzle3.readthedocs.io/webservice-client/guzzle-service-descriptions.html#no-location

dfeneck commented 1 year ago

For those interested I got around this by just setting the body directly with: 'body' => ["ONE", "TWO", "THREE"] on the first level of the config. Same level you set the realm, client_id, baseUri, etc etc.

dfeneck commented 1 year ago

Attaching to the body like that modifies each request which may interfere with other endpoints, so I actually opted to create a middleware to modify the request only for the targeted custom keycloak endpoint. Just thought I would share for anybody else needing assistance.

// Modifies the body for the expected raw JSON array on the custom request.
$middleware = Middleware::mapRequest(function (RequestInterface $r)
{
    // Ensure only for my-custom-route URI & only on PUT request.
    if($r->getUri()->getPath() == '/auth/realms/' . REALM . '/my-custom-route' && $r->getMethod() == 'PUT')
    {
        $body_content = "[\"ONE\", \"TWO\", \"THREE\"]";
        $body_stream = GuzzleHttp\Psr7\Utils::streamFor($body_content);
        return $r->withBody($body_stream);
    }
    else
    {
        return $r;
    }
});

Then just set the 'middlewares' key to use your $middleware in the client factory definition. 'middlewares' => [$middleware],