Example #1
0
    public function testDiscovery()
    {
        $testCase = $this;
        $http = new Http(new Callback(function ($request) use($testCase) {
            if ($request->getUrl()->getPath() == '/xrds') {
                $response = <<<'TEXT'
HTTP/1.1 200 OK
Date: Thu, 26 Sep 2013 16:36:26 GMT
Content-Type: application/xrds+xml; charset=UTF-8

<?xml version="1.0" encoding="UTF-8"?>
<xrds:XRDS xmlns="xri://$xrd*($v*2.0)" xmlns:xrds="xri://$xrds">
	<XRD>
		<Service>
			<URI>http://test.phpsx.org</URI>
			<Type>http://ns.test.phpsx.org/2011/test</Type>
		</Service>
	</XRD>
</xrds:XRDS>
TEXT;
            } else {
                $response = <<<TEXT
HTTP/1.1 200 OK
Date: Thu, 26 Sep 2013 16:36:26 GMT
Content-Type: text/html; charset=UTF-8
X-XRDS-Location: http://127.0.0.1/xrds

<html>
<body>
\t<h1>Oo</h1>
</body>
</html>
TEXT;
            }
            return Response::convert($response, ResponseParser::MODE_LOOSE)->toString();
        }));
        $yadis = new Yadis($http);
        $xrd = $yadis->discover(new Url('http://127.0.0.1'));
        $this->assertInstanceOf('PSX\\Xri\\Xrd', $xrd);
        $service = $xrd->getService();
        $this->assertEquals(1, count($service));
        $this->assertInstanceOf('PSX\\Xri\\Xrd\\Service', $service[0]);
        $this->assertEquals('http://test.phpsx.org', $service[0]->getUri());
        $this->assertEquals(array('http://ns.test.phpsx.org/2011/test'), $service[0]->getType());
    }
Example #2
0
 public function testRequest()
 {
     // discover endpoints
     $yadis = new Yadis($this->http);
     $xrds = $yadis->discover(new Url($this->config['psx_url']));
     // get oauth request uri
     $requestUri = null;
     foreach ($xrds->getService() as $service) {
         if (in_array('http://oauth.net/core/1.0/endpoint/request', $service->getType())) {
             $requestUri = $service->getUri();
             break;
         }
     }
     $this->assertEquals(true, !empty($requestUri), 'Could not find http://oauth.net/core/1.0/endpoint/request in xrds');
     // get request token
     $response = $this->oauth->requestToken(new Url($requestUri), $this->consumerKey, $this->consumerSecret);
     $this->assertEquals(true, strlen($response->getToken()) > 4, $this->http->getResponse());
     $this->assertEquals(true, strlen($response->getTokenSecret()) > 4, $this->http->getResponse());
     $token = $response->getToken();
     $tokenSecret = $response->getTokenSecret();
     // since we can not login and approve the request we do this manually in
     // the table
     $verifier = Security::generateToken(32);
     $con = new Condition(array('token', '=', $token));
     $this->sql->update($this->registry['table.oauth_request'], array('userId' => 1, 'status' => Oauth\Record::APPROVED, 'verifier' => $verifier), $con);
     // get oauth access uri
     $accessUri = null;
     foreach ($xrds->getService() as $service) {
         if (in_array('http://oauth.net/core/1.0/endpoint/access', $service->getType())) {
             $accessUri = $service->getUri();
             break;
         }
     }
     $this->assertEquals(true, !empty($accessUri), 'Could not find http://oauth.net/core/1.0/endpoint/access in xrds');
     // get access token
     $response = $this->oauth->accessToken(new Url($accessUri), $this->consumerKey, $this->consumerSecret, $token, $tokenSecret, $verifier);
     $this->assertEquals(true, strlen($response->getToken()) > 4, $this->http->getResponse());
     $this->assertEquals(true, strlen($response->getTokenSecret()) > 4, $this->http->getResponse());
 }
Example #3
0
 /**
  * We make a YADIS dicovery on the url. If we have found an fitting
  * service the method returns the endpoint uri and if available a
  * claimed id else it returns false
  *
  * @param PSX\Url $url
  * @return array
  */
 private function fetchXrds(Url $url)
 {
     $yadis = new Yadis($this->http);
     $xrds = $yadis->discover($url);
     if ($xrds !== false && isset($xrds->service)) {
         // OP Identifier Element
         foreach ($xrds->service as $service) {
             if (in_array('http://specs.openid.net/auth/2.0/server', $service->getType())) {
                 $this->types = $service->getType();
                 $identity = new Identity($service->getUri());
                 return $identity;
             }
         }
         // Claimed Identifier Element
         foreach ($xrds->service as $service) {
             if (in_array('http://specs.openid.net/auth/2.0/signon', $service->getType())) {
                 $this->types = $service->getType();
                 $identity = new Identity($service->getUri(), $service->getLocalId());
                 return $identity;
             }
         }
     }
     return false;
 }
Example #4
0
 public function discover(Url $url)
 {
     $yadis = new Yadis($this->http);
     $xrds = $yadis->discover($url);
     if ($xrds !== false && isset($xrds->service)) {
         $uri = null;
         foreach ($xrds->service as $service) {
             if (in_array(self::NS, $service->getType())) {
                 $uri = $service->getUri();
             }
         }
         if (!empty($uri)) {
             return new Url($uri);
         } else {
             throw new Exception('Could not find service');
         }
     } else {
         throw new Exception('Could not find xrds');
     }
 }