Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Docusign to the Oauth2 service implementation list #519

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Included service implementations
- Delicious
- Deezer
- DeviantArt
- Docusign
- Dropbox
- Eve Online
- Facebook
Expand Down
52 changes: 52 additions & 0 deletions examples/docusign.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/**
* Example of retrieving an authentication token of the Docusign service
*
* PHP version 5.4
*
* @author Naveen Gopala <[email protected]>
* @copyright Copyright (c) 2017 The authors
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*/

use OAuth\OAuth2\Service\Docusign;
use OAuth\Common\Storage\Session;
use OAuth\Common\Consumer\Credentials;

/**
* Bootstrap the example
*/
require_once __DIR__ . '/bootstrap.php';

// Session storage
$storage = new Session();

// Setup the credentials for the requests
$credentials = new Credentials(
$servicesCredentials['docusign']['key'],
$servicesCredentials['docusign']['secret'],
$currentUri->getAbsoluteUri()
);

// Instantiate the Docusign service using the credentials, http client, storage mechanism for the token and profile scope
/** @var $docusignService Docusign */
$docusignService = $serviceFactory->createService('docusign', $credentials, $storage, array('signature'));

if (!empty($_GET['code'])) {
// This was a callback request from Docusign, get the token
$token = $docusignService->requestAccessToken($_GET['code']);

// Send a request with it
$result = json_decode($docusignService->request('/oauth/userinfo'), true);

// Show some of the resultant data
echo 'Your unique Docusign user id is: ' . $result['accounts'][0]['account_id'] . ' and your name is ' . $result['name'];

} elseif (!empty($_GET['go']) && $_GET['go'] === 'go') {
$url = $docusignService->getAuthorizationUri();
header('Location: ' . $url);
} else {
$url = $currentUri->getRelativeUri() . '?go=go';
echo "<a href='$url'>Login with Docusign!</a>";
}
4 changes: 4 additions & 0 deletions examples/init.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@
'key' => '',
'secret' => '',
),
'docusign' => array(
'key' => '',
'secret' => '',
),
'dropbox' => array(
'key' => '',
'secret' => '',
Expand Down
123 changes: 123 additions & 0 deletions src/OAuth/OAuth2/Service/Docusign.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

namespace OAuth\OAuth2\Service;

use OAuth\OAuth2\Token\StdOAuth2Token;
use OAuth\Common\Http\Exception\TokenResponseException;
use OAuth\Common\Http\Uri\Uri;
use OAuth\Common\Consumer\CredentialsInterface;
use OAuth\Common\Http\Client\ClientInterface;
use OAuth\Common\Storage\TokenStorageInterface;
use OAuth\Common\Http\Uri\UriInterface;

/**
* Docusign service.
*
* @author Naveen Gopala <[email protected]>
* @link https://images-na.ssl-images-docusign.com/images/G/01/lwa/dev/docs/website-developer-guide._TTH_.pdf
*/
class Docusign extends AbstractService
{
/**
* Defined scopes
* @link https://images-na.ssl-images-docusign.com/images/G/01/lwa/dev/docs/website-developer-guide._TTH_.pdf
*/
const SCOPE_SIGNATURE = 'signature';

public function __construct(
CredentialsInterface $credentials,
ClientInterface $httpClient,
TokenStorageInterface $storage,
$scopes = array(),
UriInterface $baseApiUri = null
) {
parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);

if (null === $baseApiUri) {
// account-d.docusign.com for the developer sandbox
// account.docusign.com for the production platform.
$this->baseApiUri = new Uri('https://account-d.docusign.com');
}
}

/**
* {@inheritdoc}
*/
public function getAuthorizationUri(array $additionalParameters = array())
{
$parameters = array_merge(
$additionalParameters,
array(
'client_id' => $this->credentials->getConsumerId(),
'redirect_uri' => $this->credentials->getCallbackUrl(),
'response_type' => 'code',
)
);

$parameters['scope'] = implode(' ', $this->scopes);

// Build the url
$url = clone $this->getAuthorizationEndpoint();
foreach ($parameters as $key => $val) {
$url->addToQuery($key, $val);
}

return $url;
}

/**
* {@inheritdoc}
*/
public function getAuthorizationEndpoint()
{
return new Uri('https://account-d.docusign.com/oauth/auth');
}

/**
* {@inheritdoc}
*/
public function getAccessTokenEndpoint()
{
return new Uri('https://account-d.docusign.com/oauth/token');
}

/**
* {@inheritdoc}
*/
protected function getAuthorizationMethod()
{
return static::AUTHORIZATION_METHOD_HEADER_BEARER;
}

/**
* {@inheritdoc}
*/
protected function parseAccessTokenResponse($responseBody)
{
$data = json_decode($responseBody, true);

if (null === $data || !is_array($data)) {
throw new TokenResponseException('Unable to parse response.');
} elseif (isset($data['error_description'])) {
throw new TokenResponseException('Error in retrieving token: "' . $data['error_description'] . '"');
} elseif (isset($data['error'])) {
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
}

$token = new StdOAuth2Token();
$token->setAccessToken($data['access_token']);
$token->setLifeTime($data['expires_in']);

if (isset($data['refresh_token'])) {
$token->setRefreshToken($data['refresh_token']);
unset($data['refresh_token']);
}

unset($data['access_token']);
unset($data['expires_in']);

$token->setExtraParams($data);

return $token;
}
}
Loading