Example #1
0
 public function serve()
 {
     $http = new Http();
     $url = new Url('http://www.google.com/recaptcha/api/challenge?k=' . $this->publicKey . '&ajax=1&cachestop=' . time());
     $request = new GetRequest($url);
     $response = $http->request($request);
     if ($response->getCode() == 200) {
         // parse response body
         $data = $this->parseResponseBody($response->getBody());
         if (isset($data['programming_error'])) {
             throw new Exception($data['programming_error']);
         }
         if (isset($data['error_message'])) {
             throw new Exception($data['error_message']);
         }
         if (isset($data['site']) && isset($data['challenge'])) {
             $_SESSION['amun_recaptcha_challenge'] = $data['challenge'];
             header('HTTP/1.1 307 Temporary Redirect');
             header('Location: http://www.google.com/recaptcha/api/image?c=' . urlencode($data['challenge']) . '&cachestop=' . time());
         } else {
             throw new Exception('Couldnt parse response body');
         }
     } else {
         throw new Exception('Invalid recaptcha response code');
     }
 }
Example #2
0
 public function testFlow()
 {
     $testCase = $this;
     $http = new Http(new Callback(function ($request) use($testCase) {
         $body = new TempStream(fopen('php://memory', 'r+'));
         $response = new Response();
         $response->setBody($body);
         $testCase->loadController($request, $response);
         return $response;
     }));
     $oauth = new Oauth($http);
     // request token
     $response = $oauth->requestToken(new Url('http://127.0.0.1/request'), OauthTest::CONSUMER_KEY, OauthTest::CONSUMER_SECRET);
     $this->assertInstanceOf('PSX\\Oauth\\Provider\\Data\\Response', $response);
     $this->assertEquals(OauthTest::TMP_TOKEN, $response->getToken());
     $this->assertEquals(OauthTest::TMP_TOKEN_SECRET, $response->getTokenSecret());
     // authorize the user gets redirected and approves the application
     // access token
     $response = $oauth->accessToken(new Url('http://127.0.0.1/access'), OauthTest::CONSUMER_KEY, OauthTest::CONSUMER_SECRET, OauthTest::TMP_TOKEN, OauthTest::TMP_TOKEN_SECRET, OauthTest::VERIFIER);
     $this->assertInstanceOf('PSX\\Oauth\\Provider\\Data\\Response', $response);
     $this->assertEquals(OauthTest::TOKEN, $response->getToken());
     $this->assertEquals(OauthTest::TOKEN_SECRET, $response->getTokenSecret());
     // api request
     $url = new Url('http://127.0.0.1/api');
     $auth = $oauth->getAuthorizationHeader($url, OauthTest::CONSUMER_KEY, OauthTest::CONSUMER_SECRET, OauthTest::TOKEN, OauthTest::TOKEN_SECRET, 'HMAC-SHA1', 'GET');
     $request = new GetRequest($url, array('Authorization' => $auth));
     $response = $http->request($request);
     $this->assertEquals(200, $response->getStatusCode());
     $this->assertEquals('SUCCESS', (string) $response->getBody());
 }
Example #3
0
 public function handle(Request $request, Parameters $configuration, Context $context)
 {
     // parse json
     $headers = array('User-Agent' => 'Fusio');
     $body = $this->templateParser->parse($request, $configuration, $context, $configuration->get('body'));
     $request = new PostRequest($configuration->get('url'), $headers, $body);
     $response = $this->http->request($request);
     if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
         return new Response(200, [], ['success' => true, 'message' => 'Request successful']);
     } else {
         return new Response(500, [], ['success' => false, 'message' => 'Request failed']);
     }
 }
Example #4
0
 public function testRequest()
 {
     $testCase = $this;
     $http = new Http(new Callback(function ($request) use($testCase) {
         $body = new TempStream(fopen('php://memory', 'r+'));
         $response = new Response();
         $response->setBody($body);
         $testCase->loadController($request, $response);
         return $response;
     }));
     $url = new Url('http://127.0.0.1/oembed?url=http%3A%2F%2F127.0.0.1%2Fresource');
     $request = new GetRequest($url);
     $response = $http->request($request);
     $expect = array('url' => 'http://127.0.0.1/resource.png', 'width' => 640, 'height' => 480, 'author_name' => 'foobar');
     $this->assertEquals(200, $response->getStatusCode());
     $this->assertJsonStringEqualsJsonString(json_encode($expect), (string) $response->getBody());
 }
