Skip to content

Commit

Permalink
Feature/add refresh access token method to api client (#88)
Browse files Browse the repository at this point in the history
* Split up method so that we can re-use the logic to retrieve a new access token using a refresh token instead of authorization code

* Removed note about not being allowed to edit the ApiClient class

* Putting the code in the right array index

* Cleaned up imports

* Removed blank line
  • Loading branch information
4c0n authored Mar 21, 2020
1 parent f74bc24 commit 2b6df79
Showing 1 changed file with 55 additions and 22 deletions.
77 changes: 55 additions & 22 deletions src/Client/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,13 @@
* Generated by: https://github.com/swagger-api/swagger-codegen.git
*/

/**
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen
* Do not edit the class manually.
*/

namespace DocuSign\eSign\Client;

use DocuSign\eSign\Client\Auth\OAuth;
use \DocuSign\eSign\Configuration;
use \DocuSign\eSign\ObjectSerializer;
use SebastianBergmann\RecursionContext\InvalidArgumentException;
use DocuSign\eSign\Configuration;
use DocuSign\eSign\ObjectSerializer;
use Firebase\JWT\JWT;


/**
* ApiClient Class Doc Comment
*
Expand Down Expand Up @@ -378,7 +370,7 @@ public function selectHeaderContentType($content_type)
*
* @param string $raw_headers A string of raw HTTP response headers
*
* @return string[] Array of HTTP response heaers
* @return string[] Array of HTTP response headers
*/
protected function httpParseHeaders($raw_headers)
{
Expand Down Expand Up @@ -449,26 +441,29 @@ public function getAuthorizationURI($client_id, $scopes, $redirect_uri, $respons
}

/**
* GenerateAccessToken will exchange the authorization code for an access token and refresh tokens.
*
* @param string $grant_type Should be either "authorization_code" or "refresh_token".
* @param string $client_id DocuSign OAuth Client Id(AKA Integrator Key)
* @param string $client_secret The secret key you generated when you set up the integration in DocuSign Admin console.
* @param string $code The authorization code
* @param string $code Depending on the grant_type this should be either the authorization code or the refresh token.
*
* @return array
* @throws ApiException
* @throws InvalidArgumentException
* @throws \InvalidArgumentException
*/
public function generateAccessToken($client_id = null, $client_secret = null, $code = null)
private function accessToken($grant_type, $client_id = null, $client_secret = null, $code = null)
{
$caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS,2)[1]['function'];
if ($grant_type !== "authorization_code" && $grant_type !== "refresh_token") {
throw new \InvalidArgumentException('Grant type should be either "authorization_code" or "refresh_token"');
}
if (!$client_id) {
throw new \InvalidArgumentException('Missing the required parameter $client_id when calling generateAccessToken');
throw new \InvalidArgumentException("Missing the required parameter $client_id when calling {$caller}");
}
if (!$client_secret || !$code) {
throw new \InvalidArgumentException('Missing the required parameter $client_secret when calling generateAccessToken');
throw new \InvalidArgumentException("Missing the required parameter $client_secret when calling {$caller}");
}
if (!$code) {
throw new \InvalidArgumentException('Missing the required parameter $code when calling generateAccessToken');
throw new \InvalidArgumentException("Missing the required parameter $code when calling {$caller}");
}

$resourcePath = "/oauth/token";
Expand All @@ -479,15 +474,53 @@ public function generateAccessToken($client_id = null, $client_secret = null, $c
"Content-Type" => "application/x-www-form-urlencoded",
];
$postData = [
"grant_type" => "authorization_code",
"code" => $code
"grant_type" => $grant_type
];

if ($grant_type === 'authorization_code') {
$postData['code'] = $code;
} elseif ($grant_type === 'refresh_token') {
$postData['refresh_token'] = $code;
}

list($response, $statusCode, $httpHeader) = $this->callApi($resourcePath, self::$POST, $queryParams, $postData, $headers, null, null, true);
if(isset($response->access_token))
$this->config->addDefaultHeader("Authorization", "{$response->token_type} {$response->access_token}");
return [$this->getSerializer()->deserialize($response, '\DocuSign\eSign\Client\Auth\OAuthToken', $httpHeader), $statusCode, $httpHeader];
}

/**
* generateAccessToken will exchange the authorization code for an access token and a refresh token.
*
* @param string $client_id DocuSign OAuth Client Id(AKA Integrator Key)
* @param string $client_secret The secret key you generated when you set up the integration in DocuSign Admin console.
* @param string $authorization_code The authorization code.
*
* @return array
* @throws ApiException
* @throws \InvalidArgumentException
*/
public function generateAccessToken($client_id = null, $client_secret = null, $authorization_code = null)
{
return $this->accessToken("authorization_code", $client_id, $client_secret, $authorization_code);
}

/**
* refreshAccessToken will exchange the refresh token for a new access token and a new refresh token.
*
* @param string $client_id DocuSign OAuth Client Id(AKA Integrator Key)
* @param string $client_secret The secret key you generated when you set up the integration in DocuSign Admin console.
* @param string $refresh_token The refresh token.
*
* @return array
* @throws ApiException
* @throws \InvalidArgumentException
*/
public function refreshAccessToken($client_id = null, $client_secret = null, $refresh_token = null)
{
return $this->accessToken("refresh_token", $client_id, $client_secret, $refresh_token);
}

/**
* Request JWT User Token
*
Expand All @@ -498,7 +531,7 @@ public function generateAccessToken($client_id = null, $client_secret = null, $c
* @param int $expires_in Number of minutes token will be valid
* @return array
* @throws ApiException
* @throws InvalidArgumentException
* @throws \InvalidArgumentException
*/
public function requestJWTUserToken($client_id, $user_id, $rsa_private_key, $scopes = null, $expires_in = 60)
{
Expand Down

0 comments on commit 2b6df79

Please sign in to comment.