Skip to content

Commit

Permalink
Merge pull request #94 from omniphx/feature/add-invalid-creditials-ex…
Browse files Browse the repository at this point in the history
…ception

Add exception for invalid login creditials
  • Loading branch information
omniphx committed Apr 11, 2016
2 parents c5eed73 + 9e8a2f1 commit 5ccd4d6
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 25 deletions.
16 changes: 2 additions & 14 deletions spec/Omniphx/Forrest/Authentications/UserPasswordSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,12 @@ public function it_should_authenticate(
ClientInterface $mockedClient,
StorageInterface $mockedStorage)
{
$mockedClient->request('post', 'https://login.salesforce.com/services/oauth2/token', ['form_params' => ['grant_type' => 'password', 'client_id' => 'testingClientId', 'client_secret' => 'testingClientSecret', 'username' => '[email protected]', 'password' => 'mypassword']])->shouldBeCalled(1)->willReturn($mockedResponse);

$authenticationDecoded = json_decode($this->authenticationJSON, true);
$mockedClient->request('post', 'url/services/oauth2/token', ['form_params' => ['grant_type' => 'password', 'client_id' => 'testingClientId', 'client_secret' => 'testingClientSecret', 'username' => '[email protected]', 'password' => 'mypassword']])->shouldBeCalled(1)->willReturn($mockedResponse);

$mockedResponse->getBody()->shouldBeCalled()->willReturn($this->authenticationJSON);
$mockedStorage->putTokenData(Argument::any())->shouldBeCalled(1);

//Client->requestResource()
$mockedClient->request('get', 'https://na00.salesforce.comresourceURLs', ['headers' => ['Authorization' => 'Oauth accessToken', 'Accept' => 'application/json', 'Content-Type' => 'application/json']])->shouldBeCalled(1)->willReturn($mockedResponse);

//Client->responseFormat()
// $mockedResponse->getBody()->shouldBeCalled(1)->willReturn($this->versionJSON);
// $mockedClient->send(Argument::any())->shouldBeCalled(1)->willReturn($versionResponse);

// $versionResponse->json()->shouldBeCalled()->willReturn([['version' => '30.0'], ['version' => '31.0']]);

$this->authenticate('url')->shouldReturn(null);
}

Expand All @@ -139,8 +130,6 @@ public function it_should_refresh(
$mockedClient->request('post', 'https://login.salesforce.com/services/oauth2/token', ['form_params' => ['grant_type' => 'password', 'client_id' => 'testingClientId', 'client_secret' => 'testingClientSecret', 'username' => '[email protected]', 'password' => 'mypassword']])->shouldBeCalled()->willReturn($mockedResponse);

$mockedResponse->getBody()->shouldBeCalled(1)->willReturn($this->authenticationJSON);
// $mockedResponse->json()->shouldBeCalled()->willReturn(['key' => 'value']);
// $mockedStorage->putTokenData(Argument::type('array'))->shouldBeCalled();

$this->refresh()->shouldReturn(null);
}
Expand All @@ -152,7 +141,6 @@ public function it_should_return_the_request(
) {
$mockedClient->send($mockedRequest)->willReturn($mockedResponse);

//Forrest->Client->requestResource()
$mockedClient->request('get', 'url', ['headers' => ['Authorization' => 'Oauth accessToken', 'Accept' => 'application/json', 'Content-Type' => 'application/json']])->shouldBeCalled(1)->willReturn($mockedResponse);

$mockedResponse->getBody()->shouldBeCalled(1)->willReturn($this->responseJSON);
Expand Down
13 changes: 7 additions & 6 deletions src/Omniphx/Forrest/Authentications/UserPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public function __construct(
parent::__construct($client, $event, $input, $redirect, $storage, $settings);
}

public function authenticate($loginURL = null)
public function authenticate($url = null)
{
$tokenURL = $this->credentials['loginURL'];
$tokenURL .= '/services/oauth2/token';
$loginURL = $url === null ? $this->credentials['loginURL'] : $url;
$loginURL .= '/services/oauth2/token';
$parameters['form_params'] = [
'grant_type' => 'password',
'client_id' => $this->credentials['consumerKey'],
Expand All @@ -35,13 +35,14 @@ public function authenticate($loginURL = null)
'password' => $this->credentials['password'],
];

$response = $this->client->request('post', $tokenURL, $parameters);
$jsonResponse = $this->client->request('post', $loginURL, $parameters);

// Response returns an json of access_token, instance_url, id, issued_at, and signature.
$jsonResponse = json_decode($response->getBody(), true);
$response = json_decode($jsonResponse->getBody(), true);
$this->handleAuthenticationErrors($response);

// Encrypt token and store token and in storage.
$this->storage->putTokenData($jsonResponse);
$this->storage->putTokenData($response);

// Store resources into the storage.
$this->storeResources();
Expand Down
11 changes: 6 additions & 5 deletions src/Omniphx/Forrest/Authentications/WebServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function callback()

$tokenURL = $loginURL.'/services/oauth2/token';

$response = $this->client->request('post', $tokenURL, [
$jsonResponse = $this->client->request('post', $tokenURL, [
'form_params' => [
'code' => $code,
'grant_type' => 'authorization_code',
Expand All @@ -84,12 +84,13 @@ public function callback()
]);

// Response returns an json of access_token, instance_url, id, issued_at, and signature.
$jsonResponse = json_decode($response->getBody(), true);
$response = json_decode($jsonResponse->getBody(), true);
$this->handleAuthenticationErrors($response);

// Encrypt token and store token in storage.
$this->storage->putTokenData($jsonResponse);
if (isset($jsonResponse['refresh_token'])) {
$this->storage->putRefreshToken($jsonResponse['refresh_token']);
$this->storage->putTokenData($response);
if (isset($response['refresh_token'])) {
$this->storage->putRefreshToken($response['refresh_token']);
}

// Store resources into the storage.
Expand Down
8 changes: 8 additions & 0 deletions src/Omniphx/Forrest/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Message\ResponseInterface;
use Omniphx\Forrest\Exceptions\InvalidLoginCreditialsException;
use Omniphx\Forrest\Exceptions\SalesforceException;
use Omniphx\Forrest\Exceptions\TokenExpiredException;
use Omniphx\Forrest\Interfaces\EventInterface;
Expand Down Expand Up @@ -732,6 +733,13 @@ protected function requestResource($pURL, array $pOptions)
return '';
}

protected function handleAuthenticationErrors(array $response)
{
if (isset($response['error'])) {
throw new InvalidLoginCreditialsException($response['error_description']);
}
}

/**
* Set the headers for the request.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Omniphx\Forrest\Exceptions;

class InvalidLoginCreditialsException extends \RuntimeException
{
}

0 comments on commit 5ccd4d6

Please sign in to comment.