Example #5
0
 protected function executeRequest(RequestInterface $request, ParametersInterface $configuration, ContextInterface $context)
 {
     // parse body
     $parser = $this->templateFactory->newTextParser();
     $body = $parser->parse($request, $context, $configuration->get('body'));
     // build request
     $method = $configuration->get('method') ?: 'POST';
     $headers = $this->parserHeaders($configuration->get('headers'));
     $request = new Request(new Url($configuration->get('url')), $method, $headers, $body);
     return $this->http->request($request);
 }
Example #6
0
 /**
  * Method wich tries to request an jrd document from the given url. Used 
  * also in the webfinger class therefore it is here static
  *
  * @return PSX\Hostmeta\Document
  */
 public static function requestJrd(Http $http, Url $url)
 {
     $request = new GetRequest($url, array('Accept' => 'application/jrd+json', 'User-Agent' => __CLASS__ . ' ' . Base::VERSION));
     $request->setFollowLocation(true);
     $response = $http->request($request);
     if ($response->getStatusCode() == 200) {
         $contentType = $response->getHeader('Content-Type');
         if (strpos($contentType, 'application/jrd+json') !== false || strpos($contentType, 'application/json') !== false) {
             $jrd = new Jrd();
             $jrd->import(Json::decode($response->getBody()));
             return $jrd;
         } else {
             if (strpos($contentType, 'application/xrd+xml') !== false || strpos($contentType, 'application/xml') !== false) {
                 $xrd = new Xrd();
                 $xrd->import(simplexml_load_string((string) $response->getBody()));
                 return $xrd;
             } else {
                 throw new Exception('Received unknown content type');
             }
         }
     } else {
         throw new Exception('Invalid response code ' . $response->getStatusCode() . ' from ' . strval($url));
     }
 }
