/** * Store a return url in the cache if provided. * * @param TemporaryCredentials $temp * * @return void */ protected function storeReturnUrl(TemporaryCredentials $temp) { if ($url = $this->request->get('return_url')) { $key = 'oauth_return_url_' . $temp->getIdentifier(); $this->cache->put($key, $url, ProviderContract::CACHE_TTL); } }
/** * {@inheritdoc} */ protected function createTemporaryCredentials($body) { parse_str($body, $data); if (!$data || !is_array($data)) { throw new CredentialsException('Unable to parse temporary credentials response.'); } $temporaryCredentials = new TemporaryCredentials(); $temporaryCredentials->setIdentifier($data['oauth_token']); $temporaryCredentials->setSecret($data['oauth_token_secret']); return $temporaryCredentials; }
public function testOAuthOneProviderRedirect() { $identifier = Str::random(40); $credentials = new TemporaryCredentials(); $credentials->setIdentifier($identifier); $request = Mockery::mock('Illuminate\\Http\\Request'); $server = Mockery::mock('League\\OAuth1\\Client\\Server\\Server'); $cache = $this->app->cache; $server->shouldReceive('getTemporaryCredentials')->once()->andReturn($credentials)->shouldReceive('getAuthorizationUrl')->once()->andReturn('http://foo.bar.baz'); $request->shouldReceive('get')->once()->andReturn('http://foo.bar'); $mock = Mockery::mock(AbstractProvider::class, [$request, $server, $cache])->makePartial(); $mock->shouldReceive('temp->getIdentifier')->andReturn($identifier); $response = $mock->redirect(); $this->assertEquals('http://foo.bar.baz', $response->headers->get('location')); $this->assertEquals('http://foo.bar', Cache::get('oauth_return_url_' . $identifier)); $this->assertInstanceOf(TemporaryCredentials::class, Cache::get('oauth_temp_' . $identifier)); }
/** * Retrieves token credentials by passing in the temporary credentials, * the temporary credentials identifier as passed back by the server * and finally the verifier code. * * @param TemporaryCredentials $temporaryCredentials * @param string $temporaryIdentifier * @param string $verifier * * @return TokenCredentials */ public function getTokenCredentials(TemporaryCredentials $temporaryCredentials, $temporaryIdentifier, $verifier) { if ($temporaryIdentifier !== $temporaryCredentials->getIdentifier()) { throw new \InvalidArgumentException('Temporary identifier passed back by server does not match that of stored temporary credentials. Potential man-in-the-middle.'); } $uri = $this->urlTokenCredentials(); $bodyParameters = ['oauth_verifier' => $verifier]; $client = $this->createHttpClient(); $headers = $this->getHeaders($temporaryCredentials, 'POST', $uri, $bodyParameters); try { $response = $client->post($uri, $headers, $bodyParameters)->send(); } catch (BadResponseException $e) { return $this->handleTokenCredentialsBadResponse($e); } return ['tokenCredentials' => $this->createTokenCredentials($response->getBody()), 'credentialsResponseBody' => $response->getBody()]; }
/** * Retrieves token credentials by passing in the temporary credentials, * the temporary credentials identifier as passed back by the server * and finally the verifier code. * * @param TemporaryCredentials $temporaryCredentials * @param string $temporaryIdentifier * @param string $verifier * * @return TokenCredentials */ public function getTokenCredentials(TemporaryCredentials $temporaryCredentials, $temporaryIdentifier, $verifier) { if ($temporaryIdentifier !== $temporaryCredentials->getIdentifier()) { throw new \InvalidArgumentException('Temporary identifier passed back by server does not match that of stored temporary credentials. Potential man-in-the-middle.'); } // oauth_verifier must be at the end of the url, this doesn't seem to work otherwise $uri = $this->urlTokenCredentials() . '?oauth_verifier=' . $verifier; $bodyParameters = ['oauth_verifier' => $verifier, 'oauth_token' => $temporaryIdentifier]; $client = $this->createHttpClient(); $headers = $this->getHeaders($temporaryCredentials, 'POST', $uri, $bodyParameters); try { $response = $client->post($uri, ['headers' => $headers], ['body' => $bodyParameters]); } catch (BadResponseException $e) { return $this->handleTokenCredentialsBadResponse($e); } $responseString = (string) $response->getBody(); return ['tokenCredentials' => $this->createTokenCredentials($responseString), 'credentialsResponseBody' => $responseString]; }
/** * Creates temporary credentials from the body response. * * @param string $body * @return TemporaryCredentials */ protected function createTemporaryCredentials($body) { parse_str($body, $data); if (!$data || !is_array($data)) { throw new CredentialsException("Unable to parse temporary credentials response."); } if (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] != 'true') { throw new CredentialsException("Error in retrieving temporary credentials."); } $temporaryCredentials = new TemporaryCredentials(); $temporaryCredentials->setIdentifier($data['oauth_token']); $temporaryCredentials->setSecret($data['oauth_token_secret']); return $temporaryCredentials; }
/** * Once we have token, we can run the authorization which than give us the option to request the access token. * * @param string $requestToken Request token returned by getRequestToken method. * @param string $requestTokenSecret Request token secret returned by getRequestToken method. * @param string $oauthToken OAuth token returned by Twitter OAuth server. * @param string $oauthTokenVerifier OAuth token verifier returned by Twitter OAuth server. * * @return array[oauth_token, oauth_token_secret] */ public function requestAccessToken($requestToken, $requestTokenSecret, $oauthToken, $oauthTokenVerifier) { $ti = new TemporaryCredentials(); $ti->setIdentifier($requestToken); $ti->setSecret($requestTokenSecret); $tc = $this->instance->getTokenCredentials($ti, $oauthToken, $oauthTokenVerifier); $token = ['oauth_token' => $tc->getIdentifier(), 'oauth_token_secret' => $tc->getSecret()]; $this->setAccessToken($token); return $token; }
/** * Retrieves token credentials by passing in the temporary credentials, * the temporary credentials identifier as passed back by the server * and finally the verifier code. * * @since 0.2.3 * * @param TemporaryCredentials $temporaryCredentials * @param string $temporaryIdentifier * @param string $verifier * * @return TokenCredentials */ public function getTokenCredentials(TemporaryCredentials $temporaryCredentials, $temporaryIdentifier, $verifier) { if ($temporaryIdentifier !== $temporaryCredentials->getIdentifier()) { throw new \InvalidArgumentException('Temporary identifier passed back by server does not match that of stored temporary credentials. Potential man-in-the-middle.'); } $uri = $this->urlTokenCredentials(); $bodyParameters = array('oauth_verifier' => $verifier); $headers = $this->getHeaders($temporaryCredentials, 'POST', $uri, $bodyParameters); try { $this->get_response($uri, array('method' => 'POST', 'headers' => $headers, 'request_args' => $bodyParameters)); } catch (\Exception $e) { return $this->handleTokenCredentialsBadResponse($e); } return $this->createTokenCredentials($this->response->getBody()); }