public function it_should_callback(ClientInterface $mockedClient, InputInterface $mockedInput, ResponseInterface $tokenResponse, ResponseInterface $storeResources, StorageInterface $mockedStorage) { $mockedInput->get('code')->shouldBeCalled()->willReturn('callbackCode'); $mockedInput->get('state')->shouldBeCalled()->willReturn(urlencode('https://login.salesforce.com')); $mockedClient->request('post', 'https://login.salesforce.com/services/oauth2/token', ['form_params' => ['code' => 'callbackCode', 'grant_type' => 'authorization_code', 'client_id' => 'testingClientId', 'client_secret' => 'testingClientSecret', 'redirect_uri' => 'callbackURL']])->shouldBeCalled()->willReturn($tokenResponse); $tokenResponse->getBody()->shouldBeCalled()->willReturn($this->authenticationJSON); $mockedClient->request('get', 'https://na00.salesforce.com', ['headers' => ['Authorization' => 'Oauth accessToken', 'Accept' => 'application/json', 'Content-Type' => 'application/json']])->shouldBeCalled()->willReturn($storeResources); $mockedStorage->get('version')->willReturn(null); $mockedStorage->put('loginURL', 'https://login.salesforce.com')->shouldBeCalled(); $mockedStorage->put('resources', ['foo' => 'bar'])->shouldBeCalled(); $mockedStorage->putTokenData(['access_token' => '00Do0000000secret', 'instance_url' => 'https://na17.salesforce.com', 'id' => 'https://login.salesforce.com/id/00D', 'token_type' => 'Bearer', 'issued_at' => '1447000236011', 'signature' => 'secretsig', 'refresh_token' => 'refreshToken'])->shouldBeCalled(); $mockedStorage->putRefreshToken('refreshToken')->shouldBeCalled(); $storeResources->getBody()->shouldBeCalled()->willReturn($this->responseJSON); $this->callback()->shouldReturn(null); }
/** * When settings up your callback route, you will need to call this method to * acquire an authorization token. This token will be used for the API requests. * @return RedirectInterface */ public function callback() { //Salesforce sends us an authorization code as part of the Web Server OAuth Authentication Flow $code = $this->input->get('code'); $state = $this->input->get('state'); //Now we must make a request for the authorization token. $tokenURL = $this->creditials['loginURL'] . '/services/oauth2/token'; $response = $this->client->post($tokenURL, ['body' => ['code' => $code, 'grant_type' => 'authorization_code', 'client_id' => $this->creditials['consumerKey'], 'client_secret' => $this->creditials['consumerSecret'], 'redirect_uri' => $this->creditials['callbackURI']]]); // Response returns an json of access_token, instance_url, id, issued_at, and signature. $jsonResponse = $response->json(); \Session::put('access_token', $jsonResponse['access_token']); \Session::put('instance_url', $jsonResponse['instance_url']); // Encypt token and store token and in session. $this->session->putToken($jsonResponse); $this->session->putRefreshToken(@$jsonResponse['refresh_token']); // Store resources into the session. $this->storeResources(); }