Example #7
0
    public function testFlow()
    {
        $testCase = $this;
        $http = new Http(new Callback(function (RequestInterface $request) use($testCase) {
            // request token
            if ($request->getUri()->getPath() == '/requestToken') {
                $auth = Authentication::decodeParameters((string) $request->getHeader('Authorization'));
                $testCase->assertEquals(self::CONSUMER_KEY, $auth['oauth_consumer_key']);
                $testCase->assertEquals('HMAC-SHA1', $auth['oauth_signature_method']);
                $testCase->assertTrue(isset($auth['oauth_timestamp']));
                $testCase->assertTrue(isset($auth['oauth_nonce']));
                $testCase->assertEquals('1.0', $auth['oauth_version']);
                $testCase->assertEquals('oob', $auth['oauth_callback']);
                $testCase->assertTrue(isset($auth['oauth_signature']));
                $tmpToken = self::TMP_TOKEN;
                $tmpTokenSecret = self::TMP_TOKEN_SECRET;
                $response = <<<TEXT
HTTP/1.1 200 OK
Date: Thu, 26 Sep 2013 16:36:25 GMT
Content-Type: application/x-www-form-urlencoded

oauth_token={$tmpToken}&oauth_token_secret={$tmpTokenSecret}&oauth_callback_confirmed=1
TEXT;
            } elseif ($request->getUri()->getPath() == '/accessToken') {
                $auth = Authentication::decodeParameters((string) $request->getHeader('Authorization'));
                $testCase->assertEquals(self::CONSUMER_KEY, $auth['oauth_consumer_key']);
                $testCase->assertEquals(self::TMP_TOKEN, $auth['oauth_token']);
                $testCase->assertEquals('HMAC-SHA1', $auth['oauth_signature_method']);
                $testCase->assertTrue(isset($auth['oauth_timestamp']));
                $testCase->assertTrue(isset($auth['oauth_nonce']));
                $testCase->assertEquals('1.0', $auth['oauth_version']);
                $testCase->assertEquals(self::VERIFIER, $auth['oauth_verifier']);
                $testCase->assertTrue(isset($auth['oauth_signature']));
                $token = self::TOKEN;
                $tokenSecret = self::TOKEN_SECRET;
                $response = <<<TEXT
HTTP/1.1 200 OK
Date: Thu, 26 Sep 2013 16:36:26 GMT
Content-Type: application/x-www-form-urlencoded

oauth_token={$token}&oauth_token_secret={$tokenSecret}
TEXT;
            } elseif ($request->getUri()->getPath() == '/api') {
                $auth = Authentication::decodeParameters((string) $request->getHeader('Authorization'));
                $testCase->assertEquals(self::CONSUMER_KEY, $auth['oauth_consumer_key']);
                $testCase->assertEquals(self::TOKEN, $auth['oauth_token']);
                $testCase->assertEquals('HMAC-SHA1', $auth['oauth_signature_method']);
                $testCase->assertTrue(isset($auth['oauth_timestamp']));
                $testCase->assertTrue(isset($auth['oauth_nonce']));
                $testCase->assertEquals('1.0', $auth['oauth_version']);
                $testCase->assertTrue(isset($auth['oauth_signature']));
                $response = <<<TEXT
HTTP/1.1 200 OK
Date: Thu, 26 Sep 2013 16:36:26 GMT
Content-Type: text/html; charset=UTF-8

SUCCESS
TEXT;
            } else {
                throw new \RuntimeException('Invalid path');
            }
            return ResponseParser::convert($response, ResponseParser::MODE_LOOSE)->toString();
        }));
        $oauth = new Oauth($http);
        // request token
        $url = new Url('http://127.0.0.1/requestToken');
        $response = $oauth->requestToken($url, self::CONSUMER_KEY, self::CONSUMER_SECRET);
        $this->assertInstanceOf('PSX\\Oauth\\Provider\\Data\\Response', $response);
        $this->assertEquals(self::TMP_TOKEN, $response->getToken());
        $this->assertEquals(self::TMP_TOKEN_SECRET, $response->getTokenSecret());
        // if we have optained temporary credentials we can redirect the user
        // to grant access to the credentials
        // $oauth->userAuthorization($url, array('oauth_token' => $response->getToken()))
        // if the user gets redirected back we can exchange the temporary
        // credentials to an access token we also get an verifier as GET
        // parameter
        $url = new Url('http://127.0.0.1/accessToken');
        $response = $oauth->accessToken($url, self::CONSUMER_KEY, self::CONSUMER_SECRET, self::TMP_TOKEN, self::TMP_TOKEN, self::VERIFIER);
        $this->assertInstanceOf('PSX\\Oauth\\Provider\\Data\\Response', $response);
        $this->assertEquals(self::TOKEN, $response->getToken());
        $this->assertEquals(self::TOKEN_SECRET, $response->getTokenSecret());
        // now we can make an request to the protected api
        $url = new Url('http://127.0.0.1/api');
        $auth = $oauth->getAuthorizationHeader($url, self::CONSUMER_KEY, self::CONSUMER_SECRET, self::TOKEN, self::TOKEN_SECRET, 'HMAC-SHA1', 'GET');
        $request = new GetRequest($url, array('Authorization' => $auth));
        $response = $http->request($request);
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertEquals('SUCCESS', (string) $response->getBody());
    }
Example #8
0
 private function getMimeTypes()
 {
     $url = new Url('http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types');
     $http = new Http();
     $request = new GetRequest($url);
     $response = $http->request($request);
     $types = array();
     if ($response->getCode() == 200) {
         $lines = explode("\n", $response->getBody());
         foreach ($lines as $line) {
             if ($line[0] != '#') {
                 $pos = strpos($line, "\t");
                 $mime = trim(substr($line, 0, $pos));
                 $ext = explode(' ', trim(substr($line, $pos)));
                 $types[$mime] = $ext;
             }
         }
     }
     return $types;
 }
Example #9
0
 public function testSetGetCookieStore()
 {
     $http = new Http();
     $this->assertEmpty($http->getCookieStore());
     $http->setCookieStore(new CookieStore\Memory());
     $this->assertInstanceOf('PSX\\Http\\CookieStore\\Memory', $http->getCookieStore());
 }