Ejemplo n.º 1
0
/**
 * Processes incoming requests to our $client_redirect
 * 1. Exchanges incoming code parameter for a bearer token
 * 2. Uses bearer token in a request to Clever's "/me" API resource
 * @param   string $code         OAuth 2.0 exchange code received when our  OAuth redirect was triggered
 * @param   array  $options      Options used for Clever API requests
 * @return  array  $me_response  Hash of Clever's response when identifying a bearer token's owner
 */
function process_client_redirect($code, array $options)
{
    $bearer_token = exchange_code_for_bearer_token($code, $options);
    $request_options = array('method' => 'GET', 'bearer_token' => $bearer_token);
    $me_response = retrieve_me_response_for_bearer_token($bearer_token, $options);
    // Real world applications would store the bearer token and relevant information about the user at this stage.
    return $me_response;
}
 public function testExchangeCodeForBearerToken()
 {
     $mock_request_options = prepare_options_for_clever();
     $expected_bearer_token = 'abcd';
     $json_hash_response = array('access_token' => $expected_bearer_token);
     $json_string_response = json_encode($json_hash_response);
     $this->http->mock->when()->methodIs('POST')->pathIs('/oauth/tokens')->then()->body($json_string_response)->end();
     $this->http->setUp();
     $bearer_token = exchange_code_for_bearer_token('xyz', $mock_request_options);
     $this->assertEquals($bearer_token, $expected_bearer_token);
 }