1

I'm trying to create a custom Auth adapter for a legacy API. Let's call the adapter TR42. Right now, I'm debugging TR42::check() so I'm using hardcoded values:

<?php
class TR42 extends \lithium\core\Object {

    public function __construct(array $config = []) {
        $defaults = [
            'scheme' => 'http',
            'host' => 'localhost/tr42/mock_api_authenticate.php',
            'action' => 'authLookup',
            'fields' => ['username', 'password'],
            'method' => 'POST'
        ];
        parent::__construct($config + $defaults);
    }

    public function check($credentials, array $options = []) {
        $postConfig = [
            /**
             * Should I be using 'body' or 'query' to submit POST fields?
             */
            'body' => [
                'username' => 'housni',
                'password' => sha1('legacyHashedPassword'),
                'action' => 'authLookup'
            ],
            'query' => 'username=housni&password=' . sha1('legacyHashedPassword') . '&action=authLookup',
        ];
        $request = new Request($postConfig + $this->_config);

        $stream = new Curl($this->_config);
        $stream->open();
        $stream->write($request);
        $response = $stream->read();
        $stream->close();

        echo '<pre>' . print_r($response, true) . '</pre>';
        die();
    }
}
?>

The file http://localhost/tr42/mock_api_authenticate.php looks like this:

<?php
echo '<h1>This request is: ' . $_SERVER['REQUEST_METHOD'] . '</h1>';
if (!empty($_POST)) {
    echo '<h1>YAY</h1>';
} else {
    echo '<h1>NAY</h1>';
}

echo '<pre>' . print_r($_POST, true) . '</pre>';
?>

Since my cURL code submits a POST request, I'd expect my $_POST to be populated in mock_api_authenticate.php but that's not happening because the output from TR42::check()'s print_r() is:

HTTP/1.1 200 OK
Date: Sun, 18 Aug 2013 04:46:34 GMT
Server: Apache/2.2.14 (Ubuntu)
X-Powered-By: PHP/5.4.17-1~lucid+1
Vary: Accept-Encoding
Content-Length: 63
Connection: close
Content-Type: text/html

This request is: POST

NAY

Array
(
)

It's saying the request is POST but the POST array (the last empty array) is empty.

What am I doing wrong?

Thanks for reading :)

Housni
  • 963
  • 1
  • 10
  • 23

1 Answers1

1

I suggest the use of Lithium Connections and Service classes, to avoid writing your curl request that way.

Extract your legacy api connection configuration (host, port, etc) to a Connection named 'api' or whatever, then, in your adapter's check() body, write something like:

public function check($credentials, array $options = []) {
    return Connections::get('api')->connection->post('/tr42/mock_api_authenticate.php', $credentials);
}
Mehdi Lahmam B.
  • 2,240
  • 16
  • 22
  • I'm not sure I understand how they all fit together. Can you elaborate more, please? Are you saying TR42 should reside in `extensions\adapter\security\auth\TR42` and connections for `api` of `type` = `Http` and `adapter` = `TR42` should implement that check? I guess what's throwing me off is how connection has a `post()` method which seems to be from `Service`. I'm still rather new to some parts of Lithium :) – Housni Aug 18 '13 at 18:51
  • Also, this TR42 adapter is only an authentication API, it won't be used as a data source. – Housni Aug 18 '13 at 18:53
  • 1
    As I understand the problem, you'll have a TR42 auth adapter that implements check() method, where you need to POST credentials data to an http endpoint somewhere. You could there instantiate a Service object and make that call, or create a HTTP data source for which you'll configure a connection (with Connections::add). The HTTP data source holds a Service instance which let you to call the post() method (or send('post', ...)). See https://github.com/UnionOfRAD/lithium/blob/master/data/source/Http.php#L24 – Mehdi Lahmam B. Aug 18 '13 at 20:08
  • Ah ok, I understand. I'll take the route of [Creating Data Sources](http://lithify.me/docs/manual/working-with-data/creating-data-sources.wiki). Thanks for the suggestion. I'll post some code, once I'm done (probably tomorrow) in case it helps someone. – Housni Aug 18 '13 at 22:00